From 666e5169735b3c959515f8264ad80489462aad6c Mon Sep 17 00:00:00 2001 From: root Date: Mon, 2 Feb 2026 17:55:57 +0100 Subject: [PATCH] finish with the integration of create user account fonction --- .env.example | 11 ++ app/Http/Controllers/WalletController.php | 148 ++++++++++++++---- app/Mail/BankAccountActivated.php | 28 ++++ app/Models/UserBankAccount.php | 2 +- resources/lang/en/errors.php | 3 + resources/lang/en/messages.php | 5 +- resources/lang/fr/errors.php | 3 + resources/lang/fr/messages.php | 2 + .../emails/bank_account_activated.blade.php | 21 +++ routes/web.php | 1 + 10 files changed, 189 insertions(+), 35 deletions(-) create mode 100644 app/Mail/BankAccountActivated.php create mode 100644 resources/views/emails/bank_account_activated.blade.php diff --git a/.env.example b/.env.example index b65b431..765b231 100755 --- a/.env.example +++ b/.env.example @@ -48,3 +48,14 @@ SWAGGER_DOCS_TOKEN=ZfMqCAdHHrSH8ADdXreIejgjJtOwsH4K SENTRY_LARAVEL_DSN=https://9d6f6b6a24514295910a3b0e5bdd1449@o1053292.ingest.sentry.io/4504848762863616 SENTRY_TRACES_SAMPLE_RATE=1 + +# Configuration API GBS +BANK_API_BASE_URL=http://192.168.1.173:8084/api/v1 +BANK_API_USERNAME=sa +BANK_API_PASSWORD=Sa123456 +BANK_API_BRANCH_CODE=99001 +# Configuration API Bank Link +BANK_LINK_API_BASE_URL=http://192.168.1.173:2087/api/v1 +BANK_LINK_API_LOGIN=admin +BANK_LINK_API_PASSWORD=admin + diff --git a/app/Http/Controllers/WalletController.php b/app/Http/Controllers/WalletController.php index 5dfb26d..f710612 100755 --- a/app/Http/Controllers/WalletController.php +++ b/app/Http/Controllers/WalletController.php @@ -473,7 +473,8 @@ INNER JOIN countries c ON oc.id_country = c.id INNER JOIN type_operators top ON { $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 LIKE '%bank%' AND oc.id_country = :id_country ;", ['id_network' => $id_wallet_network, 'id_country' => $id_country]); +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 LIKE '%bank%' AND oc.id_country = :id_country ;", ['id_network' => $id_wallet_network, 'id_country' => $id_country]); return $this->successResponse($banks); } @@ -696,22 +697,23 @@ INNER JOIN countries c ON oc.id_country = c.id INNER JOIN type_operators top ON ->where('id_operator_country', $request->id_operator) ->first(); - if (isset($exist_user_request_create_account)){ - if ($exist_user_request_create_account->status == 'pending') { + if ($exist_user_request_create_account){ + if ($exist_user_request_create_account->status == 'pending' && $exist_user_request_create_account->customer_account_type_id == $request->account_type) { return $this->errorResponse(trans('errors.request_create_account_already_sended')); } } + if(isset($exist_user_request_create_account)){ if ($exist_user_request_create_account->status == 'active') { return $this->errorResponse(trans('messages.user_already_has_bank_account_with_this_operator', ['user_lastname' => $user->lastname])); } } $bank_name = $network_bank->operators_country->operator->nom; - $account_type = CustomerAccountType::where('product', $request->account_type)->first(); + $account_type = CustomerAccountType::where('id', $request->account_type)->first(); if (!$account_type) { - return $this->errorResponse(trans('errors.invalid_account_type')); + return $this->errorResponse(trans('errors.account_type_not_found')); } $bankAccount = new UserBankAccount(); @@ -747,7 +749,7 @@ INNER JOIN countries c ON oc.id_country = c.id INNER JOIN type_operators top ON Log::info('Demande de creation de compte bancaire enregistree avec succes'); // Envoi de notification par email - Mail::to($user->email)->send(new BankAccountCreatedMail($bankAccount, $bank_name, $user)); + // Mail::to($user->email)->send(new BankAccountCreatedMail($bankAccount, $bank_name, $user)); Log::info('Mail envoye a l’utilisateur : ' . $user->email); } catch (\Exception $e) { @@ -772,26 +774,40 @@ INNER JOIN countries c ON oc.id_country = c.id INNER JOIN type_operators top ON ]); $bank_account = UserBankAccount::where('id', $request->id)->with('user')->first(); + $user = $bank_account->user; if (!$bank_account) { return $this->errorResponse(trans('errors.account_type_not_found')); } - $user = $bank_account->user; - $account_type = CustomerAccountType::where('id', $bank_account->customer_account_type_id)->first(); + if(!$user){ + return $this->errorResponse(trans('errors.user_not_found')); + } - $apiUrl = "http://192.168.1.173:8084/api/v1/clients/create-with-account"; + $account_type = CustomerAccountType::where('id', $bank_account->customer_account_type_id)->first(); + if (!$account_type) { + return $this->errorResponse(trans('errors.account_type_not_found')); + } + $name_of_account_type = $account_type->name; + + // --- Récupération des configurations depuis le .env --- + $baseUrl = env('BANK_API_BASE_URL', 'http://192.168.1.173:8084/api/v1'); + $username = env('BANK_API_USERNAME', 'sa'); + $password = env('BANK_API_PASSWORD', 'Sa123456'); + $branchCode = env('BANK_API_BRANCH_CODE', '99001'); + + $apiUrl = $baseUrl . '/clients/create-with-account'; $payload = [ 'phoneNumber' => $bank_account->phone_number ?? ($user->phone ?? ''), 'email' => $user->email ?? '', 'fullname' => $bank_account->firstname . ' ' . $bank_account->lastname, - 'branchCode' => '99001', + 'branchCode' => $branchCode, 'dateOfBirth' => $bank_account->birth_date, 'isMarried' => ($bank_account->marital_status === 'marie') ? 'Yes' : 'No', 'nameOfSpouse' => $bank_account->spouse_name ?? '', 'city' => $bank_account->birth_city ?? '', - 'accountType' => $account_type->name, + 'accountType' => $name_of_account_type, 'originalNationality' => $bank_account->nationality, 'countryOfResidence' => $bank_account->birth_country, 'employersName' => $bank_account->employer_name ?? '', @@ -807,12 +823,12 @@ INNER JOIN countries c ON oc.id_country = c.id INNER JOIN type_operators top ON $client = new Client(); $response = $client->post($apiUrl, [ - 'auth' => ['sa', 'Sa123456'], + 'auth' => [$username, $password], 'json' => $payload, 'headers' => [ 'Accept' => 'application/json', ], - 'connect_timeout' => 30, + 'connect_timeout' => 60, ]); $result = json_decode($response->getBody(), true); @@ -820,16 +836,14 @@ INNER JOIN countries c ON oc.id_country = c.id INNER JOIN type_operators top ON if ($response->getStatusCode() == 200 || $response->getStatusCode() == 201) { $bank_account->update([ 'status' => 'actived', - 'reason' => 'Compte activé avec succès !' + 'reason' => 'Compte activé avec succès !', + 'account_number' => $result['accountNumber'], + 'customer_number' => $result['clientCode'] ]); - return response()->json([ - 'status' => 'success', - 'message' => 'Données et liens envoyés avec succès', - 'customer_number' => $result['customerNumber'] ?? null, - 'account_number' => $result['accountNumber'] ?? null - ]); - Log::info('Compte bancaire utilisateur activé avec succès : ' . $result); + // Mail::to($user->email)->send(new BankAccountActivated($bank_account,$name_of_account_type)); + + return $this->successResponse(trans('messages.user_bank_account_activated_successfully'),200); } } catch (RequestException $e) { @@ -840,18 +854,8 @@ INNER JOIN countries c ON oc.id_country = c.id INNER JOIN type_operators top ON 'reason' => 'Erreur API externe : ' . $errorBody ]); - return response()->json([ - 'status' => 'error', - 'message' => 'L’API distante a refusé la requête', - 'details' => json_decode($errorBody) ?? $errorBody - ], 500); - - } catch (\Exception $e) { - return response()->json([ - 'status' => 'error', - 'message' => 'Erreur lors de l’appel API', - 'error' => $e->getMessage() - ], 500); + Log::error('Error API externe pour ID ' . $bank_account->id . ' : ' . $errorBody); + return $this->errorResponse('Error_occurred', 500); } } @@ -861,4 +865,82 @@ INNER JOIN countries c ON oc.id_country = c.id INNER JOIN type_operators top ON return $this->successResponse($account_types); } + + + public function validateLinkUserBankAccount(Request $request) + { + $this->validate($request, [ + 'iban' => 'nullable|string', + 'code_client' => 'required', + 'id_transaction' => 'required' + ]); + + $user_bank_account_verfication = UsersBankingAccountVerification::where('id_transaction', $request->id_transaction)->first(); + if (!$user_bank_account_verfication) { + return $this->errorResponse('errors.transaction_not_found', 404); + } + + $baseUrl = env('BANK_LINK_API_BASE_URL', 'http://192.168.1.173:2087/api/v1'); + $login = env('BANK_LINK_API_LOGIN', 'admin'); + $password = env('BANK_LINK_API_PASSWORD', 'admin'); + + $authApi = $baseUrl . '/auth/authenticate'; + $accountApi = $baseUrl . '/account/getOne/' . $request->code_client; + + try { + $client = new Client(); + + $authResponse = $client->post($authApi, [ + 'json' => [ + 'login' => $login, + 'password' => $password + ], + 'connect_timeout' => 60, + ]); + + $authResult = json_decode($authResponse->getBody(), true); + $token = $authResult['token'] ?? null; + + if (!$token) { + Log::error('Token introuvable dans la réponse auth pour transaction: ' . $request->id_transaction); + return $this->errorResponse(trans('errors.token_not_found')); + } + + $response = $client->get($accountApi, [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + 'Accept' => 'application/json', + ], + 'connect_timeout' => 60, + ]); + + $result = json_decode($response->getBody(), true); + + if ($response->getStatusCode() == 200 || $response->getStatusCode() == 201) { + $user_bank_account_verfication->update([ + 'is_verified' => 1, + 'was_traited' => 1 + ]); + + Log::info('Compte bancaire rattaché avec succès pour transaction: ' . $request->id_transaction); + + return $this->successResponse(trans('messages.create_bank_account_linked_successfully'), $result); + } + + } catch (RequestException $e) { + $errorBody = $e->hasResponse() ? (string) $e->getResponse()->getBody() : $e->getMessage(); + + $user_bank_account_verfication->update([ + 'is_verified' => 2, + 'was_traited' => 0, + ]); + + Log::error('Erreur API Bank Link pour transaction ' . $request->id_transaction . ' : ' . $errorBody); + + return $this->errorResponse(trans('errors.Error_occurred')); + } catch (\Exception $e) { + Log::error('Erreur inattendue Bank Link : ' . $e->getMessage()); + return $this->errorResponse('Internal Server Error'); + } + } } diff --git a/app/Mail/BankAccountActivated.php b/app/Mail/BankAccountActivated.php new file mode 100644 index 0000000..35826a9 --- /dev/null +++ b/app/Mail/BankAccountActivated.php @@ -0,0 +1,28 @@ +bankAccount = $bankAccount; + $this->bankName = $bank_name; + } + + public function build() + { + return $this->subject('Votre compte bancaire a été activé') + ->view('emails.bank_account_activated'); + } +} diff --git a/app/Models/UserBankAccount.php b/app/Models/UserBankAccount.php index 11d68e7..0895dff 100755 --- a/app/Models/UserBankAccount.php +++ b/app/Models/UserBankAccount.php @@ -46,7 +46,7 @@ class UserBankAccount extends Model public function user() { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class, 'id_user'); } public function bank() diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php index 2f95300..255cc6e 100755 --- a/resources/lang/en/errors.php +++ b/resources/lang/en/errors.php @@ -96,5 +96,8 @@ Paying network : :network :country', 'request_create_account_already_sended' => "A request to create a bank account has already been sent for this bank", 'user_not_found' => "User not found", 'account_type_not_found' => "Account type not found", + 'transaction_not_found' => 'Transaction not found', + 'token_not_found' => 'Authentication token not found', + 'Error_occurred' => 'An error occurred: ', ]; diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index 6f68702..c12f504 100755 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -333,5 +333,8 @@ Transaction information : - Card number: :cart_number - Country of destination: :final_country - Destination account: :sender_code - - Amount: :net_final" + - Amount: :net_final", +'create_bank_account_linked_successfully' => 'Bank account successfully linked to your user account', +'user_bank_account_activated_successfully' => 'User bank account activated successfully', + ]; diff --git a/resources/lang/fr/errors.php b/resources/lang/fr/errors.php index 8952b3d..dc63c48 100755 --- a/resources/lang/fr/errors.php +++ b/resources/lang/fr/errors.php @@ -96,4 +96,7 @@ Réseau payeur : :network :country', 'request_create_account_already_sended' => "Une demande de création de compte bancaire a déjà été envoyée pour cette banque", 'user_not_found' => "Utilisateur non trouvé", 'account_type_not_found' => "Type de compte non trouvé", + 'transaction_not_found' => 'Transaction non trouvée', + 'token_not_found' => 'Token d\'authentification non trouvé', + 'Error_occurred' => 'Une erreur est survenue: ', ]; diff --git a/resources/lang/fr/messages.php b/resources/lang/fr/messages.php index 9fddac5..a5df1ac 100755 --- a/resources/lang/fr/messages.php +++ b/resources/lang/fr/messages.php @@ -335,4 +335,6 @@ Informations de la transaction : - Pays de destination : :final_country - Compte destinataire : :sender_code - Montant : :net_final", + 'create_bank_account_linked_successfully' => 'Compte bancaire rattaché avec succès', + 'user_bank_account_activated_successfully' => 'Compte bancaire activé avec succès', ]; diff --git a/resources/views/emails/bank_account_activated.blade.php b/resources/views/emails/bank_account_activated.blade.php new file mode 100644 index 0000000..d34d69f --- /dev/null +++ b/resources/views/emails/bank_account_activated.blade.php @@ -0,0 +1,21 @@ + + + + Activation de compte + + +

Félicitations {{ $bankAccount->user->firstname }} !

+

Votre demande d'ouverture de compte a été acceptée et activée.

+ + + +

Vous pouvez désormais utiliser nos services bancaires dès maintenant.

+ +

Cordialement,
L'équipe SunEx.

+ + diff --git a/routes/web.php b/routes/web.php index 50cf09f..a0d71e9 100755 --- a/routes/web.php +++ b/routes/web.php @@ -76,6 +76,7 @@ $router->group(['prefix' => '', 'middleware' => 'auth'], function () use ($route $router->post('create_user_bank_account', 'WalletController@createUserBankAccount'); $router->post('activate_user_bank_account', 'WalletController@activateUserBankAccount'); $router->get('customer_account_type', 'WalletController@getCustomerAccountTypes'); + $router->post('validate_link_user_bank_account', 'WalletController@validateLinkUserBankAccount'); }); });