Adyen / adyen-magento2

Adyen Payment plugin for Magento2
MIT License
155 stars 211 forks source link

[PW-5027] Adyen changes order status back to processing #1085

Closed WinstonN closed 3 years ago

WinstonN commented 3 years ago

Within our Magento website, we use different order statuses, across the order flow

The order could have a flow as below

  1. Order is created, and has a status of "Payment Review"
  2. Order is authorised at the gateway, and status changes to "Processing"
  3. Integrations picks up the order, and changes status to "Sent to warehouse"
  4. Order is picked by the warehouse, and order status changes to "Picked"
  5. Invoice is generated for the order and status changes to "Paid, ready for shipping" BUG
  6. Adyen pings back our site via webhook, and order status flips back to "Processing" with an order comment like
    Jun 24, 2021 9:40:07 AM Processing Customer Not Notified
    Captured amount of A$134.93 online. Transaction ID: "ABC123"
  7. Order is shipped and order status moves to "Complete"

Step 6 might occur at any stage where Adyen via a webhook, leaves a comment on the order

I believe the culprit is https://github.com/Adyen/adyen-magento2/blob/develop/Gateway/Response/PaymentCommentHistoryHandler.php#L68

and other places where getOrder()->addStatusHistoryComment($comment); is used without an order status

That method, addStatusHistoryComment has a status parameter which by default is false if not supplied

public function addStatusHistoryComment($comment, $status = false)
{
..
}

Looking at Magento test code, I can see

/** @var \Magento\Sales\Model\Order $order */
$order = $objectManager->get(\Magento\Sales\Model\Order::class)->loadByIncrementId('100000001');
$history = $order->addStatusHistoryComment($comment, $order->getStatus());
$history->save();

Would it be possible to incorporate the order status, when Adyen leaves comments on orders, or is this intentional?

Magento version All versions

Plugin version All versions

acampos1916 commented 3 years ago

Hello @WinstonN, thank you for this detailed report. It does make sense to use the current order status when calling addStatusHistoryComment, maybe in some places this was left out intentionally and in others is just an oversight. I've created an internal ticket to work on this ASAP.

WinstonN commented 3 years ago

hello @acampos1916 I had a quick look, and came up with this patch

If you have any comments, let me know. There still instances - with this diff in place - where the order status reverts to processing, in particular

image

These seem to be originating from vendor/adyen/module-payment/Model/Cron.php

in particular \Adyen\Payment\Logger\AdyenLogger::addAdyenNotificationCronjob

diff --git a/vendor/adyen/module-payment/Controller/Process/Redirect.php b/vendor/adyen/module-payment/Controller/Process/Redirect.php
--- a/vendor/adyen/module-payment/Controller/Process/Redirect.php
+++ b/vendor/adyen/module-payment/Controller/Process/Redirect.php
@@ -184,7 +184,7 @@ class Redirect extends \Magento\Framework\App\Action\Action

                 // check if authorise3d was successful
                 if ($responseCode == 'Authorised') {
-                    $order->addStatusHistoryComment(__('3D-secure validation was successful'))->save();
+                    $order->addStatusHistoryComment(__('3D-secure validation was successful'), $order->getStatus())->save();
                     // set back to false so when pressed back button on the success page
                     // it will reactivate 3D secure
                     $order->getPayment()->setAdditionalInformation('3dActive', '');
@@ -240,7 +240,8 @@ class Redirect extends \Magento\Framework\App\Action\Action
                         __(
                             '3D-secure validation was unsuccessful. This order will be cancelled when the related
                                 notification has been processed.'
-                        )
+                        ),
+                        $order->getStatus()
                     )->save();

                     $this->messageManager->addErrorMessage("3D-secure validation was unsuccessful");
@@ -280,7 +281,8 @@ class Redirect extends \Magento\Framework\App\Action\Action
                         The payment can be seen as unsuccessful.
                         <br />The order can be automatically cancelled based on the OFFER_CLOSED notification.
                         Please contact Adyen Support to enable this.'
-                    )
+                    ),
+                    $order->getStatus()
                 )->save();
                 $this->_view->loadLayout();
                 $this->_view->getLayout()->initMessages();
diff --git a/vendor/adyen/module-payment/Gateway/Response/CheckoutPaymentCommentHistoryHandler.php b/vendor/adyen/module-payment/Gateway/Response/CheckoutPaymentCommentHistoryHandler.php
--- a/vendor/adyen/module-payment/Gateway/Response/CheckoutPaymentCommentHistoryHandler.php
+++ b/vendor/adyen/module-payment/Gateway/Response/CheckoutPaymentCommentHistoryHandler.php
@@ -67,7 +67,7 @@ class CheckoutPaymentCommentHistoryHandler implements HandlerInterface
             $comment .= '<br /> ' . __('pspReference:') . ' ' . $pspReference;
         }

-        $payment->getOrder()->addStatusHistoryComment($comment);
+        $payment->getOrder()->addStatusHistoryComment($comment, $payment->getOrder()->getStatus());

         return $this;
     }
diff --git a/vendor/adyen/module-payment/Gateway/Response/PaymentCommentHistoryHandler.php b/vendor/adyen/module-payment/Gateway/Response/PaymentCommentHistoryHandler.php
--- a/vendor/adyen/module-payment/Gateway/Response/PaymentCommentHistoryHandler.php
+++ b/vendor/adyen/module-payment/Gateway/Response/PaymentCommentHistoryHandler.php
@@ -68,7 +68,7 @@ class PaymentCommentHistoryHandler implements HandlerInterface
             $payment->getOrder()->setAdyenResulturlEventCode($responseCode);
         }

-        $payment->getOrder()->addStatusHistoryComment($comment);
+        $payment->getOrder()->addStatusHistoryComment($comment, $payment->getOrder()->getStatus());

         return $this;
     }
diff --git a/vendor/adyen/module-payment/Gateway/Response/PaymentCommentHistoryRefundHandler.php b/vendor/adyen/module-payment/Gateway/Response/PaymentCommentHistoryRefundHandler.php
--- a/vendor/adyen/module-payment/Gateway/Response/PaymentCommentHistoryRefundHandler.php
+++ b/vendor/adyen/module-payment/Gateway/Response/PaymentCommentHistoryRefundHandler.php
@@ -69,7 +69,7 @@ class PaymentCommentHistoryRefundHandler implements HandlerInterface
                 $payment->getOrder()->setAdyenResulturlEventCode($responseCode);
             }

-            $payment->getOrder()->addStatusHistoryComment($comment);
+            $payment->getOrder()->addStatusHistoryComment($comment, $payment->getOrder()->getStatus());
         }

         return $this;
diff --git a/vendor/adyen/module-payment/Helper/Data.php b/vendor/adyen/module-payment/Helper/Data.php
--- a/vendor/adyen/module-payment/Helper/Data.php
+++ b/vendor/adyen/module-payment/Helper/Data.php
@@ -1755,7 +1755,7 @@ class Data extends AbstractHelper
                 $this->adyenLogger->error("exception: " . $message);
             }

-            $comment = $order->addStatusHistoryComment($message);
+            $comment = $order->addStatusHistoryComment($message, $order->getStatus());

             $order->addRelatedObject($comment);
         }
diff --git a/vendor/adyen/module-payment/Model/Cron.php b/vendor/adyen/module-payment/Model/Cron.php
--- a/vendor/adyen/module-payment/Model/Cron.php
+++ b/vendor/adyen/module-payment/Model/Cron.php
@@ -843,7 +843,7 @@ class Cron
             return;
         }

-        $this->_order->addStatusHistoryComment($comment);
+        $this->_order->addStatusHistoryComment($comment, $this->_order->getStatus());
         $this->_adyenLogger->addAdyenNotificationCronjob('Created comment history for this notification');
     }

@@ -1322,7 +1322,7 @@ class Cron
                     }

                     $this->_adyenLogger->addAdyenNotificationCronjob($message);
-                    $comment = $this->_order->addStatusHistoryComment($message);
+                    $comment = $this->_order->addStatusHistoryComment($message, $this->_order->getStatus());
                     $this->_order->addRelatedObject($comment);
                 }
                 //store recurring contract for alternative payments methods
@@ -1463,7 +1463,7 @@ class Cron
         $this->_adyenLogger->addAdyenNotificationCronjob(
             'Status update to default status or refund_authorized status if this is set'
         );
-        $this->_order->addStatusHistoryComment(__('Adyen Refund Successfully completed'));
+        $this->_order->addStatusHistoryComment(__('Adyen Refund Successfully completed'), $this->_order->getStatus());
     }

     /**
@@ -1585,7 +1585,7 @@ class Cron

         //capture mode
         if (!$this->_isAutoCapture()) {
-            $this->_order->addStatusHistoryComment(__('Capture Mode set to Manual'));
+            $this->_order->addStatusHistoryComment(__('Capture Mode set to Manual'), $this->_order->getStatus());
             $this->_adyenLogger->addAdyenNotificationCronjob('Capture mode is set to Manual');

             // show message if order is in manual review
@@ -2219,7 +2219,7 @@ class Cron
     {
         $comment = __('The order failed to update: %1', $errorMessage);
         if ($this->_order) {
-            $this->_order->addStatusHistoryComment($comment);
+            $this->_order->addStatusHistoryComment($comment, $this->_order->getStatus());
             $this->_order->save();
         }
     }
acampos1916 commented 3 years ago

Appreciate it @WinstonN, we'll use this to investigate and provide the fix.