787 lines
41 KiB
PHP
Executable File
787 lines
41 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Models\Network;
|
||
use App\Models\NetworksOperator;
|
||
use App\Models\User;
|
||
use App\Models\UsersBankingAccountVerification;
|
||
use App\Models\WalletAgent;
|
||
use App\Models\WalletsUser;
|
||
use App\Models\Operator;
|
||
use App\Models\OperatorsCountry;
|
||
use App\Models\UserBankAccount;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Http\Response;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
class WalletController extends Controller
|
||
{
|
||
/**
|
||
* Create a new controller instance.
|
||
*
|
||
* @return void
|
||
*/
|
||
public function __construct()
|
||
{
|
||
//
|
||
}
|
||
|
||
public function activated($id_agent)
|
||
{
|
||
$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,
|
||
cw.taux_com_client_depot , na.id AS network_agent_id , cg.category , cw.has_nano_credit FROM agents ag
|
||
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
|
||
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 (
|
||
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]);
|
||
|
||
// Create wallet if is not exist
|
||
$category = null;
|
||
if ($networks) {
|
||
$reload = false;
|
||
$id = $id_agent;
|
||
foreach ($networks as $network) {
|
||
$category = $network->category;
|
||
// Create wallet if is not exist
|
||
if (!$network->id) {
|
||
$datetime = $this->getCurrentTimeByCountryCode($network->code_country);
|
||
DB::insert('INSERT INTO wallets (id_networkAgent , created_date) VALUES (? , ?);', [$network->network_agent_id, $datetime]);
|
||
$reload = true;
|
||
}
|
||
}
|
||
if ($reload)
|
||
return $this->activated($id);
|
||
}
|
||
|
||
|
||
// Return only single wallet if it is hypervisor or supervisor
|
||
if (in_array($category, ['hyper', 'super'])) {
|
||
// Remove unnecessary fields
|
||
// $networks = $this->array_except($networks, ['category']);
|
||
return $this->successResponse(collect($networks)->first());
|
||
} else {
|
||
// Remove unnecessary fields
|
||
$networks = $this->array_except($networks, ['balance_princ', 'balance_com', 'created_date', 'taux_com_client_depot']);
|
||
return $this->successResponse($networks);
|
||
}
|
||
|
||
}
|
||
|
||
private function array_except($array, $keys)
|
||
{
|
||
foreach ($array as $row) {
|
||
foreach ($keys as $key) {
|
||
unset($row->$key);
|
||
}
|
||
}
|
||
return $array;
|
||
}
|
||
|
||
public function show($id_wallet)
|
||
{
|
||
// $wallet = Wallet::findOrFail($id_wallet);
|
||
$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,
|
||
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
|
||
WHERE wa.wallet_id = :id', ['id' => $id_wallet]))->first();
|
||
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";
|
||
}
|
||
return $this->successResponse($wallet);
|
||
} else
|
||
return $this->errorResponse(trans('errors.model_not_found', ['model' => 'wallet']), Response::HTTP_BAD_REQUEST);
|
||
}
|
||
|
||
public function create(Request $request)
|
||
{
|
||
$rules = [
|
||
'id_networkAgent' => 'required|integer|min:1'
|
||
];
|
||
|
||
$this->validate($request, $rules);
|
||
|
||
DB::insert('INSERT INTO wallets (id_networkAgent) VALUES (?);', [$request->id_networkAgent]);
|
||
return $this->successResponse(trans('messages.new_wallet_added'));
|
||
|
||
}
|
||
|
||
// Wallets users iLink
|
||
public function showWalletUser($id_user)
|
||
{
|
||
$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
|
||
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
|
||
LEFT JOIN countries_currencies card_cc ON card_cc.id = u.card_country_id
|
||
WHERE wu.idUser = :id_user AND cw.type = \'ilink\' LIMIT 1', ['id_user' => $id_user]))->first();
|
||
if ($wallet) {
|
||
return $this->successResponse($wallet);
|
||
} else
|
||
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)
|
||
{
|
||
|
||
$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]);
|
||
|
||
$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,
|
||
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) {
|
||
$data->operation = app()->isLocale('en') ? $data->operation_en : $data->operation_fr;
|
||
$date = $data->date_creation;
|
||
unset($data->date_creation);
|
||
|
||
$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;
|
||
$data->montant2 = $this->toMoney($data->montant, $data->init_country);
|
||
$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;
|
||
if ($data->type == 13)
|
||
$data->destinataire = $data->numero_carte;
|
||
|
||
if (ctype_space($data->destinataire)) {
|
||
$data->destinataire = $data->emetteur;
|
||
}
|
||
$data->date_creation = $date;
|
||
|
||
unset($data->type, $data->id_wallet_user, $data->network_destinataire, $data->nom_destinataire, $data->prenom_destinataire, $data->taxe, $data->numero_carte,
|
||
$data->montant_net_final_country, $data->montant_net, $data->nom_emetteur, $data->prenom_emetteur, $data->id_destinataire, $data->operation_fr, $data->operation_en);
|
||
return $data;
|
||
}, $transactions);
|
||
|
||
// Supprimer les underscore sur les etats
|
||
$merge = array_map(function ($demand) {
|
||
$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);
|
||
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($this->arrayPaginator($result, $request));
|
||
}
|
||
|
||
public function superHistory($agent_code, Request $request)
|
||
{
|
||
|
||
$walletSup = WalletAgent::where('codeMembre', $agent_code)->firstOrFail();
|
||
$id_wallet = $walletSup->wallet_id;
|
||
|
||
$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]);
|
||
|
||
|
||
$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,
|
||
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) {
|
||
$data->operation = app()->isLocale('en') ? $data->operation_en : $data->operation_fr;
|
||
$date = $data->date_creation;
|
||
unset($data->date_creation);
|
||
|
||
$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;
|
||
$data->montant2 = $this->toMoney($data->montant, $data->init_country);
|
||
$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;
|
||
if ($data->type == 13)
|
||
$data->destinataire = $data->numero_carte;
|
||
$data->date_creation = $date;
|
||
unset($data->type, $data->id_wallet_user, $data->network_destinataire, $data->nom_destinataire, $data->prenom_destinataire, $data->taxe, $data->numero_carte,
|
||
$data->montant_net_final_country, $data->montant_net, $data->nom_emetteur, $data->prenom_emetteur, $data->id_destinataire, $data->operation_fr, $data->operation_en);
|
||
return $data;
|
||
}, $transactions);
|
||
|
||
// Supprimer les underscore sur les etats
|
||
$demandes_mapped = array_map(function ($demand) {
|
||
$demand->etat = trans('states.' . $demand->etat);
|
||
$demand->type_caution = trans('states.' . $demand->type_caution);
|
||
return $demand;
|
||
}, $demandes);
|
||
|
||
$result = array_merge($transactions_mapped, $demandes_mapped);
|
||
|
||
usort($result, array($this, 'sortFunction')); // Trier le tout par date
|
||
|
||
return $this->successResponse($this->arrayPaginator($result, $request));
|
||
}
|
||
|
||
// Routes sans pagination
|
||
public function allHyperHistory($id_network)
|
||
{
|
||
|
||
$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]);
|
||
|
||
$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,
|
||
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) {
|
||
$data->operation = app()->isLocale('en') ? $data->operation_en : $data->operation_fr;
|
||
$date = $data->date_creation;
|
||
unset($data->date_creation);
|
||
|
||
$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;
|
||
$data->montant2 = $this->toMoney($data->montant, $data->init_country);
|
||
$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;
|
||
if ($data->type == 13)
|
||
$data->destinataire = $data->numero_carte;
|
||
|
||
if (ctype_space($data->destinataire)) {
|
||
$data->destinataire = $data->emetteur;
|
||
}
|
||
$data->date_creation = $date;
|
||
unset($data->type, $data->id_wallet_user, $data->network_destinataire, $data->nom_destinataire, $data->prenom_destinataire, $data->taxe, $data->numero_carte,
|
||
$data->montant_net_final_country, $data->montant_net, $data->nom_emetteur, $data->prenom_emetteur, $data->id_destinataire, $data->operation_fr, $data->operation_en);
|
||
return $data;
|
||
}, $transactions);
|
||
|
||
// Supprimer les underscore sur les etats
|
||
$merge = array_map(function ($demand) {
|
||
$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);
|
||
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);
|
||
}
|
||
|
||
public function allSuperHistory($agent_code)
|
||
{
|
||
|
||
$walletSup = WalletAgent::where('codeMembre', $agent_code)->firstOrFail();
|
||
$id_wallet = $walletSup->wallet_id;
|
||
|
||
$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]);
|
||
|
||
|
||
$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,
|
||
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) {
|
||
$data->operation = app()->isLocale('en') ? $data->operation_en : $data->operation_fr;
|
||
$date = $data->date_creation;
|
||
unset($data->date_creation);
|
||
|
||
$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;
|
||
$data->montant2 = $this->toMoney($data->montant, $data->init_country);
|
||
$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;
|
||
if ($data->type == 13)
|
||
$data->destinataire = $data->numero_carte;
|
||
|
||
if (ctype_space($data->destinataire)) {
|
||
$data->destinataire = $data->emetteur;
|
||
}
|
||
|
||
$data->date_creation = $date;
|
||
|
||
unset($data->type, $data->id_wallet_user, $data->network_destinataire, $data->nom_destinataire, $data->prenom_destinataire, $data->taxe, $data->numero_carte,
|
||
$data->montant_net_final_country, $data->montant_net, $data->nom_emetteur, $data->prenom_emetteur, $data->id_destinataire, $data->operation_fr, $data->operation_en);
|
||
return $data;
|
||
}, $transactions);
|
||
|
||
// Supprimer les underscore sur les etats
|
||
$demandes_mapped = array_map(function ($demand) {
|
||
$demand->etat = trans('states.' . $demand->etat);
|
||
$demand->type_caution = trans('states.' . $demand->type_caution);
|
||
return $demand;
|
||
}, $demandes);
|
||
|
||
$result = array_merge($transactions_mapped, $demandes_mapped);
|
||
|
||
usort($result, array($this, 'sortFunction')); // Trier le tout par date
|
||
|
||
return $this->successResponse($result);
|
||
}
|
||
|
||
/**
|
||
* @OA\Get(
|
||
* path="/wallets/users/operators/{type}/{id_wallet_network}",
|
||
* summary="Afficher la liste des opérateurs d'un reseau",
|
||
* tags={"Liste des opérateurs d'un reseau"},
|
||
* security={{"api_key":{}}},
|
||
* @OA\Parameter(
|
||
* parameter="type",
|
||
* name="type",
|
||
* 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",
|
||
* description="ID du reseau enregistré dans la base de données 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_operator" : 1,
|
||
* "operator_name" : "ENEO",
|
||
* "operator_address" : "Bonamoussadi",
|
||
* "country": "Cameroon"
|
||
* }},
|
||
* "error":null
|
||
* }
|
||
* )
|
||
* )
|
||
* )
|
||
*/
|
||
|
||
public function getWalletOperators($type, $id_wallet_network)
|
||
{
|
||
$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
|
||
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]);
|
||
|
||
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 enregistré dans la base de données 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 LIKE '%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 enregistré dans la base de données"
|
||
* ),
|
||
* @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 enregistré dans la base de données"
|
||
* )
|
||
* )
|
||
* )
|
||
* ),
|
||
* @OA\Response(
|
||
* response=200,
|
||
* description="OK",
|
||
* @OA\JsonContent(
|
||
* ref="#/components/schemas/ApiResponse",
|
||
* example = {
|
||
* "status" : 200,
|
||
* "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",
|
||
* "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|exists:users,id',
|
||
]);
|
||
|
||
$user = User::findOrFail($request->id_user);
|
||
|
||
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é
|
||
$identification = $this->checkUserIdentification($user->id);
|
||
|
||
//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_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();
|
||
|
||
// Envoyer une requete vers la banque contant ses informations personnelles pour verfication du code iban
|
||
Log::info('-------------------------- User - Rattacher le compte bancaire au wallet ------------------------------------');
|
||
Log::info(json_encode(
|
||
array_merge($request->toArray(), $identification->toArray(), ['id_transaction' => $user_banking_account_verif->id_transaction])
|
||
));
|
||
Log::info('------------------------------------------------------------------------------------------------');
|
||
|
||
return $this->successResponse(trans('messages.successful_bank_account_attachment_taken'));
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
/**
|
||
* @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',
|
||
'id_operator' => 'required|integer|exists:operators,id',
|
||
'id_wallet_network' => 'required|integer|exists:networks,id',
|
||
'lastname' => 'required|string',
|
||
'firstname' => 'required|string',
|
||
'nationality' => 'required|string',
|
||
'birth_date' => 'required|date',
|
||
'birth_country' => 'required|string',
|
||
'birth_city' => 'required|string',
|
||
'father_firstname' => 'required|string',
|
||
'father_lastname' => 'required|string',
|
||
'mother_firstname' => 'required|string',
|
||
'mother_lastname' => 'required|string',
|
||
'marital_name' => 'nullable|string',
|
||
'marital_status' => 'nullable|string',
|
||
'profession' => 'required|string',
|
||
'sector_activity' => 'required|string',
|
||
'subsector_activity' => 'nullable|string',
|
||
'tax_number' => 'required|string',
|
||
'employee_number' => 'nullable|string',
|
||
'position' => 'nullable|string',
|
||
'employer_name' => 'nullable|string',
|
||
'employer_address' => 'nullable|string',
|
||
]);
|
||
|
||
// Vérifier que l’utilisateur est identifié
|
||
$user = User::findOrFail($request->id_user);
|
||
$identification = $this->checkUserIdentification($user->id);
|
||
|
||
$operateur_country = OperatorsCountry::where('id_operator', $request->id_operator)->first();
|
||
|
||
if (!$operateur_country) {
|
||
return $this->errorResponse(trans('errors.bank_not_associated_with_network'));
|
||
}
|
||
|
||
// Vérifier si la banque est associée au réseau
|
||
$network_bank = NetworksOperator::where('id_network', $request->id_wallet_network)
|
||
->where('id_operator_country', $operateur_country->id)
|
||
->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'));
|
||
}
|
||
|
||
$exist_user_request_create_account = UserBankAccount::where('id_user', $request->id_user)
|
||
->where('id_operator', $request->id_operator)
|
||
->first();
|
||
|
||
if (isset($exist_user_request_create_account)){
|
||
if ($exist_user_request_create_account->status == 'pending') {
|
||
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]));
|
||
}
|
||
}
|
||
|
||
$bankAccount = new UserBankAccount();
|
||
$bankAccount->id_user = $user->id;
|
||
$bankAccount->id_operator = $request->id_operator;
|
||
$bankAccount->lastname = $request->lastname;
|
||
$bankAccount->firstname = $request->firstname;
|
||
$bankAccount->marital_name = $request->marital_name ?? 'null';
|
||
$bankAccount->nationality = $request->nationality;
|
||
$bankAccount->birth_date = $request->birth_date;
|
||
$bankAccount->birth_country = $request->birth_country;
|
||
$bankAccount->birth_city = $request->birth_city;
|
||
$bankAccount->father_firstname = $request->father_firstname;
|
||
$bankAccount->father_lastname = $request->father_lastname;
|
||
$bankAccount->mother_firstname = $request->mother_firstname;
|
||
$bankAccount->mother_lastname = $request->mother_lastname;
|
||
$bankAccount->marital_status = $request->marital_status ?? 'celibataire';
|
||
$bankAccount->profession = $request->profession;
|
||
$bankAccount->sector_activity = $request->sector_activity;
|
||
$bankAccount->subsector_activity = $request->subsector_activity ?? 'null';
|
||
$bankAccount->tax_number = $request->tax_number;
|
||
$bankAccount->employee_number = $request->employee_number;
|
||
$bankAccount->position = $request->position;
|
||
$bankAccount->employer_name = $request->employer_name ?? 'null';
|
||
$bankAccount->employer_address = $request->employer_address ?? 'null';
|
||
$bankAccount->balance = 0;
|
||
$bankAccount->status = 'pending';
|
||
$bankAccount->created_at = date('Y-m-d H:i:s');
|
||
$bankAccount->updated_at = date('Y-m-d H:i:s');
|
||
$bankAccount->save();
|
||
|
||
// Envoi des informations à la banque partenaire (via API)
|
||
// $payload = [
|
||
// 'account_number' => $bankAccount->account_number,
|
||
// 'iban' => $bankAccount->iban,
|
||
// 'swift_code' => $bankAccount->swift_code,
|
||
// 'lastname' => $bankAccount->lastname,
|
||
// 'firstname' => $bankAccount->firstname,
|
||
// 'birth_date' => $bankAccount->birth_date,
|
||
// 'nationality' => $bankAccount->nationality,
|
||
// 'birth_country' => $bankAccount->birth_country,
|
||
// 'network_code' => $network_bank->network->code ?? null,
|
||
// ];
|
||
|
||
Log::info('--- Envoi création compte bancaire à la banque partenaire ---');
|
||
// Log::info(json_encode($payload));
|
||
|
||
try {
|
||
// // Envoi à une API bancaire
|
||
// $response = Http::withHeaders([
|
||
// 'Accept' => 'application/json',
|
||
// 'Authorization' => 'Bearer ' . env('BANK_API_TOKEN'),
|
||
// ])->post(env('BANK_API_URL') . '/accounts/create', $payload);
|
||
|
||
// if ($response->failed()) {
|
||
// $bankAccount->status = 'rejected';
|
||
// $bankAccount->reason = $response->json('message') ?? 'Erreur API bancaire';
|
||
// $bankAccount->save();
|
||
// return $this->errorResponse(trans('errors.bank_api_failed'));
|
||
// }
|
||
|
||
// $bankAccount->status = 'active';
|
||
$bankAccount->reason = 'Demande de compte bancaire envoyée via API en attente de validation';
|
||
$bankAccount->save();
|
||
Log::info('Réponse API Banque: Compte bancaire créé avec succès');
|
||
|
||
} catch (\Exception $e) {
|
||
$bankAccount->status = 'rejected';
|
||
$bankAccount->reason = $e->getMessage();
|
||
$bankAccount->save();
|
||
Log::error('Erreur API Banque: ' . $e->getMessage());
|
||
return $this->errorResponse(trans('errors.bank_api_exception'));
|
||
}
|
||
|
||
return $this->successResponse([
|
||
'message' => trans('messages.successful_bank_account_creation')
|
||
]);
|
||
}
|
||
|
||
}
|