diff --git a/Block/Form/Pointofsale.php b/Block/Form/Pointofsale.php index cd1c7ab69d6..f65a1649596 100644 --- a/Block/Form/Pointofsale.php +++ b/Block/Form/Pointofsale.php @@ -9,6 +9,7 @@ use Magento\Framework\View\Element\Template\Context; use Magento\Payment\Block\Form; use Mollie\Api\Resources\Terminal; +use Mollie\Payment\Logger\MollieLogger; use Mollie\Payment\Service\Mollie\MollieApiClient; /** @@ -26,15 +27,21 @@ class Pointofsale extends Form * @var MollieApiClient */ private $mollieApiClient; + /** + * @var MollieLogger + */ + private $logger; public function __construct( Context $context, MollieApiClient $mollieApiClient, + MollieLogger $logger, array $data = [] ) { parent::__construct($context, $data); $this->mollieApiClient = $mollieApiClient; + $this->logger = $logger; } /** @@ -47,14 +54,18 @@ public function __construct( * } * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException - * @throws \Mollie\Api\Exceptions\ApiException */ public function getTerminals(): array { $storeId = $this->_storeManager->getStore()->getId(); - $mollieApiClient = $this->mollieApiClient->loadByStore((int)$storeId); - $terminals = $mollieApiClient->terminals->page(); + try { + $mollieApiClient = $this->mollieApiClient->loadByStore((int)$storeId); + $terminals = $mollieApiClient->terminals->page(); + } catch (\Mollie\Api\Exceptions\ApiException $exception) { + $this->logger->addErrorLog('terminals', $exception->getMessage()); + return []; + } $output = []; /** @var Terminal $terminal */ diff --git a/Controller/Checkout/Webhook.php b/Controller/Checkout/Webhook.php index d18a67e2d7d..12237d60e82 100644 --- a/Controller/Checkout/Webhook.php +++ b/Controller/Checkout/Webhook.php @@ -16,6 +16,7 @@ use Magento\Sales\Api\OrderRepositoryInterface; use Mollie\Payment\Helper\General as MollieHelper; use Mollie\Payment\Model\Mollie as MollieModel; +use Mollie\Payment\Service\OrderLockService; /** * Class Webhook @@ -50,24 +51,19 @@ class Webhook extends Action * @var EncryptorInterface */ private $encryptor; - /** - * Webhook constructor. - * - * @param Context $context - * @param Session $checkoutSession - * @param MollieModel $mollieModel - * @param MollieHelper $mollieHelper - * @param OrderRepositoryInterface $orderRepository - * @param EncryptorInterface $encryptor + * @var OrderLockService */ + private $orderLockService; + public function __construct( Context $context, Session $checkoutSession, MollieModel $mollieModel, MollieHelper $mollieHelper, OrderRepositoryInterface $orderRepository, - EncryptorInterface $encryptor + EncryptorInterface $encryptor, + OrderLockService $orderLockService ) { $this->checkoutSession = $checkoutSession; $this->resultFactory = $context->getResultFactory(); @@ -75,6 +71,7 @@ public function __construct( $this->mollieHelper = $mollieHelper; $this->orderRepository = $orderRepository; $this->encryptor = $encryptor; + $this->orderLockService = $orderLockService; parent::__construct($context); } @@ -99,6 +96,14 @@ public function execute() } foreach ($orders as $order) { + // If this returns true, it means that the order is just created but did go straight to "paid". + // That can happen for Apple Pay and Credit Card. In that case, Mollie immediately sends a webhook, + // but we are not ready to process it yet. + if ($this->orderLockService->isLocked($order)) { + $this->mollieHelper->addTolog('info', 'Order is locked, skipping webhook'); + continue; + } + $order->setMollieTransactionId($transactionId); $this->mollieModel->processTransactionForOrder($order, 'webhook'); } diff --git a/Service/OrderLockService.php b/Service/OrderLockService.php index 9325aa9eac7..a414273dc78 100644 --- a/Service/OrderLockService.php +++ b/Service/OrderLockService.php @@ -45,7 +45,7 @@ public function __construct( public function execute(OrderInterface $order, callable $callback) { - $key = 'mollie.order.' . $order->getEntityId(); + $key = $this->getKeyName($order); if ($this->lockService->checkIfIsLockedWithWait($key)) { throw new LocalizedException(__('Unable to get lock for %1', $key)); } @@ -85,4 +85,16 @@ public function execute(OrderInterface $order, callable $callback) return $result; } + + public function isLocked(OrderInterface $order): bool + { + $key = $this->getKeyName($order); + + return $this->lockService->isLocked($key); + } + + private function getKeyName(OrderInterface $order): string + { + return 'mollie.order.' . $order->getEntityId(); + } } diff --git a/composer.json b/composer.json index a660dee2754..c905cafbe81 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "mollie/magento2", "description": "Mollie Payment Module for Magento 2", - "version": "2.30.0", + "version": "2.30.1", "keywords": [ "mollie", "payment", diff --git a/etc/config.xml b/etc/config.xml index 74e52e8e561..6176ed6d23e 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -3,7 +3,7 @@ - v2.30.0 + v2.30.1 0 0 test diff --git a/view/frontend/web/template/payment/giftcard.html b/view/frontend/web/template/payment/giftcard.html index c9103c960e7..f3f395a5086 100644 --- a/view/frontend/web/template/payment/giftcard.html +++ b/view/frontend/web/template/payment/giftcard.html @@ -20,7 +20,7 @@
- +