From d805f26640f65c71c2b3bded07d73262b3d5ecbf Mon Sep 17 00:00:00 2001 From: jevgenijvisockij Date: Wed, 3 Apr 2024 09:03:13 +0300 Subject: [PATCH] Add exception handling for incorrect family name A new exception type WRONG_FAMILY_NAME has been added to highlight cases when the family name provided in the billing address is incorrect. This exception is triggered from the OrderExceptionHandler. Additionally, fixed the error handling in MollieOrderCreationService to always respond with proper HTTP status codes. --- src/Exception/OrderCreationException.php | 2 ++ src/Handler/Exception/OrderExceptionHandler.php | 2 ++ src/Service/MollieOrderCreationService.php | 5 +++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Exception/OrderCreationException.php b/src/Exception/OrderCreationException.php index 56740ed16..977a8e7c0 100644 --- a/src/Exception/OrderCreationException.php +++ b/src/Exception/OrderCreationException.php @@ -31,4 +31,6 @@ class OrderCreationException extends \Exception const ORDER_RESOURSE_IS_MISSING = 6; const ORDER_IS_NOT_CREATED = 7; + + const WRONG_FAMILY_NAME = 8; } diff --git a/src/Handler/Exception/OrderExceptionHandler.php b/src/Handler/Exception/OrderExceptionHandler.php index eb78838fb..0443486b3 100644 --- a/src/Handler/Exception/OrderExceptionHandler.php +++ b/src/Handler/Exception/OrderExceptionHandler.php @@ -30,6 +30,8 @@ public function handle(Exception $e) return new OrderCreationException($e->getMessage(), OrderCreationException::WRONG_BILLING_PHONE_NUMBER_EXCEPTION); } elseif (strpos($e->getMessage(), 'shippingAddress.phone')) { return new OrderCreationException($e->getMessage(), OrderCreationException::WRONG_SHIPPING_PHONE_NUMBER_EXCEPTION); + } elseif (strpos($e->getMessage(), 'billingAddress.familyName')) { + return new OrderCreationException($e->getMessage(), OrderCreationException::WRONG_FAMILY_NAME); } elseif (strpos($e->getMessage(), 'payment.amount')) { if (strpos($e->getMessage(), 'minimum')) { throw new OrderCreationException($e->getMessage(), OrderCreationException::ORDER_TOTAL_LOWER_THAN_MINIMUM); diff --git a/src/Service/MollieOrderCreationService.php b/src/Service/MollieOrderCreationService.php index 092ccb278..89fae9cf1 100644 --- a/src/Service/MollieOrderCreationService.php +++ b/src/Service/MollieOrderCreationService.php @@ -21,6 +21,7 @@ use Mollie\Config\Config; use Mollie\DTO\OrderData; use Mollie\DTO\PaymentData; +use Mollie\Errors\Http\HttpStatusCode; use Mollie\Exception\OrderCreationException; use Mollie\Handler\ErrorHandler\ErrorHandler; use Mollie\Handler\Exception\OrderExceptionHandler; @@ -66,10 +67,10 @@ public function createMollieOrder($data, MolPaymentMethod $paymentMethodObj) $apiPayment = $this->createPayment($data, $paymentMethodObj->method); } catch (OrderCreationException $e) { $errorHandler = ErrorHandler::getInstance(); - $errorHandler->handle($e, $e->getCode(), true); + $errorHandler->handle($e, HttpStatusCode::HTTP_BAD_REQUEST, true); } catch (Exception $e) { $errorHandler = ErrorHandler::getInstance(); - $errorHandler->handle($e, $e->getCode(), true); + $errorHandler->handle($e, HttpStatusCode::HTTP_INTERNAL_SERVER_ERROR, true); } }