Skip to content

Commit

Permalink
[PW-7021] - Add failure counter for pay by link attempts (#1667)
Browse files Browse the repository at this point in the history
* [PW-7021] - Add failure counter for pay by link attempts

* Update Helper/Webhook/AuthorisationWebhookHandler.php

Co-authored-by: Michael Paul <michael.paul@adyen.com>

* Update Helper/Webhook/AuthorisationWebhookHandler.php

Co-authored-by: Michael Paul <michael.paul@adyen.com>

* [PW-7021] - Code formatting

Co-authored-by: Michael Paul <michael.paul@adyen.com>
  • Loading branch information
2 people authored and Morerice committed Aug 22, 2022
1 parent dbd8acd commit 1f87a9d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
52 changes: 50 additions & 2 deletions Helper/Webhook/AuthorisationWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions Model/Ui/AdyenPayByLinkConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 1f87a9d

Please sign in to comment.