ekart/backend/app/Actions/Payment/MarkPaymentAsPaidAction.php
2026-03-25 17:17:35 +05:30

51 lines
1.3 KiB
PHP

<?php
namespace App\Actions\Payment;
use App\Actions\Cart\MarkCartAsConvertedAction;
use App\Enums\Payment\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;
}
}
}