payment_amount) || is_null($fields->usd_payment)) {
return Action::modal('modal-response', [
'title' => 'Töleg maglumatlary ýok!',
'body' => 'Töleg maglumatlary girizilmedik',
]);
}
$resource = $models->first();
$today = today();
$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_visa_master_username) {
return Action::modal('modal-response', [
'title' => 'Billing maglumatlary şahamçada ýok!',
'body' => 'Ş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);
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, $fields->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): array
{
$usd_to_tmt = CurrencyRate::where('currency_from', 'USD')->where('currency_to', 'TMT')->first('value');
$payment_warning_text = VisaMasterSettings::where('name', 'payment_warning_text')->first();
if (! $usd_to_tmt || ! $payment_warning_text) {
return [];
}
$max_value = number_format($usd_to_tmt->value * 250, 2);
return [
Heading::make(Blade::render(<<1 USD = $usd_to_tmt->value TMT
Bankyň tutumy: 20 TMT
GBÜS tutumy: 3 TMT
HTML))->asHtml(),
Text::make(__('Töleg aý'), 'month')
->fullWidth()
->readonly()
->default(today()->translatedFormat('F')),
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('');
}
}),
Hidden::make('usd_payment')
->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) {
if (is_numeric($formData->payment_amount)) {
$field->setValue(
floatval(number_format($formData->payment_amount, 2, '.', '')) + 23
);
} 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_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 = OnlinePaymentHistory::create([
'online_paymantable_id' => $resource->id,
'online_paymantable_type' => VisaMasterPaymentOrder::class,
'amount' => number_format($amount, 2, '', ''),
'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' => 'billing_visa_master_username',
'username' => $resource->branch->billing_visa_master_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)
{
VisaMasterPaymentOrderItem::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')) {
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)) {
return false;
}
}
// Condition 3: If the last day is Saturday, disallow Friday
if ($lastDayOfWeek === 'Saturday' && $today->format('l') === 'Friday') {
return false;
}
// If none of the conditions match, allow payment
return true;
}
}