2220 lines
150 KiB
PHP
Executable File
2220 lines
150 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Exceptions\AppException;
|
||
use App\Models\AgentPlus;
|
||
use App\Models\CodeGenerer;
|
||
use App\Models\ConfigWallet;
|
||
use App\Models\Country;
|
||
use App\Models\Identification;
|
||
use App\Models\NetworksAgent;
|
||
use App\Models\NetworksOperator;
|
||
use App\Models\PayingNetwork;
|
||
use App\Models\Regulation;
|
||
use App\Models\TypeIlinkTransaction;
|
||
use App\Models\TypeOperator;
|
||
use App\Models\User;
|
||
use App\Models\Wallet;
|
||
use App\Models\WalletAgent;
|
||
use App\Models\WalletIlinkTransaction;
|
||
use App\Models\WalletsUser;
|
||
use Carbon\Carbon;
|
||
use DateTime;
|
||
use Exception;
|
||
use GuzzleHttp\Client;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Http\Response;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Illuminate\Validation\ValidationException;
|
||
use Throwable;
|
||
|
||
class iLinkTransactionController extends Controller
|
||
{
|
||
|
||
private $PAYMENT_URL = "/stripe/payIn";
|
||
private $PAYOUT_URL = "/stripe/payOut";
|
||
private $REFUND_URL = "/adyen-api/v1/transaction/refund";
|
||
|
||
private $PAYMENT_CHECK_STATUS = "/check";
|
||
private $PAYING_NETWORK_SIMULATOR_SEND_FRAME_URL = 'http://localhost:8084/receive_payment';
|
||
|
||
/**
|
||
* Create a new controller instance.
|
||
*
|
||
* @return void
|
||
*/
|
||
public function __construct()
|
||
{
|
||
//
|
||
}
|
||
|
||
/**
|
||
* @OA\Post(
|
||
* path="/transactions/ilink",
|
||
* summary="Effectuer une transaction dans le wallet iLink",
|
||
* tags={"Transaction iLink"},
|
||
* security={{"api_key":{}}},
|
||
* @OA\RequestBody(
|
||
* description="Corps de la requete",
|
||
* required=true,
|
||
* @OA\MediaType(
|
||
* mediaType="application/json",
|
||
* @OA\Schema(
|
||
* oneOf={
|
||
* @OA\Schema(ref="#/components/schemas/user_payement_of_operator"),
|
||
* @OA\Schema(ref="#/components/schemas/user_remove_from_bank_to_wallet"),
|
||
* @OA\Schema(ref="#/components/schemas/user_send_from_wallet_to_bank"),
|
||
* @OA\Schema(ref="#/components/schemas/agent_send_from_cash_to_bank"),
|
||
* }
|
||
* ),
|
||
* examples = {
|
||
* @OA\Examples(
|
||
* example = "user_payement_of_operator",
|
||
* summary = "User - Payer une facture chez un opérateur donné",
|
||
* value = {"type":19,"id_wallet_user":9,"id_wallet_network":101,"no_facture":"CDE1425533","type_operator":"water","id_operator":6,"montant":100000,"password":"1234"}
|
||
* ),
|
||
* @OA\Examples(
|
||
* example = "user_remove_from_bank_to_wallet",
|
||
* summary = "User - Retrait du compte bancaire rattaché vers le wallet de l'utilisateur",
|
||
* value = {"type":20,"id_wallet_user":1,"montant":100000,"password":"1234"}
|
||
* ),
|
||
* @OA\Examples(
|
||
* example = "user_send_from_wallet_to_bank",
|
||
* summary = "User - Envoi de wallet à banque",
|
||
* value = {"type":4,"id_wallet_user":9,"id_wallet_network":101,"montant":100000,"password":"1234"}
|
||
* ),
|
||
* @OA\Examples(
|
||
* example = "agent_send_from_cash_to_bank",
|
||
* summary = "Agent - Envoi de cash vers banque",
|
||
* value = {"type":18,"id_wallet_agent":9716,"id_wallet_network":101,"iban":"CM15UBAA412341234123412341233","id_bank":4,"nom_emetteur":"Djery","prenom_emetteur":"Tom","email_emetteur":"ddietchi@ilink-app.com","type_document_emetteur":"passport","id_document_emetteur":"12345","nom_destinataire":"Brice","prenom_destinataire":"zele","montant":100000,"password":"1234"}
|
||
* )
|
||
* }
|
||
* )
|
||
* ),
|
||
* @OA\Response(
|
||
* response=200,
|
||
* description="OK",
|
||
* @OA\MediaType(
|
||
* mediaType="application/json",
|
||
* @OA\Schema(
|
||
* ref="#/components/schemas/ApiResponse",
|
||
* ),
|
||
* examples = {
|
||
* @OA\Examples(
|
||
* example = "user_payement_of_operator" ,
|
||
* summary = "User - Payer une facture chez un opérateur donné",
|
||
* value = {
|
||
* "status" : 200,
|
||
* "response" : "Paiement d'une facture chez un operateur\nInformations de la transaction :\n - Numéro : 7S9PZO26VAQ3\n - Montant de la transaction : 100 000 FCFA\n
|
||
- Frais : 0 FCFA\n - Montant net payé: 100 000 FCFA\n - Compte émetteur : s6LD9PmCJC\n - Operateur : CDE\n - Type d'operateur : Opérateur d'eau\n - No facture : CDE1425533\n\nCe message a été envoyé dans le mail de l'émetteur",
|
||
* "error":null
|
||
* }
|
||
* ),
|
||
* @OA\Examples(
|
||
* example = "user_remove_from_bank_to_wallet",
|
||
* summary = "User - Retrait du compte bancaire rattaché vers le wallet de l'utilisateur",
|
||
* value = {
|
||
* "status" : 200,
|
||
* "response" : "Retrait de la banque vers le wallet\nInformations de la transaction :\n - Numéro : 2XWQD7ZD9E7R\n - Montant de la transaction : 100 000 FCFA\n
|
||
- Frais : 0 FCFA\n - Montant net d\\'envoi: 100 000 FCFA\n - Compte émetteur : BZdsWkdfl\n - Banque : UBA Cameroon\n - IBAN : \n\nCe message a été envoyé dans le mail de l'émetteur",
|
||
* "error":null
|
||
* }
|
||
* ),
|
||
* @OA\Examples(
|
||
* example = "user_send_from_wallet_to_bank",
|
||
* summary = "User - Envoi de wallet à banque",
|
||
* value = {
|
||
* "status" : 200,
|
||
* "response" : "Envoi d'argent de votre compte vers la banque\nInformations de la transaction :\n - Numéro : NA69C5RTBY7B\n - Montant de la transaction : 100 000 FCFA\n
|
||
- Frais : 0 FCFA\n - Montant net d'envoi: 100 000 FCFA\n - Compte émetteur : s6LD9PmCJC\n - Banque : UBA Cameroon\n - IBAN : \n\nCe message a été envoyé dans le mail de l'émetteur",
|
||
* "error":null
|
||
* }
|
||
* ),
|
||
* @OA\Examples(
|
||
* example = "agent_send_from_cash_to_bank" ,
|
||
* summary = "Agent - Envoi de cash vers banque",
|
||
* value = {
|
||
* "status" : 200,
|
||
* "response" : "Envoi d'argent en cash vers la banque\nInformations de la transaction :\n - Numéro : E58K8ZFPB5AN\n - Montant de la transaction : 100 000 FCFA\n - Frais : 0 FCFA\n - Montant net d'envoi: 100 000 FCFA\n
|
||
- Compte de l'agent : 38LEDBostp\n - Noms de l'emetteur : Tom Djery\n - Noms du destinataire : zele Brice\n - IBAN du destinataire : CM15UBAA412341234123412341233\n - Banque : UBA Cameroon\n\nCe message a été envoyé dans le mail de l'émetteur",
|
||
* "error":null
|
||
* }
|
||
* ),
|
||
* }
|
||
* )
|
||
* )
|
||
* )
|
||
*/
|
||
// Transactions dans le wallet iLink
|
||
public function add(Request $request)
|
||
{
|
||
$transaction = new WalletIlinkTransaction();
|
||
$this->validate($request, [
|
||
'type' => 'required|integer|exists:type_ilink_transaction,id',
|
||
'id_wallet_agent' => 'required_without:id_wallet_user|integer|exists:wallets,id',
|
||
'id_wallet_user' => 'required_without:id_wallet_agent|integer|exists:wallets_users,id',
|
||
'password' => 'required',
|
||
'montant' => 'required|numeric|min:0|not_in:0',
|
||
]);
|
||
|
||
$id_wallet_agent = $request->input('id_wallet_agent');
|
||
$id_wallet_user = $request->input('id_wallet_user');
|
||
$type = TypeIlinkTransaction::findOrFail($request->input('type'));
|
||
|
||
if (isset($id_wallet_agent)) {
|
||
$walletAgent = Wallet::findOrFail($id_wallet_agent);
|
||
$network_agent = NetworksAgent::findOrFail($walletAgent->id_networkAgent);
|
||
$transaction->init_country = $init_country = $network_agent->network->country->id;
|
||
// Configuratrion du wallet
|
||
$config = ConfigWallet::where('id_network', $network_agent->network_id)->firstOrFail();
|
||
|
||
// Recuperation des wallets hyperviseur et superviseur
|
||
$codeGenerer = CodeGenerer::findOrFail($network_agent->codeGenerer_id);
|
||
$superviseur = AgentPlus::where('code_membre', $codeGenerer->code_parrain)->firstOrFail();
|
||
$hyperviseur = AgentPlus::where('code_membre', $superviseur->code_parrain)->firstOrFail();
|
||
|
||
$wallet_agent_sup = WalletAgent::where('agent_id', $superviseur->id)->firstOrFail();
|
||
$wallet_agent_hyp = WalletAgent::where('agent_id', $hyperviseur->id)->firstOrFail();
|
||
$walletSuperviseur = Wallet::findOrFail($wallet_agent_sup->wallet_id);
|
||
} else {
|
||
$walletUser = WalletsUser::findOrFail($id_wallet_user);
|
||
$transaction->init_country = $init_country = $walletUser->user->network->country->id;
|
||
$result = ConfigWallet::join('networks', 'networks.id', '=', 'configWallet.id_network')
|
||
->where('networks.country_id', $init_country)->where('configWallet.type', 'ilink')
|
||
->select('configWallet.id')->first();
|
||
if ($result) {
|
||
$config = ConfigWallet::findOrFail($result->id);
|
||
} else {
|
||
return $this->errorResponse(trans('errors.no_ilink_network'));
|
||
}
|
||
|
||
$hyperviseur = AgentPlus::where('category', 'hyper')->where('network_id', $config->id_network)->firstOrFail();
|
||
$wallet_agent_hyp = WalletAgent::where('agent_id', $hyperviseur->id)->firstOrFail();
|
||
}
|
||
$walletHyperviseur = Wallet::findOrFail($wallet_agent_hyp->wallet_id);
|
||
$transaction->network_emetteur = $config->id_network;
|
||
|
||
$taxesNationales = array_values(array_filter($config->taxes->all(), function ($tax) {
|
||
return $tax->destination == 'national' && $tax->categorie == 'wallet';
|
||
}));
|
||
|
||
$taxesInternationales = array_values(array_filter($config->taxes->all(), function ($tax) {
|
||
return $tax->destination == 'international' && $tax->categorie == 'wallet';
|
||
}));
|
||
|
||
$paliers_config_wallets = $config->paliers_config_wallets->all();
|
||
$paliers_commission_wallets = $config->paliers_commissions_wallets->all();
|
||
|
||
$plr_user_wallet_wallet = $this->getPaliers($paliers_config_wallets, "user_wallet_wallet_international");
|
||
$plr_user_wallet_cash = $this->getPaliers($paliers_config_wallets, "user_wallet_cash_international");
|
||
$plr_agent_depot_wallet_ilink = $this->getPaliers($paliers_config_wallets, "agent_depot_wallet_ilink_international");
|
||
$plr_agent_depot_autre_wallet = $this->getPaliers($paliers_config_wallets, "agent_depot_autre_wallet_international");
|
||
$plr_agent_cash_cash = $this->getPaliers($paliers_config_wallets, "agent_cash_cash_international");
|
||
|
||
$plr_user_wallet_wallet_national = $this->getPaliers($paliers_config_wallets, "user_wallet_wallet_national");
|
||
$plr_user_wallet_cash_national = $this->getPaliers($paliers_config_wallets, "user_wallet_cash_national");
|
||
$plr_agent_depot_wallet_ilink_national = $this->getPaliers($paliers_config_wallets, "agent_depot_wallet_ilink_national");
|
||
$plr_agent_depot_autre_wallet_national = $this->getPaliers($paliers_config_wallets, "agent_depot_autre_wallet_national");
|
||
$plr_agent_cash_cash_national = $this->getPaliers($paliers_config_wallets, "agent_cash_cash_national");
|
||
|
||
|
||
$data = $request->all();
|
||
if (isset($request->cvv) && !is_numeric($request->cvv))
|
||
return $this->errorResponse('errors.invalid_cvv');
|
||
|
||
$transaction->fill($data);
|
||
// Pay through payment service
|
||
$client = new Client([
|
||
'base_uri' => config('services.payment_service.base_uri'),
|
||
'headers' => [
|
||
'Authorization' => config('services.payment_service.key'),
|
||
]
|
||
]);
|
||
|
||
try {
|
||
DB::beginTransaction();
|
||
|
||
switch ($type->id) {
|
||
case 1: //User - Envoi wallet à wallet
|
||
$this->validate($request, $transaction->send_wallet_wallet_rules());
|
||
$user = $walletUser->user;
|
||
$this->validatePassword($request->password, $user->encrypted_password, $user->salt);
|
||
if ($init_country != $request->final_country) {
|
||
$this->checkUserIdentification($user->id);
|
||
}
|
||
|
||
if ($request->montant > $walletUser->balance) {
|
||
throw new Exception(trans('errors.insufficient_balance'), 500);
|
||
} else {
|
||
//Verification des limites reglementaires
|
||
$this->checkReguationsLimits($walletUser->id, $init_country, $request->final_country, $transaction->montant);
|
||
|
||
$transaction->frais = $frais = ($init_country != $request->final_country) ? $this->calculateFees($plr_user_wallet_wallet, $request->montant) : $this->calculateFees($plr_user_wallet_wallet_national, $request->montant);
|
||
$transaction->taxe = $taxe = ($init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
|
||
$transaction->montant_net = $montantDepot = $transaction->montant - $frais - $taxe;
|
||
$transaction->montant_net_final_country = $this->toMoneyAmount($montantDepot, $init_country, $request->final_country);
|
||
$configPayeur = ConfigWallet::where('id_network', $request->network_destinataire)->firstOrFail();
|
||
|
||
$reseauPayeur = PayingNetwork::where('id_network', $request->network_destinataire)->where('id_configWallet', $config->id)->first();
|
||
if (isset($reseauPayeur)) {
|
||
$transaction->part_reseau_payeur = $frais * $reseauPayeur->taux_partage / 100;
|
||
$transaction->part_reseau_payeur_final_country = $this->toMoneyAmount($transaction->part_reseau_payeur, $init_country, $request->final_country);
|
||
$transaction->part_reseau_emetteur = $frais - $transaction->part_reseau_payeur;
|
||
} else {
|
||
$transaction->part_reseau_payeur = 0;
|
||
$transaction->part_reseau_payeur_final_country = 0;
|
||
$transaction->part_reseau_emetteur = $frais;
|
||
}
|
||
|
||
$transaction->commission_hyp = $transaction->part_reseau_emetteur;
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
$transaction->type_id_destinataire = $request->input('type_id_destinataire');
|
||
if ($configPayeur->type == 'ilink') {
|
||
$destinataire = User::where($transaction->type_id_destinataire, $request->id_destinataire)->first();
|
||
if ($destinataire) { // Si c'est un wallet ilink
|
||
if ($destinataire->network->country->id == $request->final_country) {
|
||
$walletDestinataire = WalletsUser::where('idUser', $destinataire->id)->firstOrFail();
|
||
$walletDestinataire->balance += $this->toMoneyAmount($montantDepot, $init_country, $request->final_country);
|
||
$walletDestinataire->save();
|
||
} else {
|
||
$country = Country::findOrFail($request->final_country);
|
||
throw new Exception(trans('errors.wallet_country_not_match', ['country' => $country->name]), 500);
|
||
}
|
||
} else {
|
||
throw new Exception(trans('errors.wallet_not_defined'), 500);
|
||
}
|
||
|
||
//Mise a jour des comissions et compensation
|
||
if (isset($reseauPayeur)) {
|
||
$reseauPayeur->balance_com += $transaction->part_reseau_payeur_final_country;
|
||
$reseauPayeur->balance_compensation += $transaction->montant_net_final_country;
|
||
}
|
||
|
||
//Hyperviseur payeur
|
||
$hyperviseurPayeur = AgentPlus::where('category', 'hyper')->where('network_id', $request->network_destinataire)->firstOrFail();
|
||
if ($hyperviseurPayeur->id == $hyperviseur->id) { //Si c'est le reseau payeur est aussi emetteur
|
||
$transaction->id_wallet_hyp_payeur = $walletHyperviseur->id;
|
||
} else {
|
||
$wallet_agent_hypPayeur = WalletAgent::where('agent_id', $hyperviseurPayeur->id)->firstOrFail();
|
||
$walletHyperviseurPayeur = Wallet::findOrFail($wallet_agent_hypPayeur->wallet_id);
|
||
$walletHyperviseurPayeur->balance_com += $this->toMoneyAmount($transaction->part_reseau_payeur, $init_country, $request->final_country);
|
||
$transaction->id_wallet_hyp_payeur = $walletHyperviseurPayeur->id;
|
||
$walletHyperviseurPayeur->save();
|
||
}
|
||
} else {
|
||
//Emettre requete SSL vers wallet extene correspondant pour recharger le compte du destinataire
|
||
// La mise à jour des comissions de compensation se fera lors du traitement de la transaction
|
||
$transaction->status_reseau_payeur = 'EN_COURS';
|
||
$result = $this->sendFrame($this->PAYING_NETWORK_SIMULATOR_SEND_FRAME_URL, $transaction);
|
||
if ($result->getStatusCode() != 200) {
|
||
throw new Exception(trans('errors.unexpected_error'), 500);
|
||
}
|
||
}
|
||
$walletHyperviseur->balance_com += $transaction->part_reseau_emetteur;
|
||
$walletUser->balance -= $transaction->montant;
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
|
||
$walletHyperviseur->save();
|
||
$walletUser->save();
|
||
if (isset($reseauPayeur))
|
||
$reseauPayeur->save();
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$transaction->save();
|
||
$message = trans('messages.successful_user_send_to_wallet',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country), 'net_init' => $this->toMoney($montantDepot, $init_country),
|
||
'net_final' => $this->toMoneyWithCurrency($montantDepot, $init_country, $request->final_country), 'fees' => $this->toMoney($frais + $taxe, $init_country),
|
||
'init_country' => $this->getCountryName($init_country), 'final_country' => $this->getCountryName($request->final_country),
|
||
'sender_code' => $user->user_code, 'receiver_code' => $transaction->id_destinataire, 'network' => $this->getNetworkName($transaction->network_destinataire)]);
|
||
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
|
||
$response_message = $message . trans('messages.sent_by_mail');
|
||
}
|
||
break;
|
||
case 2: //User - Envoi de wallet à carte
|
||
$this->validate($request, $transaction->user_card_rules());
|
||
$user = $walletUser->user;
|
||
$this->validatePassword($request->password, $user->encrypted_password, $user->salt);
|
||
if ($request->montant > $walletUser->balance) {
|
||
throw new Exception(trans('errors.insufficient_balance'), 500);
|
||
} else {
|
||
if (!(isset($user->numero_carte) && isset($user->expiration_date))) {
|
||
throw new Exception(trans('errors.no_bank_card_attached'), 500);
|
||
}
|
||
|
||
$transaction->expiration_date = $user->expiration_date;
|
||
$transaction->numero_carte = $user->numero_carte;
|
||
|
||
// $final_country = $request->input('final_country');
|
||
$plr_user_wallet_cart_national = $this->getPaliers($paliers_commission_wallets, 'user_wallet_cart_national');
|
||
// $plr_user_wallet_cart_international = $this->getPaliers($paliers_commission_wallets, 'user_wallet_cart_international');
|
||
$plr_hyp_user_wallet_cart_national = $this->getPaliers($paliers_commission_wallets, 'hyp_user_wallet_cart_national');
|
||
// $plr_hyp_user_wallet_cart_international = $this->getPaliers($paliers_commission_wallets, 'hyp_user_wallet_cart_international');
|
||
$plr_bank_user_wallet_cart_national = $this->getPaliers($paliers_commission_wallets, 'bank_user_wallet_cart_national');
|
||
// $plr_bank_user_wallet_cart_international = $this->getPaliers($paliers_commission_wallets, 'bank_user_wallet_cart_international');
|
||
|
||
$frais = $this->calculateFees($plr_user_wallet_cart_national, $request->montant);
|
||
|
||
$transaction->montant_net = $montantDepot = $transaction->montant - $frais;
|
||
$identification = Identification::with(['country'])->where('id_user', $user->id)->first();
|
||
$customer_country_code = $identification ? Country::where('name', $identification->country)->first()?->code_country : $user->network->country->code_country;
|
||
|
||
// PayOut through payment service
|
||
$response = $client->post($this->PAYOUT_URL, ['json' => [
|
||
'card_no' => $user->numero_carte,
|
||
'exp_month' => date("m", strtotime($user->expiration_date)),
|
||
'exp_year' => date("Y", strtotime($user->expiration_date)),
|
||
'cvc' => $request->input('cvv'),
|
||
'amount' => $montantDepot,
|
||
'currency' => $this->getCurrency($init_country),
|
||
'payment_method' => 'CARD',
|
||
'customer_id' => $user->id,
|
||
'customer_email' => $user->email,
|
||
'customer_name' => $identification ? $identification->firstname : $user->firstname,
|
||
'customer_surname' => $identification ? $identification->lastname : $user->lastname,
|
||
'customer_phone_number' => $user->phone,
|
||
'customer_address' => $identification ? $identification->town : $user->adresse,
|
||
'customer_city' => $identification ? $identification->town : $user->adresse,
|
||
'customer_country' => $customer_country_code,
|
||
'reason' => "User - Envoi de wallet à carte"
|
||
], 'http_errors' => false]);
|
||
|
||
$code = $response->getStatusCode();
|
||
$content = json_decode($response->getBody()->getContents(), true);
|
||
if ($code == 200) {
|
||
$response = $content["response"];
|
||
$transaction->payment_transaction_id = $response["transaction_id"];
|
||
|
||
$walletUser->balance -= $transaction->montant;
|
||
//Emettre une trame SSL pour recharger le compte de la banque partenaire du montant de sa commission
|
||
$transaction->commission_banque = $this->calculateFees($plr_bank_user_wallet_cart_national, $request->montant, $frais);
|
||
$transaction->commission_hyp = $this->calculateFees($plr_hyp_user_wallet_cart_national, $request->montant, $frais);
|
||
|
||
$walletHyperviseur->balance_com += $transaction->commission_hyp;
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->frais = $frais;
|
||
$walletHyperviseur->save();
|
||
$walletUser->save();
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$transaction->save();
|
||
$message = trans('messages.successful_user_send_to_cart',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country),
|
||
'net' => $this->toMoney($montantDepot, $init_country), 'fees' => $this->toMoney($frais, $init_country),
|
||
'sender_code' => $user->user_code, 'cart_number' => wordwrap($transaction->numero_carte, 4, ' ', true)]);
|
||
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
|
||
$response_message = $message . trans('messages.sent_by_mail');
|
||
|
||
} else {
|
||
throw new Exception($content['error'] ?? __('errors.visa_api_failed'), $content['status'] ?? 500);
|
||
}
|
||
}
|
||
break;
|
||
case 3: //User - Envoi de wallet à cash
|
||
$this->validate($request, $transaction->send_wallet_cash_rules());
|
||
$user = $walletUser->user;
|
||
$this->validatePassword($request->password, $user->encrypted_password, $user->salt);
|
||
if ($init_country != $request->final_country) {
|
||
$this->checkUserIdentification($user->id);
|
||
}
|
||
|
||
if ($request->montant > $walletUser->balance) {
|
||
throw new Exception(trans('errors.insufficient_balance'), 500);
|
||
} else {
|
||
//Verification des limites reglementaires
|
||
$this->checkReguationsLimits($walletUser->id, $init_country, $request->final_country, $transaction->montant);
|
||
|
||
$transaction->frais = $frais = ($init_country != $request->final_country) ? $this->calculateFees($plr_user_wallet_cash, $request->montant) : $this->calculateFees($plr_user_wallet_cash_national, $request->montant);
|
||
$transaction->taxe = $taxe = ($init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
|
||
$transaction->montant_net = $montantRetrait = $transaction->montant - $frais - $taxe;
|
||
$transaction->montant_net_final_country = $this->toMoneyAmount($montantRetrait, $init_country, $request->final_country);
|
||
$configPayeur = ConfigWallet::where('id_network', $request->network_destinataire)->firstOrFail();
|
||
|
||
$reseauPayeur = PayingNetwork::where('id_network', $request->network_destinataire)->where('id_configWallet', $config->id)->first();
|
||
if (isset($reseauPayeur)) {
|
||
$transaction->part_reseau_payeur = $frais * $reseauPayeur->taux_partage / 100;
|
||
$transaction->part_reseau_payeur_final_country = $this->toMoneyAmount($transaction->part_reseau_payeur, $init_country, $request->final_country);
|
||
$transaction->part_reseau_emetteur = $frais - $transaction->part_reseau_payeur;
|
||
} else {
|
||
$transaction->part_reseau_payeur = 0;
|
||
$transaction->part_reseau_payeur_final_country = 0;
|
||
$transaction->part_reseau_emetteur = $frais;
|
||
}
|
||
|
||
$transaction->commission_hyp = $transaction->part_reseau_emetteur;
|
||
$transaction->commission_hyp_final_country = $this->toMoneyAmount($transaction->commission_hyp, $init_country, $request->final_country);
|
||
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
if ($configPayeur->type == 'ilink') {
|
||
//Mise a jour des comissions et compensation
|
||
if (isset($reseauPayeur)) {
|
||
$reseauPayeur->balance_com += $transaction->part_reseau_payeur_final_country;
|
||
$reseauPayeur->balance_compensation += $transaction->montant_net_final_country;
|
||
}
|
||
|
||
//Hyperviseur payeur
|
||
$hyperviseurPayeur = AgentPlus::where('category', 'hyper')->where('network_id', $request->network_destinataire)->firstOrFail();
|
||
if ($hyperviseurPayeur->id == $hyperviseur->id) { //Si c'est le reseau payeur est aussi emetteur
|
||
$transaction->id_wallet_hyp_payeur = $walletHyperviseur->id;
|
||
} else {
|
||
$wallet_agent_hypPayeur = WalletAgent::where('agent_id', $hyperviseurPayeur->id)->firstOrFail();
|
||
$walletHyperviseurPayeur = Wallet::findOrFail($wallet_agent_hypPayeur->wallet_id);
|
||
$walletHyperviseurPayeur->balance_com += $this->toMoneyAmount($transaction->part_reseau_payeur, $init_country, $request->final_country);
|
||
$transaction->id_wallet_hyp_payeur = $walletHyperviseurPayeur->id;
|
||
$walletHyperviseurPayeur->save();
|
||
}
|
||
} else {
|
||
//Emettre requete SSL vers wallet extene correspondant pour recharger le compte du destinataire
|
||
// La mise à jour des comissions de compensation se fera lors du traitement de la transaction
|
||
$transaction->status_reseau_payeur = 'EN_COURS';
|
||
$result = $this->sendFrame($this->PAYING_NETWORK_SIMULATOR_SEND_FRAME_URL, $transaction);
|
||
if ($result->getStatusCode() != 200) {
|
||
throw new Exception(trans('errors.unexpected_error'), 500);
|
||
}
|
||
}
|
||
$walletUser->balance -= $transaction->montant;
|
||
$walletHyperviseur->balance_com += $transaction->commission_hyp;
|
||
$code_retrait = $this->random_string();
|
||
$hash = $this->hashSSHA($code_retrait);
|
||
$transaction->encrypted_code_retrait = $hash['encrypted'];
|
||
$transaction->code_retrait_salt = $hash['salt'];
|
||
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
|
||
if (isset($reseauPayeur))
|
||
$reseauPayeur->save();
|
||
$walletHyperviseur->save();
|
||
$walletUser->save();
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$transaction->status_retrait = 0;
|
||
$transaction->save();
|
||
$message = trans('messages.successful_user_send_to_cash',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country), 'net_init' => $this->toMoney($montantRetrait, $init_country),
|
||
'net_final' => $this->toMoneyWithCurrency($montantRetrait, $init_country, $request->final_country), 'fees' => $this->toMoney($frais + $taxe, $init_country),
|
||
'init_country' => $this->getCountryName($init_country), 'final_country' => $this->getCountryName($request->final_country), 'code' => wordwrap($code_retrait, 4, ' ', true),
|
||
'sender_code' => $user->user_code, 'receiver_code' => $transaction->id_destinataire, 'receiver_name' => $request->prenom_destinataire . ' ' . $request->nom_destinataire,
|
||
'sender_name' => $user->lastname . ' ' . $user->firstname, 'network' => $this->getNetworkName($transaction->network_destinataire)]);
|
||
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail'));
|
||
}
|
||
break;
|
||
/**
|
||
* @OA\Schema(
|
||
* schema="user_send_from_wallet_to_bank",
|
||
* title = "User - Envoi de wallet à banque",
|
||
* @OA\Property(property="type",
|
||
* type="integer",
|
||
* enum = {4},
|
||
* default = 4,
|
||
* description="Type de la transaction"
|
||
* ),
|
||
* @OA\Property(property="id_wallet_user",
|
||
* type="integer",
|
||
* example=9,
|
||
* description="ID du wallet de l'utilisateur enregistré dans la base de données"
|
||
* ),
|
||
* @OA\Property(property="id_wallet_network",
|
||
* type="integer",
|
||
* example=101,
|
||
* description="ID du reseau enregistré dans la base de données auquel appartient le wallet"
|
||
* ),
|
||
* @OA\Property(property="montant",
|
||
* type="number",
|
||
* example = 10000,
|
||
* description="Montant de la transaction"
|
||
* ),
|
||
* @OA\Property(property="password",
|
||
* type="string",
|
||
* description="Mot de passe"
|
||
* )
|
||
* )
|
||
*/
|
||
case 4: //User - Envoi de wallet à banque
|
||
|
||
$this->validate($request, [
|
||
'id_wallet_network' => 'required|integer|min:0|not_in:0',
|
||
]);
|
||
|
||
$user = $walletUser->user;
|
||
$this->validatePassword($request->password, $user->encrypted_password, $user->salt);
|
||
|
||
$this->checkUserIdentification($user->id);
|
||
|
||
if (!(isset($user->iban) && isset($user->id_bank_country)))
|
||
throw new Exception(trans('errors.wallet_not_linked_to_bank_account'), 422);
|
||
|
||
|
||
if ($request->montant > $walletUser->balance)
|
||
throw new Exception(trans('errors.insufficient_balance'), 422);
|
||
|
||
|
||
//Reverifier si l'operateur est toujours associé au reseau
|
||
$network_bank = NetworksOperator::where('id_network', $request->id_wallet_network)->where('id_operator_country', $user->id_bank_country)->first();
|
||
if (!$network_bank)
|
||
throw new Exception(trans('errors.operator_not_associated_with_network') . '. ' . trans('errors.update_banking_information'));
|
||
|
||
if ($network_bank->operators_country->operator->type != 'bank')
|
||
throw new Exception(trans('errors.not_banking_operator') . '. ' . trans('errors.update_banking_information'));
|
||
|
||
//Reverifier le code IBAN correspond toujours
|
||
$country_code = $network_bank->network->country->code_country;
|
||
$bank_code = $network_bank->operators_country->code;
|
||
switch ($this->checkIBAN($user->iban, $country_code, $bank_code)) {
|
||
case 0:
|
||
throw new Exception(trans('errors.invalid_iban') . '. ' . trans('errors.update_banking_information'));
|
||
case 1:
|
||
throw new Exception(trans('errors.country_not_match_iban') . '. ' . trans('errors.update_banking_information'));
|
||
case 2:
|
||
throw new Exception(trans('errors.bank_not_match_iban') . '. ' . trans('errors.update_banking_information'));
|
||
}
|
||
|
||
$transaction->frais = $frais = 0;
|
||
$transaction->taxe = $taxe = 0;
|
||
// $walletUser->balance -= $transaction->montant;
|
||
//Emettre une trame SSL pour recharger le compte bancaire du montant de la transaction
|
||
$transaction->commission_banque = 0;
|
||
|
||
$transaction->commission_hyp = 0;
|
||
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->frais = $frais;
|
||
$transaction->bank = $user->bank->operators_country->operator->nom;
|
||
$transaction->country = $user->bank->network->country->name;
|
||
$transaction->id_bank = $user->id_bank_country;
|
||
$transaction->iban = $user->iban;
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
// $walletUser->save();
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
// $transaction->save();
|
||
|
||
Log::info('-------------------------- User - Envoi de wallet à banque ------------------------------------');
|
||
Log::info(json_encode($transaction));
|
||
Log::info('------------------------------------------------------------------------------------------------');
|
||
|
||
$message = trans('messages.successful_user_send_to_bank',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country),
|
||
'net' => $this->toMoney($transaction->montant, $init_country), 'iban' => $request->iban, 'fees' => $this->toMoney($frais, $init_country),
|
||
'sender_code' => $user->user_code, 'bank' => $transaction->bank, 'country' => $transaction->country]);
|
||
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail'));
|
||
|
||
break;
|
||
// case 5: //User - Envoi de carte à wallet
|
||
// $frais =$montant * $config->taux_com_user_carte_wallet / 100;
|
||
//// $taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);
|
||
// break;
|
||
// case 6: //User - Envoi de carte à cash
|
||
// $frais =$montant * $config->taux_com_user_carte_cash / 100;
|
||
//// $taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales , $frais) : $this->calculateTax($taxesNationales ,$frais);
|
||
// break;
|
||
case 9: // User - Retrait de wallet en cash
|
||
$user = $walletUser->user;
|
||
$this->validatePassword($request->password, $user->encrypted_password, $user->salt);
|
||
if ($request->montant > $walletUser->balance) {
|
||
throw new Exception(trans('errors.insufficient_balance'));
|
||
} else {
|
||
$this->checkUserIdentification($user->id);
|
||
|
||
$transaction->final_country = $init_country;
|
||
$transaction->frais = $frais = $this->calculateFees($plr_user_wallet_cash_national, $request->montant);
|
||
$transaction->taxe = $taxe = $this->calculateTax($taxesNationales, $frais);
|
||
$transaction->montant_net_final_country = $transaction->montant_net = $montantRetrait = $transaction->montant - $frais - $taxe;
|
||
$transaction->commission_hyp_final_country = $transaction->commission_hyp = $frais;
|
||
|
||
$walletUser->balance -= $transaction->montant;
|
||
$walletHyperviseur->balance_com += $transaction->commission_hyp;
|
||
$code_retrait = $this->random_string();
|
||
$hash = $this->hashSSHA($code_retrait);
|
||
$transaction->encrypted_code_retrait = $hash['encrypted'];
|
||
$transaction->code_retrait_salt = $hash['salt'];
|
||
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
|
||
$walletHyperviseur->save();
|
||
$walletUser->save();
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$transaction->status_retrait = 0;
|
||
$transaction->save();
|
||
$message = trans('messages.successful_user_remove_from_wallet_to_cash',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country), 'net' => $this->toMoney($montantRetrait, $init_country),
|
||
'fees' => $this->toMoney($frais + $taxe, $init_country), 'code' => wordwrap($code_retrait, 4, ' ', true),
|
||
'sender_code' => $user->user_code, 'init_country' => $this->getCountryName($init_country),]);
|
||
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail'));
|
||
}
|
||
break;
|
||
case 10: //User - Retrait de carte vers wallet
|
||
$this->validate($request, $transaction->user_card_rules());
|
||
$user = $walletUser->user;
|
||
$this->validatePassword($request->password, $user->encrypted_password, $user->salt);
|
||
if (!(isset($user->numero_carte) && isset($user->expiration_date)))
|
||
throw new Exception(trans('errors.no_bank_card_attached'));
|
||
|
||
$transaction->expiration_date = $user->expiration_date;
|
||
$transaction->numero_carte = $user->numero_carte;
|
||
|
||
// $final_country = $request->input('final_country');
|
||
// $transaction->final_country = $final_country;
|
||
$plr_user_cart_wallet_national = $this->getPaliers($paliers_commission_wallets, 'user_cart_wallet_national');
|
||
// $plr_user_cart_wallet_international = $this->getPaliers($paliers_commission_wallets, 'user_cart_wallet_international');
|
||
$plr_hyp_user_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'hyp_user_cart_cash_national');
|
||
// $plr_hyp_user_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'hyp_user_cart_cash_international');
|
||
$plr_bank_user_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'bank_user_cart_cash_national');
|
||
// $plr_bank_user_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'bank_user_cart_cash_international');
|
||
|
||
$frais = $this->calculateFees($plr_user_cart_wallet_national, $request->montant);
|
||
|
||
$transaction->montant_net = $montantRetrait = $transaction->montant + $frais;
|
||
$identification = Identification::where('id_user', $user->id)->first();
|
||
$customer_country_code = $identification ? Country::where('name', $identification->country)->first()?->code_country : $user->network->country->code_country;
|
||
|
||
$payment_transaction_id = $request->input('payment_transaction_id');
|
||
if (empty($payment_transaction_id)) {
|
||
// PayIn through payment service
|
||
$response = $client->post($this->PAYMENT_URL, ['json' => [
|
||
'card_no' => $user->numero_carte,
|
||
'exp_month' => date("m", strtotime($user->expiration_date)),
|
||
'exp_year' => date("Y", strtotime($user->expiration_date)),
|
||
'cvc' => $request->input('cvv'),
|
||
'amount' => $montantRetrait,
|
||
'currency' => $this->getCurrency($init_country),
|
||
'payment_method' => 'CARD',
|
||
'customer_id' => $user->id,
|
||
'customer_email' => $user->email,
|
||
'customer_name' => $identification ? $identification->firstname : $user->firstname,
|
||
'customer_surname' => $identification ? $identification->lastname : $user->lastname,
|
||
'customer_address' => $identification ? $identification->town : $user->adresse,
|
||
'customer_city' => $identification ? $identification->town : $user->adresse,
|
||
'customer_country' => $customer_country_code,
|
||
'reason' => "User - Retrait de carte vers wallet"
|
||
], 'http_errors' => false]);
|
||
|
||
} else {
|
||
// Check payment status through payment service
|
||
if (WalletIlinkTransaction::where('payment_transaction_id', $payment_transaction_id)->exists()) {
|
||
throw new Exception(__('errors.transaction_already_completed'), 500);
|
||
}
|
||
|
||
$response = $client->get($this->PAYMENT_CHECK_STATUS . '/' . $payment_transaction_id, ['http_errors' => false]);
|
||
}
|
||
|
||
$code = $response->getStatusCode();
|
||
$content = json_decode($response->getBody()->getContents(), true);
|
||
if ($code == 301) {
|
||
return $this->successResponse($content["response"], $content['status']);
|
||
}
|
||
|
||
if ($code == 200) {
|
||
$response = $content["response"];
|
||
$transaction->payment_transaction_id = $response["transaction_id"];
|
||
|
||
$transaction->commission_banque = $this->calculateFees($plr_bank_user_cart_cash_national, $request->montant, $frais);
|
||
//Emettre une trame SSL pour recharger le compte de la banque partenaire du montant de sa commission
|
||
|
||
$walletUser->balance += $transaction->montant;
|
||
$transaction->commission_hyp = $this->calculateFees($plr_hyp_user_cart_cash_national, $request->montant, $frais);
|
||
$walletHyperviseur->balance_com += $transaction->commission_hyp;
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->frais = $frais;
|
||
$walletHyperviseur->save();
|
||
$walletUser->save();
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$transaction->save();
|
||
$message = trans('messages.successful_user_remove_from_cart_to_wallet',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country),
|
||
'total' => $this->toMoney($montantRetrait, $init_country), 'fees' => $this->toMoney($frais, $init_country),
|
||
'sender_code' => $user->user_code, 'cart_number' => wordwrap($transaction->numero_carte, 4, ' ', true)]);
|
||
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail'));
|
||
|
||
} else {
|
||
throw new Exception($content['error'] ?? __('errors.visa_api_failed'), $content['status'] ?? 500);
|
||
}
|
||
break;
|
||
case 11: // User - Retrait de carte vers cash
|
||
$this->validate($request, $transaction->user_card_rules());
|
||
$user = $walletUser->user;
|
||
$this->validatePassword($request->password, $user->encrypted_password, $user->salt);
|
||
$this->checkUserIdentification($user->id);
|
||
|
||
if (!(isset($user->numero_carte) && isset($user->expiration_date)))
|
||
throw new Exception(trans('errors.no_bank_card_attached'));
|
||
|
||
$transaction->expiration_date = $user->expiration_date;
|
||
$transaction->numero_carte = $user->numero_carte;
|
||
$transaction->final_country = $init_country;
|
||
// $final_country = $request->input('final_country');
|
||
// $transaction->final_country = $final_country;
|
||
$plr_user_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'user_cart_cash_national');
|
||
// $plr_user_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'user_cart_cash_international');
|
||
$plr_hyp_user_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'hyp_user_cart_cash_national');
|
||
// $plr_hyp_user_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'hyp_user_cart_cash_international');
|
||
$plr_bank_user_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'bank_user_cart_cash_national');
|
||
// $plr_bank_user_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'bank_user_cart_cash_international');
|
||
|
||
$frais = $this->calculateFees($plr_user_cart_cash_national, $request->montant);
|
||
|
||
$transaction->montant_net = $montantRetrait = $transaction->montant + $frais;
|
||
$transaction->montant_net_final_country = $transaction->montant;
|
||
|
||
$identification = Identification::where('id_user', $user->id)->first();
|
||
$customer_country_code = $identification ? Country::where('name', $identification->country)->first()?->code_country : $user->network->country->code_country;
|
||
|
||
$payment_transaction_id = $request->input('payment_transaction_id');
|
||
if (empty($payment_transaction_id)) {
|
||
$response = $client->post($this->PAYMENT_URL, ['json' => [
|
||
'card_no' => $user->numero_carte,
|
||
'exp_month' => date("m", strtotime($user->expiration_date)),
|
||
'exp_year' => date("Y", strtotime($user->expiration_date)),
|
||
'cvc' => $request->input('cvv'),
|
||
'amount' => $montantRetrait,
|
||
'currency' => $this->getCurrency($init_country),
|
||
'payment_method' => 'CARD',
|
||
'customer_id' => $user->id,
|
||
'customer_email' => $user->email,
|
||
'customer_name' => $identification ? $identification->firstname : $user->firstname,
|
||
'customer_surname' => $identification ? $identification->lastname : $user->lastname,
|
||
'customer_address' => $identification ? $identification->town : $user->adresse,
|
||
'customer_city' => $identification ? $identification->town : $user->adresse,
|
||
'customer_country' => $customer_country_code,
|
||
'reason' => "User - Retrait de carte vers cash"
|
||
], 'http_errors' => false]);
|
||
|
||
} else {
|
||
// Check payment status through payment service
|
||
if (WalletIlinkTransaction::where('payment_transaction_id', $payment_transaction_id)->exists()) {
|
||
throw new Exception(__('errors.transaction_already_completed'), 500);
|
||
}
|
||
|
||
$response = $client->get($this->PAYMENT_CHECK_STATUS . '/' . $payment_transaction_id, ['http_errors' => false]);
|
||
}
|
||
|
||
$code = $response->getStatusCode();
|
||
$content = json_decode($response->getBody()->getContents(), true);
|
||
if ($code == 301) {
|
||
return $this->successResponse($content["response"], $content['status']);
|
||
}
|
||
|
||
if ($code == 200) {
|
||
$response = $content["response"];
|
||
$transaction->payment_transaction_id = $response["transaction_id"];
|
||
|
||
$transaction->commission_banque = $this->calculateFees($plr_bank_user_cart_cash_national, $request->montant, $frais);
|
||
//Emettre une trame SSL pour recharger le compte de la banque partenaire du montant de sa commission
|
||
|
||
$code_retrait = $this->random_string();
|
||
$hash = $this->hashSSHA($code_retrait);
|
||
$transaction->encrypted_code_retrait = $hash['encrypted'];
|
||
$transaction->code_retrait_salt = $hash['salt'];
|
||
|
||
$transaction->commission_hyp = $this->calculateFees($plr_hyp_user_cart_cash_national, $request->montant, $frais);
|
||
$walletHyperviseur->balance_com += $transaction->commission_hyp;
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->frais = $frais;
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$transaction->status_retrait = 0;
|
||
$walletHyperviseur->save();
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
$transaction->save();
|
||
$message = trans('messages.successful_user_remove_from_cart_to_cash',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country),
|
||
'final_country' => $this->getCountryName($init_country), 'init_country' => $this->getCountryName($init_country),
|
||
'total' => $this->toMoney($transaction->montant, $init_country), 'fees' => $this->toMoney($frais, $init_country), 'code' => wordwrap($code_retrait, 4, ' ', true),
|
||
'sender_code' => $user->user_code, 'cart_number' => wordwrap($transaction->numero_carte, 4, ' ', true)]);
|
||
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
|
||
return $this->successResponse($message . trans('messages.sent_by_mail'));
|
||
|
||
} else {
|
||
throw new Exception($content['error'] ?? __('errors.visa_api_failed'), $content['status'] ?? 500);
|
||
}
|
||
break;
|
||
case 12: // Agent - Retrait en cash
|
||
$this->validate($request, $transaction->remove_cash_rules());
|
||
$agent = AgentPlus::findOrFail($network_agent->agent_id);
|
||
$this->validatePassword($request->password, $agent->encrypted_password, $agent->salt);
|
||
$transaction = WalletIlinkTransaction::where('id_transaction', $request->id_transaction)->first();
|
||
if ($transaction) {
|
||
//Verifier si l'agent qui a effectué l'envoi de cash à cash ne puisse pas retirer l'argent
|
||
if ($transaction->type == 17)
|
||
if ($transaction->id_wallet_ag == $walletAgent->id)
|
||
throw new Exception(trans('errors.agent_unauthorized'));
|
||
//Verifier que le reseau payeur est de type iLink
|
||
if (in_array($transaction->type, [3, 17])) {
|
||
$configPayeur = ConfigWallet::where('id_network', $transaction->network_destinataire)->firstOrFail();
|
||
if ($configPayeur->type != 'ilink')
|
||
throw new Exception(trans('errors.withdrawal_network_unauthorized', ['network' => $this->getNetworkName($transaction->network_destinataire),
|
||
'country' => $this->getCountryName($transaction->final_country)]));
|
||
}
|
||
if ($transaction->status_retrait == 0) {
|
||
//Verifier que le pays de destinatation correspond au pays de retrait
|
||
if ($init_country != $transaction->final_country)
|
||
throw new Exception(trans('errors.operation_cannot_performed_in_country'));
|
||
if ($this->checkPassword($request->code_retrait, $transaction->encrypted_code_retrait, $transaction->code_retrait_salt)) {
|
||
$montantNet = $transaction->type == 11 ? $transaction->montant_net : $transaction->montant_net_final_country;
|
||
if (in_array($transaction->type, [3, 17])) {
|
||
$commissionHyp = ($transaction->network_emetteur != $transaction->network_destinataire) ?
|
||
$transaction->part_reseau_payeur_final_country : $transaction->commission_hyp_final_country;
|
||
} else {
|
||
$commissionHyp = $transaction->type == 11 ? $transaction->commission_hyp : $transaction->commission_hyp_final_country;
|
||
}
|
||
if ($montantNet == $request->montant) {
|
||
$transactionRetrait = $transaction->replicate();
|
||
$transactionRetrait->id = null;
|
||
$transactionRetrait->type = $request->type;
|
||
$transactionRetrait->id_transaction = $this->getTransactionID();
|
||
$transactionRetrait->montant = $montantNet;
|
||
$emailEmetteur = $transaction->wallet_user ? $transaction->wallet_user->user->email : $transaction->email_emetteur;
|
||
$emetteur = $transaction->wallet_user ? $transaction->wallet_user->user->lastname . ' ' . $transaction->wallet_user->user->firstname : $transaction->prenom_emetteur . ' ' . $transaction->nom_emetteur;
|
||
$destinataire = in_array($transaction->type, [9, 11]) ? $emetteur : $transaction->prenom_destinataire . ' ' . $transaction->nom_destinataire;
|
||
|
||
// $transactionRetrait->commission_ag = floatval($commissionHyp * $config->taux_com_ag_retrait_cash / 100);
|
||
// $transactionRetrait->commission_sup = floatval($commissionHyp * $config->taux_com_sup_retrait_cash / 100);
|
||
// $transactionRetrait->commission_hyp = $commissionHyp - $transactionRetrait->commission_ag - $transactionRetrait->commission_sup;
|
||
|
||
// Commissions
|
||
$final_country = $transaction->final_country;
|
||
$plr_agent_wallet_cash_national = $this->getPaliers($paliers_commission_wallets, 'agent_wallet_cash_national');
|
||
$plr_agent_wallet_cash_international = $this->getPaliers($paliers_commission_wallets, 'agent_wallet_cash_international');
|
||
$plr_sup_wallet_cash_national = $this->getPaliers($paliers_commission_wallets, 'sup_wallet_cash_national');
|
||
$plr_sup_wallet_cash_international = $this->getPaliers($paliers_commission_wallets, 'sup_wallet_cash_international');
|
||
|
||
$transactionRetrait->commission_ag = ($init_country != $final_country) ? $this->calculateFees($plr_agent_wallet_cash_international, $transaction->montant) : $this->calculateFees($plr_agent_wallet_cash_national, $transaction->montant);
|
||
$transactionRetrait->commission_sup = ($init_country != $final_country) ? $this->calculateFees($plr_sup_wallet_cash_international, $transaction->montant, $transactionRetrait->commission_ag) : $this->calculateFees($plr_sup_wallet_cash_national, $transaction->montant, $transactionRetrait->commission_ag);
|
||
$transactionRetrait->commission_hyp = $commissionHyp - ($transactionRetrait->commission_ag + $transactionRetrait->commission_sup);
|
||
|
||
$walletAgent->balance_princ += $montantNet;
|
||
$walletAgent->balance_com += $transactionRetrait->commission_ag;
|
||
$walletSuperviseur->balance_com += $transactionRetrait->commission_sup;
|
||
$walletHyperviseur->balance_com -= ($transactionRetrait->commission_ag + $transactionRetrait->commission_sup);
|
||
$transactionRetrait->id_wallet_ag = $walletAgent->id;
|
||
$transactionRetrait->id_wallet_sup = $walletSuperviseur->id;
|
||
$transactionRetrait->id_wallet_hyp = $walletHyperviseur->id;
|
||
|
||
$transactionRetrait->status_retrait = $transactionRetrait->commission_banque = $transactionRetrait->code_retrait_salt = $transactionRetrait->encrypted_code_retrait = null;
|
||
$transactionRetrait->frais = $transactionRetrait->taxe = 0;
|
||
$transaction->status_retrait = 1;
|
||
$transaction->date_retrait = $this->getCurrentTime($init_country);
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
//Si la transaction provient du reseau emetteur
|
||
if ($transaction->from_network_emetteur) {
|
||
$transaction->status_reseau_payeur = 'TRAITEE';
|
||
}
|
||
$walletAgent->save();
|
||
$walletSuperviseur->save();
|
||
$walletHyperviseur->save();
|
||
$transaction->save();
|
||
$transactionRetrait->save();
|
||
$message = trans('messages.successful_agent_remove_cash',
|
||
['id_transaction' => $transactionRetrait->id_transaction, 'amount' => $this->toMoney($request->montant, $init_country), 'init_country' => $this->getCountryName($transaction->init_country),
|
||
'code' => wordwrap($request->code_retrait, 4, ' ', true), 'final_country' => $this->getCountryName($transaction->final_country),
|
||
'sender_name' => $emetteur, 'receiver_name' => $destinataire, 'id_transaction_retrait' => $transaction->id_transaction]);
|
||
$this->sendMail($emailEmetteur, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail'));
|
||
} else {
|
||
throw new Exception(trans('errors.incorrect_withdrawal_amount'));
|
||
}
|
||
} else {
|
||
throw new Exception(trans('errors.invalid_withdrawal_code'));
|
||
}
|
||
} else {
|
||
throw new Exception(trans('errors.withdrawal_already_made'));
|
||
}
|
||
} else {
|
||
throw new Exception(trans('errors.transaction_not_exist'), Response::HTTP_NOT_FOUND);
|
||
}
|
||
break;
|
||
case 13: // Agent - Retrait de la carte vers cash
|
||
$this->validate($request, $transaction->card_rules());
|
||
$agent = AgentPlus::findOrFail($network_agent->agent_id);
|
||
$this->validatePassword($request->password, $agent->encrypted_password, $agent->salt);
|
||
$expiration_date = DateTime::createFromFormat('m/y', $request->expiration_date);
|
||
if (!$expiration_date)
|
||
$expiration_date = new DateTime();
|
||
$transaction->expiration_date = $expiration_date;
|
||
$transaction->final_country = $init_country;
|
||
|
||
// $final_country = $request->final_country;
|
||
$plr_customer_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'customer_cart_cash_national');
|
||
// $plr_customer_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'customer_cart_cash_international');
|
||
$plr_agent_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'agent_cart_cash_national');
|
||
// $plr_agent_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'agent_cart_cash_international');
|
||
$plr_sup_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'sup_cart_cash_national');
|
||
// $plr_sup_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'sup_cart_cash_international');
|
||
$plr_hyp_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'hyp_cart_cash_national');
|
||
// $plr_hyp_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'hyp_cart_cash_international');
|
||
$plr_bank_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'bank_cart_cash_national');
|
||
// $plr_bank_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'bank_cart_cash_international');
|
||
|
||
$frais = $this->calculateFees($plr_customer_cart_cash_national, $request->montant);
|
||
$montantRetrait = $transaction->montant + $frais;
|
||
$transaction->montant_net = $montantRetrait;
|
||
|
||
$payment_transaction_id = $request->input('payment_transaction_id');
|
||
if (empty($payment_transaction_id)) {
|
||
$response = $client->post($this->PAYMENT_URL, ['json' => [
|
||
'card_no' => $request->input('numero_carte'),
|
||
'exp_month' => $expiration_date->format('m'),
|
||
'exp_year' => $expiration_date->format('Y'),
|
||
'cvc' => $request->input('cvv'),
|
||
'amount' => $montantRetrait,
|
||
'currency' => $this->getCurrency($init_country),
|
||
'payment_method' => 'CARD',
|
||
'customer_email' => $request->input('customer_email'),
|
||
'customer_name' => $request->input('firstname'),
|
||
'customer_surname' => $request->input('lastname'),
|
||
'customer_address' => $request->input('customer_address'),
|
||
'customer_city' => $request->input('customer_city'),
|
||
'customer_country' => $request->input('customer_country'),
|
||
'customer_state' => $request->input('customer_state'),
|
||
'customer_zip_code' => $request->input('customer_zip_code'),
|
||
'reason' => " Agent - Retrait de la carte vers cash"
|
||
], 'http_errors' => false]);
|
||
} else {
|
||
// Check payment status through payment service
|
||
if (WalletIlinkTransaction::where('payment_transaction_id', $payment_transaction_id)->exists()) {
|
||
throw new Exception(__('errors.transaction_already_completed'), 500);
|
||
}
|
||
|
||
$response = $client->get($this->PAYMENT_CHECK_STATUS . '/' . $payment_transaction_id, ['http_errors' => false]);
|
||
}
|
||
|
||
$code = $response->getStatusCode();
|
||
$content = json_decode($response->getBody()->getContents(), true);
|
||
if ($code == 301) {
|
||
return $this->successResponse($content["response"], $content['status']);
|
||
}
|
||
|
||
if ($code == 200) {
|
||
$response = $content["response"];
|
||
$transaction->payment_transaction_id = $response["transaction_id"];
|
||
|
||
$banqueCommission = $this->calculateFees($plr_bank_cart_cash_national, $request->montant, $frais);
|
||
$transaction->commission_banque = $banqueCommission;
|
||
// 2---> Emmètre via API sécurisé SSL une requête de débit de notre
|
||
//compte marchand du (Part de la banque partenaire en %
|
||
//pour le dépôt qui s’applique sur les frais minimum) et créditer
|
||
//le compte des opérations défini avec notre banque
|
||
//partenaire
|
||
|
||
$walletAgent->balance_princ += $transaction->montant;
|
||
|
||
$agentCommission = $this->calculateFees($plr_agent_cart_cash_national, $request->montant, $frais);
|
||
$superviseurCommission = $this->calculateFees($plr_sup_cart_cash_national, $request->montant, $frais);
|
||
$hyperviseurCommission = $this->calculateFees($plr_hyp_cart_cash_national, $request->montant, $frais);
|
||
|
||
$walletAgent->balance_com += $agentCommission;
|
||
$transaction->commission_ag = $agentCommission;
|
||
$walletSuperviseur->balance_com += $superviseurCommission;
|
||
$transaction->commission_sup = $superviseurCommission;
|
||
$walletHyperviseur->balance_com += $hyperviseurCommission;
|
||
$transaction->commission_hyp = $hyperviseurCommission;
|
||
$transaction->id_wallet_ag = $walletAgent->id;
|
||
$transaction->id_wallet_sup = $walletSuperviseur->id;
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->frais = $frais;
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$walletAgent->save();
|
||
$walletSuperviseur->save();
|
||
$walletHyperviseur->save();
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
$transaction->save();
|
||
$response_message = (trans('messages.successful_transaction'));
|
||
|
||
} else {
|
||
throw new Exception($content['error'] ?? __('errors.visa_api_failed'), $content['status'] ?? 500);
|
||
}
|
||
break;
|
||
case 14: // Agent - Envoi de cash vers wallet iLink
|
||
$this->validate($request, $transaction->cash_wallet_rules());
|
||
$agent = AgentPlus::findOrFail($network_agent->agent_id);
|
||
$this->validatePassword($request->password, $agent->encrypted_password, $agent->salt);
|
||
if ($request->montant > $walletAgent->balance_princ)
|
||
throw new Exception(trans('errors.insufficient_balance'));
|
||
|
||
$user = User::where('user_code', $request->user_code)->first();
|
||
if (!$user)
|
||
throw new Exception(trans('errors.wallet_not_defined'));
|
||
|
||
$transaction->id_destinataire = $request->user_code;
|
||
$walletUser = WalletsUser::where('idUser', $user->id)->firstOrFail();
|
||
$transaction->final_country = $final_country = $user->network->country->id;
|
||
|
||
//Verification des limites reglementaires
|
||
$this->checkReguationsLimits($walletUser->id, $init_country, $final_country, $transaction->montant);
|
||
|
||
$frais = ($init_country != $final_country) ? $this->calculateFees($plr_agent_depot_wallet_ilink, $request->montant) : $this->calculateFees($plr_agent_depot_wallet_ilink_national, $request->montant);
|
||
$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
|
||
$montantDepot = $request->montant - $frais - $taxe;
|
||
$transaction->montant_net = $montantDepot;
|
||
$transaction->montant_net_final_country = $this->toMoneyAmount($montantDepot, $init_country, $final_country);
|
||
$walletUser->balance += $transaction->montant_net_final_country;
|
||
|
||
$plr_agent_cash_wallet_or_cash_national = $this->getPaliers($paliers_commission_wallets, 'agent_cash_wallet_or_cash_national');
|
||
$plr_agent_cash_wallet_or_cash_international = $this->getPaliers($paliers_commission_wallets, 'agent_cash_wallet_or_cash_international');
|
||
$plr_sup_cash_wallet_or_cash_national = $this->getPaliers($paliers_commission_wallets, 'sup_cash_wallet_or_cash_national');
|
||
$plr_sup_cash_wallet_or_cash_international = $this->getPaliers($paliers_commission_wallets, 'sup_cash_wallet_or_cash_international');
|
||
|
||
$commisionAgent = ($init_country != $final_country) ? $this->calculateFees($plr_agent_cash_wallet_or_cash_international, $request->montant) : $this->calculateFees($plr_agent_cash_wallet_or_cash_national, $request->montant);
|
||
$commisionSuper = ($init_country != $final_country) ? $this->calculateFees($plr_sup_cash_wallet_or_cash_international, $request->montant, $commisionAgent) : $this->calculateFees($plr_sup_cash_wallet_or_cash_national, $request->montant, $commisionAgent);
|
||
$commisionHyper = $frais - $commisionAgent - $commisionSuper;
|
||
|
||
$walletAgent->balance_princ -= $transaction->montant;
|
||
$walletAgent->balance_com += $commisionAgent;
|
||
$transaction->commission_ag = $commisionAgent;
|
||
$walletSuperviseur->balance_com += $commisionSuper;
|
||
$transaction->commission_sup = $commisionSuper;
|
||
$walletHyperviseur->balance_com += $commisionHyper;
|
||
$transaction->commission_hyp = $commisionHyper;
|
||
$transaction->taxe = $taxe;
|
||
$transaction->frais = $frais;
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$walletUser->save();
|
||
$walletAgent->save();
|
||
$walletSuperviseur->save();
|
||
$walletHyperviseur->save();
|
||
$transaction->id_wallet_user = $walletUser->id;
|
||
$transaction->id_wallet_ag = $walletAgent->id;
|
||
$transaction->id_wallet_sup = $walletSuperviseur->id;
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
$transaction->save();
|
||
$message = trans('messages.successful_agent_deposit_wallet_ilink',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country), 'net_init' => $this->toMoney($montantDepot, $init_country),
|
||
'net_final' => $this->toMoneyWithCurrency($montantDepot, $init_country, $final_country), 'fees' => $this->toMoney($frais + $taxe, $init_country), 'init_country' => $this->getCountryName($init_country),
|
||
'final_country' => $this->getCountryName($final_country), 'user_code' => $request->user_code]);
|
||
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail_to_recipient'));
|
||
break;
|
||
case 15: // Agent - Envoi de cash vers autre wallet
|
||
$this->validate($request, array_merge($transaction->cash_cash_rules(), [
|
||
'id_destinataire' => 'required'
|
||
]));
|
||
$agent = AgentPlus::findOrFail($network_agent->agent_id);
|
||
$this->validatePassword($request->password, $agent->encrypted_password, $agent->salt);
|
||
if ($request->montant > $walletAgent->balance_princ)
|
||
throw new Exception(trans('errors.insufficient_balance'), 422);
|
||
|
||
//Verification des limites reglementaires
|
||
$this->checkReguationsLimits($request->id_document_emetteur, $init_country, $request->final_country, $transaction->montant, true);
|
||
|
||
$frais = ($init_country != $request->final_country) ? $this->calculateFees($plr_agent_depot_autre_wallet, $request->montant) : $this->calculateFees($plr_agent_depot_autre_wallet_national, $request->montant);
|
||
$taxe = ($init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
|
||
$montantDepot = $request->montant - $frais - $taxe;
|
||
$transaction->montant_net = $montantDepot;
|
||
$transaction->montant_net_final_country = $this->toMoneyAmount($montantDepot, $init_country, $request->final_country);
|
||
|
||
$reseauPayeur = PayingNetwork::where('id_network', $request->network_destinataire)->where('id_configWallet', $config->id)->firstOrFail();
|
||
$transaction->part_reseau_payeur = $frais * $reseauPayeur->taux_partage / 100;
|
||
$transaction->part_reseau_emetteur = $frais - $transaction->part_reseau_payeur;
|
||
|
||
$transaction->part_reseau_payeur_final_country = $this->toMoneyAmount($transaction->part_reseau_payeur, $init_country, $request->final_country);
|
||
if (isset($reseauPayeur))
|
||
$reseauPayeur->balance_com += $transaction->part_reseau_payeur_final_country;
|
||
|
||
//Emettre une trame securise pour crediter le wallet utilisateur
|
||
// $walletUser->balance += $transaction->montant_net_final_country;
|
||
// $transaction->id_destinataire = r
|
||
|
||
$final_country = $request->final_country;
|
||
$plr_agent_cash_wallet_or_cash_national = $this->getPaliers($paliers_commission_wallets, 'agent_cash_wallet_or_cash_national');
|
||
$plr_agent_cash_wallet_or_cash_international = $this->getPaliers($paliers_commission_wallets, 'agent_cash_wallet_or_cash_international');
|
||
$plr_sup_cash_wallet_or_cash_national = $this->getPaliers($paliers_commission_wallets, 'sup_cash_wallet_or_cash_national');
|
||
$plr_sup_cash_wallet_or_cash_international = $this->getPaliers($paliers_commission_wallets, 'sup_cash_wallet_or_cash_international');
|
||
|
||
$commisionAgent = ($init_country != $final_country) ? $this->calculateFees($plr_agent_cash_wallet_or_cash_international, $request->montant) : $this->calculateFees($plr_agent_cash_wallet_or_cash_national, $request->montant);
|
||
$commisionSuper = ($init_country != $final_country) ? $this->calculateFees($plr_sup_cash_wallet_or_cash_international, $request->montant, $commisionAgent) : $this->calculateFees($plr_sup_cash_wallet_or_cash_national, $request->montant, $commisionAgent);
|
||
$commisionHyper = $transaction->part_reseau_emetteur - $commisionAgent - $commisionSuper;
|
||
|
||
$walletAgent->balance_princ -= $transaction->montant;
|
||
$walletAgent->balance_com += $commisionAgent;
|
||
$transaction->commission_ag = $commisionAgent;
|
||
$walletSuperviseur->balance_com += $commisionSuper;
|
||
$transaction->commission_sup = $commisionSuper;
|
||
$walletHyperviseur->balance_com += $commisionHyper;
|
||
$transaction->commission_hyp = $commisionHyper;
|
||
$transaction->taxe = $taxe;
|
||
$transaction->frais = $frais;
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
|
||
$walletAgent->save();
|
||
$walletSuperviseur->save();
|
||
$walletHyperviseur->save();
|
||
if (isset($reseauPayeur))
|
||
$reseauPayeur->save();
|
||
$transaction->id_wallet_ag = $walletAgent->id;
|
||
$transaction->id_wallet_sup = $walletSuperviseur->id;
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
$transaction->save();
|
||
$message = trans('messages.successful_agent_deposit_other_wallet',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country), 'net_init' => $this->toMoney($montantDepot, $init_country),
|
||
'net_final' => $this->toMoneyWithCurrency($montantDepot, $init_country, $request->final_country), 'fees' => $this->toMoney($frais + $taxe, $init_country), 'init_country' => $this->getCountryName($init_country),
|
||
'final_country' => $this->getCountryName($request->final_country), 'receiver_code' => $request->id_destinataire, 'network' => $this->getNetworkName($transaction->network_destinataire),
|
||
'sender_name' => $request->prenom_emetteur . ' ' . $request->nom_emetteur, 'receiver_name' => $request->prenom_destinataire . ' ' . $request->nom_destinataire,]);
|
||
$this->sendMail($request->email_emetteur, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail'));
|
||
break;
|
||
case 16: // Agent - Envoi de cash vers une carte visa
|
||
$this->validate($request, $transaction->card_rules());
|
||
$agent = AgentPlus::findOrFail($network_agent->agent_id);
|
||
$this->validatePassword($request->password, $agent->encrypted_password, $agent->salt);
|
||
if ($request->montant > $walletAgent->balance_princ)
|
||
throw new Exception(trans('errors.insufficient_balance'), 422);
|
||
$expiration_date = DateTime::createFromFormat('m/y', $request->expiration_date);
|
||
if (!$expiration_date)
|
||
$expiration_date = new DateTime();
|
||
$transaction->expiration_date = $expiration_date;
|
||
$transaction->final_country = $init_country;
|
||
|
||
// $final_country = $request->final_country;
|
||
$plr_customer_cash_cart_national = $this->getPaliers($paliers_commission_wallets, 'customer_cash_cart_national');
|
||
// $plr_customer_cash_cart_international = $this->getPaliers($paliers_commission_wallets, 'customer_cash_cart_international');
|
||
$plr_agent_cash_cart_national = $this->getPaliers($paliers_commission_wallets, 'agent_cash_cart_national');
|
||
// $plr_agent_cash_cart_international = $this->getPaliers($paliers_commission_wallets, 'agent_cash_cart_international');
|
||
$plr_sup_cash_cart_national = $this->getPaliers($paliers_commission_wallets, 'sup_cash_cart_national');
|
||
// $plr_sup_cash_cart_international = $this->getPaliers($paliers_commission_wallets, 'sup_cash_cart_international');
|
||
$plr_hyp_cash_cart_national = $this->getPaliers($paliers_commission_wallets, 'hyp_cash_cart_national');
|
||
// $plr_hyp_cash_cart_international = $this->getPaliers($paliers_commission_wallets, 'hyp_cash_cart_international');
|
||
$plr_bank_cash_cart_national = $this->getPaliers($paliers_commission_wallets, 'bank_cash_cart_national');
|
||
// $plr_bank_cash_cart_international = $this->getPaliers($paliers_commission_wallets, 'bank_cash_cart_international');
|
||
|
||
$frais = $this->calculateFees($plr_customer_cash_cart_national, $request->montant);
|
||
|
||
$montantDepot = $transaction->montant - $frais;
|
||
$transaction->montant_net = $montantDepot;
|
||
|
||
// PayOut through payment service
|
||
$response = $client->post($this->PAYOUT_URL, ['json' => [
|
||
'card_no' => $request->input('numero_carte'),
|
||
'exp_month' => $expiration_date->format('m'),
|
||
'exp_year' => $expiration_date->format('Y'),
|
||
'cvc' => $request->input('cvv'),
|
||
'amount' => $montantDepot,
|
||
'currency' => $this->getCurrency($init_country),
|
||
'payment_method' => 'CARD',
|
||
'customer_email' => $request->input('customer_email'),
|
||
'customer_name' => $request->input('firstname'),
|
||
'customer_surname' => $request->input('lastname'),
|
||
'customer_address' => $request->input('customer_address'),
|
||
'customer_city' => $request->input('customer_city'),
|
||
'customer_country' => $request->input('customer_country'),
|
||
'customer_state' => $request->input('customer_state'),
|
||
'customer_zip_code' => $request->input('customer_zip_code'),
|
||
'reason' => "Agent - Envoi de cash vers une carte visa"
|
||
], 'http_errors' => false]);
|
||
|
||
$code = $response->getStatusCode();
|
||
$content = json_decode($response->getBody()->getContents(), true);
|
||
|
||
if ($code == 200) {
|
||
$response = $content["response"];
|
||
$transaction->payment_transaction_id = $response["transaction_id"];
|
||
|
||
$banqueCommission = $this->calculateFees($plr_bank_cash_cart_national, $request->montant, $frais);
|
||
$transaction->commission_banque = $banqueCommission;
|
||
// 2---> Emmètre via API sécurisé SSL une requête de débit de notre
|
||
//compte marchand du (Part de la banque partenaire en %
|
||
//pour le dépôtqui s’applique sur les frais minimum) et créditer
|
||
//le compte des opérations défini avec notre banque
|
||
//partenaire
|
||
|
||
$walletAgent->balance_princ -= $transaction->montant;
|
||
|
||
$agentCommission = $this->calculateFees($plr_agent_cash_cart_national, $request->montant, $frais);
|
||
$superviseurCommission = $this->calculateFees($plr_sup_cash_cart_national, $request->montant, $frais);
|
||
$hyperviseurCommission = $this->calculateFees($plr_hyp_cash_cart_national, $request->montant, $frais);
|
||
|
||
$walletAgent->balance_com += $agentCommission;
|
||
$transaction->commission_ag = $agentCommission;
|
||
$walletSuperviseur->balance_com += $superviseurCommission;
|
||
$transaction->commission_sup = $superviseurCommission;
|
||
$walletHyperviseur->balance_com += $hyperviseurCommission;
|
||
$transaction->commission_hyp = $hyperviseurCommission;
|
||
$transaction->id_wallet_ag = $walletAgent->id;
|
||
$transaction->id_wallet_sup = $walletSuperviseur->id;
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->frais = $frais;
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$walletAgent->save();
|
||
$walletSuperviseur->save();
|
||
$walletHyperviseur->save();
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
$transaction->save();
|
||
$response_message = (trans('messages.successful_transaction'));
|
||
|
||
} else {
|
||
throw new Exception($content['error'] ?? __('errors.visa_api_failed'), $content['status'] ?? 500);
|
||
}
|
||
break;
|
||
case 17: // Agent - Envoi de cash vers cash
|
||
$this->validate($request, $transaction->cash_cash_rules());
|
||
$agent = AgentPlus::findOrFail($network_agent->agent_id);
|
||
$this->validatePassword($request->password, $agent->encrypted_password, $agent->salt);
|
||
if ($request->montant > $walletAgent->balance_princ)
|
||
throw new Exception(trans('errors.insufficient_balance'), 422);
|
||
|
||
//Verification des limites reglementaires
|
||
$this->checkReguationsLimits($request->id_document_emetteur, $init_country, $request->final_country, $transaction->montant, true);
|
||
|
||
$frais = ($init_country != $request->final_country) ? $this->calculateFees($plr_agent_cash_cash, $request->montant) : $this->calculateFees($plr_agent_cash_cash_national, $request->montant);
|
||
$taxe = ($init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
|
||
$montantRetrait = $request->montant - $frais - $taxe;
|
||
$transaction->montant_net = $montantRetrait;
|
||
$transaction->montant_net_final_country = $this->toMoneyAmount($transaction->montant_net, $init_country, $request->final_country);
|
||
|
||
$configPayeur = ConfigWallet::where('id_network', $request->network_destinataire)->firstOrFail();
|
||
|
||
$reseauPayeur = PayingNetwork::where('id_network', $request->network_destinataire)->where('id_configWallet', $config->id)->first();
|
||
if (isset($reseauPayeur)) {
|
||
$transaction->part_reseau_payeur = $frais * $reseauPayeur->taux_partage / 100;
|
||
$transaction->part_reseau_payeur_final_country = $this->toMoneyAmount($transaction->part_reseau_payeur, $init_country, $request->final_country);
|
||
$transaction->part_reseau_emetteur = $frais - $transaction->part_reseau_payeur;
|
||
} else {
|
||
$transaction->part_reseau_payeur = 0;
|
||
$transaction->part_reseau_payeur_final_country = 0;
|
||
$transaction->part_reseau_emetteur = $frais;
|
||
}
|
||
|
||
$final_country = $request->final_country;
|
||
$plr_agent_cash_wallet_or_cash_national = $this->getPaliers($paliers_commission_wallets, 'agent_cash_wallet_or_cash_national');
|
||
$plr_agent_cash_wallet_or_cash_international = $this->getPaliers($paliers_commission_wallets, 'agent_cash_wallet_or_cash_international');
|
||
$plr_sup_cash_wallet_or_cash_national = $this->getPaliers($paliers_commission_wallets, 'sup_cash_wallet_or_cash_national');
|
||
$plr_sup_cash_wallet_or_cash_international = $this->getPaliers($paliers_commission_wallets, 'sup_cash_wallet_or_cash_international');
|
||
|
||
$commisionAgent = ($init_country != $final_country) ? $this->calculateFees($plr_agent_cash_wallet_or_cash_international, $request->montant) : $this->calculateFees($plr_agent_cash_wallet_or_cash_national, $request->montant);
|
||
$commisionSuper = ($init_country != $final_country) ? $this->calculateFees($plr_sup_cash_wallet_or_cash_international, $request->montant, $commisionAgent) : $this->calculateFees($plr_sup_cash_wallet_or_cash_national, $request->montant, $commisionAgent);
|
||
$commisionHyper = $transaction->part_reseau_emetteur - $commisionAgent - $commisionSuper;
|
||
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
if ($configPayeur->type == 'ilink') {
|
||
//Mise a jour des comissions et compensation
|
||
if (isset($reseauPayeur)) {
|
||
$reseauPayeur->balance_com += $transaction->part_reseau_payeur_final_country;
|
||
$reseauPayeur->balance_compensation += $transaction->montant_net_final_country;
|
||
}
|
||
|
||
//Hyperviseur payeur
|
||
$hyperviseurPayeur = AgentPlus::where('category', 'hyper')->where('network_id', $request->network_destinataire)->firstOrFail();
|
||
if ($hyperviseurPayeur->id == $hyperviseur->id) { //Si c'est le reseau payeur est aussi emetteur
|
||
$transaction->id_wallet_hyp_payeur = $walletHyperviseur->id;
|
||
} else {
|
||
$wallet_agent_hypPayeur = WalletAgent::where('agent_id', $hyperviseurPayeur->id)->firstOrFail();
|
||
$walletHyperviseurPayeur = Wallet::findOrFail($wallet_agent_hypPayeur->wallet_id);
|
||
$walletHyperviseurPayeur->balance_com += $this->toMoneyAmount($transaction->part_reseau_payeur, $init_country, $request->final_country);
|
||
$transaction->id_wallet_hyp_payeur = $walletHyperviseurPayeur->id;
|
||
$walletHyperviseurPayeur->save();
|
||
}
|
||
} else {
|
||
//Emettre requete SSL vers wallet extene correspondant pour recharger le compte du destinataire
|
||
// La mise à jour des comissions de compensation se fera lors du traitement de la transaction
|
||
$transaction->status_reseau_payeur = 'EN_COURS';
|
||
$result = $this->sendFrame($this->PAYING_NETWORK_SIMULATOR_SEND_FRAME_URL, $transaction);
|
||
if ($result->getStatusCode() != 200)
|
||
throw new Exception(trans('errors.unexpected_error'));
|
||
}
|
||
|
||
|
||
$walletAgent->balance_com += $commisionAgent;
|
||
$transaction->commission_ag = $commisionAgent;
|
||
$walletSuperviseur->balance_com += $commisionSuper;
|
||
$transaction->commission_sup = $commisionSuper;
|
||
$walletHyperviseur->balance_com += $commisionHyper;
|
||
$transaction->commission_hyp = $commisionHyper;
|
||
$transaction->commission_hyp_final_country = $this->toMoneyAmount($transaction->commission_hyp, $init_country, $request->final_country);
|
||
$transaction->id_wallet_ag = $walletAgent->id;
|
||
$transaction->id_wallet_sup = $walletSuperviseur->id;
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
|
||
$code_retrait = $this->random_string();
|
||
$hash = $this->hashSSHA($code_retrait);
|
||
$transaction->encrypted_code_retrait = $hash['encrypted'];
|
||
$transaction->code_retrait_salt = $hash['salt'];
|
||
|
||
$walletAgent->balance_princ -= $transaction->montant;
|
||
$transaction->frais = $frais;
|
||
$transaction->taxe = $taxe;
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$transaction->status_retrait = 0;
|
||
if (isset($reseauPayeur))
|
||
$reseauPayeur->save();
|
||
$walletAgent->save();
|
||
$walletSuperviseur->save();
|
||
$walletHyperviseur->save();
|
||
|
||
$transaction->save();
|
||
$message = trans('messages.successful_agent_send_cash',
|
||
['sender_name' => $request->prenom_emetteur . ' ' . $request->nom_emetteur, 'receiver_name' => $request->prenom_destinataire . ' ' . $request->nom_destinataire,
|
||
'id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country), 'net_init' => $this->toMoney($montantRetrait, $init_country),
|
||
'net_final' => $this->toMoneyWithCurrency($montantRetrait, $init_country, $request->final_country), 'fees' => $this->toMoney($frais + $taxe, $init_country), 'init_country' => $this->getCountryName($init_country),
|
||
'final_country' => $this->getCountryName($request->final_country), 'code' => wordwrap($code_retrait, 4, ' ', true),
|
||
'network' => $this->getNetworkName($transaction->network_destinataire)]);
|
||
$this->sendMail($request->email_emetteur, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail'));
|
||
break;
|
||
|
||
/**
|
||
* @OA\Schema(
|
||
* schema="agent_send_from_cash_to_bank",
|
||
* title="Agent - Envoi de cash vers banque",
|
||
* @OA\Property(property="type",
|
||
* type="integer",
|
||
* enum = {18},
|
||
* default = 18,
|
||
* description="Type de la transaction"
|
||
* ),
|
||
* @OA\Property(property="id_wallet_user",
|
||
* type="integer",
|
||
* example=9,
|
||
* description="ID du wallet de l'agent enregistré dans la base de données"
|
||
* ),
|
||
* @OA\Property(property="id_wallet_network",
|
||
* type="integer",
|
||
* example=101,
|
||
* description="ID du reseau enregistré dans la base de données auquel appartient le wallet"
|
||
* ),
|
||
* @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="nom_emetteur",
|
||
* type="string",
|
||
* description="Noms de l'emetteur"
|
||
* ),
|
||
* @OA\Property(property="prenom_emetteur",
|
||
* type="string",
|
||
* description="Prenoms de l'emetteur"
|
||
* ),
|
||
* @OA\Property(property="email_emetteur",
|
||
* type="string",
|
||
* description="Email de l'emetteur"
|
||
* ),
|
||
* @OA\Property(property="type_document_emetteur",
|
||
* type="string",
|
||
* description="Type de document que l'emetteur a fourni pour son identification"
|
||
* ),
|
||
* @OA\Property(property="id_document_emetteur",
|
||
* type="string",
|
||
* description="ID de l'emetteur sur le document qu'il a fourni pour son identification"
|
||
* ),
|
||
* @OA\Property(property="nom_destinataire",
|
||
* type="string",
|
||
* description="Noms du destinataire"
|
||
* ),
|
||
* @OA\Property(property="prenom_destinataire",
|
||
* type="string",
|
||
* description="Prenoms du destinataire"
|
||
* ),
|
||
* @OA\Property(property="montant",
|
||
* type="number",
|
||
* example = 10000,
|
||
* description="Montant de la transaction"
|
||
* ),
|
||
* @OA\Property(property="password",
|
||
* type="string",
|
||
* description="Mot de passe"
|
||
* )
|
||
* )
|
||
*/
|
||
|
||
case 18: // Agent - Envoi de cash vers banque
|
||
$this->validate($request, [
|
||
'iban' => 'required',
|
||
'id_wallet_network' => 'required|integer|min:0|not_in:0',
|
||
'id_bank' => 'required|integer|min:0|not_in:0',
|
||
'nom_emetteur' => 'required',
|
||
'prenom_emetteur' => 'required',
|
||
'type_document_emetteur' => 'required',
|
||
'id_document_emetteur' => 'required',
|
||
'email_emetteur' => 'required|email:rfc',
|
||
'nom_destinataire' => 'required',
|
||
'prenom_destinataire' => 'required',
|
||
]);
|
||
$agent = AgentPlus::findOrFail($network_agent->agent_id);
|
||
$this->validatePassword($request->password, $agent->encrypted_password, $agent->salt);
|
||
|
||
if ($request->montant > $walletAgent->balance_princ)
|
||
throw new Exception(trans('errors.insufficient_balance'), 422);
|
||
|
||
//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)
|
||
throw new Exception(trans('errors.bank_not_associated_with_network'));
|
||
|
||
if ($network_bank->operators_country->operator->type != 'bank')
|
||
throw new Exception(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:
|
||
throw new Exception(trans('errors.invalid_iban'));
|
||
case 1:
|
||
throw new Exception(trans('errors.country_not_match_iban'));
|
||
case 2:
|
||
throw new Exception(trans('errors.bank_not_match_iban'));
|
||
}
|
||
|
||
$transaction->frais = $frais = 0;
|
||
$transaction->taxe = $taxe = 0;
|
||
// $walletUser->balance -= $transaction->montant;
|
||
//Emettre une trame SSL pour recharger le compte bancaire du montant de la transaction
|
||
$transaction->commission_banque = 0;
|
||
|
||
$transaction->commission_hyp = 0;
|
||
$transaction->commission_sup = 0;
|
||
$transaction->commission_ag = 0;
|
||
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->id_wallet_ag = $walletAgent->id;
|
||
$transaction->id_wallet_sup = $walletSuperviseur->id;
|
||
$transaction->frais = $frais;
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$transaction->bank = $network_bank->operators_country->operator->nom;
|
||
$transaction->country = $network_bank->network->country->name;
|
||
$transaction->id_bank = $request->id_bank;
|
||
$transaction->iban = $request->iban;
|
||
// $walletUser->save();
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
// $transaction->save();
|
||
|
||
Log::info('-------------------------- Agent - Envoi de cash vers banque ------------------------------------');
|
||
Log::info(json_encode($transaction));
|
||
Log::info('------------------------------------------------------------------------------------------------');
|
||
|
||
$message = trans('messages.successful_agent_send_from_cash_to_bank',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country), 'receiver_name' => $request->prenom_destinataire . ' ' . $request->nom_destinataire,
|
||
'net' => $this->toMoney($transaction->montant, $init_country), 'iban' => $request->iban, 'fees' => $this->toMoney($frais, $init_country), 'sender_name' => $request->prenom_emetteur . ' ' . $request->nom_emetteur,
|
||
'sender_code' => $codeGenerer->code_membre, 'bank' => $transaction->bank, 'country' => $transaction->country]);
|
||
$this->sendMail($request->email_emetteur, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail'));
|
||
break;
|
||
|
||
/**
|
||
* @OA\Schema(
|
||
* schema="user_payement_of_operator",
|
||
* title = "User - Payer une facture chez un operateur",
|
||
* @OA\Property(property="type",
|
||
* type="integer",
|
||
* enum = {19},
|
||
* default = 19,
|
||
* description="Type de la transaction"
|
||
* ),
|
||
* @OA\Property(property="id_wallet_user",
|
||
* type="integer",
|
||
* example=9,
|
||
* description="ID du wallet de l'utilisateur enregistré dans la base de données"
|
||
* ),
|
||
* @OA\Property(property="id_wallet_network",
|
||
* type="integer",
|
||
* example=101,
|
||
* description="ID du reseau enregistré dans la base de données auquel appartient le wallet"
|
||
* ),
|
||
* @OA\Property(property="no_facture",
|
||
* type="string",
|
||
* description="Numero de la facture"
|
||
* ),
|
||
* @OA\Property(property="type_operator",
|
||
* type="string",
|
||
* enum={"electricity", "phone" , "tv" ,"school" , "water"},
|
||
* default="electricity",
|
||
* description="Type d'operateur"
|
||
* ),
|
||
* @OA\Property(property="id_operator",
|
||
* type="integer",
|
||
* example = 16,
|
||
* description="ID de l'operateur"
|
||
* ),
|
||
* @OA\Property(property="montant",
|
||
* type="number",
|
||
* example = 10000,
|
||
* description="Montant de la transaction"
|
||
* ),
|
||
* @OA\Property(property="password",
|
||
* type="string",
|
||
* description="Mot de passe"
|
||
* )
|
||
* )
|
||
*/
|
||
case 19: // User - Payer un operateur
|
||
$this->validate($request, [
|
||
'id_operator' => 'required|integer|min:0|not_in:0',
|
||
'id_wallet_network' => 'required|integer|min:0|not_in:0',
|
||
'type_operator' => 'required|in:water,electricity,tv,school',
|
||
'no_facture' => 'required',
|
||
]);
|
||
$user = $walletUser->user;
|
||
$this->validatePassword($request->password, $user->encrypted_password, $user->salt);
|
||
|
||
if ($request->montant > $walletUser->balance)
|
||
throw new Exception(trans('errors.insufficient_balance'));
|
||
|
||
//Verifier si l'operateur est associée au reseau
|
||
$network_bank = NetworksOperator::where('id_network', $request->id_wallet_network)->where('id_operator_country', $request->id_operator)->first();
|
||
if (!$network_bank)
|
||
throw new Exception(trans('errors.operator_not_associated_with_network'));
|
||
|
||
if ($network_bank->operators_country->operator->type != $request->type_operator) {
|
||
$type_operator = TypeOperator::where('code', $request->type_operator)->firstOrFail();
|
||
$type = app()->isLocale('en') ? $type_operator->description_en : $type_operator->description_fr;
|
||
throw new Exception(trans('errors.not_matching_operator', ['type_operator' => $type]));
|
||
}
|
||
|
||
|
||
$transaction->frais = $frais = 0;
|
||
$transaction->taxe = $taxe = 0;
|
||
// $walletUser->balance -= $transaction->montant;
|
||
//Emettre une trame SSL pour recharger l'api de l'operateur du montant de la transaction
|
||
|
||
$transaction->commission_hyp = 0;
|
||
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->frais = $frais;
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$transaction->operator = $network_bank->operators_country->operator->nom;
|
||
$type_operator = $network_bank->operators_country->operator->type_operator;
|
||
$transaction->type_operator = app()->isLocale('en') ? $type_operator->description_en : $type_operator->description_fr;
|
||
$transaction->country = $network_bank->network->country->name;
|
||
$transaction->id_operator = $request->id_operator;
|
||
$transaction->no_facture = $request->no_facture;
|
||
// $walletUser->save();
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
// $transaction->save();
|
||
|
||
Log::info('-------------------------- User - Payer une facture chez un operateur ------------------------------------');
|
||
Log::info(json_encode($transaction));
|
||
Log::info('------------------------------------------------------------------------------------------------');
|
||
|
||
$message = trans('messages.successful_user_payment_of_operator',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country),
|
||
'net' => $this->toMoney($transaction->montant, $init_country), 'no_facture' => $request->no_facture, 'fees' => $this->toMoney($frais, $init_country),
|
||
'sender_code' => $user->user_code, 'operator' => $transaction->operator, 'type_operator' => $transaction->type_operator]);
|
||
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail'));
|
||
break;
|
||
|
||
/**
|
||
* @OA\Schema(
|
||
* schema="user_remove_from_bank_to_wallet",
|
||
* title="User - Retrait de la banque vers le wallet",
|
||
* @OA\Property(property="type",
|
||
* type="integer",
|
||
* enum = {20},
|
||
* default = 20,
|
||
* description="Type de la transaction"
|
||
* ),
|
||
* @OA\Property(property="id_wallet_user",
|
||
* type="integer",
|
||
* example=9,
|
||
* description="ID du wallet de l'utilisateur enregistré dans la base de données"
|
||
* ),
|
||
* @OA\Property(property="montant",
|
||
* type="number",
|
||
* example = 10000,
|
||
* description="Montant de la transaction"
|
||
* ),
|
||
* @OA\Property(property="password",
|
||
* type="string",
|
||
* description="Mot de passe"
|
||
* )
|
||
* )
|
||
*/
|
||
case 20: //User - Retrait de la banque vers le wallet
|
||
|
||
// $this->validate($request, [
|
||
// 'code_retrait' => 'required',
|
||
// ]);
|
||
|
||
$user = $walletUser->user;
|
||
$this->validatePassword($request->password, $user->encrypted_password, $user->salt);
|
||
|
||
$this->checkUserIdentification($user->id);
|
||
|
||
if (!(isset($user->iban) && isset($user->id_bank_country)))
|
||
throw new Exception(trans('errors.wallet_not_linked_to_bank_account'));
|
||
|
||
//Reverifier si l'operateur est toujours associé au reseau
|
||
$network_bank = NetworksOperator::where('id_network', $transaction->network_emetteur)->where('id_operator_country', $user->id_bank_country)->first();
|
||
if (!$network_bank)
|
||
throw new Exception(trans('errors.operator_not_associated_with_network') . '. ' . trans('errors.update_banking_information'));
|
||
|
||
if ($network_bank->operators_country->operator->type != 'bank')
|
||
throw new Exception(trans('errors.not_banking_operator') . '. ' . trans('errors.update_banking_information'));
|
||
|
||
//Reverifier le code IBAN correspond toujours
|
||
$country_code = $network_bank->network->country->code_country;
|
||
$bank_code = $network_bank->operators_country->code;
|
||
switch ($this->checkIBAN($user->iban, $country_code, $bank_code)) {
|
||
case 0:
|
||
throw new Exception(trans('errors.invalid_iban') . '. ' . trans('errors.update_banking_information'));
|
||
case 1:
|
||
throw new Exception(trans('errors.country_not_match_iban') . '. ' . trans('errors.update_banking_information'));
|
||
case 2:
|
||
throw new Exception(trans('errors.bank_not_match_iban') . '. ' . trans('errors.update_banking_information'));
|
||
}
|
||
|
||
// Envoyer la trame vers banque pour debiter le compte client et recharger le compte bancaire de iLink
|
||
// ----->
|
||
|
||
$transaction->frais = $frais = 0;
|
||
$transaction->taxe = $taxe = 0;
|
||
// $walletUser->balance += $transaction->montant;
|
||
$transaction->commission_banque = 0;
|
||
|
||
$transaction->commission_hyp = 0;
|
||
|
||
$transaction->id_wallet_hyp = $walletHyperviseur->id;
|
||
$transaction->frais = $frais;
|
||
$transaction->date = $this->getCurrentTime($init_country);
|
||
$transaction->bank = $user->bank->operators_country->operator->nom;
|
||
$transaction->country = $user->bank->network->country->name;
|
||
$transaction->id_bank = $user->id_bank_country;
|
||
$transaction->iban = $user->iban;
|
||
// $walletUser->save();
|
||
$transaction->id_transaction = $this->getTransactionID();
|
||
// $transaction->save();
|
||
|
||
Log::info('-------------------------- User - Retrait de la banque vers le wallet ------------------------------------');
|
||
Log::info(json_encode($transaction));
|
||
Log::info('------------------------------------------------------------------------------------------------');
|
||
|
||
$message = trans('messages.successful_user_remove_from_bank_to_wallet',
|
||
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country),
|
||
'net' => $this->toMoney($transaction->montant, $init_country), 'iban' => $user->iban, 'fees' => $this->toMoney($frais, $init_country),
|
||
'sender_code' => $user->user_code, 'bank' => $transaction->bank, 'country' => $transaction->country]);
|
||
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
|
||
$response_message = ($message . trans('messages.sent_by_mail'));
|
||
break;
|
||
default:
|
||
$response_message = "Default response message";
|
||
|
||
}
|
||
|
||
DB::commit();
|
||
return $this->successResponse($response_message);
|
||
} catch (Throwable $e) {
|
||
DB::rollBack();
|
||
if ($e instanceof ValidationException) {
|
||
throw $e;
|
||
}
|
||
Log::error($e->getMessage() . "\n" . $e->getTraceAsString());
|
||
return $this->errorResponse($e->getMessage(), $e->getCode());
|
||
}
|
||
}
|
||
|
||
public function lastUserTransactions($id_user)
|
||
{
|
||
$user = User::findOrFail($id_user);
|
||
$wallet_user = WalletsUser::where('idUser', $user->id)->firstOrFail();
|
||
|
||
$transactions = DB::select('SELECT id_wallet_user , operation_fr , operation_en, id_transaction ,network_destinataire, init_country , final_country, date , id , montant , frais , taxe , pays_init , pays_final,
|
||
destinataire_phone , destinataire_name , nom_destinataire, prenom_destinataire, montant_net ,montant_net_final_country FROM infos_ilink_transaction WHERE id_wallet_user = :id_wallet AND type <> 12 AND type <> 14
|
||
ORDER BY date DESC LIMIT 10;', ['id_wallet' => $wallet_user->id]);
|
||
|
||
foreach ($transactions as $data) {
|
||
$data->operation = app()->isLocale('en') ? $data->operation_en : $data->operation_fr;
|
||
$date = $data->date;
|
||
unset($data->date);
|
||
|
||
$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;
|
||
$emetteur = 'Moi';
|
||
|
||
if ($data->destinataire_phone) {
|
||
$destinataire = $data->destinataire_name;
|
||
} else {
|
||
$destinataire = $data->prenom_destinataire ? $data->prenom_destinataire . ' ' . $data->nom_destinataire : $emetteur;
|
||
}
|
||
// $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->montant = $this->toMoney($data->montant, $data->init_country);
|
||
$data->init_country = $data->pays_init;
|
||
$data->final_country = $data->pays_final;
|
||
$data->reseau_payeur = isset($data->network_destinataire) ? $this->getNetworkName($data->network_destinataire) . ' ' . $data->final_country : null;
|
||
$data->date = $date;
|
||
unset($data->type, $data->destinataire_name, $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->pays_init, $data->pays_final, $data->id_destinataire, $data->destinataire_phone, $data->operation_fr, $data->operation_en);
|
||
}
|
||
return $this->successResponse($transactions);
|
||
}
|
||
|
||
public function lastAgentTransactions($id_wallet_agent)
|
||
{
|
||
$transactions = DB::select('SELECT 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 , 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_ag = :id_wallet
|
||
ORDER BY wit.date DESC LIMIT 10;', ['id_wallet' => $id_wallet_agent]);
|
||
|
||
foreach ($transactions as $data) {
|
||
$data->operation = app()->isLocale('en') ? $data->operation_en : $data->operation_fr;
|
||
$date = $data->date;
|
||
unset($data->date);
|
||
$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)->orWhere('phone', $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->montant = $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;
|
||
$data->date = $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 $this->successResponse($transactions);
|
||
}
|
||
|
||
public function calculateCommission(Request $request)
|
||
{
|
||
$rules = [
|
||
'type' => 'required|integer|min:0|not_in:0',
|
||
'id_wallet_agent' => 'required_without:id_wallet_user|integer|min:0|not_in:0',
|
||
'id_wallet_user' => 'required_without:id_wallet_agent|integer|min:0|not_in:0',
|
||
'montant' => 'required|numeric|min:0|not_in:0',
|
||
];
|
||
$this->validate($request, $rules);
|
||
if (isset($request->id_wallet_agent)) {
|
||
$walletAgent = Wallet::findOrFail($request->get('id_wallet_agent'));
|
||
$network_agent = NetworksAgent::findOrFail($walletAgent->id_networkAgent);
|
||
// Configuratrion du wallet
|
||
$config = ConfigWallet::where('id_network', $network_agent->network_id)->firstOrFail();
|
||
$init_country = $network_agent->network->country->id;
|
||
|
||
|
||
} else {
|
||
$walletUser = WalletsUser::findOrFail($request->id_wallet_user);
|
||
$init_country = $walletUser->user->network->country->id;
|
||
$result = ConfigWallet::join('networks', 'networks.id', '=', 'configWallet.id_network')
|
||
->where('networks.country_id', $init_country)->where('configWallet.type', 'ilink')
|
||
->select('configWallet.id')->first();
|
||
if ($result) {
|
||
$config = ConfigWallet::findOrFail($result->id);
|
||
} else {
|
||
return $this->errorResponse(trans('errors.no_ilink_network'));
|
||
}
|
||
}
|
||
|
||
$taxesNationales = array_values(array_filter($config->taxes->all(), function ($tax) {
|
||
return $tax->destination == 'national' && $tax->categorie == 'wallet';
|
||
}));
|
||
|
||
$taxesInternationales = array_values(array_filter($config->taxes->all(), function ($tax) {
|
||
return $tax->destination == 'international' && $tax->categorie == 'wallet';
|
||
}));
|
||
|
||
$paliers_config_wallets = $config->paliers_config_wallets->all();
|
||
$paliers_commission_wallets = $config->paliers_commissions_wallets->all();
|
||
|
||
$plr_user_wallet_wallet = $this->getPaliers($paliers_config_wallets, "user_wallet_wallet_international");
|
||
$plr_user_wallet_cash = $this->getPaliers($paliers_config_wallets, "user_wallet_cash_international");
|
||
$plr_agent_depot_wallet_ilink = $this->getPaliers($paliers_config_wallets, "agent_depot_wallet_ilink_international");
|
||
$plr_agent_depot_autre_wallet = $this->getPaliers($paliers_config_wallets, "agent_depot_autre_wallet_international");
|
||
$plr_agent_cash_cash = $this->getPaliers($paliers_config_wallets, "agent_cash_cash_international");
|
||
|
||
$plr_user_wallet_wallet_national = $this->getPaliers($paliers_config_wallets, "user_wallet_wallet_national");
|
||
$plr_user_wallet_cash_national = $this->getPaliers($paliers_config_wallets, "user_wallet_cash_national");
|
||
$plr_agent_depot_wallet_ilink_national = $this->getPaliers($paliers_config_wallets, "agent_depot_wallet_ilink_national");
|
||
$plr_agent_depot_autre_wallet_national = $this->getPaliers($paliers_config_wallets, "agent_depot_autre_wallet_national");
|
||
$plr_agent_cash_cash_national = $this->getPaliers($paliers_config_wallets, "agent_cash_cash_national");
|
||
switch ($request->type) {
|
||
case 1: //User - Envoi wallet à wallet
|
||
$this->validate($request, [
|
||
'final_country' => 'required|integer|exists:countries,id',
|
||
'id_destinataire' => 'required_without:phone_destinataire',
|
||
'network_destinataire' => 'required|integer|min:0|not_in:0',
|
||
]);
|
||
$configNetworkDestinataire = ConfigWallet::where('id_network', $request->network_destinataire)->firstOrFail();
|
||
$frais = ($init_country != $request->final_country) ? $this->calculateFees($plr_user_wallet_wallet, $request->montant) : $this->calculateFees($plr_user_wallet_wallet_national, $request->montant);
|
||
$taxe = ($init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
|
||
//Verifier si c'est pas un reseau ilink
|
||
if ($configNetworkDestinataire->type != 'ilink') {
|
||
$data['destinataire'] = $request->id_destinataire;
|
||
} else {
|
||
$destinataire = User::where('user_code', $request->id_destinataire)->orWhere('phone', $request->id_destinataire)->first();
|
||
$data['destinataire'] = $destinataire ? $destinataire->lastname . ' ' . $destinataire->firstname : $request->id_destinataire;
|
||
}
|
||
$data['frais'] = round($frais + $taxe, 2);
|
||
$data['montant_net_init'] = round($request->montant - $data['frais'], 2);
|
||
$data['montant_net_final'] = $this->toMoneyWithCurrency($data['montant_net_init'], $init_country, $request->final_country);
|
||
break;
|
||
case 2: //User - Envoi de wallet à carte
|
||
$plr_user_wallet_cart_national = $this->getPaliers($paliers_commission_wallets, 'user_wallet_cart_national');
|
||
// $plr_user_wallet_cart_international = $this->getPaliers($paliers_commission_wallets, 'user_wallet_cart_international');
|
||
$frais = $this->calculateFees($plr_user_wallet_cart_national, $request->montant);
|
||
$data['frais'] = round($frais, 2);
|
||
$data['montant_net'] = round($request->montant - $data['frais'], 2);
|
||
break;
|
||
case 3: // User - Envoi wallet à cash
|
||
$this->validate($request, [
|
||
'final_country' => 'required|integer|min:0|not_in:0',
|
||
]);
|
||
$frais = ($init_country != $request->final_country) ? $this->calculateFees($plr_user_wallet_cash, $request->montant) : $this->calculateFees($plr_user_wallet_cash_national, $request->montant);
|
||
$taxe = ($init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
|
||
$data['frais'] = round($frais + $taxe, 2);
|
||
$data['montant_net_init'] = round($request->montant - $data['frais'], 2);
|
||
$data['montant_net_final'] = $this->toMoneyWithCurrency($data['montant_net_init'], $init_country, $request->final_country);
|
||
break;
|
||
case 9: // User - Retrait de wallet en cash
|
||
$frais = $this->calculateFees($plr_user_wallet_cash_national, $request->montant);
|
||
$taxe = $this->calculateTax($taxesNationales, $frais);
|
||
$data['frais'] = round($frais + $taxe, 2);
|
||
$data['montant_net'] = round($request->montant - $data['frais'], 2);
|
||
break;
|
||
case 10: //User - Retrait de carte vers wallet
|
||
$plr_user_cart_wallet_national = $this->getPaliers($paliers_commission_wallets, 'user_cart_wallet_national');
|
||
// $plr_user_cart_wallet_international = $this->getPaliers($paliers_commission_wallets, 'user_cart_wallet_international');
|
||
$frais = $this->calculateFees($plr_user_cart_wallet_national, $request->montant);
|
||
$data['frais'] = round($frais, 2);
|
||
$data['montant_net'] = round($request->montant + $data['frais'], 2);
|
||
break;
|
||
case 11: // User - Retrait de carte vers cash
|
||
$plr_user_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'user_cart_cash_national');
|
||
// $plr_user_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'user_cart_cash_international');
|
||
$frais = $this->calculateFees($plr_user_cart_cash_national, $request->montant);
|
||
$data['frais'] = round($frais, 2);
|
||
$data['montant_net'] = round($request->montant + $data['frais'], 2);
|
||
break;
|
||
case 13: // Agent - Retrait de la carte vers cash
|
||
$plr_customer_cart_cash_national = $this->getPaliers($paliers_commission_wallets, 'customer_cart_cash_national');
|
||
// $plr_customer_cart_cash_international = $this->getPaliers($paliers_commission_wallets, 'customer_cart_cash_international');
|
||
$frais = $this->calculateFees($plr_customer_cart_cash_national, $request->montant);
|
||
$data['frais'] = round($frais, 2);
|
||
$data['montant_net'] = round($request->montant + $data['frais'], 2);
|
||
break;
|
||
case 14: // Agent - Envoi de cash vers wallet iLink
|
||
$this->validate($request, [
|
||
'user_code' => 'required',
|
||
]);
|
||
$user = User::where('user_code', $request->user_code)->first();
|
||
if (!$user)
|
||
return $this->errorResponse(trans('errors.wallet_not_defined'));
|
||
$final_country = $user->network->country->id;
|
||
$frais = ($init_country != $final_country) ? $this->calculateFees($plr_agent_depot_wallet_ilink, $request->montant) : $this->calculateFees($plr_agent_depot_wallet_ilink_national, $request->montant);
|
||
$taxe = ($init_country != $final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
|
||
$data['frais'] = round($frais + $taxe, 2);
|
||
$data['montant_net_init'] = round($request->montant - $data['frais'], 2);
|
||
$data['montant_net_final'] = $this->toMoneyWithCurrency($data['montant_net_init'], $init_country, $final_country);
|
||
break;
|
||
case 15: // Agent - Envoi de cash vers autre wallet
|
||
$this->validate($request, [
|
||
'final_country' => 'required|integer|exists:countries,id',
|
||
]);
|
||
$frais = ($init_country != $request->final_country) ? $this->calculateFees($plr_agent_depot_autre_wallet, $request->montant) : $this->calculateFees($plr_agent_depot_autre_wallet_national, $request->montant);
|
||
$taxe = ($init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
|
||
$data['frais'] = round($frais + $taxe, 2);
|
||
$data['montant_net_init'] = round($request->montant - $data['frais'], 2);
|
||
$data['montant_net_final'] = $this->toMoneyWithCurrency($data['montant_net_init'], $init_country, $request->final_country);
|
||
break;
|
||
case 16: // Agent - Envoi de cash vers une carte visa
|
||
$plr_customer_cash_cart_national = $this->getPaliers($paliers_commission_wallets, 'customer_cash_cart_national');
|
||
// $plr_customer_cash_cart_international = $this->getPaliers($paliers_commission_wallets, 'customer_cash_cart_international');
|
||
$frais = $this->calculateFees($plr_customer_cash_cart_national, $request->montant);
|
||
$data['frais'] = round($frais, 2);
|
||
$data['montant_net'] = round($request->montant - $data['frais'], 2);
|
||
break;
|
||
case 17: // Agent - Envoi de cash vers cash
|
||
$this->validate($request, [
|
||
'final_country' => 'required|integer|exists:countries,id',
|
||
]);
|
||
$frais = ($init_country != $request->final_country) ? $this->calculateFees($plr_agent_cash_cash, $request->montant) : $this->calculateFees($plr_agent_cash_cash_national, $request->montant);
|
||
$taxe = ($init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
|
||
$data['frais'] = round($frais + $taxe, 2);
|
||
$data['montant_net_init'] = round($request->montant - $data['frais'], 2);
|
||
$data['montant_net_final'] = $this->toMoneyWithCurrency($data['montant_net_init'], $init_country, $request->final_country);
|
||
break;
|
||
}
|
||
$net = $data['montant_net'] ?? $data['montant_net_init'];
|
||
if (isset($net))
|
||
if ($net < 0)
|
||
return $this->errorResponse(trans('errors.incorrect_net_amount'));
|
||
return $this->successResponse($data);
|
||
}
|
||
|
||
public function getTransactionRetrait(Request $request)
|
||
{
|
||
$this->validate($request, [
|
||
'id_transaction' => 'required',
|
||
'id_wallet_agent' => 'required|integer|min:0|not_in:0',
|
||
'code_retrait' => 'required',
|
||
]);
|
||
|
||
$transaction = WalletIlinkTransaction::select('nom_destinataire', 'prenom_destinataire', 'type_document_destinataire', 'id_document_destinataire', 'type', 'encrypted_code_retrait', 'code_retrait_salt',
|
||
'id_wallet_user', 'init_country', 'final_country', 'network_destinataire', 'id_transaction', 'montant_net_final_country', 'montant_net')->where('id_transaction', $request->id_transaction)->first();
|
||
|
||
if (!$transaction)
|
||
return $this->errorResponse(trans('errors.transaction_not_exist'), Response::HTTP_NOT_FOUND);
|
||
|
||
if (!$this->checkPassword($request->code_retrait, $transaction->encrypted_code_retrait, $transaction->code_retrait_salt))
|
||
return $this->errorResponse(trans('errors.invalid_withdrawal_code'));
|
||
|
||
|
||
//Verifier que le reseau payeur est de type iLink
|
||
if (in_array($transaction->type, [3, 17])) {
|
||
$configPayeur = ConfigWallet::where('id_network', $transaction->network_destinataire)->firstOrFail();
|
||
if ($configPayeur->type != 'ilink')
|
||
return $this->errorResponse(trans('errors.withdrawal_network_unauthorized', ['network' => $this->getNetworkName($transaction->network_destinataire),
|
||
'country' => $this->getCountryName($transaction->final_country)]));
|
||
}
|
||
|
||
if ($transaction->status_retrait != 0)
|
||
return $this->errorResponse(trans('errors.withdrawal_already_made'));
|
||
|
||
$walletAgent = Wallet::findOrFail($request->get('id_wallet_agent'));
|
||
$network_agent = NetworksAgent::findOrFail($walletAgent->id_networkAgent);
|
||
$agent_country = $network_agent->network->country->id;
|
||
if ($agent_country != $transaction->final_country)
|
||
return $this->errorResponse(trans('errors.operation_cannot_performed_in_country'));
|
||
|
||
$data = $transaction->replicate();
|
||
if (in_array($transaction->type, [9, 11])) {
|
||
$identification = $identification = Identification::where('id_user', $transaction->wallet_user->user->id)->first();
|
||
if (!$identification)
|
||
return $this->errorResponse(trans('errors.user_identification_required'));
|
||
if ($identification->status != 1)
|
||
return $this->errorResponse(trans('errors.validation_user_identification_required'));
|
||
|
||
$data->nom_destinataire = $transaction->wallet_user->user->lastname;
|
||
$data->prenom_destinataire = $transaction->wallet_user->user->firstname;
|
||
$data->type_document_destinataire = $identification->identity_document;
|
||
$data->id_document_destinataire = $identification->id_identity_document;
|
||
// $data->user_code = $transaction->wallet_user->user->user_code;
|
||
}
|
||
$data->montant = $transaction->type == 11 ? $transaction->montant_net : $transaction->montant_net_final_country;
|
||
unset($data->type, $data->init_country, $data->final_country, $data->id_wallet_user, $data->network_destinataire, $data->code_retrait_salt,
|
||
$data->encrypted_code_retrait, $data->montant_net_final_country, $data->montant_net);
|
||
|
||
return $this->successResponse($data);
|
||
}
|
||
|
||
|
||
private function getPaliers(array $paliers, $type)
|
||
{
|
||
return array_values(array_filter($paliers, function ($palier) use ($type) {
|
||
return $palier->type == $type;
|
||
}));
|
||
}
|
||
|
||
//Calcul des frais internationaux
|
||
|
||
/**
|
||
* @throws AppException
|
||
*/
|
||
private function calculateFees(array $paliers, $montant, $frais = null)
|
||
{
|
||
$size = sizeof($paliers);
|
||
$fees = 0;
|
||
if ($size > 0) {
|
||
$min = $paliers[0]->min;
|
||
$max = $paliers[$size - 1]->max;
|
||
$palier = null;
|
||
foreach ($paliers as $p) {
|
||
if ($montant >= $p->min && $montant <= $p->max) {
|
||
$palier = $p;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if ($palier) {
|
||
if (isset($frais)) {
|
||
// Pour le calcul par paliers des commissions uniquement
|
||
$montant = $frais;
|
||
}
|
||
$fees = round(($montant * $palier->taux / 100), 2);
|
||
} else {
|
||
throw new AppException(trans('errors.amount_not_allowed', ['min' => floatval($min), 'max' => floatval($max)]));
|
||
}
|
||
|
||
if (!empty($palier->plafond) && ($fees > $palier->plafond)) {
|
||
$fees = $palier->plafond;
|
||
}
|
||
}
|
||
return $fees;
|
||
}
|
||
|
||
|
||
private function random_string()
|
||
{
|
||
$character_set_array = array();
|
||
$character_set_array[] = array('count' => 12, 'characters' => 'ABCDEFGHJKMNPQRSTUVWXYZ');
|
||
$character_set_array[] = array('count' => 4, 'characters' => '23456789');
|
||
$temp_array = array();
|
||
foreach ($character_set_array as $character_set) {
|
||
for ($i = 0; $i < $character_set['count']; $i++) {
|
||
$temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)];
|
||
}
|
||
}
|
||
shuffle($temp_array);
|
||
return implode('', $temp_array);
|
||
}
|
||
|
||
private function getTransactionID()
|
||
{
|
||
do {
|
||
$code = $this->generateTransactionCode();
|
||
$result = collect(DB::select('SELECT * FROM wallet_ilink_transaction WHERE id_transaction = :code', ['code' => $code]));
|
||
$codeCorrect = sizeof($result) < 0;
|
||
} while ($codeCorrect);
|
||
return $code;
|
||
}
|
||
|
||
|
||
// public function index(Request $request){
|
||
//
|
||
// $notices = DB::select('select notices.id,notices.title,notices.body,notices.created_at,notices.updated_at,
|
||
//users.name,departments.department_name
|
||
//FROM notices
|
||
//INNER JOIN users ON notices.user_id = users.id
|
||
//INNER JOIN departments on users.dpt_id = departments.id
|
||
//ORDER BY users.id DESC');
|
||
//
|
||
// $notices = $this->arrayPaginator($notices, $request);
|
||
//
|
||
// return view('welcome')->with('allNotices', $notices);
|
||
//
|
||
// }
|
||
|
||
public function cancel($id_transaction)
|
||
{
|
||
$transaction = WalletIlinkTransaction::where('id_transaction', $id_transaction)->firstOrFail();
|
||
$transactionInverse = $transaction->replicate();
|
||
$transactionInverse->date = $this->getCurrentTimeByCountryCode($transaction->country->code_country);
|
||
$transactionInverse->montant = -$transaction->montant;
|
||
$transactionInverse->montant_net = -$transaction->montant_net;
|
||
$transactionInverse->montant_net_final_country = -$transaction->montant_net_final_country;
|
||
$transactionInverse->taxe = -$transaction->taxe;
|
||
$transactionInverse->frais = -$transaction->frais;
|
||
$transactionInverse->commission_ag = -$transaction->commission_ag;
|
||
$transactionInverse->commission_sup = -$transaction->commission_sup;
|
||
$transactionInverse->commission_hyp = -$transaction->commission_hyp;
|
||
$transactionInverse->commission_hyp_final_country = -$transaction->commission_hyp_final_country;
|
||
$transactionInverse->commission_banque = -$transaction->commission_banque;
|
||
$transactionInverse->id = null;
|
||
$transactionInverse->id_transaction = $this->getTransactionID();
|
||
|
||
// $walletAgent = $transaction->wallet;
|
||
// $walletSuperviseur = $transaction->wallet_sup;
|
||
// $walletHyperviseur = $transaction->wallet_hyp;
|
||
//
|
||
// if ($transaction->type == 'credit'){
|
||
// //Depot
|
||
// $walletAgent->balance_princ += $transaction->montant;
|
||
// }elseif ($transaction->type == 'debit'){
|
||
// //Retrait
|
||
// $walletAgent->balance_princ -= $transaction->montant;
|
||
// }
|
||
//
|
||
// $walletAgent->balance_com -= $transaction->commission_ag;
|
||
// $walletSuperviseur->balance_com -= $transaction->commission_sup;
|
||
// $walletHyperviseur->balance_com -= $transaction->commission_hyp;
|
||
$transaction->canceled = 1;
|
||
|
||
// $walletAgent->save();
|
||
// $walletSuperviseur->save();
|
||
// $walletHyperviseur->save();
|
||
$transaction->save();
|
||
$transactionInverse->save();
|
||
return $this->successResponse(trans('messages.canceled_transaction'));
|
||
}
|
||
|
||
//Verfier les limites reglementaires
|
||
|
||
/**
|
||
* @throws Exception
|
||
*/
|
||
public function checkReguationsLimits($identifiant, $init_country, $final_country, $montant_transaction, $is_id_document_emetteur = false)
|
||
{
|
||
|
||
$regulation = Regulation::where('id_country', $init_country)->first();
|
||
|
||
if (!$regulation)
|
||
return;
|
||
|
||
// Total montants journalier
|
||
if ($is_id_document_emetteur)
|
||
$daily_sum = WalletIlinkTransaction::where('id_document_emetteur', $identifiant)->where('final_country', $final_country)->whereDate('date', Carbon::today())->sum('montant');
|
||
else
|
||
$daily_sum = WalletIlinkTransaction::where('id_wallet_user', $identifiant)->where('final_country', $final_country)->whereDate('date', Carbon::today())->sum('montant');
|
||
$max_jour = ($init_country == $final_country) ? $regulation->montant_max_jour_national : $regulation->montant_max_jour_international;
|
||
$amount_admitted = $max_jour - $daily_sum;
|
||
if (($daily_sum + $montant_transaction) > $max_jour)
|
||
throw new Exception(($init_country == $final_country) ? trans('errors.national_daily_regulations_limits_reached') : trans('errors.international_daily_regulations_limits_reached') . ' '
|
||
. ($amount_admitted > 0 ? trans('errors.regulations_limits_amount_transaction', ['amount' => $this->toMoney($amount_admitted, $init_country)]) : ''), 500);
|
||
|
||
// Total montants hebdomadaire
|
||
if ($is_id_document_emetteur)
|
||
$weekly_sum = WalletIlinkTransaction::where('id_document_emetteur', $identifiant)->where('final_country', $final_country)->whereBetween('date', [Carbon::today()->subDay(7), Carbon::today()])->sum('montant');
|
||
else
|
||
$weekly_sum = WalletIlinkTransaction::where('id_wallet_user', $identifiant)->where('final_country', $final_country)->whereBetween('date', [Carbon::today()->subDay(7), Carbon::today()])->sum('montant');
|
||
$max_hebdo = ($init_country == $final_country) ? $regulation->montant_max_hebdo_national : $regulation->montant_max_hebdo_international;
|
||
$amount_admitted = $max_hebdo - $weekly_sum;
|
||
if (($weekly_sum + $montant_transaction) > $max_hebdo)
|
||
throw new Exception(($init_country == $final_country) ? trans('errors.national_weekly_regulations_limits_reached') : trans('errors.international_weekly_regulations_limits_reached') . ' '
|
||
. ($amount_admitted > 0 ? trans('errors.regulations_limits_amount_transaction', ['amount' => $this->toMoney($amount_admitted, $init_country)]) : ''), 500);
|
||
|
||
|
||
// Total montants mensuel
|
||
if ($is_id_document_emetteur)
|
||
$monthly_sum = WalletIlinkTransaction::where('id_document_emetteur', $identifiant)->where('final_country', $final_country)->whereBetween('date', [Carbon::today()->subDay(30), Carbon::today()])->sum('montant');
|
||
else
|
||
$monthly_sum = WalletIlinkTransaction::where('id_wallet_user', $identifiant)->where('final_country', $final_country)->whereBetween('date', [Carbon::today()->subDay(30), Carbon::today()])->sum('montant');
|
||
$max_mensuel = ($init_country == $final_country) ? $regulation->montant_max_mensuel_national : $regulation->montant_max_mensuel_international;
|
||
$amount_admitted = $max_mensuel - $monthly_sum;
|
||
if (($monthly_sum + $montant_transaction) > $max_mensuel)
|
||
throw new Exception(($init_country == $final_country) ? trans('errors.national_monthly_regulations_limits_reached') : trans('errors.international_monthly_regulations_limits_reached') . ' '
|
||
. ($amount_admitted > 0 ? trans('errors.regulations_limits_amount_transaction', ['amount' => $this->toMoney($amount_admitted, $init_country)]) : ''), 500);
|
||
|
||
}
|
||
|
||
// Recevoir le traitement d'une transaction venant d'un reseau payeur
|
||
public function receiveRequestProcessingResult(Request $request)
|
||
{
|
||
$this->validate($request, [
|
||
'id_transaction' => 'required',
|
||
'status' => 'required|in:NON_TRAITEE,TRAITEE'
|
||
]);
|
||
$transaction = WalletIlinkTransaction::where('id_transaction', $request->id_transaction)->firstOrFail();
|
||
if (!in_array($transaction->type, [1, 3, 17]))
|
||
return $this->errorResponse(trans('errors.forbidden'));
|
||
|
||
if ($transaction->status_reseau_payeur != 'EN_COURS')
|
||
return $this->errorResponse(trans('errors.request_already_processed'));
|
||
// modifier l'etat et mettre les comptes comissions et compensatios du reseau payeur
|
||
|
||
$transaction->status_reseau_payeur = $request->status;
|
||
if ($request->status == 'TRAITEE') {
|
||
//Mise a jour des comissions et compensation
|
||
$config = ConfigWallet::where('id_network', $transaction->network_emetteur)->firstOrFail();
|
||
$reseauPayeur = PayingNetwork::where('id_network', $transaction->network_destinataire)->where('id_configWallet', $config->id)->firstOrFail();
|
||
|
||
$reseauPayeur->balance_com += $transaction->part_reseau_payeur_final_country;
|
||
$reseauPayeur->balance_compensation += $transaction->montant_net_final_country;
|
||
$reseauPayeur->save();
|
||
}
|
||
$transaction->save();
|
||
return $this->successResponse(trans('messages.success_treated_demand'));
|
||
|
||
}
|
||
|
||
}
|