105 lines
2.3 KiB
PHP
105 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\RestaurantDelivery\Exceptions;
|
|
|
|
use Exception;
|
|
|
|
class PaymentException extends Exception
|
|
{
|
|
protected string $errorCode;
|
|
|
|
public function __construct(
|
|
string $message,
|
|
string $errorCode = 'PAYMENT_ERROR',
|
|
int $code = 400,
|
|
?\Throwable $previous = null
|
|
) {
|
|
$this->errorCode = $errorCode;
|
|
parent::__construct($message, $code, $previous);
|
|
}
|
|
|
|
/**
|
|
* Get error code.
|
|
*/
|
|
public function getErrorCode(): string
|
|
{
|
|
return $this->errorCode;
|
|
}
|
|
|
|
/**
|
|
* Render the exception as JSON response.
|
|
*/
|
|
public function render(): \Illuminate\Http\JsonResponse
|
|
{
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => $this->getMessage(),
|
|
'error_code' => $this->errorCode,
|
|
], $this->getCode());
|
|
}
|
|
|
|
/**
|
|
* Create exception for payment failed.
|
|
*/
|
|
public static function failed(string $reason = ''): self
|
|
{
|
|
$message = 'Payment processing failed';
|
|
if ($reason) {
|
|
$message .= ": {$reason}";
|
|
}
|
|
|
|
return new self($message, 'PAYMENT_FAILED', 400);
|
|
}
|
|
|
|
/**
|
|
* Create exception for invalid amount.
|
|
*/
|
|
public static function invalidAmount(): self
|
|
{
|
|
return new self(
|
|
'Invalid payment amount',
|
|
'INVALID_PAYMENT_AMOUNT',
|
|
400
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create exception for tip already paid.
|
|
*/
|
|
public static function tipAlreadyPaid(): self
|
|
{
|
|
return new self(
|
|
'This tip has already been paid',
|
|
'TIP_ALREADY_PAID',
|
|
400
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create exception for payout failed.
|
|
*/
|
|
public static function payoutFailed(string $reason = ''): self
|
|
{
|
|
$message = 'Payout processing failed';
|
|
if ($reason) {
|
|
$message .= ": {$reason}";
|
|
}
|
|
|
|
return new self($message, 'PAYOUT_FAILED', 400);
|
|
}
|
|
|
|
/**
|
|
* Create exception for insufficient balance.
|
|
*/
|
|
public static function insufficientBalance(): self
|
|
{
|
|
return new self(
|
|
'Insufficient balance for payout',
|
|
'INSUFFICIENT_BALANCE',
|
|
400
|
|
);
|
|
}
|
|
}
|