290 lines
11 KiB
PHP
290 lines
11 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\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\Hidden;
|
|
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.
|
|
*/
|
|
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
|
|
*/
|
|
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);
|
|
|
|
if (! $usd_to_tmt || ! $usd_to_rub || ! $rub_to_tmt) {
|
|
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('');
|
|
}
|
|
}),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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' => '',
|
|
'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 $payment
|
|
* @param $resource
|
|
*/
|
|
public function createPaymentRecord($payment, $resource, $total_amount, $usd_payment)
|
|
{
|
|
SberPaymentOrderItem::create([
|
|
'visa_master_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,
|
|
]);
|
|
}
|
|
|
|
public function canAcceptPayment($today)
|
|
{
|
|
$year = $today->format('Y');
|
|
$month = $today->format('m');
|
|
$lastDay = lastDayOfMonth(year: $year, month: $month);
|
|
|
|
// Condition 1: Check if today is the last day of the month
|
|
if ($today->format('Y-m-d') === $lastDay->format('Y-m-d')) {
|
|
info('check 1');
|
|
|
|
return false;
|
|
}
|
|
|
|
// Determine the day of the week for the last day of the month
|
|
$lastDayOfWeek = $lastDay->format('l'); // e.g., 'Sunday', 'Saturday'
|
|
|
|
// Condition 2: If the last day is Sunday, disallow Friday, Saturday, Sunday
|
|
if ($lastDayOfWeek === 'Sunday') {
|
|
$forbiddenDays = ['Friday', 'Saturday', 'Sunday'];
|
|
if (in_array($today->format('l'), $forbiddenDays)) {
|
|
info('check 2');
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Condition 3: If the last day is Saturday, disallow Friday
|
|
if ($lastDayOfWeek === 'Saturday' && $today->format('l') === 'Friday') {
|
|
info('check 3');
|
|
|
|
return false;
|
|
}
|
|
|
|
// If none of the conditions match, allow payment
|
|
return true;
|
|
}
|
|
}
|