Files
online.tbbank.gov.tm-larave…/app/Nova/Actions/MakeSberPaymentAction.php
2024-10-30 17:05:25 +05:00

181 lines
6.6 KiB
PHP

<?php
namespace App\Nova\Actions;
use App\Models\CurrencyRate;
use App\Models\Payment\OnlinePaymentHistory;
use App\Modules\SberPaymentOrder\Models\SberPaymentOrder;
use App\Repos\Payment\OnlinePaymentRepo;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
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\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
class MakeSberPaymentAction extends Action
{
use InteractsWithQueue, Queueable;
/**
* Perform the action on the given models.
*
* @param \Laravel\Nova\Fields\ActionFields $fields
* @param \Illuminate\Support\Collection $models
* @return mixed
*/
public function handle(ActionFields $fields, Collection $models)
{
$usd_to_tmt = CurrencyRate::where('currency_from', 'USD')->where('currency_to', 'TMT')->first('value');
if (! $usd_to_tmt) {
return ActionResponse::danger('Walýuta hasaby girizilmedik, operator bilen habarlaşmagyňyzy haýyş edýärin.');
}
$resource = $models->first();
if (! $resource->branch || ! $resource->branch->billing_sber_username) {
return ActionResponse::danger('Şahamça sber tölegi kabul edip bilmeýär.');
}
$tvebTaxTMT = $usd_to_tmt->value * 18;
$bankTax = 120.75;
$total_amount = number_format($fields->payment_amount + $tvebTaxTMT + $bankTax, 2, '.', '');
$payment = $this->order($resource, $total_amount);
return $payment['status'] === 'success'
? ActionResponse::openInNewTab($payment['url'])
: ActionResponse::danger('Töleg ýerinde näsazlyk!');
}
/**
* Get the fields available on the action.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function fields(NovaRequest $request)
{
$usd_to_tmt = CurrencyRate::where('currency_from', 'USD')->where('currency_to', 'TMT')->first('value');
if (! $usd_to_tmt) {
return [];
}
$max_value = number_format(96.05 * 250 * 0.036621, 2, '.', '');
$tvebTaxTMT = $usd_to_tmt->value * 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->value TMT</h3>
<h3 class="uppercase tracking-wide font-bold text-xs" dusk="heading">1 RUB = 0.036621 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) {
if ($formData->payment_amount) {
$usdValue = number_format($formData->payment_amount / (96.05 * 0.036621), 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) {
if (is_numeric($formData->payment_amount)) {
$field->setValue(
number_format($formData->payment_amount + $tvebTaxTMT + $bankTax, 2, '.', '')
);
} else {
$field->setValue('');
}
}),
];
}
/**
* Order a payment page
*/
public function order($resource, $amount)
{
$onlinePaymentRepo = OnlinePaymentRepo::make();
$orderNumber = $onlinePaymentRepo->generateOrderNumber($resource);
$paymentResponse = Http::get('https://mpi.gov.tm/payment/rest/register.do', [
'orderNumber' => $orderNumber,
'amount' => number_format($amount, 2, '', ''),
'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' => '',
];
}
info([
$paymentResponse->body()
]);
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'],
];
}
}