walletservice/app/Http/Controllers/iLinkTransactionController.php

774 lines
48 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Models\AgentPlus;
use App\Models\CodeGenerer;
use App\Models\ConfigWallet;
2020-06-24 07:36:54 +00:00
use App\Models\Country;
use App\Models\NetworksAgent;
2020-06-21 21:49:24 +00:00
use App\Models\PayingNetwork;
use App\Models\TypeIlinkTransaction;
use App\Models\PaliersConfigWallet;
use App\Models\User;
use App\Models\Wallet;
use App\Models\WalletAgent;
use App\Models\WalletIlinkTransaction;
use App\Models\WalletsUser;
use App\Models\WalletTransaction;
use App\Traits\ApiResponser;
use App\Traits\Helper;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use function GuzzleHttp\Promise\all;
class iLinkTransactionController extends Controller
{
use ApiResponser;
use Helper;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
public function add(Request $request)
{
$transaction = new WalletIlinkTransaction();
$this->validate($request, [
'type' => 'required|integer|min:0|not_in:0',
2020-06-21 21:49:24 +00:00
'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',
'password' => 'required',
'montant' => 'required|numeric|min:0|not_in:0',
]);
$type = TypeIlinkTransaction::findOrFail($request->type);
2020-06-21 21:49:24 +00:00
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();
// 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);
$walletHyperviseur = Wallet::findOrFail($wallet_agent_hyp->wallet_id);
} elseif (isset($request->id_wallet_user)) {
2020-06-24 07:36:54 +00:00
$walletUser = WalletsUser::findOrFail($request->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'));
}
2020-06-21 21:49:24 +00:00
2020-06-24 07:36:54 +00:00
$hyperviseur = AgentPlus::where('category', 'hyper')->where('network_id', $config->id_network)->firstOrFail();
2020-06-21 21:49:24 +00:00
$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';
}));
$taxesInternationales = array_values(array_filter($config->taxes->all(), function ($tax) {
return $tax->destination == 'international';
}));
$plr_user_wallet_wallet = $this->getPaliers($config->paliers_config_wallets->all(), "user_wallet_wallet_international");
$plr_user_wallet_cash = $this->getPaliers($config->paliers_config_wallets->all(), "user_wallet_cash_international");
$plr_agent_depot_wallet_ilink = $this->getPaliers($config->paliers_config_wallets->all(), "agent_depot_wallet_ilink_international");
$plr_agent_depot_autre_wallet = $this->getPaliers($config->paliers_config_wallets->all(), "agent_depot_autre_wallet_international");
$plr_agent_cash_cash = $this->getPaliers($config->paliers_config_wallets->all(), "agent_cash_cash_international");
$plr_user_wallet_wallet_national = $this->getPaliers($config->paliers_config_wallets->all(), "user_wallet_wallet_national");
$plr_user_wallet_cash_national = $this->getPaliers($config->paliers_config_wallets->all(), "user_wallet_cash_national");
$plr_agent_depot_wallet_ilink_national = $this->getPaliers($config->paliers_config_wallets->all(), "agent_depot_wallet_ilink_national");
$plr_agent_depot_autre_wallet_national = $this->getPaliers($config->paliers_config_wallets->all(), "agent_depot_autre_wallet_national");
$plr_agent_cash_cash_national = $this->getPaliers($config->paliers_config_wallets->all(), "agent_cash_cash_national");
$data = $request->all();
if (isset($request->cvv) && !is_numeric($request->cvv))
return $this->errorResponse('errors.invalid_cvv');
$transaction->fill($data);
// Client API
$client = new \GuzzleHttp\Client([
'base_uri' => env('VISA_API_URL'),
'auth' => [env('VISA_API_USERNAME'), env('VISA_API_PASSWORD')]
]);
switch ($type->id) {
case 1: //User - Envoi wallet à wallet
2020-06-21 21:49:24 +00:00
$this->validate($request, $transaction->send_wallet_wallet_rules());
2020-06-24 07:36:54 +00:00
$user = $walletUser->user;
2020-06-21 21:49:24 +00:00
if ($this->checkPassword($request->password, $user->encrypted_password, $user->salt)) {
2020-06-24 07:36:54 +00:00
if ($request->montant > $walletUser->balance) {
return $this->errorResponse(trans('errors.insufficient_balance'));
2020-06-24 07:36:54 +00:00
} else {
$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_depot = $montantDepot = $transaction->montant - $frais - $taxe;
$configPayeur = ConfigWallet::where('id_network', $request->network_destinataire)->firstOrFail();
$reseauPayeur = PayingNetwork::where('id_network', $request->network_destinataire)->where('id_configWallet', $config->id)->firstOrFail();
2020-06-24 07:36:54 +00:00
if ($init_country != $request->final_country) {
$transaction->part_reseau_payeur = $frais * $reseauPayeur->taux_partage / 100;
$transaction->part_reseau_emetteur = $frais - $transaction->part_reseau_payeur;
2020-06-24 07:36:54 +00:00
} else {
$transaction->part_reseau_payeur = 0;
$transaction->part_reseau_emetteur = $frais;
}
$transaction->commission_hyp = $transaction->part_reseau_emetteur;
2020-06-27 15:04:28 +00:00
$reseauPayeur->balance_com += $this->toMoneyAmount($transaction->part_reseau_payeur, $init_country, $request->final_country);
if ($configPayeur->type == 'ilink') {
$destinataire = User::where('user_code', $request->id_destinataire)->first();
2020-06-24 07:36:54 +00:00
if ($destinataire) { // Si c'est un wallet ilink
if ($destinataire->network->country->id == $request->final_country) {
$walletDestinataire = WalletsUser::where('idUser', $destinataire->id)->firstOrFail();
2020-06-27 15:04:28 +00:00
$walletDestinataire->balance += $this->toMoneyAmount($montantDepot, $init_country, $request->final_country);
2020-06-24 07:36:54 +00:00
$walletDestinataire->save();
} else {
$country = Country::findOrFail($request->final_country);
return $this->errorResponse(trans('errors.wallet_country_not_match', ['country' => $country->name]));
}
} else {
return $this->errorResponse(trans('errors.wallet_not_defined'));
}
//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
$walletHyperviseur->balance_com += $transaction->part_reseau_payeur;
2020-06-27 15:04:28 +00:00
$reseauPayeur->balance_com += $this->toMoneyAmount($transaction->part_reseau_emetteur, $init_country, $request->final_country);
$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);
2020-06-27 15:04:28 +00:00
$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 dont l'id est :
// $transaction->id_destinataire ;
// et le montant est:
// $montantDepot;
}
2020-06-24 17:28:11 +00:00
$walletHyperviseur->balance_com += $transaction->part_reseau_emetteur;
$walletUser->balance -= $transaction->montant;
$transaction->id_wallet_hyp = $walletHyperviseur->id;
2020-06-25 08:01:59 +00:00
$transaction->id_transaction = $this->getTransactionID();
2020-06-24 17:28:11 +00:00
$walletHyperviseur->save();
$walletUser->save();
$reseauPayeur->save();
$transaction->save();
2020-06-27 15:04:28 +00:00
$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),
2020-06-25 08:01:59 +00:00
'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),
2020-06-27 15:04:28 +00:00
'sender_code' => $user->user_code, 'receiver_code' => $transaction->id_destinataire]);
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
return $this->successResponse($message.trans('messages.sent_by_mail'));
2020-06-22 15:09:26 +00:00
}
2020-06-21 21:49:24 +00:00
} else {
return $this->errorResponse(trans('messages.incorrect_user_password'));
}
break;
case 2: //User - Envoi de wallet à carte
$this->validate($request, $transaction->card_rules());
2020-06-24 07:36:54 +00:00
$user = $walletUser->user;
if ($this->checkPassword($request->password, $user->encrypted_password, $user->salt)) {
if ($request->montant > $walletUser->balance) {
return $this->errorResponse(trans('errors.insufficient_balance'));
} else {
$expiration_date = \DateTime::createFromFormat('m/y', $request->expiration_date);
if (!$expiration_date)
$expiration_date = new \DateTime();
$transaction->expiration_date = $expiration_date;
$frais = $request->montant * $config->taux_com_user_wallet_carte / 100;
$transaction->montant_depot = $montantDepot = $transaction->montant - $frais;
$body['amount'] = $montantDepot;
$body['card_number'] = $request->numero_carte;
$body['cvv'] = $request->cvv;
$body['expiry_date'] = $expiration_date->format('Y-m');
$response = $client->post('fund-transfer-api/v1/transaction/push', ['json' => $body]);
$code = $response->getStatusCode();
if ($code == 200) {
$walletUser->balance -= $transaction->montant;
$walletHyperviseur->balance_com += $frais;
$transaction->commission_hyp = $frais;
$transaction->id_wallet_hyp = $walletHyperviseur->id;
$transaction->frais = $frais;
$transaction->date = new \DateTime();
$walletHyperviseur->save();
$walletUser->save();
2020-06-25 08:01:59 +00:00
$transaction->id_transaction = $this->getTransactionID();
$transaction->save();
2020-06-27 15:04:28 +00:00
$message = trans('messages.successful_user_send_to_cart',
2020-06-25 08:01:59 +00:00
['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country),
2020-06-24 07:36:54 +00:00
'net' => $this->toMoney($montantDepot, $init_country), 'fees' => $this->toMoney($frais, $init_country),
2020-06-27 15:04:28 +00:00
'sender_code' => $user->user_code, 'cart_number' => wordwrap($request->numero_carte, 4, ' ', true)]);
$this->sendMail($user->email, trans('messages.successful_transaction'), $message);
return $this->successResponse($message.trans('messages.sent_by_mail'));
} else {
return $this->errorResponse(trans('errors.visa_api_failed'), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
2020-06-24 07:36:54 +00:00
} else {
return $this->errorResponse(trans('messages.incorrect_user_password'));
}
break;
case 3: //User - Envoi de wallet à cash
2020-06-27 15:04:28 +00:00
$this->validate($request, $transaction->send_wallet_cash_rules());
$user = $walletUser->user;
if ($this->checkPassword($request->password, $user->encrypted_password, $user->salt)) {
if ($request->montant > $walletUser->balance) {
return $this->errorResponse(trans('errors.insufficient_balance'));
} else {
$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_retrait = $montantRetrait = $transaction->montant - $frais - $taxe;
$walletUser->balance -= $transaction->montant;
$transaction->commission_hyp = $frais;
$walletHyperviseur->balance_com += $frais;
$code_retrait = $this->random_string();
$hash = $this->hashSSHA($code_retrait);
$transaction->encrypted_code_retrait = $hash['encrypted'];
$transaction->code_retrait_salt = $hash['salt'];
2020-06-27 15:04:28 +00:00
$transaction->id_wallet_hyp = $walletHyperviseur->id;
$transaction->id_transaction = $this->getTransactionID();
$walletHyperviseur->save();
$walletUser->save();
$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]);
$this->sendMail($user->email, trans('messages.successful_transaction'),$message );
return $this->successResponse($message.trans('messages.sent_by_mail'));
}
} else {
return $this->errorResponse(trans('messages.incorrect_user_password'));
}
break;
case 4: //User - Envoi de wallet à banque
// Non disponible
//Pas de taxes
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
break;
case 10: //User - Retrait de carte vers wallet
break;
case 11: // User - Retrait de carte vers cash
break;
case 12: // Agent - Retrait en cash
$this->validate($request, $transaction->remove_cash_rules());
$agent = AgentPlus::findOrFail($network_agent->agent_id);
if ($this->checkPassword($request->password, $agent->encrypted_password, $agent->salt)) {
$transaction = WalletIlinkTransaction::find($request->id_transaction);
2020-06-21 21:49:24 +00:00
if ($transaction) {
if ($transaction->status_retrait == 0) {
if ($this->checkPassword($request->code_retrait, $transaction->encrypted_code_retrait, $transaction->code_retrait_salt)) {
if ($transaction->montant_retrait == $request->montant) {
$part_ag = floatval($transaction->frais * $config->taux_com_ag_retrait_cash / 100);
$part_sup = floatval($transaction->frais * $config->taux_com_sup_retrait_cash / 100);
$walletAgent->balance_princ += $transaction->montant_retrait;
$walletAgent->balance_com += $part_ag;
$walletSuperviseur->balance_com += $part_sup;
$walletHyperviseur->balance_com -= ($part_ag + $part_sup);
$transaction->status_retrait = 1;
$transaction->date_retrait = new \DateTime();
$walletAgent->save();
$walletSuperviseur->save();
$walletHyperviseur->save();
$transaction->save();
return $this->successResponse(trans('messages.successful_transaction'));
} else {
return $this->errorResponse('Montant de retrait incorrect');
}
} else {
return $this->errorResponse('Code de retrait invalide');
}
2020-06-21 21:49:24 +00:00
} else {
return $this->errorResponse('Retrait déjà éffectuée');
}
2020-06-21 21:49:24 +00:00
} else {
return $this->errorResponse('Cette transaction n\'existe pas', Response::HTTP_NOT_FOUND);
}
2020-06-21 21:49:24 +00:00
} else {
return $this->errorResponse(trans('messages.incorrect_user_password'));
}
break;
case 13: // Agent - Retrait de la carte vers cash
2020-06-21 21:49:24 +00:00
$this->validate($request, $transaction->card_rules());
$agent = AgentPlus::findOrFail($network_agent->agent_id);
if ($this->checkPassword($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;
$frais = floatval($request->montant * $config->taux_com_wallet_ag_carte_cash / 100);
$montantRetrait = $transaction->montant + $frais;
$transaction->montant_retrait = $montantRetrait;
$body['amount'] = $montantRetrait;
$body['card_number'] = $request->numero_carte;
$body['cvv'] = $request->cvv;
$body['expiry_date'] = $expiration_date->format('Y-m');
2020-06-21 21:49:24 +00:00
// $response = $client->post('fund-transfer-api/v1/transaction/pull', ['json' => $body]);
$code = 200; // $response->getStatusCode();
if ($code == 200) {
$banqueCommission = floatval($frais * $config->taux_com_banque_retrait_carte_cash / 100);
$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 sapplique sur les frais minimum) et créditer
//le compte des opérations défini avec notre banque
//partenaire
$walletAgent->balance_princ += $transaction->montant;
$agentCommission = floatval($frais * $config->taux_com_ag_retrait_carte_cash / 100);
$superviseurCommission = floatval($frais * $config->taux_com_sup_retrait_carte_cash / 100);
$hyperviseurCommission = floatval($frais * $$config->taux_com_hyp_retrait_carte_cash / 100);
$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 = new \DateTime();
$walletAgent->save();
$walletSuperviseur->save();
$walletHyperviseur->save();
$transaction->save();
return $this->successResponse(trans('messages.successful_transaction'));
} else {
return $this->errorResponse(trans('errors.visa_api_failed'), Response::HTTP_INTERNAL_SERVER_ERROR);
}
} else {
return $this->errorResponse(trans('messages.incorrect_user_password'));
}
break;
case 14: // Agent - Envoi de cash vers wallet iLink
$this->validate($request, $transaction->cash_wallet_rules());
2020-06-19 19:31:32 +00:00
$agent = AgentPlus::findOrFail($network_agent->agent_id);
if ($this->checkPassword($request->password, $agent->encrypted_password, $agent->salt)) {
2020-06-19 19:31:32 +00:00
$user = User::where('user_code', $request->user_code)->firstOrFail();
$walletUser = WalletsUser::where('idUser', $user->id)->firstOrFail();
$frais = ($request->init_country != $request->final_country) ? $this->calculateFees($plr_agent_depot_wallet_ilink, $request->montant) : $this->calculateFees($plr_agent_depot_wallet_ilink_national, $request->montant);
$taxe = ($request->init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
$montantDepot = $request->montant - $frais - $taxe;
2020-06-19 19:31:32 +00:00
$transaction->montant_depot = $montantDepot;
$walletUser->balance += $montantDepot;
$commisionAgent = floatval($frais * $config->taux_com_ag_envoi_cash / 100);
$commisionSuper = floatval($frais * $config->taux_com_sup_envoi_cash / 100);
2020-06-19 18:33:10 +00:00
$commisionHyper = floatval($frais * $config->taux_com_hyp_envoi_cash / 100);
$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 = new \DateTime();
$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->save();
$this->sendMail($user->email, trans('messages.successful_transaction'), trans('messages.successful_deposit_ilink',
2020-06-21 21:49:24 +00:00
['id_transaction' => $transaction->id, 'amount' => $this->toMoney($transaction->montant, $request->init_country), 'net_init' => $this->toMoney($montantDepot, $request->init_country),
2020-06-25 08:01:59 +00:00
'net_final' => $this->toMoneyWithCurrency($montantDepot, $request->init_country, $request->final_country), 'fees' => $this->toMoney($frais, $request->init_country), 'tax' => $this->toMoney($taxe, $request->init_country),
'user_code' => $request->user_code]));
return $this->successResponse(trans('messages.successful_transaction'));
} else {
return $this->errorResponse(trans('messages.incorrect_user_password'));
}
break;
case 15: // Agent - Envoi de cash vers autre wallet
$this->validate($request, $transaction->cash_cash_rules());
2020-06-19 19:31:32 +00:00
$agent = AgentPlus::findOrFail($network_agent->agent_id);
if ($this->checkPassword($request->password, $agent->encrypted_password, $agent->salt)) {
2020-06-19 19:31:32 +00:00
$frais = ($request->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 = ($request->init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
$montantDepot = $request->montant - $frais - $taxe;
$transaction->montant_depot = $montantDepot;
//Emettre une trame securise pour crediter le wallet utilisateur
// $walletUser->balance += $montantDepot;
// $transaction->id_destinataire = r
$commisionAgent = floatval($frais * $config->taux_com_ag_envoi_cash / 100);
$commisionSuper = floatval($frais * $config->taux_com_sup_envoi_cash / 100);
$commisionHyper = floatval($frais * $config->taux_com_hyp_envoi_cash / 100);
$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 = new \DateTime();
2020-06-19 19:31:32 +00:00
$walletAgent->save();
$walletSuperviseur->save();
$walletHyperviseur->save();
$transaction->id_wallet_ag = $walletAgent->id;
$transaction->id_wallet_sup = $walletSuperviseur->id;
$transaction->id_wallet_hyp = $walletHyperviseur->id;
$transaction->save();
} else {
2020-06-19 19:31:32 +00:00
return $this->errorResponse(trans('messages.incorrect_user_password'));
}
break;
case 16: // Agent - Envoi de cash vers une carte visa
2020-06-21 21:49:24 +00:00
$this->validate($request, $transaction->card_rules());
2020-06-19 19:31:32 +00:00
$agent = AgentPlus::findOrFail($network_agent->agent_id);
if ($this->checkPassword($request->password, $agent->encrypted_password, $agent->salt)) {
2020-06-19 19:31:32 +00:00
$expiration_date = \DateTime::createFromFormat('m/y', $request->expiration_date);
if (!$expiration_date)
$expiration_date = new \DateTime();
$transaction->expiration_date = $expiration_date;
$frais = $request->montant * $config->taux_com_wallet_ag_envoi_cash_carte / 100;
$montantDepot = $transaction->montant - $frais;
$transaction->montant_depot = $montantDepot;
$body['amount'] = $montantDepot;
$body['card_number'] = $request->numero_carte;
$body['cvv'] = $request->cvv;
$body['expiry_date'] = $expiration_date->format('Y-m');
$response = $client->post('fund-transfer-api/v1/transaction/push', ['json' => $body]);
$code = $response->getStatusCode();
if ($code == 200) {
$banqueCommission = floatval($frais * $config->taux_com_banque_depot_cash_carte / 100);
$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 sapplique sur les frais minimum) et créditer
//le compte des opérations défini avec notre banque
//partenaire
$walletAgent->balance_princ -= $transaction->montant;
$agentCommission = floatval($frais * $config->taux_com_ag_depot_cash_carte / 100);
$superviseurCommission = floatval($frais * $config->taux_com_sup_depot_cash_carte / 100);
$hyperviseurCommission = floatval($frais * $config->taux_com_hyp_depot_cash_carte / 100);
$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 = new \DateTime();
$walletAgent->save();
$walletSuperviseur->save();
$walletHyperviseur->save();
$transaction->save();
return $this->successResponse(trans('messages.successful_transaction'));
} else {
return $this->errorResponse(trans('errors.visa_api_failed'), Response::HTTP_INTERNAL_SERVER_ERROR);
}
} else {
2020-06-19 19:31:32 +00:00
return $this->errorResponse(trans('messages.incorrect_user_password'));
}
break;
case 17: // Agent - Envoi de cash vers cash
$this->validate($request, array_merge($transaction->cash_cash_rules(), [
'email_emetteur' => 'required',
]));
2020-06-19 19:31:32 +00:00
$agent = AgentPlus::findOrFail($network_agent->agent_id);
if ($this->checkPassword($request->password, $agent->encrypted_password, $agent->salt)) {
$frais = ($request->init_country != $request->final_country) ? $this->calculateFees($plr_agent_cash_cash, $request->montant) : $this->calculateFees($plr_agent_cash_cash_national, $request->montant);
$taxe = ($request->init_country != $request->final_country) ? $this->calculateTax($taxesInternationales, $frais) : $this->calculateTax($taxesNationales, $frais);
2020-06-19 19:31:32 +00:00
$montantRetrait = $request->montant - $frais - $taxe;
$commisionAgent = floatval($frais * $config->taux_com_ag_envoi_cash / 100);
$commisionSuper = floatval($frais * $config->taux_com_sup_envoi_cash / 100);
$commisionHyper = floatval($frais * $config->taux_com_hyp_envoi_cash / 100);
2020-06-19 19:31:32 +00:00
$transaction->montant_retrait = $montantRetrait;
$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->id_wallet_ag = $walletAgent->id;
$transaction->id_wallet_sup = $walletSuperviseur->id;
$transaction->id_wallet_hyp = $walletHyperviseur->id;
2020-06-19 19:31:32 +00:00
$code_retrait = $this->random_string();
$hash = $this->hashSSHA($code_retrait);
2020-06-19 19:31:32 +00:00
$transaction->encrypted_code_retrait = $hash['encrypted'];
$transaction->code_retrait_salt = $hash['salt'];
$walletAgent->balance_princ -= $transaction->montant;
$transaction->frais = $frais;
2020-06-19 19:31:32 +00:00
$transaction->taxe = $taxe;
$transaction->date = new \DateTime();
2020-06-19 19:31:32 +00:00
$transaction->status_retrait = 0;
2020-06-19 18:33:10 +00:00
$walletAgent->save();
$walletSuperviseur->save();
$walletHyperviseur->save();
$transaction->save();
$this->sendMail($request->email_emetteur, trans('messages.successful_transaction'), trans('messages.successful_send_cash',
2020-06-27 15:04:28 +00:00
['sender_name' => $request->prenom_emetteur . ' ' . $request->nom_emetteur, 'receiver_name' => $request->prenom_destinataire . ' ' . $request->nom_destinataire,
2020-06-21 21:49:24 +00:00
'id_transaction' => $transaction->id, 'amount' => $this->toMoney($transaction->montant, $request->init_country), 'net_init' => $this->toMoney($montantRetrait, $request->init_country),
2020-06-25 08:01:59 +00:00
'net_final' => $this->toMoneyWithCurrency($montantRetrait, $request->init_country, $request->final_country), 'fees' => $this->toMoney($frais, $request->init_country), 'tax' => $this->toMoney($taxe, $request->init_country),
2020-06-21 21:49:24 +00:00
'code' => wordwrap($code_retrait, 4, ' ', true)]));
return $this->successResponse(trans('messages.successful_transaction'));
} else {
2020-06-19 19:31:32 +00:00
return $this->errorResponse(trans('messages.incorrect_user_password'));
}
break;
case 18: // Agent - Envoi de cash vers banque
// Indisponible
break;
}
}
2020-06-24 07:36:54 +00:00
public function lastUserTransactions($id_wallet_user)
{
$transactions = DB::select('SELECT tit.nom as operation , tit.type ,tit.acteur as source , wit.montant ,
CASE
WHEN u.lastname IS NOT NULL THEN u.lastname
ELSE
CASE
WHEN wit.id_destinataire IS NULL THEN wit.nom_destinataire
ELSE wit.id_destinataire
END
END AS destinataire , wit.date , wit.id FROM wallet_ilink_transaction wit
INNER JOIN type_ilink_transaction tit ON wit.type = tit.id LEFT JOIN users u ON u.user_code = wit.id_destinataire WHERE wit.id_wallet_user = :id_wallet
2020-06-27 15:04:28 +00:00
ORDER BY wit.date DESC LIMIT 10;', ['id_wallet' => $id_wallet_user]);
2020-06-24 07:36:54 +00:00
return $this->successResponse($transactions);
}
public function lastAgentTransactions($id_wallet_agent)
{
2020-06-24 07:36:54 +00:00
$transactions = DB::select('SELECT tit.nom as operation , tit.type ,tit.acteur as source , wit.montant ,
CASE
WHEN u.lastname IS NOT NULL THEN u.lastname
ELSE
CASE
WHEN wit.id_destinataire IS NULL THEN wit.nom_destinataire
ELSE wit.id_destinataire
END
END AS destinataire , wit.date , wit.id FROM wallet_ilink_transaction wit
INNER JOIN type_ilink_transaction tit ON wit.type = tit.id LEFT JOIN users u ON u.user_code = wit.id_destinataire WHERE wit.id_wallet_ag = :id_wallet
2020-06-27 15:04:28 +00:00
ORDER BY wit.date DESC LIMIT 10;', ['id_wallet' => $id_wallet_agent]);
return $this->successResponse($transactions);
}
public function calculateCommission(Request $request)
{
$rules = [
2020-06-24 07:36:54 +00:00
'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);
2020-06-24 07:36:54 +00:00
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();
2020-06-24 07:36:54 +00:00
} 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';
}));
$taxesInternationales = array_values(array_filter($config->taxes->all(), function ($tax) {
return $tax->destination == 'international';
}));
$plr_user_wallet_wallet = $this->getPaliers($config->paliers_config_wallets->all(), "user_wallet_wallet_international");
$plr_user_wallet_cash = $this->getPaliers($config->paliers_config_wallets->all(), "user_wallet_cash_international");
$plr_agent_depot_wallet_ilink = $this->getPaliers($config->paliers_config_wallets->all(), "agent_depot_wallet_ilink_international");
$plr_agent_depot_autre_wallet = $this->getPaliers($config->paliers_config_wallets->all(), "agent_depot_autre_wallet_international");
$plr_agent_cash_cash = $this->getPaliers($config->paliers_config_wallets->all(), "agent_cash_cash_international");
$plr_user_wallet_wallet_national = $this->getPaliers($config->paliers_config_wallets->all(), "user_wallet_wallet_national");
$plr_user_wallet_cash_national = $this->getPaliers($config->paliers_config_wallets->all(), "user_wallet_cash_national");
$plr_agent_depot_wallet_ilink_national = $this->getPaliers($config->paliers_config_wallets->all(), "agent_depot_wallet_ilink_national");
$plr_agent_depot_autre_wallet_national = $this->getPaliers($config->paliers_config_wallets->all(), "agent_depot_autre_wallet_national");
$plr_agent_cash_cash_national = $this->getPaliers($config->paliers_config_wallets->all(), "agent_cash_cash_national");
switch ($request->type) {
case 1: //User - Envoi wallet à wallet
$this->validate($request, [
'final_country' => 'required|integer|min:0|not_in:0',
'id_destinataire' => 'required_without:phone_destinataire'
]);
$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);
$destinataire = User::where('user_code', $request->id_destinataire)->first();
2020-06-25 08:01:59 +00:00
$data['destinataire'] = $destinataire ? $destinataire->lastname . ' ' . $destinataire->firstname : $request->id_destinataire;
2020-06-25 11:43:27 +00:00
$data['frais'] = $frais + $taxe;
$data['montant_net_init'] = $request->montant - $frais - $taxe;
2020-06-27 15:04:28 +00:00
$data['montant_net_final'] = $this->toMoneyWithCurrency($data['montant_net_init'], $init_country, $request->final_country);
2020-06-24 07:36:54 +00:00
return $this->successResponse($data);
break;
case 2: //User - Envoi de wallet à carte
$frais = $request->montant * $config->taux_com_user_wallet_carte / 100;
2020-06-25 16:54:46 +00:00
$data['frais'] = $frais;
$data['montant_net_init'] = $request->montant - $frais;
2020-06-24 07:36:54 +00:00
return $this->successResponse($data);
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'] = $frais + $taxe;
$data['montant_net_init'] = $request->montant - $frais - $taxe;
$data['montant_net_final'] = $this->toMoneyWithCurrency($data['montant_net_init'], $init_country, $request->final_country);
return $this->successResponse($data);
break;
}
}
private function getPaliers(array $paliers, $type)
{
return array_values(array_filter($paliers, function ($palier) use ($type) {
return $palier->type == $type;
}));
}
//Calcul des frais internationaux
private function calculateFees(array $paliers, $montant)
{
$size = sizeof($paliers);
2020-06-24 07:36:54 +00:00
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;
}
}
2020-06-24 07:36:54 +00:00
if ($palier) {
return (($palier->min + $palier->max) / 2 * $palier->taux / 100);
} else {
if ($montant < $min)
return $min * $paliers[0]->taux / 100;
else if ($montant > $max)
return $max * $paliers[$size - 1]->taux / 100;
}
}
2020-06-25 11:43:27 +00:00
return 0;
}
//Calcul des taxes
private function calculateTax(array $taxes, $frais)
{
$sommeTaux = 0;
$sommeFixe = 0;
foreach ($taxes as $tax) {
if ($tax->type == '%')
$sommeTaux += $tax->valeur;
if ($tax->type == 'fixe')
$sommeFixe += $tax->valeur;
}
return ($frais * $sommeTaux / 100) + $sommeFixe;
}
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);
}
}