This commit is contained in:
2024-11-25 19:32:51 +05:00
parent 7259cdcb26
commit 9d77e50191
11 changed files with 557 additions and 77 deletions

View File

@@ -5,7 +5,7 @@ namespace App\Nova\Actions;
use App\Models\CurrencyRate;
use App\Models\Payment\OnlinePaymentHistory;
use App\Modules\SberPaymentOrder\Models\SberPaymentOrder;
use App\Nova\Actions\Sber\SberActionFields;
use App\Modules\SberPaymentOrder\Models\SberPaymentOrderItem;
use App\Repos\Payment\OnlinePaymentRepo;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
@@ -15,7 +15,9 @@ 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;
@@ -26,19 +28,46 @@ class MakeSberPaymentAction extends Action
/**
* Perform the action on the given models.
*/
public function handle(SberActionFields $fields, Collection $models): mixed
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->payment_amount);
if (! $usd_to_tmt || ! $payment_amount) {
if (! $usd_to_tmt || ! property_exists($fields, 'payment_amount') || ! property_exists($fields, 'usd_payment')) {
return ActionResponse::danger('Walýuta hasaby girizilmedik, operator bilen habarlaşmagyňyzy haýyş edýärin.');
}
$today = today();
$resource = $models->first();
$payment_amount = floatval($fields->payment_amount);
$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 ActionResponse::danger('Şahamça sber tölegi kabul edip bilmeýär.');
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;
@@ -47,9 +76,16 @@ class MakeSberPaymentAction extends Action
$payment = $this->order($resource, $total_amount);
return $payment['status'] === 'success'
? ActionResponse::openInNewTab($payment['url'])
: ActionResponse::danger('Töleg ýerinde näsazlyk!');
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']);
}
/**
@@ -102,6 +138,17 @@ class MakeSberPaymentAction extends Action
}
}),
Hidden::make('usd_payment')
->dependsOn('payment_amount', function ($field, $request, $formData) use ($usd_to_rub, $rub_to_tmt) {
if (property_exists($formData, 'payment_amount')) {
$usdValue = number_format($formData->payment_amount / ($usd_to_rub * $rub_to_tmt), 2, '.', '');
$field->setValue($usdValue);
} else {
$field->setValue('');
}
}),
Text::make(__('Jemi (TMT)'), 'total_amount')
->fullWidth()
->readonly()
@@ -149,10 +196,13 @@ class MakeSberPaymentAction extends Action
return [
'status' => 'failed',
'url' => '',
'order_id' => '',
'order_number' => '',
'online_payment_history_id' => '',
];
}
OnlinePaymentHistory::create([
$onlinePaymentHistory = OnlinePaymentHistory::create([
'online_paymantable_id' => $resource->id,
'online_paymantable_type' => SberPaymentOrder::class,
'amount' => number_format($amount, 2, '', ''),
@@ -170,6 +220,63 @@ class MakeSberPaymentAction extends Action
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;
}
}