246 lines
7.1 KiB
PHP
246 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Repos\Payment;
|
|
|
|
use App\Models\Branch\Branch;
|
|
use App\Models\Payment\OnlinePaymentHistory;
|
|
use App\Repos\Payment\Billing\HandlesBillingSyncing;
|
|
use App\Repos\Payment\Card\HandlesCardOrderPayments;
|
|
use App\Repos\Payment\Sber\HandlesSberPeyments;
|
|
use App\Repos\Payment\VisaMaster\HandlesVisaMasterPayments;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Laravel\Nova\Makeable;
|
|
|
|
class OnlinePaymentRepo
|
|
{
|
|
use HandlesBillingSyncing;
|
|
use HandlesCardOrderPayments;
|
|
use HandlesSberPeyments;
|
|
use HandlesVisaMasterPayments;
|
|
use Makeable;
|
|
|
|
/**
|
|
* Pending orders are brand new orders that have not been processed yet.
|
|
*/
|
|
public const PENDING = 'pending';
|
|
|
|
/**
|
|
* Failed orders are existing orders that could be paid
|
|
*/
|
|
public const FAILED = 'failed';
|
|
|
|
/**
|
|
* Paid orders are existing order that could be paid successfully
|
|
*/
|
|
public const PAID = 'paid';
|
|
|
|
/**
|
|
* Status Values
|
|
*
|
|
* @return array<null|string, string>
|
|
*/
|
|
public static function statusValues(): array
|
|
{
|
|
return [
|
|
null => '-',
|
|
self::PENDING => __('Pending'),
|
|
self::PAID => __('Paid'),
|
|
self::FAILED => __('Cancelled'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Tailwind
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public static function statusClasses(): array
|
|
{
|
|
return [
|
|
null => '-',
|
|
self::PENDING => 'warning',
|
|
self::PAID => 'success',
|
|
self::FAILED => 'danger',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Status icons
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public static function statusIcons(): array
|
|
{
|
|
return [
|
|
null => '-',
|
|
'success' => 'check-circle',
|
|
'info' => 'information-circle',
|
|
'primary' => 'clipboard-list',
|
|
'danger' => 'ban',
|
|
'warning' => 'exclamation-circle',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Set price
|
|
*/
|
|
public function getPrice(int|float|string $price): string
|
|
{
|
|
return number_format(floatval($price), 2, '', '');
|
|
}
|
|
|
|
/**
|
|
* Pay card order
|
|
*
|
|
* @param mixed $resource
|
|
* @return array<string, string>
|
|
*/
|
|
public function payCardOrder(mixed $resource): array
|
|
{
|
|
$orderNumber = $this->generateOrderNumber($resource);
|
|
|
|
$paymentResponse = Http::get('https://mpi.gov.tm/payment/rest/register.do', [
|
|
'orderNumber' => $orderNumber,
|
|
'amount' => $this->getPrice($resource->priceAmount()),
|
|
'currency' => 934,
|
|
'language' => 'ru',
|
|
'userName' => $resource->branch->billing_username,
|
|
'password' => $resource->branch->billing_password,
|
|
'returnUrl' => route('online-payment-store'),
|
|
'pageView' => 'DESKTOP',
|
|
'description' => 'Kart tölegi',
|
|
])->onError(function ($response) {
|
|
Log::channel('halkbank_payment_error')
|
|
->error('Payment error', [
|
|
'response' => [
|
|
'body' => $response->body(),
|
|
],
|
|
]);
|
|
});
|
|
|
|
if ($paymentResponse->failed()) {
|
|
return [
|
|
'status' => 'failed',
|
|
'url' => '',
|
|
];
|
|
}
|
|
|
|
$onlinePaymentHistory = new OnlinePaymentHistory;
|
|
$onlinePaymentHistory->online_paymantable_id = $resource->id;
|
|
$onlinePaymentHistory->online_paymantable_type = $resource::$model;
|
|
$onlinePaymentHistory->amount = $resource->priceAmount();
|
|
$onlinePaymentHistory->orderNumber = $orderNumber;
|
|
$onlinePaymentHistory->description = 'Kart tölegi';
|
|
$onlinePaymentHistory->orderId = $paymentResponse['orderId'];
|
|
$onlinePaymentHistory->formUrl = $paymentResponse['formUrl'];
|
|
$onlinePaymentHistory->successUrl = route('online-payment-store');
|
|
$onlinePaymentHistory->errorUrl = route('online-payment-store');
|
|
$onlinePaymentHistory->api_client = 'billing_username';
|
|
$onlinePaymentHistory->username = $resource->branch->billing_username;
|
|
$onlinePaymentHistory->paymentStatus = self::PENDING;
|
|
$onlinePaymentHistory->save();
|
|
|
|
return [
|
|
'status' => 'success',
|
|
'url' => $paymentResponse['formUrl'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Generate order number for payment
|
|
*/
|
|
public function generateOrderNumber(mixed $resource): string
|
|
{
|
|
return date('dmyHis');
|
|
}
|
|
|
|
/**
|
|
* Status view
|
|
*/
|
|
public static function statusView(): string
|
|
{
|
|
return 'orders.cards.online-payment.status';
|
|
}
|
|
|
|
/**
|
|
* Log resource not found
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \App\Models\Payment\OnlinePaymentHistory $paymentHistory
|
|
*/
|
|
protected static function logResourceNotFound(Request $request, OnlinePaymentHistory $paymentHistory): void
|
|
{
|
|
Log::channel('halkbank_payment_check_error')
|
|
->error('Related resource not found', [
|
|
'orderId' => $request->orderId,
|
|
'onlinePaymentHistory_id' => $paymentHistory->id,
|
|
'related_resource' => [
|
|
'type' => $paymentHistory->online_paymantable_type,
|
|
'id' => $paymentHistory->online_paymantable_id,
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Successfully payment response
|
|
*
|
|
* @return array<string, bool|string>
|
|
*/
|
|
public static function successfulPaymentResponse(
|
|
OnlinePaymentHistory $paymentHistory,
|
|
Branch $bank_branch,
|
|
Model $resource,
|
|
string $returnURL
|
|
): array {
|
|
return [
|
|
'success' => true,
|
|
'title' => __('Payment is successful'),
|
|
'pnr' => $paymentHistory->orderNumber,
|
|
'branch_name' => $bank_branch->name,
|
|
'price_amount' => \convertToOriginalFormat($paymentHistory->amount).' TMT',
|
|
'return_url' => $returnURL,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Failed payment response
|
|
*
|
|
* @return array<string, bool|string>
|
|
*/
|
|
public static function failedPaymentResponse(
|
|
OnlinePaymentHistory $paymentHistory,
|
|
Branch $bank_branch,
|
|
Model $resource,
|
|
string $returnURL
|
|
): array {
|
|
return [
|
|
'success' => false,
|
|
'title' => __('Payment has failed'),
|
|
'pnr' => $paymentHistory->orderNumber,
|
|
'branch_name' => $bank_branch->name,
|
|
'price_amount' => convertToOriginalFormat($paymentHistory->amount).' TMT',
|
|
'return_url' => $returnURL,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Resource not found
|
|
*
|
|
* @return array<string, string|bool>
|
|
*/
|
|
protected static function resourceNotFound(): array
|
|
{
|
|
return [
|
|
'success' => false,
|
|
'title' => __('Payment has failed').' '.'(RESOURCE NOT FOUND)',
|
|
'pnr' => '',
|
|
'branch_name' => '',
|
|
'price_amount' => '',
|
|
'return_url' => url('/'),
|
|
];
|
|
}
|
|
}
|