This commit is contained in:
2025-11-02 18:19:25 +05:00
parent 90b5a6bd6f
commit 910a7dd1b2
5 changed files with 119 additions and 99 deletions

View File

@@ -5,11 +5,9 @@ namespace App\Modules\OnlinePayment\Repositories;
use App\Modules\Makeable;
use App\Modules\OnlinePayment\Contracts\PaymentProviderContract;
use App\Modules\OnlinePayment\Models\OnlinePayment;
use Exception;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
class OnlinePaymentRepository
{
@@ -157,107 +155,43 @@ class OnlinePaymentRepository
*/
public function checkPayment(string $orderId)
{
// Find payment order from history
/** @var \App\Modules\OnlinePayment\Models\OnlinePayment Find payment order from history */
$paymentHistory = OnlinePayment::where('orderId', $orderId)->first();
// Find related resource
$resource = (new $paymentHistory->online_paymantable_type)->find(id: $paymentHistory->online_paymantable_id);
/** @var \App\Modules\CardOrder\Models\CardOrder Find related resource */
$relatedResource = (new $paymentHistory->online_paymantable_type)->find(id: $paymentHistory->online_paymantable_id);
// If resource could not be found or does not exist, then inform it via logs
if (! $resource) {
return [
'success' => false,
'title' => __('Payment has failed').' '.'(RESOURCE NOT FOUND)',
'pnr' => '',
'branch_name' => '',
'price_amount' => '',
'return_url' => url('/'),
'bank_branch' => null,
'resource' => null,
'paymentHistory' => null,
];
// Check if resource could not be found or does not exist
if (! $relatedResource) {
return $this->paymentFailed('(RESOURCE NOT FOUND)');
}
$resource->load('branch');
$relatedResource->load('branch');
$bank_branch = $resource->branch;
/** @var \App\Modules\Branch\Models\Branch */
$bankBranch = $relatedResource->branch;
if (! $bank_branch) {
return [
'success' => false,
'title' => __('Payment has failed').' '.'(BRANCH NOT FOUND)',
'pnr' => '',
'branch_name' => '',
'price_amount' => '',
'return_url' => url('/'),
'bank_branch' => null,
'resource' => null,
'paymentHistory' => null,
];
if (! $bankBranch) {
return $this->paymentFailed('(BRANCH NOT FOUND)');
}
try {
$response = Http::asForm()->post('https://mpi.gov.tm/payment/rest/getOrderStatus.do', [
'language' => 'ru',
'orderId' => $orderId,
'userName' => $bank_branch->billing_username,
'password' => $bank_branch->billing_password,
]);
} catch (Exception $e) {
return [
'success' => false,
'title' => __('Payment has failed').' '.'(REQUEST FAILURE)',
'pnr' => '',
'branch_name' => '',
'price_amount' => '',
'return_url' => url('/'),
'bank_branch' => null,
'resource' => null,
'paymentHistory' => null,
];
$this->provider->setUsername($bankBranch->billing_username);
$this->provider->setPassword($bankBranch->billing_password);
$response = $this->provider->checkPayment($orderId);
if ($response['ErrorCode'] == '99') {
return $this->paymentFailed('(REQUEST FAILURE)');
}
$payment_status = $response['ErrorCode'] == '0';
$returnURL = '/';
if ($payment_status) {
$resource->update([
'paid' => true,
]);
$paymentHistory->update([
'paymentStatus' => OnlinePaymentRepository::PAID,
]);
return [
'success' => true,
'title' => __('Payment is successful'),
'pnr' => $paymentHistory->orderNumber,
'branch_name' => $bank_branch->name,
'price_amount' => $paymentHistory->amount.' TMT',
'return_url' => $returnURL,
'bank_branch' => $bank_branch,
'resource' => $resource,
'paymentHistory' => $paymentHistory,
'response' => $response,
];
return $this->paymentSuccessful($relatedResource, $paymentHistory, $bankBranch);
}
$paymentHistory->update([
'paymentStatus' => OnlinePaymentRepository::FAILED,
]);
return [
'success' => false,
'title' => __('Payment has failed'),
'pnr' => $paymentHistory->orderNumber,
'branch_name' => $bank_branch->name,
'price_amount' => $paymentHistory->amount.' TMT',
'return_url' => $returnURL,
'paymentHistory' => $paymentHistory,
'bank_branch' => $bank_branch,
'response' => $response,
];
return $this->paymentFailed(
title: __('Payment has failed'),
);
}
/**
@@ -293,8 +227,65 @@ class OnlinePaymentRepository
OnlinePayment::create($data);
}
public function paymentSuccessful($relatedResource, $paymentHistory, $bankBranch): array
{
$relatedResource->update([
'paid' => true,
]);
$paymentHistory->update([
'paymentStatus' => OnlinePaymentRepository::PAID,
]);
return $this->paymentCheckResponseTemplate(
success: true,
pnr: $paymentHistory->orderNumber,
branch_name: $bankBranch->name,
price_amount: $paymentHistory->amount.' TMT'
);
}
/**
* Failed payment
*/
public function paymentFailed(string $title = '', $paymentHistory = null): array
{
if ($paymentHistory) {
$paymentHistory->update([
'paymentStatus' => OnlinePaymentRepository::FAILED,
]);
}
return $this->paymentCheckResponseTemplate(false, $title);
}
/**
* Check payment response
*
* @return array{success: bool, title: string, pnr: string, branch_name: string, price_amount: string, return_url: string}
*/
public function paymentCheckResponseTemplate(
bool $success,
string $title = '',
string $pnr = '',
string $branch_name = '',
string $price_amount = '',
string $return_url = ''
): array {
return [
'success' => $success,
'title' => $title !== '' ? $title : ($success ? __('Payment is successful') : __('Payment has failed')),
'pnr' => $pnr,
'branch_name' => $branch_name,
'price_amount' => $price_amount,
'return_url' => $return_url !== '' ? $return_url : url('/'),
];
}
/**
* Show payment status
*
* @param array{success: bool, title: string, pnr: string, branch_name: string, price_amount: string, return_url: string} $data
*/
public function paymentStatusView(array $data): View
{