From f1cbc52e82f269d97132172d1156028ea77b15c4 Mon Sep 17 00:00:00 2001 From: Matt75 <5262628+Matt75@users.noreply.github.com> Date: Tue, 1 Aug 2023 09:29:07 +0200 Subject: [PATCH] Avoid fatal error on payment due to OS mapping --- .../CreateOrderCommandHandler.php | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Order/CommandHandler/CreateOrderCommandHandler.php b/src/Order/CommandHandler/CreateOrderCommandHandler.php index efa930e00..4a7e7921d 100644 --- a/src/Order/CommandHandler/CreateOrderCommandHandler.php +++ b/src/Order/CommandHandler/CreateOrderCommandHandler.php @@ -34,7 +34,9 @@ use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderException; use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderNotFoundException; use PrestaShop\Module\PrestashopCheckout\Order\Service\CheckOrderAmount; +use PrestaShop\Module\PrestashopCheckout\Order\State\Exception\OrderStateException; use PrestaShop\Module\PrestashopCheckout\Order\State\OrderStateConfigurationKeys; +use PrestaShop\Module\PrestashopCheckout\Order\State\OrderStateInstaller; use PrestaShop\Module\PrestashopCheckout\Order\State\Service\OrderStateMapper; use PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository; use PrestaShopCollection; @@ -143,18 +145,25 @@ public function handle(CreateOrderCommand $command) $currencyId = Currency::getIdByIsoCode($capture['amount']['currency_code'], (int) $cart->id_shop); } - if ($paidAmount) { - switch ($this->checkOrderAmount->checkAmount((string) $paidAmount, (string) $cart->getOrderTotal(true, \Cart::BOTH))) { - case CheckOrderAmount::ORDER_NOT_FULL_PAID: - $orderStateId = $this->psOrderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_PAID); - break; - case CheckOrderAmount::ORDER_FULL_PAID: - $orderStateId = $this->psOrderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_COMPLETED); - break; - case CheckOrderAmount::ORDER_TO_MUCH_PAID: - $orderStateId = $this->psOrderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_COMPLETED); + try { + if ($paidAmount) { + switch ($this->checkOrderAmount->checkAmount((string) $paidAmount, (string) $cart->getOrderTotal(true, \Cart::BOTH))) { + case CheckOrderAmount::ORDER_NOT_FULL_PAID: + $orderStateId = $this->psOrderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_PAID); + break; + case CheckOrderAmount::ORDER_FULL_PAID: + $orderStateId = $this->psOrderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_COMPLETED); + break; + case CheckOrderAmount::ORDER_TO_MUCH_PAID: + $orderStateId = $this->psOrderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_COMPLETED); + } + } else { + $orderStateId = $this->psOrderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PENDING); + } + } catch (OrderStateException $exception) { + if ($exception->getCode() === OrderStateException::INVALID_MAPPING) { + (new OrderStateInstaller())->install(); } - } else { $orderStateId = $this->psOrderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PENDING); }