72 lines
1.9 KiB
PHP
72 lines
1.9 KiB
PHP
<?php
|
||
|
||
namespace App\Modules\CardOrder\Nova\Actions;
|
||
|
||
use App\Repos\Order\OrderRepo;
|
||
use App\Repos\Payment\OnlinePaymentRepo;
|
||
use Illuminate\Bus\Queueable;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Queue\InteractsWithQueue;
|
||
use Illuminate\Support\Collection;
|
||
use Laravel\Nova\Actions\Action;
|
||
use Laravel\Nova\Actions\ActionResponse;
|
||
use Laravel\Nova\Fields\ActionFields;
|
||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||
|
||
class RetryNovaCardOrderPayment extends Action
|
||
{
|
||
use InteractsWithQueue, Queueable;
|
||
|
||
/**
|
||
* Name.
|
||
*/
|
||
public function name(): string
|
||
{
|
||
return __('Retry payment');
|
||
}
|
||
|
||
/**
|
||
* Determine if the action is executable for the given request.
|
||
*
|
||
* @param \Illuminate\Http\Request $request
|
||
* @param \Illuminate\Database\Eloquent\Model $model
|
||
* @return bool
|
||
*/
|
||
public function authorizedToRun(Request $request, $model)
|
||
{
|
||
/** @var \App\Models\Order\Card\CardOrder */
|
||
$cardOrder = $model;
|
||
$this->authorizedToRunAction = ! $cardOrder->paid && $cardOrder->status === OrderRepo::PENDING;
|
||
|
||
return $this->authorizedToRunAction;
|
||
}
|
||
|
||
/**
|
||
* Perform the action on the given models.
|
||
*
|
||
* @param ActionFields $fields
|
||
* @param Collection<array-key, \Illuminate\Database\Eloquent\Model> $models
|
||
* @return mixed
|
||
*/
|
||
public function handle(ActionFields $fields, Collection $models): mixed
|
||
{
|
||
$resource = $models->first();
|
||
|
||
$payment = (new OnlinePaymentRepo)->payCardOrder($resource);
|
||
|
||
return $payment['status'] === 'success'
|
||
? ActionResponse::openInNewTab($payment['url'])
|
||
: ActionResponse::danger('Тöleg sistemada registrasiýa bolmady!');
|
||
}
|
||
|
||
/**
|
||
* Get the fields available on the action.
|
||
*
|
||
* @return array<int, \Laravel\Nova\Fields\Field>
|
||
*/
|
||
public function fields(NovaRequest $request): array
|
||
{
|
||
return [];
|
||
}
|
||
}
|