validate($request, [ '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', 'password' => 'required', 'montant' => 'required|numeric|min:0|not_in:0', ]); $type = TypeIlinkTransaction::findOrFail($request->type); 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)) { $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')); } $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'; })); $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 $this->validate($request, $transaction->send_wallet_wallet_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_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; $configPayeur = ConfigWallet::where('id_network', $request->network_destinataire)->firstOrFail(); if ($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; } else { $transaction->part_reseau_payeur = 0; $transaction->part_reseau_emetteur = $frais; } $transaction->montant_net_final_country = $this->toMoneyAmount($montantDepot, $init_country, $request->final_country); $transaction->commission_hyp = $transaction->part_reseau_emetteur; $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; if ($configPayeur->type == 'ilink') { $destinataire = User::where('user_code', $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); 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; $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 dont l'id est : // $transaction->id_destinataire ; // et le montant est: // $montantDepot; } $walletHyperviseur->balance_com += $transaction->part_reseau_emetteur; $walletUser->balance -= $transaction->montant; $transaction->id_wallet_hyp = $walletHyperviseur->id; $transaction->id_transaction = $this->getTransactionID(); $walletHyperviseur->save(); $walletUser->save(); if(isset($reseauPayeur)) $reseauPayeur->save(); $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]); $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 2: //User - Envoi de wallet à carte $this->validate($request, $transaction->user_card_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 { if(!(isset($user->numero_carte) && isset($user->expiration_date))) return $this->errorResponse(trans('errors.no_bank_card_attached')); $transaction->expiration_date = $user->expiration_date; $transaction->numero_carte = $user->numero_carte; $frais = $request->montant * $config->taux_com_user_wallet_carte / 100; $transaction->montant_net = $montantDepot = $transaction->montant - $frais; $body['amount'] = $this->toUSDAmount($montantDepot,$init_country);; $body['card_number'] = $user->numero_carte; $body['cvv'] = $request->cvv; $body['expiry_date'] = $user->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(); $transaction->id_transaction = $this->getTransactionID(); $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); return $this->successResponse($message.trans('messages.sent_by_mail')); } 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 3: //User - Envoi de wallet à cash $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_net = $montantRetrait = $transaction->montant - $frais - $taxe; $configPayeur = ConfigWallet::where('id_network', $request->network_destinataire)->firstOrFail(); if ($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; } else { $transaction->part_reseau_payeur = 0; $transaction->part_reseau_emetteur = $frais; } $transaction->montant_net_final_country = $this->toMoneyAmount($montantRetrait, $init_country, $request->final_country); $transaction->commission_hyp = $transaction->part_reseau_emetteur; $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; if ($configPayeur->type == 'ilink') { //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; $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 dont l'id est : // $transaction->id_destinataire ; // et le montant est: // $montantDepot; } $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(); if(isset($reseauPayeur)) $reseauPayeur->save(); $walletHyperviseur->save(); $walletUser->save(); $transaction->date = new \DateTime(); $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]); $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 $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 = $this->calculateFees($plr_user_wallet_cash_national, $request->montant); $transaction->taxe = $taxe = $this->calculateTax($taxesNationales, $frais); $transaction->montant_net = $montantRetrait = $transaction->montant - $frais - $taxe; $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 = new \DateTime(); $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]); $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 10: //User - Retrait de carte vers wallet $this->validate($request, $transaction->user_card_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 { if(!(isset($user->numero_carte) && isset($user->expiration_date))) return $this->errorResponse(trans('errors.no_bank_card_attached')); $transaction->expiration_date = $user->expiration_date; $transaction->numero_carte = $user->numero_carte; $frais = $request->montant * $config->taux_com_user_carte_wallet / 100; $transaction->montant_net = $montantRetrait = $transaction->montant - $frais; $body['amount'] = $this->toUSDAmount($montantRetrait,$init_country); $body['card_number'] = $user->numero_carte; $body['cvv'] = $request->cvv; $body['expiry_date'] = $user->expiration_date->format('Y-m'); $response = $client->post('fund-transfer-api/v1/transaction/pull', ['json' => $body]); $code = $response->getStatusCode(); if ($code == 200) { $transaction->commission_banque = $commissionBanque = $frais * $config->taux_com_banque_retrait_carte_cash_ilink / 100 ; //Emettre une trame SSL pour recharger le compte de la banque partenaire du montant de sa commission $walletUser->balance += $montantRetrait; $transaction->commission_hyp = $frais * $config->taux_com_hyp_retrait_carte_cash_ilink / 100 ; $walletHyperviseur->balance_com += $transaction->commission_hyp; $transaction->id_wallet_hyp = $walletHyperviseur->id; $transaction->frais = $frais; $transaction->date = new \DateTime(); $walletHyperviseur->save(); $walletUser->save(); $transaction->id_transaction = $this->getTransactionID(); $transaction->save(); $message = trans('messages.successful_user_remove_from_cart_to_wallet', ['id_transaction' => $transaction->id_transaction, 'amount' => $this->toMoney($transaction->montant, $init_country), 'net' => $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); return $this->successResponse($message.trans('messages.sent_by_mail')); } 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 11: // User - Retrait de carte vers cash $this->validate($request, $transaction->user_card_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 { if(!(isset($user->numero_carte) && isset($user->expiration_date))) return $this->errorResponse(trans('errors.no_bank_card_attached')); $transaction->expiration_date = $user->expiration_date; $transaction->numero_carte = $user->numero_carte; $frais = $request->montant * $config->taux_com_user_carte_cash/ 100; $transaction->montant_net = $montantRetrait = $transaction->montant - $frais; $body['amount'] = $this->toUSDAmount($montantRetrait,$init_country); $body['card_number'] = $user->numero_carte; $body['cvv'] = $request->cvv; $body['expiry_date'] = $user->expiration_date->format('Y-m'); $response = $client->post('fund-transfer-api/v1/transaction/pull', ['json' => $body]); $code = $response->getStatusCode(); if ($code == 200) { $transaction->commission_banque = $commissionBanque = $frais * $config->taux_com_banque_retrait_carte_cash_ilink / 100 ; //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']; $walletUser->balance -= $transaction->montant; $transaction->commission_hyp = $frais * $config->taux_com_hyp_retrait_carte_cash_ilink / 100 ; $walletHyperviseur->balance_com += $transaction->commission_hyp; $transaction->id_wallet_hyp = $walletHyperviseur->id; $transaction->frais = $frais; $transaction->date = new \DateTime(); $transaction->status_retrait = 0; $walletHyperviseur->save(); $walletUser->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), 'net' => $this->toMoney($montantRetrait, $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 { return $this->errorResponse(trans('errors.visa_api_failed'), Response::HTTP_INTERNAL_SERVER_ERROR); } } } else { return $this->errorResponse(trans('messages.incorrect_user_password')); } 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); if ($transaction) { if ($transaction->status_retrait == 0) { if ($this->checkPassword($request->code_retrait, $transaction->encrypted_code_retrait, $transaction->code_retrait_salt)) { if ($transaction->montant_net == $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_net; $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'); } } else { return $this->errorResponse('Retrait déjà éffectuée'); } } else { return $this->errorResponse('Cette transaction n\'existe pas', Response::HTTP_NOT_FOUND); } } else { return $this->errorResponse(trans('messages.incorrect_user_password')); } break; case 13: // Agent - Retrait de la carte vers cash $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_net = $montantRetrait; $body['amount'] = $montantRetrait; $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/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 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 = 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()); $agent = AgentPlus::findOrFail($network_agent->agent_id); if ($this->checkPassword($request->password, $agent->encrypted_password, $agent->salt)) { $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; $transaction->montant_net = $montantDepot; $walletUser->balance += $montantDepot; $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(); $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', ['id_transaction' => $transaction->id, 'amount' => $this->toMoney($transaction->montant, $request->init_country), 'net_init' => $this->toMoney($montantDepot, $request->init_country), '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()); $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_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_net = $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(); $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 { return $this->errorResponse(trans('messages.incorrect_user_password')); } break; case 16: // Agent - Envoi de cash vers une carte visa $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 = $request->montant * $config->taux_com_wallet_ag_envoi_cash_carte / 100; $montantDepot = $transaction->montant - $frais; $transaction->montant_net = $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 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 = 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 { 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', ])); $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); $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); $transaction->montant_net = $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; $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 = new \DateTime(); $transaction->status_retrait = 0; $walletAgent->save(); $walletSuperviseur->save(); $walletHyperviseur->save(); $transaction->save(); $this->sendMail($request->email_emetteur, trans('messages.successful_transaction'), trans('messages.successful_send_cash', ['sender_name' => $request->prenom_emetteur . ' ' . $request->nom_emetteur, 'receiver_name' => $request->prenom_destinataire . ' ' . $request->nom_destinataire, 'id_transaction' => $transaction->id, 'amount' => $this->toMoney($transaction->montant, $request->init_country), 'net_init' => $this->toMoney($montantRetrait, $request->init_country), '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), 'code' => wordwrap($code_retrait, 4, ' ', true)])); return $this->successResponse(trans('messages.successful_transaction')); } else { return $this->errorResponse(trans('messages.incorrect_user_password')); } break; case 18: // Agent - Envoi de cash vers banque // Indisponible break; } } 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 ORDER BY wit.date DESC LIMIT 10;', ['id_wallet' => $id_wallet_user]); return $this->successResponse($transactions); } public function lastAgentTransactions($id_wallet_agent) { $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 ORDER BY wit.date DESC LIMIT 10;', ['id_wallet' => $id_wallet_agent]); 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(); } 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(); $data['destinataire'] = $destinataire ? $destinataire->lastname . ' ' . $destinataire->firstname : $request->id_destinataire; $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; case 2: //User - Envoi de wallet à carte $frais = $request->montant * $config->taux_com_user_wallet_carte / 100; $data['frais'] = $frais; $data['montant_net_init'] = $request->montant - $frais; 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; 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'] = $frais + $taxe; $data['montant_net_init'] = $request->montant - $frais - $taxe; return $this->successResponse($data); break; case 10: //User - Retrait de carte vers wallet $frais = $request->montant * $config->taux_com_user_carte_wallet / 100;; $data['frais'] = $frais; $data['montant_net_init'] = $request->montant - $frais; return $this->successResponse($data); break; case 11: // User - Retrait de carte vers cash $frais = $request->montant * $config->taux_com_user_carte_cash/ 100; $data['frais'] = $frais ; $data['montant_net_init'] = $request->montant - $frais; 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); 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) { 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; } } 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); } }