From 1a8a20444c583078a739236bf4b7dba69d361831 Mon Sep 17 00:00:00 2001 From: Arthur Moreira Date: Thu, 16 Nov 2023 23:39:13 +0100 Subject: [PATCH 1/6] Remove unexistant order canceled webhook --- Controller/Webhooks/Index.php | 3 +-- Observer/Config/Save.php | 8 +------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/Controller/Webhooks/Index.php b/Controller/Webhooks/Index.php index ecb17db..c99e21a 100644 --- a/Controller/Webhooks/Index.php +++ b/Controller/Webhooks/Index.php @@ -77,7 +77,7 @@ public function execute() try { $content = $this->getRequest()->getContent(); - + $headers = $this->getRequest()->getHeaders()->toArray(); $signature = hash_hmac('sha256', $content, $this->_monduConfig->getWebhookSecret()); if ($signature !== ($headers['X-Mondu-Signature'] ?? null)) { @@ -94,7 +94,6 @@ public function execute() case 'order/pending': [$resBody, $resStatus] = $this->handlePending($params); break; - case 'order/canceled': case 'order/declined': [$resBody, $resStatus] = $this->handleDeclinedOrCanceled($params); break; diff --git a/Observer/Config/Save.php b/Observer/Config/Save.php index 6bc6338..483c168 100644 --- a/Observer/Config/Save.php +++ b/Observer/Config/Save.php @@ -31,8 +31,7 @@ class Save implements ObserverInterface private $subscriptions = [ 'order/confirmed', 'order/declined', - 'order/pending', - 'order/canceled' + 'order/pending' ]; /** @@ -94,11 +93,6 @@ public function execute(Observer $observer) ->setTopic('order/declined') ->process(); - $this->requestFactory - ->create(RequestFactory::WEBHOOKS_REQUEST_METHOD) - ->setTopic('order/canceled') - ->process(); - $this->monduConfig->clearConfigurationCache(); } catch (\Exception $e) { throw new LocalizedException(__($e->getMessage())); From e7bbfb9bb6c194bdc4f00c73b201dc6a3d81769e Mon Sep 17 00:00:00 2001 From: Arthur Moreira Date: Thu, 16 Nov 2023 23:40:31 +0100 Subject: [PATCH 2/6] Update docker compose --- docker-compose.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 8c1cc7e..14fde9a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,17 +7,19 @@ services: ports: - 80:8080 environment: + - BITNAMI_DEBUG=true - MAGENTO_HOST=localhost - MAGENTO_MODE=developer - MAGENTO_DATABASE_HOST=mariadb - MAGENTO_DATABASE_PORT_NUMBER=3306 - MAGENTO_DATABASE_USER=bn_magento - MAGENTO_DATABASE_NAME=bitnami_magento + - MAGENTO_ELASTICSEARCH_HOST=elasticsearch + - MAGENTO_ELASTICSEARCH_PORT_NUMBER=9200 - MAGENTO_USERNAME=mondu - - MAGENTO_PASSWORD=mondu + - MAGENTO_PASSWORD=mondu123 + - MAGENTO_EMAIL=mondu@mondu.ai - ALLOW_EMPTY_PASSWORD=yes - - ELASTICSEARCH_HOST=elasticsearch - - ELASTICSEARCH_PORT_NUMBER=9200 - PHP_MEMORY_LIMIT=5120M volumes: - magento_data:/bitnami/magento From f764ba094181f2eabeee0bb6b4c4c59053c374ea Mon Sep 17 00:00:00 2001 From: Tigran Hovhannisyan Date: Mon, 27 Nov 2023 01:28:20 +0400 Subject: [PATCH 3/6] Change Magento order status to "Payment Review" if the order is in the pending state. --- Controller/Webhooks/Index.php | 5 ++++- Model/Ui/ConfigProvider.php | 2 +- Observer/CreateOrder.php | 17 ++++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Controller/Webhooks/Index.php b/Controller/Webhooks/Index.php index c99e21a..6299bc9 100644 --- a/Controller/Webhooks/Index.php +++ b/Controller/Webhooks/Index.php @@ -134,7 +134,8 @@ public function handlePending($params): array if (empty($order->getData())) { return [['message' => 'Order does not exist', 'error' => 0], 200]; } - + $order->setStatus(Order::STATE_PAYMENT_REVIEW); + $order->save(); $this->_monduLogger->updateLogMonduData($monduId, $params['order_state']); return [['message' => 'ok', 'error' => 0], 200]; @@ -162,6 +163,8 @@ public function handleConfirmed($params): array return [['message' => 'Order does not exist', 'error' => 0], 200]; } + $order->setStatus(Order::STATE_PROCESSING); + $order->save(); $this->_monduLogger->updateLogMonduData($monduId, $params['order_state'], $viban); return [['message' => 'ok', 'error' => 0], 200]; diff --git a/Model/Ui/ConfigProvider.php b/Model/Ui/ConfigProvider.php index 4bbc379..84dea40 100644 --- a/Model/Ui/ConfigProvider.php +++ b/Model/Ui/ConfigProvider.php @@ -101,7 +101,7 @@ public function getApiUrl($path = null): string return $baseUrl . ($path ? '/'.$path : ''); } - /** + /** * Returns mondu.js url * * @return string diff --git a/Observer/CreateOrder.php b/Observer/CreateOrder.php index fc3816b..62cd0c5 100644 --- a/Observer/CreateOrder.php +++ b/Observer/CreateOrder.php @@ -150,7 +150,7 @@ public function _execute(Observer $observer) $order->setData('mondu_reference_id', $orderUid); $order->addStatusHistoryComment(__('Mondu: order id %1', $orderData['uuid'])); - + $order = $this->assignMagentoStatus($order, $orderData['state']); $order->save(); $this->monduFileLogger->info('Saved the order in Magento ', ['orderNumber' => $order->getIncrementId()]); @@ -184,4 +184,19 @@ protected function confirmAuthorizedOrder($orderData, $orderNumber) } return $orderData; } + + /** + * AssignMagentoStatus + * + * @param Order $order + * @param string $monduOrderState + * @return Order + */ + protected function assignMagentoStatus($order, $monduOrderState) + { + if ($monduOrderState === 'pending') { + $order->setStatus(Order::STATE_PAYMENT_REVIEW); + } + return $order; + } } From e9cf02ea1b3c1a7c2ac6d8c00a558292259c7aa4 Mon Sep 17 00:00:00 2001 From: Tigran Hovhannisyan Date: Mon, 27 Nov 2023 01:29:01 +0400 Subject: [PATCH 4/6] Bumped magento version. --- composer.json | 2 +- etc/module.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b45d676..681fe5f 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "mondu_gmbh/magento2-payment", "description": "Mondu payment method for magento 2", "type": "magento2-module", - "version": "2.2.1", + "version": "2.2.2", "license": [ "MIT" ], diff --git a/etc/module.xml b/etc/module.xml index ef8682a..bf5d2e7 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + From b9d515e5e10a3e194975ad0405ce5e9b3c2c43d5 Mon Sep 17 00:00:00 2001 From: Tigran Hovhannisyan Date: Mon, 27 Nov 2023 17:46:18 +0400 Subject: [PATCH 5/6] Moved Order state logic to a new observer. --- Observer/AfterPlaceOrder.php | 53 ++++++++++++++++++++++++++++++++++++ Observer/CreateOrder.php | 16 ----------- etc/events.xml | 3 ++ 3 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 Observer/AfterPlaceOrder.php diff --git a/Observer/AfterPlaceOrder.php b/Observer/AfterPlaceOrder.php new file mode 100644 index 0000000..48b803e --- /dev/null +++ b/Observer/AfterPlaceOrder.php @@ -0,0 +1,53 @@ +monduLogger = $monduLogger; + } + + /** + * Execute + * + * @param Observer $observer + * @return void + */ + public function _execute(Observer $observer) + { + $order = $observer->getEvent()->getOrder(); + $monduUuid = $order->getMonduReferenceId(); + $orderData = $this->monduLogger->getTransactionByOrderUid($monduUuid); + + if (isset($orderData['mondu_state']) && $orderData['mondu_state'] === 'pending') { + $order->addStatusHistoryComment( + __('Mondu: Order Status changed to Payment Review because it needs manual confirmation') + ); + $order->setState(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW); + $order->setStatus(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW); + $order->save(); + } + } +} diff --git a/Observer/CreateOrder.php b/Observer/CreateOrder.php index 62cd0c5..c4de23d 100644 --- a/Observer/CreateOrder.php +++ b/Observer/CreateOrder.php @@ -150,7 +150,6 @@ public function _execute(Observer $observer) $order->setData('mondu_reference_id', $orderUid); $order->addStatusHistoryComment(__('Mondu: order id %1', $orderData['uuid'])); - $order = $this->assignMagentoStatus($order, $orderData['state']); $order->save(); $this->monduFileLogger->info('Saved the order in Magento ', ['orderNumber' => $order->getIncrementId()]); @@ -184,19 +183,4 @@ protected function confirmAuthorizedOrder($orderData, $orderNumber) } return $orderData; } - - /** - * AssignMagentoStatus - * - * @param Order $order - * @param string $monduOrderState - * @return Order - */ - protected function assignMagentoStatus($order, $monduOrderState) - { - if ($monduOrderState === 'pending') { - $order->setStatus(Order::STATE_PAYMENT_REVIEW); - } - return $order; - } } diff --git a/etc/events.xml b/etc/events.xml index 72d89f6..08d53b7 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -5,6 +5,9 @@ + + + Date: Mon, 27 Nov 2023 18:11:48 +0400 Subject: [PATCH 6/6] Minor change. --- Controller/Webhooks/Index.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Controller/Webhooks/Index.php b/Controller/Webhooks/Index.php index 6299bc9..6f97956 100644 --- a/Controller/Webhooks/Index.php +++ b/Controller/Webhooks/Index.php @@ -134,7 +134,11 @@ public function handlePending($params): array if (empty($order->getData())) { return [['message' => 'Order does not exist', 'error' => 0], 200]; } + $order->setState(Order::STATE_PAYMENT_REVIEW); $order->setStatus(Order::STATE_PAYMENT_REVIEW); + $order->addStatusHistoryComment( + __('Mondu: Order Status changed to Payment Review by a webhook') + ); $order->save(); $this->_monduLogger->updateLogMonduData($monduId, $params['order_state']); @@ -163,7 +167,11 @@ public function handleConfirmed($params): array return [['message' => 'Order does not exist', 'error' => 0], 200]; } + $order->setState(Order::STATE_PROCESSING); $order->setStatus(Order::STATE_PROCESSING); + $order->addStatusHistoryComment( + __('Mondu: Order Status changed to Processing by a webhook') + ); $order->save(); $this->_monduLogger->updateLogMonduData($monduId, $params['order_state'], $viban); @@ -192,6 +200,10 @@ public function handleDeclinedOrCanceled($params): array return [['message' => 'Order does not exist', 'error' => 0], 200]; } + $order->addStatusHistoryComment( + __('Mondu: Order has been declined') + ); + if ($orderState === 'canceled') { $order->setStatus(Order::STATE_CANCELED)->save(); } elseif ($orderState === 'declined') {