+ Add One signal Push notifications

This commit is contained in:
Djery-Tom 2020-08-06 07:21:41 +01:00
parent 1a998259c8
commit c8b7185591
8 changed files with 194 additions and 67 deletions

View File

@ -33,3 +33,6 @@ MAIL_FROM_NAME="iLink World"
VISA_API_URL=localhost:8082
VISA_API_USERNAME=admin
VISA_API_PASSWORD=PasswordHere!
NOTIFICATION_SERVICE_URL=localhost:8083
NOTIFICATION_SERVICE_KEY=RfXvPQzQRgwpzQYPnLfWpZzgx4QseHlg

View File

@ -10,6 +10,7 @@ use App\Models\TransfertCommissionTransaction;
use App\Models\UsersGroup;
use App\Models\UsersGroupsDemandesValidation;
use App\Models\Wallet;
use App\Models\WalletsUser;
use App\Models\WalletTransaction;
use App\Traits\ApiResponser;
use App\Traits\Helper;
@ -53,26 +54,48 @@ class NanoCreditController extends Controller
if (!$this->checkPassword($request->password, $user->encrypted_password, $user->salt))
return $this->errorResponse(trans('messages.incorrect_user_password'));
//Verifier s'il appartient a un groupe
if (isset($user->group_id))
return $this->errorResponse(trans('errors.user_belongs_to_group'));
$init_country = $user->network->country->id;
$result = ConfigWallet::join('networks', 'networks.id', '=', 'configWallet.id_network')
->where('networks.country_id', $init_country)->where('configWallet.type', 'ilink')
->select('configWallet.id')->first();
if ($result) {
$config = ConfigWallet::findOrFail($result->id);
} else {
return $this->errorResponse(trans('errors.no_ilink_network'));
}
if (!$config->has_nano_credit)
return $this->errorResponse(trans('errors.nano_credit_not_available'));
if ($request->limite_credit > $config->limite_credit_max)
return $this->errorResponse(trans('errors.credit_limit_is_greater_than_max'));
if ($request->limite_credit < $config->limite_credit_min)
return $this->errorResponse(trans('errors.credit_limit_is_less_than_min'));
$group->fill($request->all());
// Envoyer des codes sponsors differents
//Check sponsor 1
$resp1 = $this->checkSponsorIdentification($request->code_sponsor1, 1);
if (is_numeric($resp1))
$group->id_sponsor1 = $resp1;
$resp1 = $this->checkSponsorIdentification($request->code_sponsor1, 1, $init_country);
if ($resp1 instanceof Identification)
$group->id_sponsor1 = $resp1->id_user;
else
return $resp1;
//Check sponsor 2
$resp2 = $this->checkSponsorIdentification($request->code_sponsor2, 2);
if (is_numeric($resp2))
$group->id_sponsor2 = $resp2;
$resp2 = $this->checkSponsorIdentification($request->code_sponsor2, 2, $init_country);
if ($resp2 instanceof Identification)
$group->id_sponsor2 = $resp2->id_user;
else
return $resp2;
//Check sponsor 3
$resp3 = $this->checkSponsorIdentification($request->code_sponsor3, 3);
if (is_numeric($resp3))
$group->id_sponsor3 = $resp3;
$resp3 = $this->checkSponsorIdentification($request->code_sponsor3, 3, $init_country);
if ($resp3 instanceof Identification)
$group->id_sponsor3 = $resp3->id_user;
else
return $resp3;
@ -83,9 +106,9 @@ class NanoCreditController extends Controller
$group->valide = false;
$group->save();
$this->sendNotificationToSponsor($group->id, $group->id_sponsor1);
$this->sendNotificationToSponsor($group->id, $group->id_sponsor2);
$this->sendNotificationToSponsor($group->id, $group->id_sponsor3);
$this->sendNotificationToSponsor($group->id, $group->id_sponsor1, $request->code_sponsor1, $user);
$this->sendNotificationToSponsor($group->id, $group->id_sponsor2, $request->code_sponsor2, $user);
$this->sendNotificationToSponsor($group->id, $group->id_sponsor3, $request->code_sponsor3, $user);
return $this->successResponse(trans('messages.successful_user_group_created'));
@ -104,6 +127,7 @@ class NanoCreditController extends Controller
if ($demande->id_sponsor != $request->id_user)
return $this->errorResponse(trans('errors.not_group_sponsor'));
$sponsor = User::findOrFail($request->id_user);
$group = UsersGroup::findOrFail($demande->id_group);
if (!in_array($demande->id_sponsor, [$group->id_sponsor1, $group->id_sponsor2, $group->id_sponsor3]))
return $this->errorResponse(trans('errors.not_group_sponsor'));
@ -129,7 +153,13 @@ class NanoCreditController extends Controller
$demande->statut = true;
$demande->date_validation = new \DateTime();
$demande->save();
// Create credit and epargne account
// Notififier le createur
$data = new \stdClass();
$data->item = "accepted_user_group_validation";
$data->id_group = $group->id;
$this->sendPushNotificationToUser($group->createur->user_code,
trans('notifications.accepted_group_validation_request', ['name' => $sponsor->lastname . ' ' . $sponsor->firstname]), $data);
return $this->successResponse(trans_choice('messages.successful_group_validation', 3 - $group->nombre_validation, ['count' => (3 - $group->nombre_validation)]));
}
@ -153,12 +183,27 @@ class NanoCreditController extends Controller
$sponsor2 = User::where('user_code', $request->code_sponsor2)->firstOrFail();
$sponsor3 = User::where('user_code', $request->code_sponsor3)->firstOrFail();
$init_country = $user->network->country->id;
$result = ConfigWallet::join('networks', 'networks.id', '=', 'configWallet.id_network')
->where('networks.country_id', $init_country)->where('configWallet.type', 'ilink')
->select('configWallet.id')->first();
if ($result) {
$config = ConfigWallet::findOrFail($result->id);
} else {
return $this->errorResponse(trans('errors.no_ilink_network'));
}
if ($request->limite_credit > $config->limite_credit_max)
return $this->errorResponse(trans('errors.credit_limit_is_greater_than_max'));
if ($request->limite_credit < $config->limite_credit_min)
return $this->errorResponse(trans('errors.credit_limit_is_less_than_min'));
if ($sponsor1->id != $group->id_sponsor1) {
$prevSponsor1 = $group->sponsor1;
//Check sponsor 1
$resp1 = $this->checkSponsorIdentification($request->code_sponsor1, 1);
if (is_numeric($resp1))
$group->id_sponsor1 = $resp1;
$resp1 = $this->checkSponsorIdentification($request->code_sponsor1, 1, $init_country);
if ($resp1 instanceof Identification)
$group->id_sponsor1 = $resp1->id_user;
else
return $resp1;
@ -168,9 +213,9 @@ class NanoCreditController extends Controller
if ($sponsor2->id != $group->id_sponsor2) {
$prevSponsor2 = $group->sponsor2;
//Check sponsor 2
$resp2 = $this->checkSponsorIdentification($request->code_sponsor2, 2);
if (is_numeric($resp2))
$group->id_sponsor2 = $resp2;
$resp2 = $this->checkSponsorIdentification($request->code_sponsor2, 2, $init_country);
if ($resp2 instanceof Identification)
$group->id_sponsor2 = $resp2->id_user;
else
return $resp2;
@ -180,9 +225,9 @@ class NanoCreditController extends Controller
if ($sponsor3->id != $group->id_sponsor3) {
$prevSponsor3 = $group->sponsor3;
//Check sponsor 3
$resp3 = $this->checkSponsorIdentification($request->code_sponsor3, 3);
if (is_numeric($resp3))
$group->id_sponsor3 = $resp3;
$resp3 = $this->checkSponsorIdentification($request->code_sponsor3, 3, $init_country);
if ($resp3 instanceof Identification)
$group->id_sponsor3 = $resp3->id_user;
else
return $resp3;
@ -195,15 +240,15 @@ class NanoCreditController extends Controller
$group->date_activation = null;
if (isset($prevSponsor1)) {
$prevSponsor1->save();
$this->sendNotificationToSponsor($group->id, $group->id_sponsor1);
$this->sendNotificationToSponsor($group->id, $group->id_sponsor1, $request->code_sponsor1, $user);
}
if (isset($prevSponsor2)) {
$prevSponsor2->save();
$this->sendNotificationToSponsor($group->id, $group->id_sponsor2);
$this->sendNotificationToSponsor($group->id, $group->id_sponsor2, $request->code_sponsor2, $user);
}
if (isset($prevSponsor3)) {
$prevSponsor3->save();
$this->sendNotificationToSponsor($group->id, $group->id_sponsor3);
$this->sendNotificationToSponsor($group->id, $group->id_sponsor3, $request->code_sponsor3, $user);
}
$group->save();
@ -211,7 +256,7 @@ class NanoCreditController extends Controller
}
private function sendNotificationToSponsor($id_group, $id_sponsor)
public function sendNotificationToSponsor($id_group, $id_sponsor, $sponsor_code, User $sender)
{
$demande = new UsersGroupsDemandesValidation();
$demande->id_group = $id_group;
@ -219,18 +264,27 @@ class NanoCreditController extends Controller
$demande->date_creation = new \DateTime();
$demande->statut = false;
$demande->save();
$data = new \stdClass();
$data->item = "user_group_validation";
$data->id_demande = $demande->id;
$this->sendPushNotificationToUser($sponsor_code,
trans('notifications.group_validation_request', ['name' => $sender->lastname . ' ' . $sender->firstname]), $data);
}
private function checkSponsorIdentification($code_sponsor, $id)
private function checkSponsorIdentification($code_sponsor, $id, $init_country)
{
$sponsor = User::where('user_code', $code_sponsor)->first();
//Verifier s'il appartient a un groupe
if (isset($sponsor->group_id))
return $this->errorResponse(trans('errors.sponsor_belongs_to_group', ['id' => $id]));
$sponsor_country = $sponsor->network->country->id;
if ($init_country != $sponsor_country)
return $this->errorResponse(trans('errors.sponsor_not_registered_in_same_country', ['id' => $id]));
if ($sponsor) {
return $this->checkUserIdentification($sponsor->id, $id);
} else {
return $this->errorResponse('Ce code sponsor' . $id . ' n\'existe pas ');
return $this->errorResponse(trans('errors.sponsor_not_found', ['id' => $id]));
}
}
@ -241,7 +295,7 @@ class NanoCreditController extends Controller
if ($identification->status == 0)
return $this->errorResponse(trans('errors.validation_sponsor_identification_required', ['id' => $id]));
else
return $id_user;
return $identification;
} else {
return $this->errorResponse(trans('errors.sponsor_identification_required', ['id' => $id]));
}

View File

@ -45,10 +45,16 @@ use Illuminate\Database\Eloquent\Model;
* @property float $taux_com_banque_retrait_carte_cash
* @property float $taux_com_hyp_retrait_carte_cash_ilink
* @property float $taux_com_banque_retrait_carte_cash_ilink
* @property float $taux_com_hyp_envoi_wallet_carte_ilink
* @property float $taux_com_banque_envoi_wallet_carte_ilink
* @property string $type
* @property int $has_nano_credit
* @property float $limite_credit_min
* @property float $limite_credit_max
*
* @property Network $network
* @property Collection|PaliersConfigWallet[] $paliers_config_wallets
* @property Collection|PayingNetwork[] $paying_networks
* @property Collection|Tax[] $taxes
*
* @package App\Models
@ -90,7 +96,12 @@ class ConfigWallet extends Model
'taux_com_hyp_retrait_carte_cash' => 'float',
'taux_com_banque_retrait_carte_cash' => 'float',
'taux_com_hyp_retrait_carte_cash_ilink' => 'float',
'taux_com_banque_retrait_carte_cash_ilink' => 'float'
'taux_com_banque_retrait_carte_cash_ilink' => 'float',
'taux_com_hyp_envoi_wallet_carte_ilink' => 'float',
'taux_com_banque_envoi_wallet_carte_ilink' => 'float',
'has_nano_credit' => 'int',
'limite_credit_min' => 'float',
'limite_credit_max' => 'float'
];
protected $fillable = [
@ -126,7 +137,12 @@ class ConfigWallet extends Model
'taux_com_banque_retrait_carte_cash',
'taux_com_hyp_retrait_carte_cash_ilink',
'taux_com_banque_retrait_carte_cash_ilink',
'type'
'taux_com_hyp_envoi_wallet_carte_ilink',
'taux_com_banque_envoi_wallet_carte_ilink',
'type',
'has_nano_credit',
'limite_credit_min',
'limite_credit_max'
];
public function network()
@ -139,6 +155,11 @@ class ConfigWallet extends Model
return $this->hasMany(PaliersConfigWallet::class, 'idConfig');
}
public function paying_networks()
{
return $this->hasMany(PayingNetwork::class, 'id_configWallet');
}
public function taxes()
{
return $this->hasMany(Tax::class, 'idConfig');

View File

@ -10,6 +10,7 @@ use App\Models\Network;
use Brick\Money\Context\AutoContext;
use Brick\Money\Context\CustomContext;
use DateTime;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Facades\Mail;
use Brick\Money\CurrencyConverter;
use Brick\Money\ExchangeRateProvider\PDOProvider;
@ -19,6 +20,7 @@ use Brick\Money\Money;
use Brick\Math\RoundingMode;
use PDO;
use Illuminate\Support\Facades\DB;
use Psr\Http\Message\ResponseInterface;
trait Helper
{
@ -33,6 +35,31 @@ trait Helper
// return $this->successResponse("mail envoye");
}
public function sendPushNotificationToUser($user_code, $message, $data = null)
{
$client = new \GuzzleHttp\Client([
'base_uri' => env('NOTIFICATION_SERVICE_URL'),
]);
$headers = [
'Authorization' => env('NOTIFICATION_SERVICE_KEY'),
];
$body = new \stdClass();
$body->user_code = $user_code;
$body->message = $message;
$body->data = $data;
$promise = $client->requestAsync('POST', '/onesignal/pushToUser', ['json' => $body, 'headers' => $headers])->then();
// function (ResponseInterface $res) {
// echo $res->getStatusCode() . "\n";
// },
// function (RequestException $e) {
// echo $e->getMessage() . "\n";
// echo $e->getRequest()->getMethod();
// }
// );
$promise->wait();
// return $response->getBody()->getContents();
}
public function checkPassword($password, $encrypted_password, $salt)
{
$encrypted_password_to_check = base64_encode(sha1($password . $salt, true) . $salt);

View File

@ -31,4 +31,10 @@ Paying network : :network :country',
'group_already_activated' => 'This group is already activated',
'sponsor_belongs_to_group' => 'Sponsor :id already belongs to a group',
'treated_group_demand' => 'Group validation request already processed',
'credit_limit_is_greater_than_max' => 'The credit limit is greater than the maximum limit',
'credit_limit_is_less_than_min' => 'The credit limit is lower than the minimum limit',
'sponsor_not_registered_in_same_country' => 'Sponsor :id is not registered in your country',
'sponsor_not_found' => 'This sponsor :id code does not exist',
'nano_credit_not_available' => 'Nano credit is not available in your country',
'user_belongs_to_group' => 'You already belong to a group',
];

View File

@ -0,0 +1,5 @@
<?php
return [
"group_validation_request" => ":name has sent you a group validation request",
"accepted_group_validation_request" => ":name has accepted your group validation request"
];

View File

@ -31,4 +31,10 @@ Réseau payeur : :network :country',
'group_already_activated' => 'Ce groupe est déjà activé',
'sponsor_belongs_to_group' => 'Sponsor :id appartient déjà à un groupe',
'treated_group_demand' => 'Demande de validation de groupe déjà traitée',
'credit_limit_is_greater_than_max' => 'La limite de crédit est supérieur à la limite maximale',
'credit_limit_is_less_than_min' => 'La limite de crédit est inférieure à la limite minimale',
'sponsor_not_registered_in_same_country' => 'Sponsor :id n\'est pas enregistré dans votre pays',
'sponsor_not_found' => 'Ce code sponsor :id n\'existe pas',
'nano_credit_not_available' => 'Le nano credit n\'est pas disponible dans votre pays',
'user_belongs_to_group' => 'Vous appartenez déjà à un groupe',
];

View File

@ -0,0 +1,5 @@
<?php
return [
"group_validation_request" => ":name vous a envoyé une demande de validation de groupe",
"accepted_group_validation_request" => ":name a accepté votre demande de validation de groupe"
];