fix: improve insurance payment

This commit is contained in:
Djery-Tom 2023-07-31 13:50:33 +01:00
parent 8218c8053b
commit 21cf029a13
2 changed files with 81 additions and 57 deletions

1
.gitignore vendored
View File

@ -10,4 +10,3 @@ Homestead.yaml
/public/insurances-subscriptions-docs
/public/qrcodes
/public/invoices-docs
composer.lock

View File

@ -24,10 +24,12 @@ use App\Models\PaymentAggregator;
use App\Models\PaymentTransaction;
use App\Models\User;
use App\Models\Wallet;
use App\Models\WalletIlinkTransaction;
use App\Rules\PasswordValidation;
use App\Traits\Helper;
use Carbon\Carbon;
use DateTime;
use Exception;
use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
@ -252,10 +254,10 @@ class InsuranceInvoiceController extends Controller
$this->validate($request, [
'password' => 'required|string',
'amount' => 'required|numeric|min:0',
'payment_method' => 'required|string',
'payment_phone' => 'required_unless:payment_method,wallet|string',
'payment_method' => 'required|string|in:CARD,MOBILE_MONEY,WALLET',
'payment_phone' => 'required_unless:payment_method,WALLET|string',
'payment_transaction_id' => 'nullable|exists:payment_transactions,transaction_id', // A envoyer apres avoir effectuer le paiement
'payment_token' => 'required_with:payment_transaction_id|string', // A envoyer apres avoir effectuer le paiement
// 'payment_token' => 'required_with:payment_transaction_id|string', // A envoyer apres avoir effectuer le paiement
'otp' => 'nullable|string'
]);
@ -298,22 +300,44 @@ class InsuranceInvoiceController extends Controller
return $this->errorResponse(trans('errors.minimum amount_to_paid', ['amount' => $this->toMoneyWithCurrencyCode($invoice->amount_per_split, $currency)]));
}
if (empty($transaction_id)) {
if ($payment_method == 'wallet') {
if ($payment_method == 'WALLET') {
if ($user->wallet->balance < $amountToPaid) {
$remains_amount = $amountToPaid - $user->wallet->balance;
return $this->errorResponse(trans('errors.insufficient_balance', ['amount' => $this->toMoneyWithCurrencyCode($remains_amount, $currency)]));
}
} else {
$aggregator = PaymentAggregator::where('status', 1)->first();
if (!$aggregator) {
return $this->errorResponse(trans('errors.payment_not_available'));
}
$result = $this->handlePayIn([
// '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' => $amountToPaid,
'currency' => $user->network->country->currency_code,
'customer_id' => $user->id,
'customer_email' => $user->email,
'customer_name' => $user->lastname,
'customer_surname' => $user->lastname,
'customer_phone_number' => $user->phone,
'customer_address' => $user->adresse,
'customer_country' => $user->network->country->code_country,
'customer_city' => $user->adresse,
'customer_state' => $user->network->country->code_country,
'customer_zip_code' => '00237',
'payment_method' => $request->input('payment_method'),
'reason' => trans('messages.insurance_invoice'),
], $transaction_id);
if (!is_string($result)) {
return $result;
}
} else {
$transaction = PaymentTransaction::where('transaction_id', $transaction_id)->where('payment_token', $request->input('payment_token'))->where('state', 'ACCEPTED')->first();
$transaction_id = $result;
}
if (!empty($transaction_id)) {
$transaction = PaymentTransaction::where('transaction_id', $transaction_id)->where('state', 'ACCEPTED')->first();
if (!$transaction) {
return $this->errorResponse(trans('errors.payment_not_found'), 404);
}
@ -339,46 +363,6 @@ class InsuranceInvoiceController extends Controller
if ($payment_method == 'wallet') {
$user->wallet->balance -= $amountToPaid;
$user->wallet->save();
} else {
// Pay through payment service
$client = new Client([
'base_uri' => config('variable.payment_service_url'),
'headers' => [
'Authorization' => config('variable.payment_service_key'),
]
]);
$paymentResponse = $client->post('/pay', [
'json' => [
'aggregator_id' => $aggregator->id,
'amount' => $amountToPaid,
'currency' => $user->network->country->currency_code,
'customer_id' => $user->id,
'customer_email' => $user->email,
'customer_name' => $user->lastname,
'customer_surname' => $user->lastname,
'customer_phone_number' => $request->input('payment_phone'),
'customer_address' => $user->adresse,
'customer_country' => $user->network->country->code_country,
'customer_city' => $user->adresse,
'customer_state' => $user->network->country->code_country,
'customer_zip_code' => '00237',
'payment_method' => $request->input('payment_method'),
'reason' => trans('messages.insurance_invoice'),
'otp' => $request->input('otp')
],
'http_errors' => false
]);
$responseData = json_decode($paymentResponse->getBody()->getContents());
$responseCode = $paymentResponse->getStatusCode();
if ($responseCode == 200) {
// $transaction_id = $responseData->response->transaction_id;
return $this->successResponse($responseData->response);
} else {
return $this->errorResponse($responseData->error ?? trans('errors.unexpected_error'), $responseCode);
}
}
}
@ -470,6 +454,47 @@ class InsuranceInvoiceController extends Controller
}
}
/**
* @throws Exception
*/
private function handlePayIn($data, $payment_transaction_id = null)
{
// Pay through payment service
$client = new Client([
'base_uri' => config('variable.payment_service_url'),
'headers' => [
'Authorization' => config('variable.payment_service_key'),
]
]);
// PayIn through payment service
if (empty($payment_transaction_id)) {
// PayIn through payment service
$response = $client->post('/pay', ['json' => $data, 'http_errors' => false]);
} else {
// Check payment status through payment service
if (NhInsurancesPayment::where('payment_id', $payment_transaction_id)->exists()) {
throw new Exception(__('errors.transaction_already_completed'), 500);
}
$response = $client->get('/checkStatus/' . $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"];
return $response["transaction_id"];
} else {
throw new Exception($content['error'] ?? __('errors.visa_api_failed'), $content['status'] ?? 500);
// return $this->errorResponse($responseData->error ?? trans('errors.unexpected_error'), $responseCode);
}
}
// public function generateInvoices()
// {
// try {