Files
online.tbbank.gov.tm-larave…/app/Nova/Actions/MakeSberPaymentAction.php
2025-09-09 10:12:15 +05:00

323 lines
12 KiB
PHP

<?php
namespace App\Nova\Actions;
use App\Models\CurrencyRate;
use App\Models\Payment\OnlinePaymentHistory;
use App\Modules\SberPaymentOrder\Models\SberPaymentOrder;
use App\Modules\SberPaymentOrder\Models\SberPaymentOrderItem;
use App\Modules\VisaMasterSettings\Models\VisaMasterSettings;
use App\Repos\Payment\OnlinePaymentRepo;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Actions\ActionResponse;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\Heading;
use Laravel\Nova\Fields\Hidden;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
class MakeSberPaymentAction extends Action
{
use InteractsWithQueue, Queueable;
/**
* Name.
*/
public function name(): string
{
return __('Make payment');
}
/**
* Perform the action on the given models.
*
* @param ActionFields $fields
* @param Collection<array-key, \App\Modules\SberPaymentOrder\Models\SberPaymentOrder> $models
*/
public function handle(ActionFields $fields, Collection $models): mixed
{
$usd_to_tmt = CurrencyRate::where('currency_from', 'USD')->where('currency_to', 'TMT')->first('value');
$payment_amount = floatval($fields->get('payment_amount'));
$usd_payment = $fields->get('usd_payment');
if (! $usd_to_tmt || ! $payment_amount || ! $usd_payment) {
return ActionResponse::danger('Walýuta hasaby girizilmedik, operator bilen habarlaşmagyňyzy haýyş edýärin.');
}
$today = today();
$resource = $models->first();
$hasBeenPaid = false;
$resource->paymentItems->each(function ($item) use (&$hasBeenPaid, $today) {
if ($item->paid) {
if ($today->format('m Y') == $item->created_at->format('m Y')) {
$hasBeenPaid = true;
}
}
});
if ($hasBeenPaid) {
return Action::modal('modal-response', [
'title' => 'Bul aý töleg edildi!',
'body' => 'Bul aý töleg edildi.',
]);
}
if (! $this->canAcceptPayment($today)) {
return Action::modal('modal-response', [
'title' => 'Bu gun aýyn sonky guni!',
'body' => 'Ayyn sonky guni toleg alynmayar.',
]);
}
if (! $resource->branch || ! $resource->branch->billing_sber_username) {
return Action::modal('modal-response', [
'title' => 'Billing maglumatlary şahamçada ýok!',
'body' => 'Şahamça visa/master tölegi kabul edip bilmeýär.',
]);
}
$tvebTaxTMT = floatval($usd_to_tmt->value) * 18;
$bankTax = 120.75;
$total_amount = number_format($payment_amount + $tvebTaxTMT + $bankTax, 2, '.', '');
$payment = $this->order($resource, $total_amount);
if ($payment['status'] !== 'success') {
return Action::modal('modal-response', [
'title' => 'Töleg ýerinde näsazlyk!',
'body' => 'Halkbank apida mesele bar.',
]);
}
$this->createPaymentRecord($payment, $resource, $total_amount, $usd_payment);
return ActionResponse::openInNewTab($payment['url']);
}
/**
* Get the fields available on the action.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array<int, \Laravel\Nova\Fields\Field>
*/
public function fields(NovaRequest $request)
{
$usd_to_tmt = floatval(CurrencyRate::where('currency_from', 'USD')->where('currency_to', 'TMT')->first('value')?->value);
$usd_to_rub = floatval(CurrencyRate::where('currency_from', 'USD')->where('currency_to', 'RUB')->first('value')?->value);
$rub_to_tmt = floatval(CurrencyRate::where('currency_from', 'RUB')->where('currency_to', 'TMT')->first('value')?->value);
$payment_warning_text = VisaMasterSettings::where('name', 'sber_payment_warning')->first();
if (! $usd_to_tmt || ! $usd_to_rub || ! $rub_to_tmt || ! $payment_warning_text) {
return [];
}
$max_value = number_format($usd_to_rub * 250 * $rub_to_tmt, 2, '.', '');
$tvebTaxTMT = $usd_to_tmt * 18;
$bankTax = 120.75;
return [
Heading::make(Blade::render(<<<HTML
<h3 class="uppercase tracking-wide font-bold text-xs" dusk="heading">1 USD = $usd_to_tmt TMT</h3>
<h3 class="uppercase tracking-wide font-bold text-xs" dusk="heading">1 RUB = $rub_to_tmt TMT</h3>
<br>
<h3 class="uppercase tracking-wide font-bold text-xs" dusk="heading">TVEB USD tutumy: 18 USD</h3>
<h3 class="uppercase tracking-wide font-bold text-xs" dusk="heading">TVEB tutumy TMT = $tvebTaxTMT TMT</h3>
<br>
<h3 class="uppercase tracking-wide font-bold text-xs" dusk="heading">Bank tutumy: $bankTax TMT</h3>
HTML))->asHtml(),
Text::make(sprintf('%s (%s)', __('Töleg möçberi'), __('TMT')), 'payment_amount')
->fullWidth()
->required()
->rules('required', 'numeric', 'max:'.$max_value)
->help("Iň ýokary möçberi: {$max_value} TMT"),
Text::make(__('USD ekwalendi'), 'usd_rate')
->fullWidth()
->readonly()
->dependsOn('payment_amount', function ($field, $request, $formData) use ($usd_to_rub, $rub_to_tmt) {
$payment_amount = $formData->get('payment_amount');
if ($payment_amount) {
$usdValue = number_format($payment_amount / ($usd_to_rub * $rub_to_tmt), 2, '.', '');
$field->setValue($usdValue);
} else {
$field->setValue('');
}
}),
Hidden::make('usd_payment')
->dependsOn('payment_amount', function ($field, $request, $formData) use ($usd_to_rub, $rub_to_tmt) {
$payment_amount = $formData->get('payment_amount');
if ($payment_amount) {
$usdValue = number_format($payment_amount / ($usd_to_rub * $rub_to_tmt), 2, '.', '');
$field->setValue($usdValue);
} else {
$field->setValue('');
}
}),
Text::make(__('Jemi (TMT)'), 'total_amount')
->fullWidth()
->readonly()
->dependsOn('payment_amount', function ($field, $request, $formData) use ($tvebTaxTMT, $bankTax) {
$payment_amount = $formData->get('payment_amount');
if ($payment_amount) {
$field->setValue(
number_format($payment_amount + $tvebTaxTMT + $bankTax, 2, '.', '')
);
} else {
$field->setValue('');
}
}),
Heading::make(Blade::render(<<<HTML
<div class="w-full border text-left appearance-none rounded text-sm font-bold focus:outline-none focus:ring ring-primary-200 dark:ring-gray-600 relative inline-flex items-center justify-center shadow h-9 px-3 bg-primary-500 border-primary-500 text-white dark:text-gray-900">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="stroke-current shrink-0 w-6 h-6 mr-2">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
<span>$payment_warning_text->value</span>
</div>
HTML))->asHtml(),
];
}
/**
* Order a payment page
*
* @return array<string, string>
*/
public function order(SberPaymentOrder $resource, int|float|string $amount): array
{
$onlinePaymentRepo = OnlinePaymentRepo::make();
$orderNumber = $onlinePaymentRepo->generateOrderNumber($resource);
$final_amount = number_format($amount, 2, '', '');
$final_amount = '001';
$paymentResponse = Http::get('https://mpi.gov.tm/payment/rest/register.do', [
'orderNumber' => $orderNumber,
'amount' => $final_amount,
'currency' => 934,
'language' => 'ru',
'userName' => $resource->branch->billing_sber_username,
'password' => $resource->branch->billing_sber_password,
'returnUrl' => route('online-payment-store-sber'),
'pageView' => 'DESKTOP',
'description' => 'Sber tölegi',
])->onError(function ($response) {
Log::channel('halkbank_payment_error')
->error('Payment error', [
'response' => [
'body' => $response->body(),
],
]);
});
if ($paymentResponse->failed()) {
return [
'status' => 'failed',
'url' => '',
'order_id' => '',
'order_number' => '',
'online_payment_history_id' => '',
];
}
$onlinePaymentHistory = OnlinePaymentHistory::create([
'online_paymantable_id' => $resource->id,
'online_paymantable_type' => SberPaymentOrder::class,
'amount' => number_format($amount, 2, '', ''),
'orderNumber' => $orderNumber,
'description' => 'Sber tölegi',
'orderId' => $paymentResponse['orderId'],
'formUrl' => $paymentResponse['formUrl'],
'successUrl' => route('online-payment-store-sber'),
'errorUrl' => route('online-payment-store-sber'),
'api_client' => 'billing_sber_username',
'username' => $resource->branch->billing_sber_username,
'paymentStatus' => OnlinePaymentRepo::PENDING,
]);
return [
'status' => 'success',
'url' => $paymentResponse['formUrl'],
'order_id' => $paymentResponse['orderId'],
'order_number' => $orderNumber,
'online_payment_history_id' => $onlinePaymentHistory->id,
];
}
/**
* Create payment record
*
* @param array<string, string> $payment
*/
public function createPaymentRecord(
array $payment,
SberPaymentOrder $resource,
int|float|string $total_amount,
int|float|string $usd_payment
): void {
SberPaymentOrderItem::create([
'sber_payment_order_id' => $resource->id,
'online_payment_history_id' => $payment['online_payment_history_id'],
'payment_order_number' => $payment['order_number'],
'tmt_payment_amount' => $total_amount,
'usd_payment_amount' => $usd_payment,
]);
}
/**
* Can payment be accepted?
*
* @param \Illuminate\Support\Carbon $today
*/
public function canAcceptPayment(Carbon $today): bool
{
$lastDay = $today->copy()->endOfMonth();
$lastDayOfWeek = $lastDay->format('l');
$oneDayBefore = $lastDay->copy()->subDay(); // Saturday
$twoDaysBefore = $lastDay->copy()->subDays(2); // Friday
// Condition 1: Today is the last day
if ($today->isSameDay($lastDay)) {
info('check 1');
return false;
}
// Condition 2: Last day is Sunday → forbid Friday & Saturday
if ($lastDayOfWeek === 'Sunday' && ($today->isSameDay($oneDayBefore) || $today->isSameDay($twoDaysBefore))) {
info('check 2');
return false;
}
// Condition 3: Last day is Saturday → forbid Friday
if ($lastDayOfWeek === 'Saturday' && $today->isSameDay($oneDayBefore)) {
info('check 3');
return false;
}
// Default: allow payment
return true;
}
}