175 lines
6.2 KiB
PHP
175 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Nova\Actions;
|
|
|
|
use App\Models\CurrencyRate;
|
|
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\Blade;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Validation\Rule;
|
|
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 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)
|
|
{
|
|
$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_visa_master_username) {
|
|
return ActionResponse::danger('Şahamça visa/master tölegi kabul edip bilmeýär.');
|
|
}
|
|
|
|
$total_amount = floatval(number_format($fields->payment_amount, 2)) + 23;
|
|
|
|
$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): array
|
|
{
|
|
$usd_to_tmt = CurrencyRate::where('currency_from', 'USD')->where('currency_to', 'TMT')->first('value');
|
|
|
|
if (! $usd_to_tmt) {
|
|
return [];
|
|
}
|
|
|
|
$max_value = number_format($usd_to_tmt->value * 250, 2);
|
|
|
|
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">Bankyň tutumy: 20 TMT</h3>
|
|
<h3 class="uppercase tracking-wide font-bold text-xs" dusk="heading">GBÜS tutumy: 3 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_tmt) {
|
|
if ($formData->payment_amount) {
|
|
$field->setValue(number_format($formData->payment_amount / $usd_to_tmt->value, 2));
|
|
} else {
|
|
$field->setValue('');
|
|
}
|
|
}),
|
|
|
|
Text::make(__('Jemi (TMT)'), 'total_amount')
|
|
->fullWidth()
|
|
->readonly()
|
|
->dependsOn('payment_amount', function ($field, $request, $formData) use ($usd_to_tmt) {
|
|
if ($formData->payment_amount) {
|
|
$field->setValue(
|
|
floatval(number_format($formData->payment_amount, 2)) + 23
|
|
);
|
|
} 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_visa_master_username,
|
|
'password' => $resource->branch->billing_visa_master_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'],
|
|
];
|
|
}
|
|
}
|