From 1f87a9d9e8ba1adbec8e1a142870c1d5d5d2016b Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Thu, 18 Aug 2022 17:05:26 +0200 Subject: [PATCH] [PW-7021] - Add failure counter for pay by link attempts (#1667) * [PW-7021] - Add failure counter for pay by link attempts * Update Helper/Webhook/AuthorisationWebhookHandler.php Co-authored-by: Michael Paul * Update Helper/Webhook/AuthorisationWebhookHandler.php Co-authored-by: Michael Paul * [PW-7021] - Code formatting Co-authored-by: Michael Paul --- .../Webhook/AuthorisationWebhookHandler.php | 52 ++++++++++++++++++- Model/Ui/AdyenPayByLinkConfigProvider.php | 1 + 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Helper/Webhook/AuthorisationWebhookHandler.php b/Helper/Webhook/AuthorisationWebhookHandler.php index dc2ddb6a7..fa1935c9c 100644 --- a/Helper/Webhook/AuthorisationWebhookHandler.php +++ b/Helper/Webhook/AuthorisationWebhookHandler.php @@ -22,6 +22,7 @@ use Adyen\Payment\Helper\PaymentMethods; use Adyen\Payment\Logger\AdyenLogger; use Adyen\Payment\Model\Notification; +use Adyen\Payment\Model\Ui\AdyenPayByLinkConfigProvider; use Adyen\Webhook\PaymentStates; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Serialize\SerializerInterface; @@ -91,7 +92,7 @@ public function handleWebhook(Order $order, Notification $notification, string $ if ($transitionState === PaymentStates::STATE_PAID) { $order = $this->handleSuccessfulAuthorisation($order, $notification); } elseif ($transitionState === PaymentStates::STATE_FAILED) { - $order = $this->handleFailedAuthorisation($order); + $order = $this->handleFailedAuthorisation($order, $notification); } return $order; @@ -154,7 +155,7 @@ private function handleSuccessfulAuthorisation(Order $order, Notification $notif /** * @throws LocalizedException */ - private function handleFailedAuthorisation(Order $order): Order + private function handleFailedAuthorisation(Order $order, Notification $notification): Order { $previousAdyenEventCode = $order->getData('adyen_notification_event_code'); @@ -184,6 +185,13 @@ private function handleFailedAuthorisation(Order $order): Order return $order; } + // If the payment method is PBL, use failure counter before cancelling the order + if ($order->getPayment()->getMethod() == AdyenPayByLinkConfigProvider::CODE) { + if (!$this->canCancelPayByLinkOrder($order, $notification)) { + return $order; + } + } + // Move the order from PAYMENT_REVIEW to NEW, so that can be cancelled if (!$order->canCancel() && $this->configHelper->getNotificationsCanCancel($order->getStoreId())) { $order->setState(Order::STATE_NEW); @@ -229,4 +237,44 @@ private function handleManualCapture(Order $order, Notification $notification, b return $order; } + + /** + * @param Order $order + * @param Notification $notification + * @return bool + * @throws \Exception + */ + private function canCancelPayByLinkOrder(Order $order, Notification $notification): bool + { + $payByLinkFailureCount = $order->getPayment()->getAdditionalInformation('payByLinkFailureCount'); + $payByLinkFailureCount = isset($payByLinkFailureCount) ? ++$payByLinkFailureCount : 1; + + $order->getPayment()->setAdditionalInformation('payByLinkFailureCount', $payByLinkFailureCount); + + if ($payByLinkFailureCount >= AdyenPayByLinkConfigProvider::MAX_FAILURE_COUNT) { + // Order can be cancelled. + return true; + } + + $notification->setDone(true); + $notification->setProcessing(false); + $notification->save(); + + $order->addStatusHistoryComment(__(sprintf( + "Order wasn't cancelled by this webhook notification. Pay by Link failure count: %s/%s", + $payByLinkFailureCount, + AdyenPayByLinkConfigProvider::MAX_FAILURE_COUNT + )), false); + + $this->adyenLogger->addAdyenNotification( + __(sprintf( + "Order wasn't cancelled by this webhook notification. Pay by Link failure count: %s/%s", + $payByLinkFailureCount, + AdyenPayByLinkConfigProvider::MAX_FAILURE_COUNT + )), + $this->adyenLogger->getOrderContext($order) + ); + + return false; + } } diff --git a/Model/Ui/AdyenPayByLinkConfigProvider.php b/Model/Ui/AdyenPayByLinkConfigProvider.php index 942a5ddf0..e1198f57b 100755 --- a/Model/Ui/AdyenPayByLinkConfigProvider.php +++ b/Model/Ui/AdyenPayByLinkConfigProvider.php @@ -17,6 +17,7 @@ class AdyenPayByLinkConfigProvider implements ConfigProviderInterface { const CODE = 'adyen_pay_by_link'; + const MAX_FAILURE_COUNT = 5; const MIN_EXPIRY_DAYS = 1; const MAX_EXPIRY_DAYS = 70; const DAYS_TO_EXPIRE_CONFIG_PATH = 'payment/adyen_pay_by_link/days_to_expire';