Skip to content

Commit

Permalink
Fixed potential multiple purchase webhook events.
Browse files Browse the repository at this point in the history
  • Loading branch information
chaikovskyia committed Sep 8, 2024
1 parent 7f0c1d2 commit 3113a3d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog

v1.0.18
- Fixed potential issue with sending multiple purchase webhook events.

v1.0.17
- Hyva theme compatibility added.

Expand Down
17 changes: 17 additions & 0 deletions Observer/OrderSaveCommitAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,11 @@ class OrderSaveCommitAfter implements ObserverInterface
*/
private $cookieManager;

/**
* @var OrderPaymentRepositoryInterface $orderPaymentRepository
*/
private $orderPaymentRepository;

/**
* @var LoggerInterface $logger
*/
Expand All @@ -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;
}

Expand Down Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": [
"GPL-3.0-only"
],
"version": "1.0.17",
"version": "1.0.18",
"require": {
"php": ">=7.4.0"
},
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Stape_Gtm" schema_version="1.0.17" setup_version="1.0.17">
<module name="Stape_Gtm" schema_version="1.0.18" setup_version="1.0.18">
<sequence>
<module name="Magento_Backend" />
<module name="Magento_Catalog" />
Expand Down

0 comments on commit 3113a3d

Please sign in to comment.