50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Enums\PaymentStatusEnum;
|
|
use App\Models\Payment;
|
|
use App\Models\PaymentStatus;
|
|
use DB;
|
|
use Log;
|
|
use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
|
use Throwable;
|
|
|
|
final readonly class MarkPaymentAsPaidAction
|
|
{
|
|
public function __construct(private MarkCartAsConvertedAction $cartAsConvertedAction) {}
|
|
|
|
/**
|
|
* Execute the action.
|
|
*
|
|
* @throws NotFoundResourceException|Throwable
|
|
*/
|
|
public function execute(Payment $payment): bool
|
|
{
|
|
try {
|
|
|
|
DB::beginTransaction();
|
|
// get the cart and make the status to converted
|
|
$cart = $payment->order->cart;
|
|
$this->cartAsConvertedAction->execute($cart);
|
|
|
|
$status = PaymentStatus::whereName(PaymentStatusEnum::Paid->value)->value('id');
|
|
if (! $status) {
|
|
throw new NotFoundResourceException('Paid Status not found');
|
|
}
|
|
$payment->payment_status_id = $status;
|
|
|
|
$payment->save();
|
|
DB::commit();
|
|
|
|
return true;
|
|
|
|
} catch (Throwable $e) {
|
|
Log::error('Cannot mark order payment as paid', [$e->getMessage()]);
|
|
DB::rollBack();
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|