2020-04-15 23:08:09 +00:00
< ? php
namespace App\Http\Controllers ;
2020-11-13 06:39:24 +00:00
use App\Models\Network ;
use App\Models\NetworksOperator ;
2020-10-05 16:32:31 +00:00
use App\Models\User ;
2020-12-01 10:51:05 +00:00
use App\Models\UsersBankingAccountVerification ;
2020-10-05 16:32:31 +00:00
use App\Models\WalletAgent ;
2020-06-01 18:31:25 +00:00
use App\Models\WalletsUser ;
2025-11-13 15:48:14 +00:00
use App\Models\Operator ;
use App\Models\OperatorsCountry ;
use App\Models\UserBankAccount ;
2020-04-17 15:26:12 +00:00
use Illuminate\Http\Request ;
use Illuminate\Http\Response ;
2020-04-15 23:08:09 +00:00
use Illuminate\Support\Facades\DB ;
2020-11-28 08:22:30 +00:00
use Illuminate\Support\Facades\Log ;
2025-11-14 11:20:10 +00:00
use App\Mail\BankAccountCreatedMail ;
2026-02-18 14:21:42 +00:00
use App\Mail\BankAccountActivated ;
2026-01-30 14:59:41 +00:00
use App\Models\CustomerAccountType ;
2025-11-14 11:20:10 +00:00
use Illuminate\Support\Facades\Mail ;
2026-01-30 14:59:41 +00:00
use GuzzleHttp\Client ;
2026-02-18 14:21:42 +00:00
use GuzzleHttp\Exception\RequestException ;
2020-04-15 23:08:09 +00:00
class WalletController extends Controller
{
/**
* Create a new controller instance .
*
* @ return void
*/
public function __construct ()
{
//
}
2020-04-17 15:26:12 +00:00
public function activated ( $id_agent )
{
2020-11-13 14:21:17 +00:00
$networks = DB :: select ( ' SELECT ne . name as network , cc . name AS country , cc . code_country , cc . currency_code , w . id , w . balance_princ , w . balance_com , w . created_date , cw . type ,
2021-12-22 09:42:27 +00:00
cw . taux_com_client_depot , na . id AS network_agent_id , cg . category , cw . has_nano_credit FROM agents ag
2020-04-21 14:07:57 +00:00
INNER JOIN networks_agents na ON ag . id = na . agent_id INNER JOIN codeGenerer cg ON cg . id = na . codeGenerer_id INNER JOIN networks ne ON ne . id = na . network_id INNER JOIN configWallet cw ON ne . id = cw . id_network
2020-09-28 09:43:39 +00:00
INNER JOIN countries_currencies cc ON ne . country_id = cc . id LEFT JOIN wallets w ON na . id = w . id_networkAgent WHERE ag . id = : id AND network_id IN (
2020-04-17 15:26:12 +00:00
SELECT networks . id FROM networks LEFT JOIN configWallet ON configWallet . id_network = networks . id WHERE status = 1 AND id_network IS NOT NULL ) ', [' id ' => $id_agent ]);
2020-04-15 23:08:09 +00:00
2020-05-05 08:27:08 +00:00
// Create wallet if is not exist
2020-05-05 20:42:57 +00:00
$category = null ;
2020-11-11 14:37:26 +00:00
if ( $networks ) {
2020-05-05 08:27:08 +00:00
$reload = false ;
$id = $id_agent ;
2020-11-11 14:37:26 +00:00
foreach ( $networks as $network ) {
2020-05-05 20:42:57 +00:00
$category = $network -> category ;
// Create wallet if is not exist
2020-11-11 14:37:26 +00:00
if ( ! $network -> id ) {
2020-11-13 14:21:17 +00:00
$datetime = $this -> getCurrentTimeByCountryCode ( $network -> code_country );
2021-12-22 09:42:27 +00:00
DB :: insert ( 'INSERT INTO wallets (id_networkAgent , created_date) VALUES (? , ?);' , [ $network -> network_agent_id , $datetime ]);
2020-05-05 08:27:08 +00:00
$reload = true ;
}
}
2020-11-11 14:37:26 +00:00
if ( $reload )
2020-05-05 08:27:08 +00:00
return $this -> activated ( $id );
}
2020-05-05 20:42:57 +00:00
2020-05-05 22:29:26 +00:00
2020-05-05 20:42:57 +00:00
// Return only single wallet if it is hypervisor or supervisor
2020-11-11 14:37:26 +00:00
if ( in_array ( $category , [ 'hyper' , 'super' ])) {
2020-05-05 22:29:26 +00:00
// Remove unnecessary fields
2022-02-15 15:31:08 +00:00
// $networks = $this->array_except($networks, ['category']);
2020-05-05 20:42:57 +00:00
return $this -> successResponse ( collect ( $networks ) -> first ());
2020-11-11 14:37:26 +00:00
} else {
2020-05-05 22:29:26 +00:00
// Remove unnecessary fields
2022-02-15 15:31:08 +00:00
$networks = $this -> array_except ( $networks , [ 'balance_princ' , 'balance_com' , 'created_date' , 'taux_com_client_depot' ]);
2020-05-05 20:42:57 +00:00
return $this -> successResponse ( $networks );
2020-05-05 22:29:26 +00:00
}
}
2020-11-11 14:37:26 +00:00
private function array_except ( $array , $keys )
{
foreach ( $array as $row ) {
foreach ( $keys as $key ) {
2020-05-05 22:29:26 +00:00
unset ( $row -> $key );
}
}
return $array ;
2020-04-15 23:08:09 +00:00
}
2020-04-17 15:26:12 +00:00
public function show ( $id_wallet )
{
2020-05-05 19:18:08 +00:00
// $wallet = Wallet::findOrFail($id_wallet);
2021-12-22 09:50:28 +00:00
$wallet = collect ( DB :: select ( ' SELECT wa . wallet_id AS id , wa . balance_princ , wa . balance_com , wa . created_date , wa . network , cw . taux_com_client_depot , c . name AS country , cw . type , wa . networks_agent_id as network_agent_id ,
2022-02-15 15:31:08 +00:00
c . currency_code , cw . id_network , cw . has_nano_credit , cG . category FROM wallet_agent wa INNER JOIN configWallet cw ON wa . network_id = cw . id_network INNER JOIN networks n ON n . id = wa . network_id
INNER JOIN countries_currencies c ON c . id = n . country_id INNER JOIN networks_agents na on na . id = wa . networks_agent_id INNER JOIN codeGenerer cG on na . codeGenerer_id = cG . id
2020-11-11 14:37:26 +00:00
WHERE wa . wallet_id = : id ', [' id ' => $id_wallet ])) -> first ();
2022-03-28 13:13:41 +00:00
if ( $wallet ) {
// Recuperer la config du nano santé
$nhConfig = collect ( DB :: select ( 'SELECT * FROM nh_networks_configs WHERE network_id = :network_id LIMIT 50' , [ 'network_id' => $wallet -> id_network ])) -> first ();
if ( isset ( $nhConfig )) {
$wallet -> password_validation = $nhConfig -> password_validation ? ? " MAX " ;
}
2020-05-05 19:18:08 +00:00
return $this -> successResponse ( $wallet );
2022-03-28 13:13:41 +00:00
} else
2020-11-11 14:37:26 +00:00
return $this -> errorResponse ( trans ( 'errors.model_not_found' , [ 'model' => 'wallet' ]), Response :: HTTP_BAD_REQUEST );
2020-04-17 15:26:12 +00:00
}
2020-04-21 14:07:57 +00:00
public function create ( Request $request )
{
$rules = [
2020-11-11 14:37:26 +00:00
'id_networkAgent' => 'required|integer|min:1'
2020-04-21 14:07:57 +00:00
];
2020-11-11 14:37:26 +00:00
$this -> validate ( $request , $rules );
2020-04-21 14:07:57 +00:00
DB :: insert ( 'INSERT INTO wallets (id_networkAgent) VALUES (?);' , [ $request -> id_networkAgent ]);
2020-04-28 16:12:05 +00:00
return $this -> successResponse ( trans ( 'messages.new_wallet_added' ));
2020-04-21 14:07:57 +00:00
}
2020-06-01 18:31:25 +00:00
// Wallets users iLink
2020-11-11 14:37:26 +00:00
public function showWalletUser ( $id_user )
{
2023-07-26 12:52:32 +00:00
$wallet = collect ( DB :: select ( ' SELECT wu .* , u . user_code , u . numero_carte , u . expiration_date , n2 . id as id_wallet_network , n2 . name as network , cc . name as country , cc . currency_code , card_cc . currency_code as card_currency_code , cw . has_nano_credit from wallets_users wu
2020-07-25 18:02:54 +00:00
INNER JOIN users u ON u . id = wu . idUser
INNER JOIN networks n1 ON n1 . id = u . network_id
INNER JOIN networks n2 ON n2 . country_id = n1 . country_id
INNER JOIN configWallet cw ON cw . id_network = n2 . id
INNER JOIN countries_currencies cc ON cc . id = n2 . country_id
2023-07-26 12:52:32 +00:00
LEFT JOIN countries_currencies card_cc ON card_cc . id = u . card_country_id
2020-10-05 16:32:31 +00:00
WHERE wu . idUser = : id_user AND cw . type = \ 'ilink\' LIMIT 1' , [ 'id_user' => $id_user ])) -> first ();
if ( $wallet ) {
2020-06-02 08:35:37 +00:00
return $this -> successResponse ( $wallet );
2020-06-16 05:49:49 +00:00
} else
2020-10-05 16:32:31 +00:00
return $this -> errorResponse ( trans ( 'errors.model_not_found' , [ 'model' => 'wallet' ]), Response :: HTTP_BAD_REQUEST );
}
//Les historiques globals des hyperviseur et superviseur
public function hyperHistory ( $id_network , Request $request )
{
2020-10-16 18:43:44 +00:00
$demandes = DB :: select ( " SELECT 'N' as type_historique, i.montant , i.user as destinataire , i.* FROM infos_users_demandes_credits i WHERE id_network = :id ; " , [ 'id' => $id_network ]);
$savings = DB :: select ( " SELECT 'E' as type_historique , i.montant , i.user as destinataire , i.* FROM infos_users_epargnes i WHERE id_network = :id; " , [ 'id' => $id_network ]);
2020-10-05 16:32:31 +00:00
2020-10-27 10:52:37 +00:00
$transactions = DB :: select ( " SELECT 'T' as type_historique, wit.id_transaction, tit.nom as operation_fr , tit.name as operation_en , wit.montant ,wit.nom_emetteur, wit.prenom_emetteur, wit.id_wallet_user,wit.frais,wit.taxe,wit.id_destinataire,
2020-10-05 16:32:31 +00:00
wit . nom_destinataire , wit . prenom_destinataire , wit . type , wit . id_wallet_user , wit . init_country , wit . final_country , wit . network_destinataire , wit . montant_net_final_country ,
wit . date as date_creation , wit . id , wit . numero_carte , wit . montant_net FROM wallet_ilink_transaction wit
INNER JOIN type_ilink_transaction tit ON wit . type = tit . id WHERE wit . network_emetteur = : id ; " , ['id' => $id_network ]);
$transactions_mapped = array_map ( function ( $data ) {
2020-10-27 10:52:37 +00:00
$data -> operation = app () -> isLocale ( 'en' ) ? $data -> operation_en : $data -> operation_fr ;
2020-10-16 16:21:27 +00:00
$date = $data -> date_creation ;
unset ( $data -> date_creation );
2020-10-05 16:32:31 +00:00
$wallet_user = isset ( $data -> id_wallet_user ) ? WalletsUser :: findOrFail ( $data -> id_wallet_user ) : null ;
$user_destinataire = isset ( $data -> id_destinataire ) ? User :: where ( 'user_code' , $data -> id_destinataire ) -> first () : null ;
$emetteur = $wallet_user ? $wallet_user -> user -> lastname . ' ' . $wallet_user -> user -> firstname : $data -> prenom_emetteur . ' ' . $data -> nom_emetteur ;
if ( ! $wallet_user && ! $data -> nom_emetteur )
$emetteur = $data -> numero_carte ;
$destinataire = in_array ( $data -> type , [ 12 , 16 ]) ? $emetteur : ( $user_destinataire ? $user_destinataire -> lastname . ' ' . $user_destinataire -> firstname :
$data -> prenom_destinataire . ' ' . $data -> nom_destinataire );
$data -> emetteur = $emetteur ;
$data -> destinataire = $destinataire ;
$data -> frais = $this -> toMoney ( $data -> frais + $data -> taxe , $data -> init_country );
$data -> montant_net_init = $this -> toMoney ( $data -> montant_net , $data -> init_country );
$data -> montant_net_final = $data -> montant_net_final_country ? $this -> toMoney ( $data -> montant_net_final_country , $data -> final_country ) : $data -> montant_net_init ;
2020-10-16 19:30:27 +00:00
$data -> montant2 = $this -> toMoney ( $data -> montant , $data -> init_country );
2020-10-05 16:32:31 +00:00
$data -> init_country = $this -> getCountryName ( $data -> init_country );
$data -> final_country = $data -> montant_net_final_country ? $this -> getCountryName ( $data -> final_country ) : '' ;
$data -> reseau_payeur = isset ( $data -> network_destinataire ) ? $this -> getNetworkName ( $data -> network_destinataire ) . ' ' . $data -> final_country : null ;
2020-10-16 18:13:56 +00:00
if ( $data -> type == 13 )
$data -> destinataire = $data -> numero_carte ;
2020-10-16 19:30:27 +00:00
if ( ctype_space ( $data -> destinataire )) {
$data -> destinataire = $data -> emetteur ;
}
2020-10-16 16:21:27 +00:00
$data -> date_creation = $date ;
2020-10-05 16:32:31 +00:00
unset ( $data -> type , $data -> id_wallet_user , $data -> network_destinataire , $data -> nom_destinataire , $data -> prenom_destinataire , $data -> taxe , $data -> numero_carte ,
2020-10-27 10:52:37 +00:00
$data -> montant_net_final_country , $data -> montant_net , $data -> nom_emetteur , $data -> prenom_emetteur , $data -> id_destinataire , $data -> operation_fr , $data -> operation_en );
2020-10-05 16:32:31 +00:00
return $data ;
}, $transactions );
// Supprimer les underscore sur les etats
$merge = array_map ( function ( $demand ) {
2020-11-20 16:25:54 +00:00
$demand -> etat = trans ( 'states.' . $demand -> etat );
if ( isset ( $demand -> type ))
$demand -> type = trans ( 'states.' . $demand -> type );
if ( isset ( $demand -> type_caution ))
$demand -> type_caution = trans ( 'states.' . $demand -> type_caution );
2020-10-05 16:32:31 +00:00
return $demand ;
}, array_merge ( $demandes , $savings ));
$result = array_merge ( $transactions_mapped , $merge );
usort ( $result , array ( $this , 'sortFunction' )); // Trier le tout par date
2020-10-07 09:07:11 +00:00
return $this -> successResponse ( $this -> arrayPaginator ( $result , $request ));
2020-10-05 16:32:31 +00:00
}
2020-11-20 16:25:54 +00:00
public function superHistory ( $agent_code , Request $request )
2020-10-05 16:32:31 +00:00
{
2020-11-20 16:25:54 +00:00
$walletSup = WalletAgent :: where ( 'codeMembre' , $agent_code ) -> firstOrFail ();
$id_wallet = $walletSup -> wallet_id ;
2020-10-05 16:32:31 +00:00
2020-10-16 18:43:44 +00:00
$demandes = DB :: select ( " SELECT 'N' as type_historique , i.montant ,i.user as destinataire , i.* FROM infos_users_demandes_credits i WHERE codeParrain = :code ; " , [ 'code' => $walletSup -> codeMembre ]);
2020-10-05 16:32:31 +00:00
2020-10-27 10:52:37 +00:00
$transactions = DB :: select ( " SELECT 'T' as type_historique, wit.id_transaction, tit.nom as operation_fr , tit.name as operation_en , wit.montant ,wit.nom_emetteur, wit.prenom_emetteur, wit.id_wallet_user,wit.frais,wit.taxe,wit.id_destinataire,
2020-10-05 16:32:31 +00:00
wit . nom_destinataire , wit . prenom_destinataire , wit . type , wit . id_wallet_user , wit . init_country , wit . final_country , wit . network_destinataire , wit . montant_net_final_country ,
wit . date as date_creation , wit . id , wit . numero_carte , wit . montant_net FROM wallet_ilink_transaction wit
INNER JOIN type_ilink_transaction tit ON wit . type = tit . id WHERE wit . id_wallet_sup = : id ; " , ['id' => $id_wallet ]);
$transactions_mapped = array_map ( function ( $data ) {
2020-10-27 10:52:37 +00:00
$data -> operation = app () -> isLocale ( 'en' ) ? $data -> operation_en : $data -> operation_fr ;
2020-10-16 16:21:27 +00:00
$date = $data -> date_creation ;
unset ( $data -> date_creation );
2020-10-05 16:32:31 +00:00
$wallet_user = isset ( $data -> id_wallet_user ) ? WalletsUser :: findOrFail ( $data -> id_wallet_user ) : null ;
$user_destinataire = isset ( $data -> id_destinataire ) ? User :: where ( 'user_code' , $data -> id_destinataire ) -> first () : null ;
$emetteur = $wallet_user ? $wallet_user -> user -> lastname . ' ' . $wallet_user -> user -> firstname : $data -> prenom_emetteur . ' ' . $data -> nom_emetteur ;
if ( ! $wallet_user && ! $data -> nom_emetteur )
$emetteur = $data -> numero_carte ;
$destinataire = in_array ( $data -> type , [ 12 , 16 ]) ? $emetteur : ( $user_destinataire ? $user_destinataire -> lastname . ' ' . $user_destinataire -> firstname :
$data -> prenom_destinataire . ' ' . $data -> nom_destinataire );
$data -> emetteur = $emetteur ;
$data -> destinataire = $destinataire ;
$data -> frais = $this -> toMoney ( $data -> frais + $data -> taxe , $data -> init_country );
$data -> montant_net_init = $this -> toMoney ( $data -> montant_net , $data -> init_country );
$data -> montant_net_final = $data -> montant_net_final_country ? $this -> toMoney ( $data -> montant_net_final_country , $data -> final_country ) : $data -> montant_net_init ;
2020-10-16 19:30:27 +00:00
$data -> montant2 = $this -> toMoney ( $data -> montant , $data -> init_country );
2020-10-05 16:32:31 +00:00
$data -> init_country = $this -> getCountryName ( $data -> init_country );
$data -> final_country = $data -> montant_net_final_country ? $this -> getCountryName ( $data -> final_country ) : '' ;
$data -> reseau_payeur = isset ( $data -> network_destinataire ) ? $this -> getNetworkName ( $data -> network_destinataire ) . ' ' . $data -> final_country : null ;
2020-10-16 18:13:56 +00:00
if ( $data -> type == 13 )
$data -> destinataire = $data -> numero_carte ;
2020-10-16 16:21:27 +00:00
$data -> date_creation = $date ;
2020-10-05 16:32:31 +00:00
unset ( $data -> type , $data -> id_wallet_user , $data -> network_destinataire , $data -> nom_destinataire , $data -> prenom_destinataire , $data -> taxe , $data -> numero_carte ,
2020-10-27 10:52:37 +00:00
$data -> montant_net_final_country , $data -> montant_net , $data -> nom_emetteur , $data -> prenom_emetteur , $data -> id_destinataire , $data -> operation_fr , $data -> operation_en );
2020-10-05 16:32:31 +00:00
return $data ;
}, $transactions );
// Supprimer les underscore sur les etats
$demandes_mapped = array_map ( function ( $demand ) {
2020-11-20 16:25:54 +00:00
$demand -> etat = trans ( 'states.' . $demand -> etat );
$demand -> type_caution = trans ( 'states.' . $demand -> type_caution );
2020-10-05 16:32:31 +00:00
return $demand ;
}, $demandes );
$result = array_merge ( $transactions_mapped , $demandes_mapped );
usort ( $result , array ( $this , 'sortFunction' )); // Trier le tout par date
2020-10-07 09:07:11 +00:00
return $this -> successResponse ( $this -> arrayPaginator ( $result , $request ));
2020-06-01 18:31:25 +00:00
}
2020-10-16 07:40:10 +00:00
// Routes sans pagination
public function allHyperHistory ( $id_network )
{
2020-10-16 18:43:44 +00:00
$demandes = DB :: select ( " SELECT 'N' as type_historique, i.montant , i.user as destinataire , i.* FROM infos_users_demandes_credits i WHERE id_network = :id ; " , [ 'id' => $id_network ]);
$savings = DB :: select ( " SELECT 'E' as type_historique , i.montant , i.user as destinataire , i.* FROM infos_users_epargnes i WHERE id_network = :id; " , [ 'id' => $id_network ]);
2020-10-16 07:40:10 +00:00
2020-10-27 10:52:37 +00:00
$transactions = DB :: select ( " SELECT 'T' as type_historique, wit.id_transaction, tit.nom as operation_fr , tit.name as operation_en , wit.montant ,wit.nom_emetteur, wit.prenom_emetteur, wit.id_wallet_user,wit.frais,wit.taxe,wit.id_destinataire,
2020-10-16 07:40:10 +00:00
wit . nom_destinataire , wit . prenom_destinataire , wit . type , wit . id_wallet_user , wit . init_country , wit . final_country , wit . network_destinataire , wit . montant_net_final_country ,
wit . date as date_creation , wit . id , wit . numero_carte , wit . montant_net FROM wallet_ilink_transaction wit
INNER JOIN type_ilink_transaction tit ON wit . type = tit . id WHERE wit . network_emetteur = : id ; " , ['id' => $id_network ]);
$transactions_mapped = array_map ( function ( $data ) {
2020-10-27 10:52:37 +00:00
$data -> operation = app () -> isLocale ( 'en' ) ? $data -> operation_en : $data -> operation_fr ;
2020-10-16 16:21:27 +00:00
$date = $data -> date_creation ;
unset ( $data -> date_creation );
2020-10-16 07:40:10 +00:00
$wallet_user = isset ( $data -> id_wallet_user ) ? WalletsUser :: findOrFail ( $data -> id_wallet_user ) : null ;
$user_destinataire = isset ( $data -> id_destinataire ) ? User :: where ( 'user_code' , $data -> id_destinataire ) -> first () : null ;
$emetteur = $wallet_user ? $wallet_user -> user -> lastname . ' ' . $wallet_user -> user -> firstname : $data -> prenom_emetteur . ' ' . $data -> nom_emetteur ;
if ( ! $wallet_user && ! $data -> nom_emetteur )
$emetteur = $data -> numero_carte ;
$destinataire = in_array ( $data -> type , [ 12 , 16 ]) ? $emetteur : ( $user_destinataire ? $user_destinataire -> lastname . ' ' . $user_destinataire -> firstname :
$data -> prenom_destinataire . ' ' . $data -> nom_destinataire );
$data -> emetteur = $emetteur ;
$data -> destinataire = $destinataire ;
$data -> frais = $this -> toMoney ( $data -> frais + $data -> taxe , $data -> init_country );
$data -> montant_net_init = $this -> toMoney ( $data -> montant_net , $data -> init_country );
$data -> montant_net_final = $data -> montant_net_final_country ? $this -> toMoney ( $data -> montant_net_final_country , $data -> final_country ) : $data -> montant_net_init ;
2020-10-16 19:30:27 +00:00
$data -> montant2 = $this -> toMoney ( $data -> montant , $data -> init_country );
2020-10-16 07:40:10 +00:00
$data -> init_country = $this -> getCountryName ( $data -> init_country );
$data -> final_country = $data -> montant_net_final_country ? $this -> getCountryName ( $data -> final_country ) : '' ;
$data -> reseau_payeur = isset ( $data -> network_destinataire ) ? $this -> getNetworkName ( $data -> network_destinataire ) . ' ' . $data -> final_country : null ;
2020-10-16 18:13:56 +00:00
if ( $data -> type == 13 )
$data -> destinataire = $data -> numero_carte ;
2020-10-16 19:30:27 +00:00
if ( ctype_space ( $data -> destinataire )) {
$data -> destinataire = $data -> emetteur ;
}
2020-10-16 16:21:27 +00:00
$data -> date_creation = $date ;
2020-10-16 07:40:10 +00:00
unset ( $data -> type , $data -> id_wallet_user , $data -> network_destinataire , $data -> nom_destinataire , $data -> prenom_destinataire , $data -> taxe , $data -> numero_carte ,
2020-10-27 10:52:37 +00:00
$data -> montant_net_final_country , $data -> montant_net , $data -> nom_emetteur , $data -> prenom_emetteur , $data -> id_destinataire , $data -> operation_fr , $data -> operation_en );
2020-10-16 07:40:10 +00:00
return $data ;
}, $transactions );
// Supprimer les underscore sur les etats
$merge = array_map ( function ( $demand ) {
2020-11-20 16:25:54 +00:00
$demand -> etat = trans ( 'states.' . $demand -> etat );
if ( isset ( $demand -> type ))
$demand -> type = trans ( 'states.' . $demand -> type );
if ( isset ( $demand -> type_caution ))
$demand -> type_caution = trans ( 'states.' . $demand -> type_caution );
2020-10-16 07:40:10 +00:00
return $demand ;
}, array_merge ( $demandes , $savings ));
$result = array_merge ( $transactions_mapped , $merge );
usort ( $result , array ( $this , 'sortFunction' )); // Trier le tout par date
return $this -> successResponse ( $result );
}
2020-11-20 16:25:54 +00:00
public function allSuperHistory ( $agent_code )
2020-10-16 07:40:10 +00:00
{
2020-11-20 16:25:54 +00:00
$walletSup = WalletAgent :: where ( 'codeMembre' , $agent_code ) -> firstOrFail ();
$id_wallet = $walletSup -> wallet_id ;
2020-10-16 07:40:10 +00:00
2020-10-16 18:43:44 +00:00
$demandes = DB :: select ( " SELECT 'N' as type_historique, i.montant , i.user as destinataire , i.* FROM infos_users_demandes_credits i WHERE codeParrain = :code ; " , [ 'code' => $walletSup -> codeMembre ]);
2020-10-16 07:40:10 +00:00
2020-10-27 10:52:37 +00:00
$transactions = DB :: select ( " SELECT 'T' as type_historique, wit.id_transaction, tit.nom as operation_fr , tit.name as operation_en , wit.montant ,wit.nom_emetteur, wit.prenom_emetteur, wit.id_wallet_user,wit.frais,wit.taxe,wit.id_destinataire,
2020-10-16 07:40:10 +00:00
wit . nom_destinataire , wit . prenom_destinataire , wit . type , wit . id_wallet_user , wit . init_country , wit . final_country , wit . network_destinataire , wit . montant_net_final_country ,
wit . date as date_creation , wit . id , wit . numero_carte , wit . montant_net FROM wallet_ilink_transaction wit
INNER JOIN type_ilink_transaction tit ON wit . type = tit . id WHERE wit . id_wallet_sup = : id ; " , ['id' => $id_wallet ]);
$transactions_mapped = array_map ( function ( $data ) {
2020-10-27 10:52:37 +00:00
$data -> operation = app () -> isLocale ( 'en' ) ? $data -> operation_en : $data -> operation_fr ;
2020-10-16 16:21:27 +00:00
$date = $data -> date_creation ;
unset ( $data -> date_creation );
2020-10-16 07:40:10 +00:00
$wallet_user = isset ( $data -> id_wallet_user ) ? WalletsUser :: findOrFail ( $data -> id_wallet_user ) : null ;
$user_destinataire = isset ( $data -> id_destinataire ) ? User :: where ( 'user_code' , $data -> id_destinataire ) -> first () : null ;
$emetteur = $wallet_user ? $wallet_user -> user -> lastname . ' ' . $wallet_user -> user -> firstname : $data -> prenom_emetteur . ' ' . $data -> nom_emetteur ;
if ( ! $wallet_user && ! $data -> nom_emetteur )
$emetteur = $data -> numero_carte ;
$destinataire = in_array ( $data -> type , [ 12 , 16 ]) ? $emetteur : ( $user_destinataire ? $user_destinataire -> lastname . ' ' . $user_destinataire -> firstname :
$data -> prenom_destinataire . ' ' . $data -> nom_destinataire );
$data -> emetteur = $emetteur ;
$data -> destinataire = $destinataire ;
$data -> frais = $this -> toMoney ( $data -> frais + $data -> taxe , $data -> init_country );
$data -> montant_net_init = $this -> toMoney ( $data -> montant_net , $data -> init_country );
$data -> montant_net_final = $data -> montant_net_final_country ? $this -> toMoney ( $data -> montant_net_final_country , $data -> final_country ) : $data -> montant_net_init ;
2020-10-16 19:30:27 +00:00
$data -> montant2 = $this -> toMoney ( $data -> montant , $data -> init_country );
2020-10-16 07:40:10 +00:00
$data -> init_country = $this -> getCountryName ( $data -> init_country );
$data -> final_country = $data -> montant_net_final_country ? $this -> getCountryName ( $data -> final_country ) : '' ;
$data -> reseau_payeur = isset ( $data -> network_destinataire ) ? $this -> getNetworkName ( $data -> network_destinataire ) . ' ' . $data -> final_country : null ;
2020-10-16 18:13:56 +00:00
if ( $data -> type == 13 )
$data -> destinataire = $data -> numero_carte ;
2020-10-16 19:30:27 +00:00
if ( ctype_space ( $data -> destinataire )) {
$data -> destinataire = $data -> emetteur ;
}
2020-10-16 16:21:27 +00:00
$data -> date_creation = $date ;
2020-10-16 07:40:10 +00:00
unset ( $data -> type , $data -> id_wallet_user , $data -> network_destinataire , $data -> nom_destinataire , $data -> prenom_destinataire , $data -> taxe , $data -> numero_carte ,
2020-10-27 10:52:37 +00:00
$data -> montant_net_final_country , $data -> montant_net , $data -> nom_emetteur , $data -> prenom_emetteur , $data -> id_destinataire , $data -> operation_fr , $data -> operation_en );
2020-10-16 07:40:10 +00:00
return $data ;
}, $transactions );
// Supprimer les underscore sur les etats
$demandes_mapped = array_map ( function ( $demand ) {
2020-11-20 16:25:54 +00:00
$demand -> etat = trans ( 'states.' . $demand -> etat );
$demand -> type_caution = trans ( 'states.' . $demand -> type_caution );
2020-10-16 07:40:10 +00:00
return $demand ;
}, $demandes );
$result = array_merge ( $transactions_mapped , $demandes_mapped );
usort ( $result , array ( $this , 'sortFunction' )); // Trier le tout par date
return $this -> successResponse ( $result );
}
2020-11-11 14:37:26 +00:00
/**
* @ OA\Get (
2022-02-22 10:27:00 +00:00
* path = " /wallets/users/operators/ { type}/ { id_wallet_network} " ,
2020-11-11 15:46:33 +00:00
* summary = " Afficher la liste des opérateurs d'un reseau " ,
* tags = { " Liste des opérateurs d'un reseau " },
2020-11-11 14:37:26 +00:00
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
2022-02-22 10:27:00 +00:00
* parameter = " type " ,
* name = " type " ,
2020-11-11 14:37:26 +00:00
* description = " Type d'operateur " ,
* in = " path " ,
* required = true ,
* @ OA\Schema (
* type = " string " ,
* enum = { " bank " , " electricity " , " phone " , " tv " , " school " , " water " },
* default = " bank "
* )
* ),
* @ OA\Parameter (
* parameter = " id_wallet_network " ,
* name = " id_wallet_network " ,
2020-11-28 08:22:30 +00:00
* description = " ID du reseau enregistré dans la base de données auquel appartient le wallet " ,
2020-11-11 14:37:26 +00:00
* 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_operator " : 1 ,
* " operator_name " : " ENEO " ,
* " operator_address " : " Bonamoussadi " ,
* " country " : " Cameroon "
* }},
* " error " : null
* }
* )
* )
* )
*/
2022-02-22 10:27:00 +00:00
public function getWalletOperators ( $type , $id_wallet_network )
2020-10-27 10:52:37 +00:00
{
2020-11-06 17:40:08 +00:00
$operators = DB :: select ( " SELECT oc.id as id_operator, o.nom as operator_name , oc.adresse as operator_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
2022-02-22 10:27:00 +00:00
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 = : type_operator ; " , ['id_network' => $id_wallet_network , 'type_operator' => $type ]);
2020-10-27 10:52:37 +00:00
2020-11-06 17:40:08 +00:00
return $this -> successResponse ( $operators );
2020-10-27 10:52:37 +00:00
}
2020-11-13 06:39:24 +00:00
/**
* @ 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 " ,
2020-11-28 08:22:30 +00:00
* description = " ID du reseau enregistré dans la base de données auquel appartient le wallet " ,
2020-11-13 06:39:24 +00:00
* 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
2026-02-02 16:55:57 +00:00
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 ]);
2020-11-13 06:39:24 +00:00
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 ,
2020-11-28 08:22:30 +00:00
* description = " ID de la banque enregistré dans la base de données "
2020-11-13 06:39:24 +00:00
* ),
* @ 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 ,
2020-11-28 08:22:30 +00:00
* description = " ID de l'utilisateur enregistré dans la base de données "
2020-11-13 06:39:24 +00:00
* )
* )
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
2020-11-28 08:22:30 +00:00
* " response " : " Votre requête de rattachement de votre compte bancaire a été prise en compte, vous recevrez un mail de confirmation dès lors que la banque aura validé votre code IBAN " ,
2020-11-13 06:39:24 +00:00
* " error " : null
* }
* )
* )
* )
*/
2020-12-01 10:51:05 +00:00
2020-11-13 06:39:24 +00:00
// 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' ,
2022-04-20 16:03:13 +00:00
'id_user' => 'required|integer|exists:users,id' ,
2020-11-13 06:39:24 +00:00
]);
$user = User :: findOrFail ( $request -> id_user );
2020-11-28 08:22:30 +00:00
if ( isset ( $user -> iban ) && isset ( $user -> id_bank_country ))
return $this -> errorResponse ( trans ( 'errors.wallet_already_linked_to_bank_account' ));
//Verifier si l'utilisateur est identifié
2026-02-18 14:21:42 +00:00
$this -> checkUserIdentification ( $user -> id );
2020-11-28 08:22:30 +00:00
2020-11-13 06:39:24 +00:00
//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
2026-02-03 16:28:09 +00:00
// $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'));
// }
2020-11-13 06:39:24 +00:00
2026-02-18 14:21:42 +00:00
$existingLinkingAccount = UsersBankingAccountVerification :: where ( 'iban' , $request -> iban )
-> where ( 'is_verified' , 1 )
-> first ();
if ( $existingLinkingAccount )
if ( $existingLinkingAccount -> is_verified == 1 && $existingLinkingAccount -> was_treated == 0 ){
return $this -> errorResponse ( trans ( 'errors.you_already_have_request_in_progress_for_this_account' ));
} else if ( $existingLinkingAccount -> is_verified == 1 && $existingLinkingAccount -> was_treated == 1 ){
return $this -> errorResponse ( trans ( 'errors.wallet_already_linked_to_bank_account' ));
}
2020-12-01 10:51:05 +00:00
$user_banking_account_verif = new UsersBankingAccountVerification ();
$user_banking_account_verif -> id_transaction = $this -> getTransactionID ();
$user_banking_account_verif -> iban = $request -> iban ;
$user_banking_account_verif -> user_code = $user -> user_code ;
$user_banking_account_verif -> id_bank_country = $request -> id_bank ;
$user_banking_account_verif -> is_verified = $user_banking_account_verif -> was_treated = 0 ;
$user_banking_account_verif -> id_network = $request -> id_wallet_network ;
$user_banking_account_verif -> save ();
2020-11-28 08:28:49 +00:00
return $this -> successResponse ( trans ( 'messages.successful_bank_account_attachment_taken' ));
2020-11-13 06:39:24 +00:00
}
2020-12-01 10:51:05 +00:00
private function getTransactionID ()
{
do {
$code = $this -> generateTransactionCode ();
$result = collect ( DB :: select ( 'SELECT * FROM users_banking_account_verification WHERE id_transaction = :code' , [ 'code' => $code ]));
$codeCorrect = sizeof ( $result ) < 0 ;
} while ( $codeCorrect );
return $code ;
}
2025-11-13 15:48:14 +00:00
/**
* @ OA\Post (
* path = " /wallets/users/create_bank_account " ,
* summary = " Créer un compte bancaire pour un utilisateur simple " ,
* tags = { " Créer un compte bancaire pour un utilisateur simple " },
* security = {{ " api_key " : {}}},
* @ OA\RequestBody (
* description = " Corps de la requete " ,
* required = true ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema (
* @ OA\Property ( property = " id_user " ,
* type = " integer " ,
* example = 39 ,
* description = " ID de l'utilisateur enregistré dans la base de données "
* ),
* @ OA\Property ( property = " id_operator " ,
* type = " integer " ,
* example = 4 ,
* description = " ID de la banque enregistré dans la base de données "
* ),
* @ OA\Property ( property = " id_wallet_network " ,
* type = " integer " ,
* example = 243 ,
* description = " ID du réseau auquel appartient le wallet exemple reseau Ilink World "
* ),
* @ OA\Property ( property = " lastname " ,
* type = " string " ,
* example = " Doe " ,
* description = " Nom de famille de l'utilisateur "
* ),
* // ... Autres propriétés ici ...
* )
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
* " response " : " Votre demande de création de compte bancaire a été prise en compte, vous recevrez un mail de confirmation dès lors que la banque aura validé votre demande " ,
* " error " : null
* }
* )
* )
* )
*/
public function createUserBankAccount ( Request $request )
{
$this -> validate ( $request , [
'id_user' => 'required|integer|exists:users,id' ,
2025-11-18 11:06:06 +00:00
'id_operator' => 'required|integer|exists:operators_countries,id' ,
2025-11-13 15:48:14 +00:00
'id_wallet_network' => 'required|integer|exists:networks,id' ,
2026-01-30 14:59:41 +00:00
'account_type' => 'required' ,
2025-11-13 15:48:14 +00:00
'lastname' => 'required|string' ,
'firstname' => 'required|string' ,
'nationality' => 'required|string' ,
'birth_date' => 'required|date' ,
2026-01-27 12:22:23 +00:00
'birth_country' => 'required|string|max:3' ,
2025-11-13 15:48:14 +00:00
'birth_city' => 'required|string' ,
2026-01-27 12:22:23 +00:00
'identification_number' => 'required|string' ,
2026-01-30 14:59:41 +00:00
'spouse_name' => 'nullable|string' ,
'phone_number' => 'required|string' ,
2025-11-13 15:48:14 +00:00
'marital_status' => 'nullable|string' ,
'profession' => 'required|string' ,
2026-01-27 12:22:23 +00:00
'niu' => 'nullable|string' ,
'employer_city' => 'nullable|string' ,
2025-11-13 15:48:14 +00:00
'employer_name' => 'nullable|string' ,
'employer_address' => 'nullable|string' ,
]);
2026-02-18 14:21:42 +00:00
// 1. Initialisation et Vérifications de base
2025-11-13 15:48:14 +00:00
$user = User :: findOrFail ( $request -> id_user );
2026-02-18 14:21:42 +00:00
$this -> checkUserIdentification ( $user -> id );
2025-11-13 15:48:14 +00:00
$network_bank = NetworksOperator :: where ( 'id_network' , $request -> id_wallet_network )
2026-02-18 14:21:42 +00:00
-> where ( 'id_operator_country' , $request -> id_operator )
2025-11-13 15:48:14 +00:00
-> first ();
2026-02-18 14:21:42 +00:00
if ( ! $network_bank || $network_bank -> operators_country -> operator -> type != 'bank' ) {
$errorKey = ( ! $network_bank ) ? 'errors.bank_not_associated_with_network' : 'errors.not_banking_operator' ;
return $this -> errorResponse ( trans ( $errorKey ));
2025-11-13 15:48:14 +00:00
}
2026-02-18 14:21:42 +00:00
// Préparation des données communes pour éviter la répétition plus bas
$commonAccountData = [
'id_user' => $request -> id_user ,
'id_operator_country' => $request -> id_operator ,
'customer_account_type_id' => $request -> account_type ,
'firstname' => $request -> firstname ,
'lastname' => $request -> lastname ,
'nationality' => $request -> nationality ,
'birth_date' => $request -> birth_date ,
'birth_country' => $request -> birth_country ,
'birth_city' => $request -> birth_city ,
'marital_status' => $request -> marital_status ? ? 'celibataire' ,
'profession' => $request -> profession ,
'identification_number' => $request -> identification_number ,
'phone_number' => $request -> phone_number ? ? 'null' ,
'spouse_name' => $request -> spouse_name ,
'niu' => $request -> niu ? ? 'null' ,
'employer_city' => $request -> employer_city ? ? 'null' ,
'employer_name' => $request -> employer_name ? ? 'null' ,
'employer_address' => $request -> employer_address ? ? 'null' ,
'balance' => 0 ,
];
2025-11-13 15:48:14 +00:00
2026-02-18 14:21:42 +00:00
// 2. CAS A : Même produit (Même opérateur ET même type)
$sameTypeAccount = UserBankAccount :: where ( 'id_user' , $request -> id_user )
2026-02-03 16:28:09 +00:00
-> where ( 'id_operator_country' , $request -> id_operator )
-> where ( 'customer_account_type_id' , $request -> account_type )
-> whereIn ( 'status' , [ 'pending' , 'actived' , 'rejected' , 'closed' ])
-> first ();
2025-11-13 15:48:14 +00:00
2026-02-18 14:21:42 +00:00
if ( $sameTypeAccount ) {
$statusMessages = [
'actived' => trans ( 'messages.user_already_has_bank_account_with_this_operator' , [ 'user_lastname' => $user -> lastname ]),
'pending' => trans ( 'errors.you_already_have_request_in_progress_for_this_product' ),
'rejected' => trans ( 'errors.your_previous_request_for_this_product_was_rejected' ),
'closed' => trans ( 'errors.your_previous_request_for_this_product_was_closed' ),
];
return $this -> errorResponse ( $statusMessages [ $sameTypeAccount -> status ] ? ? 'Error' , 500 );
}
2026-02-02 16:55:57 +00:00
2026-02-18 14:21:42 +00:00
// 3. CAS B : Autre produit chez le même opérateur (Ouverture via API)
$differentTypeAccount = UserBankAccount :: where ( 'id_user' , $request -> id_user )
-> where ( 'id_operator_country' , $request -> id_operator )
-> where ( 'customer_account_type_id' , '!=' , $request -> account_type )
-> whereNotNull ( 'account_number' )
-> whereIn ( 'status' , [ 'pending' , 'actived' , 'rejected' , 'closed' ])
-> first ();
2026-02-03 16:28:09 +00:00
2026-02-18 14:21:42 +00:00
if ( $differentTypeAccount ) {
$customer_account_type = CustomerAccountType :: find ( $request -> account_type );
if ( ! $customer_account_type ) return $this -> errorResponse ( trans ( 'errors.account_type_not_found' ));
try {
$client = new Client ([ 'connect_timeout' => 60 ]);
$baseUrl = env ( 'BANK_API_BASE_URL' );
// Auth API
$authResponse = $client -> post ( $baseUrl . '/auth/authenticate' , [
'json' => [ 'login' => env ( 'BANK_API_LOGIN' ), 'password' => env ( 'BANK_API_PASSWORD' )]
]);
$authResult = json_decode ( $authResponse -> getBody (), true );
$token = $authResult [ 'data' ][ 'token' ] ? ? null ;
if ( ! $token ) {
Log :: error ( 'Token introuvable pour user: ' . $request -> id_user );
return $this -> errorResponse ( trans ( 'errors.token_not_found' ), 404 );
}
// Create Account API
$response = $client -> post ( $baseUrl . '/clients/' . $differentTypeAccount -> account_number . '/account' , [
'headers' => [ 'Authorization' => 'Bearer ' . $token , 'Accept' => 'application/json' ],
'json' => [ 'productCode' => $customer_account_type -> product ]
]);
$result = json_decode ( $response -> getBody (), true );
2026-02-03 16:28:09 +00:00
2026-02-18 14:21:42 +00:00
if ( $response -> getStatusCode () <= 301 && ( $result [ 'success' ] ? ? true ) != false ) {
$newAccount = new UserBankAccount ( $commonAccountData );
$newAccount -> account_number = $result [ 'accountNumber' ] ? ? null ;
$newAccount -> customer_number = $result [ 'clientMatricul' ] ? ? null ;
$newAccount -> reason = trans ( 'messages.user_bank_account_activated_successfully' );
$newAccount -> status = 'actived' ;
$newAccount -> save ();
2026-01-30 14:59:41 +00:00
2026-02-18 14:21:42 +00:00
try {
Mail :: to ( $user -> email ) -> send ( new BankAccountActivated ( $newAccount , $customer_account_type -> name ));
} catch ( \Exception $e ) {
Log :: error ( " Mail error: " . $e -> getMessage ());
}
return $this -> successResponse ( trans ( 'messages.create_bank_account_linked_successfully' ), 200 );
}
Log :: error ( 'Échec API Banque User: ' . $request -> id_user . ' Res: ' . $response -> getBody ());
return $this -> errorResponse ( trans ( 'errors.bank_api_exception' ), 500 );
} catch ( \Exception $e ) {
Log :: error ( 'Exception API Banque User: ' . $request -> id_user . ' Error: ' . $e -> getMessage ());
return $this -> errorResponse ( trans ( 'errors.bank_api_exception' ), 500 );
}
2026-01-30 14:59:41 +00:00
}
2025-12-11 14:48:59 +00:00
2026-02-18 14:21:42 +00:00
// 4. CAS STANDARD : Création d'une demande "Pending"
$account_type = CustomerAccountType :: find ( $request -> account_type );
if ( ! $account_type ) return $this -> errorResponse ( trans ( 'errors.account_type_not_found' ));
$bankAccount = new UserBankAccount ( $commonAccountData );
2025-11-13 15:48:14 +00:00
$bankAccount -> status = 'pending' ;
$bankAccount -> save ();
try {
2026-02-18 14:21:42 +00:00
$bankAccount -> update ([ 'reason' => trans ( 'messages.request_bank_account_pending_validation_by_administrator' )]);
$bank_name = $network_bank -> operators_country -> operator -> nom ;
Mail :: to ( $user -> email ) -> send ( new BankAccountCreatedMail ( $bankAccount , $bank_name , $user ));
2025-11-13 15:48:14 +00:00
2026-02-18 14:21:42 +00:00
return $this -> successResponse ([ 'message' => trans ( 'messages.successful_bank_account_creation' )]);
2025-11-14 11:20:10 +00:00
2025-11-13 15:48:14 +00:00
} catch ( \Exception $e ) {
2026-02-18 14:21:42 +00:00
$bankAccount -> update ([ 'status' => 'rejected' , 'reason' => $e -> getMessage ()]);
Log :: error ( 'Erreur Mail/Creation: ' . $e -> getMessage ());
2025-11-13 15:48:14 +00:00
return $this -> errorResponse ( trans ( 'errors.bank_api_exception' ));
}
}
2026-01-30 14:59:41 +00:00
public function activateUserBankAccount ( Request $request )
{
$this -> validate ( $request , [
'id' => 'required|integer|exists:user_bank_accounts,id' ,
'doc_front' => 'required|url' ,
'doc_back' => 'required|url' ,
]);
2026-02-18 14:21:42 +00:00
// 1. Récupération des données avec chargement des relations
$bank_account = UserBankAccount :: with ( 'user' ) -> find ( $request -> id );
2026-01-30 14:59:41 +00:00
2026-02-18 14:21:42 +00:00
if ( ! $bank_account ) return $this -> errorResponse ( trans ( 'errors.account_type_not_found' ));
2026-01-30 14:59:41 +00:00
2026-02-18 14:21:42 +00:00
$user = $bank_account -> user ;
if ( ! $user ) return $this -> errorResponse ( trans ( 'errors.user_not_found' ));
2026-01-30 14:59:41 +00:00
2026-02-18 14:21:42 +00:00
$account_type = CustomerAccountType :: find ( $bank_account -> customer_account_type_id );
if ( ! $account_type ) return $this -> errorResponse ( trans ( 'errors.account_type_not_found' ));
2026-02-02 16:55:57 +00:00
2026-02-18 14:21:42 +00:00
// 2. Préparation des variables API
$baseUrl = env ( 'BANK_API_BASE_URL' );
$login = env ( 'BANK_API_LOGIN' );
$password = env ( 'BANK_API_PASSWORD' );
$name_of_account_type = $account_type -> name ;
2026-01-30 14:59:41 +00:00
$payload = [
2026-02-18 14:21:42 +00:00
'phoneNumber' => $bank_account -> phone_number ? ? ( $user -> phone ? ? '' ),
'email' => $user -> email ? ? '' ,
'fullname' => $bank_account -> firstname . ' ' . $bank_account -> lastname ,
'branchCode' => env ( 'BANK_API_BRANCH_CODE' ),
'dateOfBirth' => $bank_account -> birth_date ,
'isMarried' => ( $bank_account -> marital_status === 'marie' ) ? 'Yes' : 'No' ,
'nameOfSpouse' => $bank_account -> spouse_name ? ? '' ,
'city' => $bank_account -> birth_city ? ? '' ,
'accountType' => $name_of_account_type ,
'originalNationality' => $bank_account -> nationality ,
'countryOfResidence' => $bank_account -> birth_country ,
'employersName' => $bank_account -> employer_name ? ? '' ,
'employersAddress' => $bank_account -> employer_address ? ? '' ,
'employersCity' => $bank_account -> employer_city ? ? '' ,
'product' => $account_type -> product ? ? '' ,
'identificationNumber' => $bank_account -> identification_number ,
'paySlip' => $request -> doc_front ,
'signatureCard' => $request -> doc_back ,
2026-01-30 14:59:41 +00:00
];
try {
2026-02-18 14:21:42 +00:00
$client = new Client ([ 'connect_timeout' => 60 ]);
2026-01-30 14:59:41 +00:00
2026-02-18 14:21:42 +00:00
$authResponse = $client -> post ( $baseUrl . '/auth/authenticate' , [
'json' => [
'login' => $login ,
'password' => $password
]
]);
$authResult = json_decode ( $authResponse -> getBody (), true );
$token = $authResult [ 'data' ][ 'token' ] ? ? null ;
if ( ! $token ) {
Log :: error ( 'Token introuvable (Activation) pour compte ID: ' . $bank_account -> id );
return $this -> errorResponse ( trans ( 'errors.token_not_found' ), 404 );
}
$response = $client -> post ( $baseUrl . '/clients/create-with-account' , [
2026-01-30 14:59:41 +00:00
'headers' => [
2026-02-18 14:21:42 +00:00
'Authorization' => 'Bearer ' . $token ,
'Accept' => 'application/json' ,
2026-01-30 14:59:41 +00:00
],
2026-02-18 14:21:42 +00:00
'json' => $payload ,
2026-01-30 14:59:41 +00:00
]);
$result = json_decode ( $response -> getBody (), true );
if ( $response -> getStatusCode () == 200 || $response -> getStatusCode () == 201 ) {
$bank_account -> update ([
2026-02-18 14:21:42 +00:00
'status' => 'actived' ,
'reason' => 'Compte activé avec succès !' ,
'account_number' => $result [ 'accountNumber' ] ? ? null ,
'customer_number' => $result [ 'clientCode' ] ? ? null
]);
$user -> update ([
'id_bank_country' => $bank_account -> id_operator_country ,
'iban' => $result [ 'accountNumber' ] ? ? null
2026-01-30 14:59:41 +00:00
]);
2026-02-18 14:21:42 +00:00
try {
Mail :: to ( $user -> email ) -> send ( new BankAccountActivated ( $bank_account , $name_of_account_type ));
} catch ( \Exception $e ) {
Log :: error ( " Mail error lors de l'activation: " . $e -> getMessage ());
}
2026-02-02 16:55:57 +00:00
2026-02-18 14:21:42 +00:00
return $this -> successResponse ( trans ( 'messages.user_bank_account_activated_successfully' ), 200 );
2026-01-30 14:59:41 +00:00
}
2026-02-18 14:21:42 +00:00
return $this -> errorResponse ( 'Erreur lors de l\'activation API' , 500 );
2026-01-30 14:59:41 +00:00
} catch ( RequestException $e ) {
$errorBody = $e -> hasResponse () ? ( string ) $e -> getResponse () -> getBody () : $e -> getMessage ();
$bank_account -> update ([
'status' => 'rejected' ,
'reason' => 'Erreur API externe : ' . $errorBody
]);
2026-02-18 14:21:42 +00:00
Log :: error ( 'Erreur Activation API pour ID ' . $bank_account -> id . ' : ' . $errorBody );
2026-02-02 16:55:57 +00:00
return $this -> errorResponse ( 'Error_occurred' , 500 );
2026-01-30 14:59:41 +00:00
}
}
2026-02-02 09:39:27 +00:00
public function getCustomerAccountTypes ()
{
$account_types = CustomerAccountType :: all ([ 'id' , 'name' ]);
return $this -> successResponse ( $account_types );
}
2026-02-02 16:55:57 +00:00
public function validateLinkUserBankAccount ( Request $request )
{
$this -> validate ( $request , [
2026-02-03 16:28:09 +00:00
'iban' => 'required' ,
2026-02-02 16:55:57 +00:00
'id_transaction' => 'required'
]);
$user_bank_account_verfication = UsersBankingAccountVerification :: where ( 'id_transaction' , $request -> id_transaction ) -> first ();
if ( ! $user_bank_account_verfication ) {
2026-02-03 16:28:09 +00:00
return $this -> errorResponse ( trans ( 'errors.transaction_not_found' ), 404 );
}
$user = User :: where ( 'user_code' , $user_bank_account_verfication -> user_code ) -> firstOrFail ();( $user_bank_account_verfication -> user_code );
if ( ! $user ) {
return $this -> errorResponse ( trans ( 'errors.user_not_found' ), 404 );
2026-02-02 16:55:57 +00:00
}
2026-02-18 14:21:42 +00:00
$baseUrl = env ( 'BANK_API_BASE_URL' ,);
$login = env ( 'BANK_API_LOGIN' );
$password = env ( 'BANK_API_PASSWORD' );
2026-02-02 16:55:57 +00:00
$authApi = $baseUrl . '/auth/authenticate' ;
2026-02-03 16:28:09 +00:00
$accountApi = $baseUrl . '/account/getOne/' . $request -> iban ;
2026-02-02 16:55:57 +00:00
try {
$client = new Client ();
$authResponse = $client -> post ( $authApi , [
'json' => [
'login' => $login ,
'password' => $password
],
'connect_timeout' => 60 ,
]);
$authResult = json_decode ( $authResponse -> getBody (), true );
2026-02-03 16:28:09 +00:00
$token = $authResult [ 'data' ][ 'token' ] ? ? null ;
2026-02-02 16:55:57 +00:00
if ( ! $token ) {
Log :: error ( 'Token introuvable dans la réponse auth pour transaction: ' . $request -> id_transaction );
2026-02-03 16:28:09 +00:00
return $this -> errorResponse ( trans ( 'errors.token_not_found' ), 404 );
2026-02-02 16:55:57 +00:00
}
$response = $client -> get ( $accountApi , [
'headers' => [
'Authorization' => 'Bearer ' . $token ,
'Accept' => 'application/json' ,
],
'connect_timeout' => 60 ,
]);
$result = json_decode ( $response -> getBody (), true );
2026-02-03 16:28:09 +00:00
if (( $response -> getStatusCode () == 200 || $response -> getStatusCode () <= 301 ) && ( $result [ 'data' ][ 'accountID' ] == $user_bank_account_verfication -> iban )) {
2026-02-02 16:55:57 +00:00
$user_bank_account_verfication -> update ([
'is_verified' => 1 ,
2026-02-03 16:28:09 +00:00
'was_treated' => 1
2026-02-02 16:55:57 +00:00
]);
2026-02-18 14:21:42 +00:00
$user -> update ([
'iban' => $user_bank_account_verfication -> iban ,
'id_bank_country' => $user_bank_account_verfication -> id_bank_country
]);
2026-02-02 16:55:57 +00:00
Log :: info ( 'Compte bancaire rattaché avec succès pour transaction: ' . $request -> id_transaction );
2026-02-03 16:28:09 +00:00
try {
// Mail::to($user->email)->send(new BankAccountLinked($user_bank_account_verfication));
} catch ( \Exception $e ) {
Log :: error ( " Mail error: " . $e -> getMessage ());
}
return $this -> successResponse ( trans ( 'messages.create_bank_account_linked_successfully' ), 200 );
2026-02-02 16:55:57 +00:00
}
} 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 ());
2026-02-03 16:28:09 +00:00
return $this -> errorResponse ( trans ( 'errors.Error_occurred' ));
2026-02-02 16:55:57 +00:00
}
}
2020-04-15 23:08:09 +00:00
}