From 3113a3d4c0e50f1b71c20315770c9a2b2dd582de Mon Sep 17 00:00:00 2001 From: Andrii Chaikovskyi Date: Sun, 8 Sep 2024 22:38:15 +0300 Subject: [PATCH] Fixed potential multiple purchase webhook events. --- CHANGELOG.md | 4 ++++ Observer/OrderSaveCommitAfter.php | 17 +++++++++++++++++ composer.json | 2 +- etc/module.xml | 2 +- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11b5505..3595fa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # Changelog + +v1.0.18 +- Fixed potential issue with sending multiple purchase webhook events. + v1.0.17 - Hyva theme compatibility added. diff --git a/Observer/OrderSaveCommitAfter.php b/Observer/OrderSaveCommitAfter.php index 798fc51..dff9a2b 100644 --- a/Observer/OrderSaveCommitAfter.php +++ b/Observer/OrderSaveCommitAfter.php @@ -5,6 +5,7 @@ use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Stdlib\CookieManagerInterface; +use \Magento\Sales\Api\OrderPaymentRepositoryInterface; use Psr\Log\LoggerInterface; use Stape\Gtm\Model\ConfigProvider; use Stape\Gtm\Model\Webhook\Adapter; @@ -26,6 +27,11 @@ class OrderSaveCommitAfter implements ObserverInterface */ private $cookieManager; + /** + * @var OrderPaymentRepositoryInterface $orderPaymentRepository + */ + private $orderPaymentRepository; + /** * @var LoggerInterface $logger */ @@ -46,17 +52,20 @@ class OrderSaveCommitAfter implements ObserverInterface * @param ConfigProvider $configProvider * @param Adapter $adapter * @param CookieManagerInterface $cookieManager + * @param OrderPaymentRepositoryInterface $orderPaymentRepository * @param LoggerInterface $logger */ public function __construct( ConfigProvider $configProvider, Adapter $adapter, CookieManagerInterface $cookieManager, + OrderPaymentRepositoryInterface $orderPaymentRepository, LoggerInterface $logger ) { $this->configProvider = $configProvider; $this->adapter = $adapter; $this->cookieManager = $cookieManager; + $this->orderPaymentRepository = $orderPaymentRepository; $this->logger = $logger; } @@ -98,8 +107,16 @@ public function execute(Observer $observer) return; } + $payment = $order->getPayment(); + + if ($payment->getAdditionalInformation('stape_purchase_webhook_processed')) { + return; + } + try { $this->adapter->purchase($order, $this->prepareAdditionalInfo()); + $payment->setAdditionalInformation('stape_purchase_webhook_processed', true); + $this->orderPaymentRepository->save($payment); } catch (\Exception $e) { $this->logger->notice($e->getMessage()); } diff --git a/composer.json b/composer.json index c8d05bc..1a7ca53 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": [ "GPL-3.0-only" ], - "version": "1.0.17", + "version": "1.0.18", "require": { "php": ">=7.4.0" }, diff --git a/etc/module.xml b/etc/module.xml index ebbc509..5553851 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,7 +1,7 @@ - +