From efbbc24f8d862074d744dd129c1cba3f25470f77 Mon Sep 17 00:00:00 2001 From: Ebano Lopes Date: Mon, 13 Apr 2020 01:35:38 +0000 Subject: [PATCH 1/7] MAG-543: Do not process cases/creation on webhooks --- Controller/Webhooks/Index.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Controller/Webhooks/Index.php b/Controller/Webhooks/Index.php index 4d710504..0f2ef1ac 100644 --- a/Controller/Webhooks/Index.php +++ b/Controller/Webhooks/Index.php @@ -124,10 +124,20 @@ public function processRequest($request, $hash, $topic) $requestJson = json_decode($request); if (json_last_error() == JSON_ERROR_NONE) { - // Test is only verifying that the endpoint is reachable. So we just complete here - if ($topic === 'cases/test') { - $this->getResponse()->setStatusCode(Http::STATUS_CODE_200); - return; + switch (($topic)) { + case 'cases/test': + // Test is only verifying that the endpoint is reachable. So we just complete here + $this->getResponse()->setStatusCode(Http::STATUS_CODE_200); + return; + break; + + case 'cases/creation': + $message = 'Case creation will not be processed by Magento'; + $this->getResponse()->appendBody($message); + $this->logger->debug("API: {$message}"); + $this->getResponse()->setStatusCode(Http::STATUS_CODE_200); + return; + break; } /** @var $order \Magento\Sales\Model\Order */ From 1242de8bb622dd5343097437b973979603ee6c57 Mon Sep 17 00:00:00 2001 From: Ebano Lopes Date: Mon, 13 Apr 2020 02:22:14 +0000 Subject: [PATCH 2/7] MAG-539: Fix to avoid issues with track numbers on fulfillments --- Helper/FulfillmentHelper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Helper/FulfillmentHelper.php b/Helper/FulfillmentHelper.php index 7319506d..ea4b9a47 100644 --- a/Helper/FulfillmentHelper.php +++ b/Helper/FulfillmentHelper.php @@ -253,8 +253,8 @@ public function getTrackingNumbers(\Magento\Sales\Model\Order\Shipment $shipment foreach ($trackingCollection->getItems() as $tracking) { $number = trim($tracking->getNumber()); - if (empty($number) == false) { - $trackingNumbers[] = $tracking->getNumber(); + if (empty($number) === false && is_object($number) === false && is_array($number) === false) { + $trackingNumbers[] = $number; } } From 1ff5939612dbe1dcf3f4743c7bcb00c40bb15b4d Mon Sep 17 00:00:00 2001 From: Ebano Lopes Date: Sat, 16 May 2020 19:55:41 +0000 Subject: [PATCH 3/7] MAG-549: Fix to hold async payment methods on case creation --- Observer/Purchase.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Observer/Purchase.php b/Observer/Purchase.php index ae938dd9..d307fe35 100644 --- a/Observer/Purchase.php +++ b/Observer/Purchase.php @@ -198,6 +198,9 @@ public function execute(Observer $observer, $checkOwnEventsMethods = true) $case->save(); $message = 'Case for order:#' . $incrementId . ' was not sent because of an async payment method'; $this->logger->debug($message); + + // Initial hold order + $this->holdOrder($order); } catch (\Exception $ex) { $this->logger->error($ex->__toString()); } From 1a88ed8e17e3c93006e6a41e861e32ac94018f61 Mon Sep 17 00:00:00 2001 From: Ebano Lopes Date: Sun, 17 May 2020 00:56:54 +0000 Subject: [PATCH 4/7] MAG-539: Fix to avoid duplicated tracking numbers on fulfillments --- Helper/FulfillmentHelper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Helper/FulfillmentHelper.php b/Helper/FulfillmentHelper.php index ea4b9a47..287f3014 100644 --- a/Helper/FulfillmentHelper.php +++ b/Helper/FulfillmentHelper.php @@ -253,7 +253,8 @@ public function getTrackingNumbers(\Magento\Sales\Model\Order\Shipment $shipment foreach ($trackingCollection->getItems() as $tracking) { $number = trim($tracking->getNumber()); - if (empty($number) === false && is_object($number) === false && is_array($number) === false) { + if (empty($number) === false && is_object($number) === false && is_array($number) === false && + in_array($number, $trackingNumbers) == false) { $trackingNumbers[] = $number; } } From e64d557386fb208ebd60fb5a2e211235dcc54b3d Mon Sep 17 00:00:00 2001 From: Ebano Lopes Date: Wed, 1 Jul 2020 19:45:30 +0000 Subject: [PATCH 5/7] MAG-563: Change version number and minor MCS fixes --- Controller/Webhooks/Index.php | 2 -- composer.json | 2 +- etc/module.xml | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Controller/Webhooks/Index.php b/Controller/Webhooks/Index.php index 0f2ef1ac..d67f6343 100644 --- a/Controller/Webhooks/Index.php +++ b/Controller/Webhooks/Index.php @@ -129,7 +129,6 @@ public function processRequest($request, $hash, $topic) // Test is only verifying that the endpoint is reachable. So we just complete here $this->getResponse()->setStatusCode(Http::STATUS_CODE_200); return; - break; case 'cases/creation': $message = 'Case creation will not be processed by Magento'; @@ -137,7 +136,6 @@ public function processRequest($request, $hash, $topic) $this->logger->debug("API: {$message}"); $this->getResponse()->setStatusCode(Http::STATUS_CODE_200); return; - break; } /** @var $order \Magento\Sales\Model\Order */ diff --git a/composer.json b/composer.json index 237ab467..00933852 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "php": ">=5.5.22" }, "type": "magento2-module", - "version": "3.6.1", + "version": "3.6.2", "autoload": { "files": [ "registration.php" diff --git a/etc/module.xml b/etc/module.xml index b6fc617c..bf08f2e7 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -5,7 +5,7 @@ */ --> - + From 74fb33099a72f10d8b83c22097ab71b873fd7668 Mon Sep 17 00:00:00 2001 From: Ebano Lopes Date: Fri, 10 Jul 2020 11:14:13 +0000 Subject: [PATCH 6/7] MAG-566: Avoid save order on after_commit event This was causing issues on Commerce edition with gift cards when payment method was set to authorize and capture --- Helper/PurchaseHelper.php | 9 --- Observer/Order/Save/Before.php | 111 +++++++++++++++++++++++++++++++++ Observer/Purchase.php | 31 +-------- etc/events.xml | 4 ++ 4 files changed, 117 insertions(+), 38 deletions(-) create mode 100644 Observer/Order/Save/Before.php diff --git a/Helper/PurchaseHelper.php b/Helper/PurchaseHelper.php index 0d0bf072..68ff5092 100644 --- a/Helper/PurchaseHelper.php +++ b/Helper/PurchaseHelper.php @@ -249,15 +249,6 @@ protected function makeShipments(Order $order) return $shipments; } - public function isAdmin() - { - /** @var \Magento\Framework\ObjectManagerInterface $om */ - $om = \Magento\Framework\App\ObjectManager::getInstance(); - /** @var \Magento\Framework\App\State $state */ - $state = $om->get(\Magento\Framework\App\State::class); - return 'adminhtml' === $state->getAreaCode(); - } - /** * @param $mageAddress Address * @return SignifydAddress diff --git a/Observer/Order/Save/Before.php b/Observer/Order/Save/Before.php new file mode 100644 index 00000000..7214c839 --- /dev/null +++ b/Observer/Order/Save/Before.php @@ -0,0 +1,111 @@ +logger = $loger; + $this->appState = $appState; + $this->configHelper = $configHelper; + $this->storeManager = $storeManager; + $this->request = $request; + } + + /** + * @param Observer $observer + * @param bool $checkOwnEventsMethods + */ + public function execute(Observer $observer, $checkOwnEventsMethods = true) + { + try { + /** @var $order Order */ + $order = $observer->getEvent()->getOrder(); + + if (!is_object($order)) { + return; + } + + if ($this->configHelper->isEnabled($order) == false) { + return; + } + + // Saving store code to order, to know where the order is been created + if (empty($order->getData('origin_store_code')) && is_object($this->storeManager)) { + $isAdmin = ('adminhtml' === $this->appState->getAreaCode()); + $storeCode = $this->storeManager->getStore($isAdmin ? 'admin' : true)->getCode(); + + if (!empty($storeCode)) { + $order->setData('origin_store_code', $storeCode); + } + } + + // Fix for Magento bug https://github.com/magento/magento2/issues/7227 + // x_forwarded_for should be copied from quote, but quote does not have the field on database + if (empty($order->getData('x_forwarded_for')) && is_object($this->request)) { + $xForwardIp = $this->request->getServer('HTTP_X_FORWARDED_FOR'); + + if (empty($xForwardIp) == false) { + $order->setData('x_forwarded_for', $xForwardIp); + } + } + } catch (\Exception $ex) { + $context = []; + + if (isset($order) && $order instanceof Order) { + $context['entity'] = $order; + } + + $this->logger->error($ex->getMessage(), $context); + } + } +} diff --git a/Observer/Purchase.php b/Observer/Purchase.php index ae938dd9..65e82459 100644 --- a/Observer/Purchase.php +++ b/Observer/Purchase.php @@ -23,12 +23,12 @@ class Purchase implements ObserverInterface { /** - * @var \Signifyd\Connect\Logger\Logger; + * @var Logger; */ protected $logger; /** - * @var \Signifyd\Connect\Helper\PurchaseHelper + * @var PurchaseHelper */ protected $helper; @@ -126,33 +126,6 @@ public function execute(Observer $observer, $checkOwnEventsMethods = true) return; } - $saveOrder = false; - - // Saving store code to order, to know where the order is been created - if (empty($order->getData('origin_store_code')) && is_object($this->storeManager)) { - $storeCode = $this->storeManager->getStore($this->helper->isAdmin() ? 'admin' : true)->getCode(); - - if (!empty($storeCode)) { - $order->setData('origin_store_code', $storeCode); - $saveOrder = true; - } - } - - // Fix for Magento bug https://github.com/magento/magento2/issues/7227 - // x_forwarded_for should be copied from quote, but quote does not have the field on database - if (empty($order->getData('x_forwarded_for')) && is_object($this->request)) { - $xForwardIp = $this->request->getServer('HTTP_X_FORWARDED_FOR'); - - if (empty($xForwardIp) == false) { - $order->setData('x_forwarded_for', $xForwardIp); - $saveOrder = true; - } - } - - if ($saveOrder) { - $order->save(); - } - // Check if a payment is available for this order yet if ($order->getState() == \Magento\Sales\Model\Order::STATE_PENDING_PAYMENT) { return; diff --git a/etc/events.xml b/etc/events.xml index a4d8cbde..9296312e 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -8,6 +8,10 @@ + + + + From 7d87b76ec8a1b04ed489dd862fe249d387941270 Mon Sep 17 00:00:00 2001 From: Ebano Lopes Date: Fri, 10 Jul 2020 13:54:14 +0000 Subject: [PATCH 7/7] MAG-564: Workaround to work with Stripe official extension Stripe needs to be reinitialized for each order when running on background to make sure correct settings are in place --- Cron/RetryCaseJob.php | 97 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 7 deletions(-) diff --git a/Cron/RetryCaseJob.php b/Cron/RetryCaseJob.php index 0be979bb..37bfb97f 100644 --- a/Cron/RetryCaseJob.php +++ b/Cron/RetryCaseJob.php @@ -8,10 +8,14 @@ namespace Signifyd\Connect\Cron; use Magento\Framework\ObjectManagerInterface; +use Magento\Sales\Model\ResourceModel\Order as OrderResourceModel; +use Magento\Sales\Model\OrderFactory; use Signifyd\Connect\Logger\Logger; use Signifyd\Connect\Helper\PurchaseHelper; use Signifyd\Connect\Helper\Retry; use Signifyd\Connect\Model\Casedata; +use Signifyd\Connect\Model\ResourceModel\Casedata as CasedataResourceModel; +use Signifyd\Connect\Model\CasedataFactory; class RetryCaseJob { @@ -35,16 +39,60 @@ class RetryCaseJob */ protected $caseRetryObj; + /** + * @var OrderResourceModel + */ + protected $orderResourceModel; + + /** + * @var OrderFactory + */ + protected $orderFactory; + + /** + * @var CasedataResourceModel + */ + protected $casedataResourceModel; + + /** + * @var CasedataFactory\ + */ + protected $casedataFactory; + + /** + * @var \StripeIntegration\Payments\Model\Config + */ + protected $stripeConfig; + + /** + * RetryCaseJob constructor. + * @param ObjectManagerInterface $objectManager + * @param PurchaseHelper $helper + * @param Logger $logger + * @param Retry $caseRetryObj + * @param OrderResourceModel $orderResourceModel + * @param OrderFactory $orderFactory + * @param CasedataResourceModel $casedataResourceModel + * @param CasedataFactory $casedataFactory + */ public function __construct( ObjectManagerInterface $objectManager, PurchaseHelper $helper, Logger $logger, - Retry $caseRetryObj + Retry $caseRetryObj, + OrderResourceModel $orderResourceModel, + OrderFactory $orderFactory, + CasedataResourceModel $casedataResourceModel, + CasedataFactory $casedataFactory ) { $this->_objectManager = $objectManager; $this->_helper = $helper; $this->logger = $logger; $this->caseRetryObj = $caseRetryObj; + $this->orderResourceModel = $orderResourceModel; + $this->orderFactory = $orderFactory; + $this->casedataResourceModel = $casedataResourceModel; + $this->casedataFactory = $casedataFactory; } /** @@ -70,13 +118,15 @@ public function execute() $result = $this->_helper->postCaseToSignifyd($caseData, $order); if ($result) { - /** @var \Signifyd\Connect\Model\Casedata $caseObj */ - $caseObj = $this->_objectManager->create(\Signifyd\Connect\Model\Casedata::class) - ->load($case->getOrderIncrement()) - ->setCode($result) + /** @var Casedata $caseObj */ + $caseObj = $this->casedataFactory->create(); + $this->casedataResourceModel->load($caseObj, $case->getOrderIncrement()); + + $caseObj->setCode($result) ->setMagentoStatus(Casedata::IN_REVIEW_STATUS) ->setUpdated(strftime('%Y-%m-%d %H:%M:%S', time())); - $caseObj->save(); + + $this->casedataResourceModel->save($caseObj); } } @@ -109,8 +159,41 @@ public function execute() return $this; } + /** + * @param $incrementId + * @return \Magento\Sales\Model\Order + */ public function getOrder($incrementId) { - return $this->_objectManager->get(\Magento\Sales\Model\Order::class)->loadByIncrementId($incrementId); + $order = $this->orderFactory->create(); + $this->orderResourceModel->load($order, $incrementId, 'increment_id'); + + if ($order->getPayment()->getMethod() == 'stripe_payments') { + $this->reInitStripe($order); + } + + return $order; + } + + /** + * @param \Magento\Sales\Model\Order $order + * @return bool + */ + public function reInitStripe(\Magento\Sales\Model\Order $order) + { + if (class_exists(\StripeIntegration\Payments\Model\Config::class) === false) { + return false; + } + + if ($this->stripeConfig === null) { + $this->stripeConfig = $this->_objectManager->get(\StripeIntegration\Payments\Model\Config::class); + } + + if (version_compare(\StripeIntegration\Payments\Model\Config::$moduleVersion, '1.8.8') >= 0 && + method_exists($this->stripeConfig, 'reInitStripe')) { + $this->stripeConfig->reInitStripe($order->getStoreId(), $order->getBaseCurrencyCode(), null); + } + + return true; } }