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, '.', ''); $total_amount = 0.1; $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); $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(<<1 USD = $usd_to_tmt TMT

1 RUB = $rub_to_tmt TMT


TVEB USD tutumy: 18 USD

TVEB tutumy TMT = $tvebTaxTMT TMT


Bank tutumy: $bankTax TMT

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(<< $payment_warning_text->value HTML))->asHtml(), ]; } /** * 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([ '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, ]); } 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; } }