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

@@ -3,7 +3,6 @@
namespace App\Modules\HalkbankOnlinePayment\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\CardOrder\Models\CardOrder;
use App\Modules\HalkbankOnlinePayment\Repositories\HalkbankOnlinePaymentRepository;
use App\Modules\OnlinePayment\Repositories\OnlinePaymentRepository;
use Illuminate\Contracts\View\View;
@@ -22,13 +21,6 @@ class HalkbankOnlinePaymentController extends Controller
$paymentStatus = $onlinePaymentRepository->checkPayment($request->string('orderId'));
return $onlinePaymentRepository->paymentStatusView([
'success' => $paymentStatus['success'],
'title' => __('Payment has failed'),
'pnr' => $paymentStatus['paymentHistory']->orderNumber,
'branch_name' => $paymentStatus['bank_branch']->name,
'price_amount' => $paymentStatus['paymentHistory']->amount.' TMT',
'return_url' => $paymentStatus['return_url'],
]);
return $onlinePaymentRepository->paymentStatusView($paymentStatus);
}
}

View File

@@ -56,6 +56,7 @@ class HalkbankOnlinePaymentRepository implements PaymentProviderContract
Log::channel('halkbank_payment_error')
->error('Payment error', [
'response' => [
'title' => 'REGISTER',
'error' => $e->getMessage(),
'file_line' => $e->getFile().':'.$e->getLine(),
],
@@ -71,9 +72,39 @@ class HalkbankOnlinePaymentRepository implements PaymentProviderContract
return $paymentResponse;
}
public function checkPayment()
public function checkPayment(string $orderId): Response
{
// ...
try {
$paymentCheckResponse = Http::asForm()->post('https://mpi.gov.tm/payment/rest/getOrderStatus.do', [
'language' => 'ru',
'orderId' => $orderId,
'userName' => $this->username,
'password' => $this->password,
]);
} catch (Exception $e) {
Config::set('logging.channels.halkbank_payment_error', [
'driver' => 'single',
'path' => storage_path('logs/halkbank_payment_error.log'),
'level' => 'debug',
]);
Log::channel('halkbank_payment_error')
->error('Payment error', [
'response' => [
'title' => 'CHECKING',
'error' => $e->getMessage(),
'file_line' => $e->getFile().':'.$e->getLine(),
],
]);
return new Response(new GuzzleResponse(
503,
['Content-Type' => 'application/json'],
sprintf('{"ErrorCode":"99","ErrorMessage":"%s"}', $e->getMessage())
));
}
return $paymentCheckResponse;
}
/**

View File

@@ -8,6 +8,8 @@ interface PaymentProviderContract
{
public function sendRequest(): Response;
public function checkPayment(string $orderId): Response;
public function orderNumber(): int|string;
public function amount(): int|float|string;
@@ -17,4 +19,8 @@ interface PaymentProviderContract
public function returnUrl(): string;
public function username(): string;
public function setUsername(string $username): self;
public function setPassword(string $password): self;
}

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
{

View File

@@ -27,7 +27,7 @@
<h3 class="{{ $success ? 'success' : 'danger' }}">
{{ $title }}
</h3>
<span>Töleg nomeri: {{ $pnr }}</span>
<span>Sargyt belgisi: {{ $pnr }}</span>
</div>
<div class="bottom">
<div class="key-value">