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\VisaMasterPaymentOrder\Models\VisaMasterPaymentOrder;
|
|
use App\Modules\VisaMasterPaymentOrder\Models\VisaMasterPaymentOrderItem;
|
|
use App\Modules\VisaMasterSettings\Models\VisaMasterSettings;
|
|
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 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)
|
|
{
|
|
if (is_null($fields->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(<<<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(__('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(<<<HTML
|
|
<div class="w-full border text-left appearance-none rounded text-sm font-bold focus:outline-none focus:ring ring-primary-200 dark:ring-gray-600 relative inline-flex items-center justify-center shadow h-9 px-3 bg-primary-500 border-primary-500 text-white dark:text-gray-900">
|
|
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="stroke-current shrink-0 w-6 h-6 mr-2">
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
|
|
</svg>
|
|
<span>$payment_warning_text->value</span>
|
|
</div>
|
|
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;
|
|
}
|
|
}
|