Files
online.tbbank.gov.tm-larave…/app/Nova/Actions/MakePaymentNovaVisaMaster.php
2024-10-14 13:16:48 +05:00

124 lines
4.0 KiB
PHP

<?php
namespace App\Nova\Actions;
use App\Models\Payment\OnlinePaymentHistory;
use App\Repos\Payment\OnlinePaymentRepo;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
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\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
class MakePaymentNovaVisaMaster extends Action
{
use InteractsWithQueue, Queueable;
/**
* Action name
*/
public function name(): string
{
return __('Send payment');
}
/**
* 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)
{
$resource = $models->first();
if (! $resource->branch || ! $resource->branch->billing_visa_master_username) {
return ActionResponse::danger('Şahamça visa/master tölegi kabul edip bilmeýär.');
}
$payment = $this->order($resource, $fields->payment_method);
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): array
{
return [
Text::make(__('Töleg möçberi'), 'payment_amount')
->fullWidth()
->required()
->rules('required', 'rules:250'),
];
}
/**
* Order a payment page
*/
public function order($resource, $paymentMethod)
{
$onlinePaymentRepo = OnlinePaymentRepo::make();
$orderNumber = $onlinePaymentRepo->generateOrderNumber($resource);
$paymentResponse = Http::get('https://mpi.gov.tm/payment/rest/register.do', [
'orderNumber' => $orderNumber,
'amount' => $onlinePaymentRepo->getPrice($paymentMethod == 'usd' ? '90238' : '105860'),
'currency' => 934,
'language' => 'ru',
'userName' => $resource->branch->billing_username,
'password' => $resource->branch->billing_password,
'returnUrl' => route('online-payment-store-visa-master'),
'pageView' => 'DESKTOP',
'description' => 'Visa/Master 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::create([
'online_paymantable_id' => $resource->id,
'online_paymantable_type' => '\App\Modules\VisaMasterPaymentOrder\Models\VisaMasterPaymentOrder',
'amount' => $onlinePaymentRepo->getPrice($paymentMethod == 'usd' ? '902.38' : '1058,60'),
'orderNumber' => $orderNumber,
'description' => 'Visa/Master tölegi',
'orderId' => $paymentResponse['orderId'],
'formUrl' => $paymentResponse['formUrl'],
'successUrl' => route('online-payment-store-visa-master'),
'errorUrl' => route('online-payment-store-visa-master'),
'api_client' => config('app.url'),
'username' => $resource->branch->billing_username,
'paymentStatus' => OnlinePaymentRepo::PENDING,
]);
return [
'status' => 'success',
'url' => $paymentResponse['formUrl'],
];
}
}