From 152b0987e28f03abc988131b6e77505606caf525 Mon Sep 17 00:00:00 2001 From: Djery-Tom Date: Fri, 13 Nov 2020 07:39:24 +0100 Subject: [PATCH] + Link bank account to user wallet --- app/Http/Controllers/WalletController.php | 134 +++++++++++++++ .../iLinkTransactionController.php | 162 +++++++++++++++--- app/Models/User.php | 26 ++- resources/lang/en/errors.php | 1 + resources/lang/en/messages.php | 10 ++ resources/lang/fr/errors.php | 1 + resources/lang/fr/messages.php | 10 ++ routes/web.php | 2 + 8 files changed, 317 insertions(+), 29 deletions(-) diff --git a/app/Http/Controllers/WalletController.php b/app/Http/Controllers/WalletController.php index 886702c..b52aa5d 100755 --- a/app/Http/Controllers/WalletController.php +++ b/app/Http/Controllers/WalletController.php @@ -2,6 +2,8 @@ namespace App\Http\Controllers; +use App\Models\Network; +use App\Models\NetworksOperator; use App\Models\User; use App\Models\WalletAgent; use App\Models\WalletsUser; @@ -406,4 +408,136 @@ INNER JOIN countries c ON oc.id_country = c.id INNER JOIN type_operators top ON return $this->successResponse($operators); } + /** + * @OA\Get( + * path="/wallets/users/banks_for_link/{id_wallet_network}", + * summary="Afficher la liste des banques d'un réseau pour le rattachement à un wallet", + * tags={"Rattacher un compte bancaire à un wallet utilisateur simple"}, + * security={{"api_key":{}}}, + * @OA\Parameter( + * parameter="id_wallet_network", + * name="id_wallet_network", + * description="ID du reseau auquel appartient le wallet", + * in="path", + * required=true, + * @OA\Schema( + * type="integer", default=101 + * ) + * ), + * @OA\Response( + * response=200, + * description="OK", + * @OA\JsonContent( + * ref="#/components/schemas/ApiResponse", + * example = { + * "status" : 200, + * "response" : {{ + * "id_bank" : 1, + * "bank_name" : "UBA", + * "bank_address" : "Bonamoussadi", + * "country": "Cameroon" + * }}, + * "error":null + * } + * ) + * ) + * ) + */ + + //Banques d'un réseau pour la liaison + public function getBanksInNetworkForLink($id_wallet_network) + { + $id_country = Network::findOrFail($id_wallet_network)->country->id; + $banks = DB::select("SELECT oc.id as id_bank, o.nom as bank_name , oc.adresse as bank_address, c.name as country FROM networks_operators nop INNER JOIN operators_countries oc ON oc.id = nop.id_operator_country INNER JOIN operators o ON o.id = oc.id_operator +INNER JOIN countries c ON oc.id_country = c.id INNER JOIN type_operators top ON o.type = top.code WHERE nop.id_network = :id_network AND o.type = 'bank' AND oc.id_country = :id_country ;", ['id_network' => $id_wallet_network, 'id_country' => $id_country]); + + return $this->successResponse($banks); + } + + /** + * @OA\Post( + * path="/wallets/users/link_bank_account", + * summary="Rattacher le compte bancaire d'un utilisateur à son wallet", + * tags={"Rattacher un compte bancaire à un wallet utilisateur simple"}, + * security={{"api_key":{}}}, + * @OA\RequestBody( + * description="Corps de la requete", + * required=true, + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema( + * @OA\Property(property="iban", + * type="string", + * description="Identifiant bancaire" + * ), + * @OA\Property(property="id_bank", + * type="integer", + * example=4, + * description="ID de la banque" + * ), + * @OA\Property(property="id_wallet_network", + * type="integer", + * example=101, + * description="ID du réseau auquel appartient le wallet" + * ), + * @OA\Property(property="id_user", + * type="integer", + * example = 12, + * description="ID de l'utilisateur" + * ) + * ) + * ) + * ), + * @OA\Response( + * response=200, + * description="OK", + * @OA\JsonContent( + * ref="#/components/schemas/ApiResponse", + * example = { + * "status" : 200, + * "response" : "Rattachement de votre compte bancaire effectué", + * "error":null + * } + * ) + * ) + * ) + */ + // Rattacher le compte bancaire au wallet + public function linkBankAccount(Request $request) + { + $this->validate($request, [ + 'iban' => 'required', + 'id_bank' => 'required|integer|min:0|not_in:0', + 'id_wallet_network' => 'required|integer|min:0|not_in:0', + 'id_user' => 'required|integer|min:0|not_in:0', + ]); + + $user = User::findOrFail($request->id_user); + + //Verifier si la banque est associée au reseau + $network_bank = NetworksOperator::where('id_network', $request->id_wallet_network)->where('id_operator_country', $request->id_bank)->first(); + if (!$network_bank) + return $this->errorResponse(trans('errors.bank_not_associated_with_network')); + + if ($network_bank->operators_country->operator->type != 'bank') + return $this->errorResponse(trans('errors.not_banking_operator')); + + //Verifier le code IBAN + $country_code = $network_bank->network->country->code_country; + $bank_code = $network_bank->operators_country->code; + switch ($this->checkIBAN($request->iban, $country_code, $bank_code)) { + case 0: + return $this->errorResponse(trans('errors.invalid_iban')); + case 1: + return $this->errorResponse(trans('errors.country_not_match_iban')); + case 2: + return $this->errorResponse(trans('errors.bank_not_match_iban')); + } + + $user->iban = $request->iban; + $user->id_bank_country = $request->id_bank; + $user->save(); + + return $this->successResponse(trans('messages.successful_bank_account_attachment')); + } } diff --git a/app/Http/Controllers/iLinkTransactionController.php b/app/Http/Controllers/iLinkTransactionController.php index 26f0a52..ab4e91a 100755 --- a/app/Http/Controllers/iLinkTransactionController.php +++ b/app/Http/Controllers/iLinkTransactionController.php @@ -47,6 +47,65 @@ class iLinkTransactionController extends Controller // } + /** + * @OA\Post( + * path="/transactions/ilink", + * summary="Effectuer une transaction dans le wallet iLink", + * tags={"Transaction iLink"}, + * security={{"api_key":{}}}, + * @OA\RequestBody( + * description="Corps de la requete", + * required=true, + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema( + * oneOf={ + * @OA\Schema(ref="#/components/schemas/user_payement_of_operator"), + * @OA\Schema(ref="#/components/schemas/user_remove_from_bank_to_wallet"), + * } + * ), + * examples = { + * "user_payement_of_operator" : { + * "summary" : "User - Payer une facture chez un opérateur donné", + * "value" : {"type":19,"id_wallet_user":9,"id_wallet_network":101,"no_facture":"CDE1425533","type_operator":"water","id_operator":6,"montant":100000,"password":"1234"} + * }, + * "user_remove_from_bank_to_wallet" : { + "summary" : "User - Retrait du compte bancaire rattaché vers le wallet de l'utilisateur", + * "value": {"type":20,"id_wallet_user":1,"montant":100000,"password":"1234"} + * } + * } + * ) + * ), + * @OA\Response( + * response=200, + * description="OK", + * @OA\JsonContent( + * ref="#/components/schemas/ApiResponse", + * examples = { + * "user_payement_of_operator" : { + * "summary" : "User - Payer une facture chez un opérateur donné", + * "value" : { + * "status" : 200, + * "response" : "Paiement d'une facture chez un operateur\nInformations de la transaction :\n - Numéro : 7S9PZO26VAQ3\n - Montant de la transaction : 100 000 FCFA\n + - Frais : 0 FCFA\n - Montant net payé: 100 000 FCFA\n - Compte émetteur : s6LD9PmCJC\n - Operateur : CDE\n - Type d'operateur : Opérateur d'eau\n - No facture : CDE1425533\n\nCe message a été envoyé dans le mail de l'émetteur", + * "error":null + * } + * }, + * "user_remove_from_bank_to_wallet" : { + * "summary" : "User - Retrait du compte bancaire rattaché vers le wallet de l'utilisateur", + * "value" : { + * "status" : 200, + * "response" : "Retrait de la banque vers le wallet\nInformations de la transaction :\n - Numéro : 2XWQD7ZD9E7R\n - Montant de la transaction : 100 000 FCFA\n + - Frais : 0 FCFA\n - Montant net d\\'envoi: 100 000 FCFA\n - Compte émetteur : BZdsWkdfl\n - Banque : UBA Cameroon\n - IBAN : \n\nCe message a été envoyé dans le mail de l'émetteur", + * "error":null + * } + * }, + * } + * ) + * ) + * ) + */ + // Transactions dans le wallet iLink public function add(Request $request) { $transaction = new WalletIlinkTransaction(); @@ -1146,17 +1205,9 @@ class iLinkTransactionController extends Controller return $this->successResponse($message . trans('messages.sent_by_mail')); /** - * @OA\Post( - * path="/transactions/ilink", - * summary="Payer une facture chez un opérateur donné", - * tags={"Paiement d'une facture chez un opérateur"}, - * security={{"api_key":{}}}, - * @OA\RequestBody( - * description="Corps de la requete", - * required=true, - * @OA\MediaType( - * mediaType="application/json", * @OA\Schema( + * schema="user_payement_of_operator", + * title = "User - Payer un operateur", * @OA\Property(property="type", * type="integer", * enum = {19}, @@ -1198,22 +1249,6 @@ class iLinkTransactionController extends Controller * description="Mot de passe" * ) * ) - * ) - * ), - * @OA\Response( - * response=200, - * description="OK", - * @OA\JsonContent( - * ref="#/components/schemas/ApiResponse", - * example = { - * "status" : 200, - * "response" : "Paiement d'une facture chez un operateur\nInformations de la transaction :\n - Numéro : 7S9PZO26VAQ3\n - Montant de la transaction : 100 000 FCFA\n - - Frais : 0 FCFA\n - Montant net payé: 100 000 FCFA\n - Compte émetteur : s6LD9PmCJC\n - Operateur : CDE\n - Type d'operateur : Opérateur d'eau\n - No facture : CDE1425533\n\nCe message a été envoyé dans le mail de l'émetteur", - * "error":null - * } - * ) - * ) - * ) */ case 19: // User - Payer un operateur $this->validate($request, [ @@ -1273,6 +1308,81 @@ class iLinkTransactionController extends Controller $this->sendMail($user->email, trans('messages.successful_transaction'), $message); return $this->successResponse($message . trans('messages.sent_by_mail')); + /** + * @OA\Schema( + * schema="user_remove_from_bank_to_wallet", + * title="User - Retrait de la banque vers le wallet", + * @OA\Property(property="type", + * type="integer", + * enum = {20}, + * default = 20, + * description="Type de la transaction" + * ), + * @OA\Property(property="id_wallet_user", + * type="integer", + * example=9, + * description="ID du wallet de l'utilisateur" + * ), + * @OA\Property(property="montant", + * type="number", + * example = 10000, + * description="Montant de la transaction" + * ), + * @OA\Property(property="password", + * type="string", + * description="Mot de passe" + * ) + * ) + */ + case 20: //User - Retrait de la banque vers le wallet + +// $this->validate($request, [ +// 'code_retrait' => 'required', +// ]); + + $user = $walletUser->user; + if (!$this->checkPassword($request->password, $user->encrypted_password, $user->salt)) + return $this->errorResponse(trans('messages.incorrect_user_password')); + + $rep = $this->checkUserIdentification($user->id); + if ($rep instanceof JsonResponse) + return $rep; + + if (!(isset($user->iban) && isset($user->id_bank_country))) + return $this->errorResponse(trans('errors.wallet_not_linked_to_bank_account')); + + // Envoyer la trame vers banque pour debiter le compte client et recharger le compte bancaire de iLink + // -----> + + $transaction->frais = $frais = 0; + $transaction->taxe = $taxe = 0; +// $walletUser->balance += $transaction->montant; + $transaction->commission_banque = 0; + + $transaction->commission_hyp = 0; + + $transaction->id_wallet_hyp = $walletHyperviseur->id; + $transaction->frais = $frais; + $transaction->date = new \DateTime(); + $transaction->bank = $user->bank->operators_country->operator->nom; + $transaction->country = $user->bank->network->country->name; + $transaction->id_bank = $user->id_bank_country; + $transaction->iban = $user->iban; +// $walletUser->save(); + $transaction->id_transaction = $this->getTransactionID(); +// $transaction->save(); + + Log::info('-------------------------- User - Retrait de la banque vers le wallet ------------------------------------'); + Log::info($transaction->toArray()); + Log::info('------------------------------------------------------------------------------------------------'); + + $message = trans('messages.successful_user_remove_from_bank_to_wallet', + ['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country), + 'net' => $this->toMoney($transaction->montant, $init_country), 'iban' => $user->iban, 'fees' => $this->toMoney($frais, $init_country), + 'sender_code' => $user->user_code, 'bank' => $transaction->bank, 'country' => $transaction->country]); + $this->sendMail($user->email, trans('messages.successful_transaction'), $message); + return $this->successResponse($message . trans('messages.sent_by_mail')); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 63f3785..9b53b10 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -32,6 +32,11 @@ use Illuminate\Database\Eloquent\Model; * @property Carbon $date_created * @property int $network_id * @property int $group_id + * @property float $balance_credit + * @property float $balance_epargne + * @property Carbon|null $date_adhesion + * @property int|null $id_bank_country + * @property string|null $iban * * @property Collection|Identification[] $identifications * @property Collection|WalletsUser[] $wallets_users @@ -47,13 +52,17 @@ class User extends Model 'solde' => 'float', 'active' => 'int', 'network_id' => 'int', - 'group_id' => 'int' + 'group_id' => 'int', + 'balance_credit' => 'float', + 'balance_epargne' => 'float', + 'id_bank_country' => 'int' ]; protected $dates = [ 'expiration_date', 'date_modified', - 'date_created' + 'date_created', + 'date_adhesion' ]; protected $hidden = [ @@ -76,7 +85,13 @@ class User extends Model 'validation_code', 'active', 'date_modified', - 'date_created' + 'date_created', + 'group_id', + 'balance_credit', + 'balance_epargne', + 'date_adhesion', + 'id_bank_country', + 'iban' ]; public function identifications() @@ -93,4 +108,9 @@ class User extends Model { return $this->belongsTo(Network::class, 'network_id'); } + + public function bank() + { + return $this->belongsTo(NetworksOperator::class, 'id_bank_country', 'id_operator_country'); + } } diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php index 615ff1b..32fd42b 100755 --- a/resources/lang/en/errors.php +++ b/resources/lang/en/errors.php @@ -81,4 +81,5 @@ Paying network : :network :country', "not_banking_operator" => "This operator is not a banking operator", "operator_not_associated_with_network" => "This operator is not associated with your network", "not_matching_operator" => "This operator is not a :type_operator", + "wallet_not_linked_to_bank_account" => "Your wallet is not linked to a bank account", ]; diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index 0f42064..7213411 100755 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -289,4 +289,14 @@ Transaction Information: - Operator : :operator - Type of operator : :type_operator - Invoice number : :invoice_number", + 'successful_bank_account_attachment' => 'Connection of your bank account completed', + 'successful_user_remove_from_bank_to_wallet' => 'Withdrawal from the bank to the wallet +Transaction Information: + - Number: :id_transaction + - Amount of the transaction: :amount + - Fees: :fees + - Net shipping amount:: net + - Issuer account: :sender_code + - Bank : :bank :country + - IBAN: :iban', ]; diff --git a/resources/lang/fr/errors.php b/resources/lang/fr/errors.php index 6b89e31..d49c85a 100755 --- a/resources/lang/fr/errors.php +++ b/resources/lang/fr/errors.php @@ -81,4 +81,5 @@ Réseau payeur : :network :country', "not_banking_operator" => "Cet opérateur n'est pas un opérateur bancaire", "operator_not_associated_with_network" => "Cette operateur n'est pas associé à votre réseau", "not_matching_operator" => "Cet opérateur n'est pas un :type_operator", + "wallet_not_linked_to_bank_account" => "Votre wallet n'est pas rattaché à un compte bancaire", ]; diff --git a/resources/lang/fr/messages.php b/resources/lang/fr/messages.php index db18a59..e0bbe43 100755 --- a/resources/lang/fr/messages.php +++ b/resources/lang/fr/messages.php @@ -290,4 +290,14 @@ Informations de la transaction : - Operateur : :operator - Type d'operateur : :type_operator - No facture : :no_facture", + 'successful_bank_account_attachment' => 'Rattachement de votre compte bancaire effectué', + 'successful_user_remove_from_bank_to_wallet' => "Retrait de la banque vers le wallet +Informations de la transaction : + - Numéro : :id_transaction + - Montant de la transaction : :amount + - Frais : :fees + - Montant net d\'envoi: :net + - Compte émetteur : :sender_code + - Banque : :bank :country + - IBAN : :iban", ]; diff --git a/routes/web.php b/routes/web.php index cc88242..c643e3f 100755 --- a/routes/web.php +++ b/routes/web.php @@ -71,6 +71,8 @@ $router->group(['prefix' => '', 'middleware' => 'auth'], function () use ($route $router->get('{id_user}', 'WalletController@showWalletUser'); //Liste des operateurs $router->get('operators/{type}/{id_wallet_network}', 'WalletController@getWalletOperators'); + $router->get('banks_for_link/{id_wallet_network}', 'WalletController@getBanksInNetworkForLink'); + $router->post('link_bank_account', 'WalletController@linkBankAccount'); }); });