feat: add endpoint to check balance
This commit is contained in:
parent
fa3d1af0da
commit
03940b4a85
|
@ -2,7 +2,10 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\PaymentMethod;
|
||||
use App\Enums\PaymentTransactionStatus;
|
||||
use App\Enums\PaymentType;
|
||||
use App\Models\Country;
|
||||
use App\Models\PaymentAggregator;
|
||||
use App\Models\PaymentTransaction;
|
||||
use GuzzleHttp\Client;
|
||||
|
@ -491,4 +494,97 @@ class CinetpayController extends Controller
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public function checkBalance(Request $request)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'country_id' => 'required|integer|exists:countries,id',
|
||||
'amount' => 'required|numeric',
|
||||
'payment_channel' => 'nullable|string',
|
||||
]);
|
||||
|
||||
|
||||
$amount = $request->input('amount');
|
||||
$paymentChannel = $request->input('payment_channel');
|
||||
$countryId = $request->input('country_id');
|
||||
$country = Country::where('id', $countryId)->firstOrFail();
|
||||
$countryCode = $country->code_country;
|
||||
|
||||
$client = new Client([
|
||||
'base_uri' => config('variables.cinetpay_transfert_url')
|
||||
]);
|
||||
|
||||
// Login
|
||||
$loginResponse = $client->post('auth/login', [
|
||||
'form_params' => [
|
||||
"apikey" => config('variables.cinetpay_api_key'),
|
||||
"password" => config('variables.cinetpay_transfert_password'),
|
||||
],
|
||||
'timeout' => $this->timeout,
|
||||
'http_errors' => false
|
||||
]);
|
||||
|
||||
$responseData = json_decode($loginResponse->getBody()->getContents());
|
||||
$token = $responseData->data->token;
|
||||
$responseCode = $loginResponse->getStatusCode();
|
||||
if ( $responseCode == 200 && !empty($token)) {
|
||||
// Add Contact
|
||||
$balanceResponse = $client->get('transfer/check/balance', [
|
||||
'query' => [
|
||||
'token' => $token
|
||||
],
|
||||
'timeout' => $this->timeout,
|
||||
'http_errors' => false
|
||||
]);
|
||||
|
||||
$responseCode = $balanceResponse->getStatusCode();
|
||||
$responseData = json_decode($balanceResponse->getBody()->getContents());
|
||||
if ($responseCode == 200) {
|
||||
$amountAvailable = $responseData?->data?->countryBalance?->{$countryCode}?->available ?? 0;
|
||||
$fees = 0;
|
||||
$aggregator = PaymentAggregator::where('name','like','%cinetpay%')->first();
|
||||
if(!empty($aggregator)){
|
||||
$baseQuery = $aggregator->rates()->where('type', PaymentType::CASH_IN)
|
||||
->where('method', PaymentMethod::WALLET)
|
||||
->when($paymentChannel, function ($q) use($paymentChannel){
|
||||
return $q->where('channel',$paymentChannel);
|
||||
});
|
||||
|
||||
$rate = (clone $baseQuery)->where('country', $countryCode)->first();
|
||||
if(empty($rate)){
|
||||
$rate = (clone $baseQuery)->where('country','ALL')->first();
|
||||
}
|
||||
|
||||
if(!empty($rate)){
|
||||
if(!empty($rate->fixed_fees)){
|
||||
$targetCurrency = $country->currency->code;
|
||||
$sourceCurrency = $targetCurrency;
|
||||
if(!empty($rate->fixed_fees_currency)){
|
||||
$sourceCurrency = $rate->fixed_fees_currency;
|
||||
}
|
||||
$fixed_fees = $this->toMoneyAmount($rate->fixed_fees, $sourceCurrency, $targetCurrency);
|
||||
$fees = (($amount - $fixed_fees) * $rate->rate / 100 ) + $fixed_fees;
|
||||
}else{
|
||||
$fees = $amount * $rate->rate / 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$amount += $fees;
|
||||
|
||||
if ($amountAvailable >= $amount){
|
||||
return $this->successResponse("Solde disponible");
|
||||
}else{
|
||||
Log::error("Solde insuffisant :: ".$amountAvailable);
|
||||
return $this->errorResponse(__('errors.service_unavailable_try_later'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$errorMessage = $responseData?->description ?? $responseData?->message;
|
||||
Log::error("Error CinetPay check balance");
|
||||
Log::error("Response data :: ".json_encode($responseData ?? ''));
|
||||
return $this->errorResponse($errorMessage ?? __('errors.unexpected_error'));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -212,4 +212,9 @@ class PaymentController extends Controller
|
|||
return $this->errorResponse(__('errors.service_unavailable_try_later'));
|
||||
}
|
||||
}
|
||||
|
||||
public function checkBalance(Request $request)
|
||||
{
|
||||
return app(CinetpayController::class)->checkBalance($request);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,6 +51,7 @@ $router->group(['middleware' => 'auth'], function () use ($router) {
|
|||
*/
|
||||
$router->get('methods','PaymentController@getMethods');
|
||||
$router->post('pay','PaymentController@pay');
|
||||
$router->get('checkBalance','PaymentController@checkBalance');
|
||||
$router->get('checkStatus/{transaction_id}','PaymentController@checkStatus');
|
||||
$router->post('payOut','PaymentController@payOut');
|
||||
$router->get('fees','PaymentController@getFees');
|
||||
|
@ -75,6 +76,7 @@ $router->group(['middleware' => 'auth'], function () use ($router) {
|
|||
$router->get('methods',['as' => 'cinetpay.methods', 'uses' => 'CinetpayController@getMethods']);
|
||||
$router->addRoute(['GET','POST'],'pay',['as' => 'cinetpay.pay', 'uses' => 'CinetpayController@pay']);
|
||||
$router->post('payOut',['as' => 'cinetpay.payout', 'uses' => 'CinetpayController@payOut']);
|
||||
$router->get('checkBalance',['as' => 'cinetpay.payout', 'uses' => 'CinetpayController@payOut']);
|
||||
});
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue