Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECP-8687] - Update Module-Payment to handle dispute notifications. #2550

Merged
merged 20 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Helper/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,9 @@ public function refundOrder(MagentoOrder $order, Notification $notification): Ma
);
}

$order->addStatusHistoryComment(__('Refund Webhook successfully handled'), $order->getStatus());

$order->addStatusHistoryComment(__(sprintf(
'%s Webhook successfully handled',
$notification->getEventCode())), $order->getStatus());
return $order;
}

Expand Down
6 changes: 4 additions & 2 deletions Helper/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Adyen\Webhook\Processor\ProcessorFactory;
use DateTime;
use Exception;
use Adyen\Payment\Model\Notification as NotificationEntity;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Sales\Model\Order;
Expand All @@ -39,7 +40,8 @@ class Webhook
Order::STATE_PROCESSING => PaymentStates::STATE_IN_PROGRESS,
Order::STATE_COMPLETE => PaymentStates::STATE_PAID,
Order::STATE_CANCELED => PaymentStates::STATE_CANCELLED,
Order::STATE_CLOSED => PaymentStates::STATE_REFUNDED
Order::STATE_CLOSED => PaymentStates::STATE_REFUNDED,
NotificationEntity::STATE_ADYEN_AUTHORIZED => PaymentStates::STATE_PENDING
];

/**
Expand Down Expand Up @@ -271,7 +273,7 @@ private function getTransitionState(Notification $notification, $currentOrderSta
'additionalData' => !empty($notification->getAdditionalData())
? $this->serializer->unserialize($notification->getAdditionalData()) : null,
]);
$processor = ProcessorFactory::create($webhookNotificationItem, $currentOrderState, $this->logger);
$processor = ProcessorFactory::create($webhookNotificationItem, $currentOrderState);

return $processor->process();
}
Expand Down
81 changes: 81 additions & 0 deletions Helper/Webhook/AbstractDisputeWebhookHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2024 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/

namespace Adyen\Payment\Helper\Webhook;

use Adyen\Payment\Helper\Order;
use Adyen\Payment\Model\Notification;
use Adyen\Webhook\PaymentStates;
use Magento\Sales\Model\Order as MagentoOrder;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Helper\Config;


abstract class AbstractDisputeWebhookHandler implements WebhookHandlerInterface
{
/** @var Order */
private $orderHelper;

/** @var AdyenLogger */
private $adyenLogger;

/** @var Config */
private $configHelper;

public function __construct(Order $orderHelper, AdyenLogger $adyenLogger, Config $configHelper)
{
$this->orderHelper = $orderHelper;
$this->adyenLogger = $adyenLogger;
$this->configHelper = $configHelper;
}

public function handleWebhook(MagentoOrder $order, Notification $notification, string $transitionState): MagentoOrder
{
$ignoreDisputeNotifications = $this->configHelper->getConfigData(
'ignore_dispute_notification',
'adyen_abstract',
$order->getStoreId()
);

if ($transitionState === PaymentStates::STATE_REFUNDED && !$ignoreDisputeNotifications){
$order = $this->orderHelper->refundOrder($order, $notification);
$this->adyenLogger->addAdyenNotification(sprintf(
'The order has been updated by the %s notification. ',
$notification->getEventCode(),
), [
'pspReference' => $order->getPayment()->getData('adyen_psp_reference'),
'merchantReference' => $order->getPayment()->getData('entity_id')
]);
}
elseif ($ignoreDisputeNotifications){
$this->adyenLogger->addAdyenNotification(sprintf(
'Config to ignore dispute notification is enabled. Notification %s will be ignored',
$notification->getId()
), [
'pspReference' => $notification->getPspreference(),
'merchantReference' => $notification->getMerchantReference()
]);
}
else {
$this->orderHelper->addWebhookStatusHistoryComment($order, $notification);
$this->adyenLogger->addAdyenNotification(sprintf(
'There is a %s notification for the order.',
$notification->getEventCode(),
), [
'pspReference' => $order->getPayment()->getData('adyen_psp_reference'),
'merchantReference' => $order->getPayment()->getData('entity_id')
]);
}
return $order;
}

}
23 changes: 23 additions & 0 deletions Helper/Webhook/ChargebackReversedWebhookHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2024 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/

namespace Adyen\Payment\Helper\Webhook;

use Adyen\Payment\Helper\Order;
use Adyen\Payment\Model\Notification;
use Magento\Sales\Model\Order as MagentoOrder;


class ChargebackReversedWebhookHandler extends AbstractDisputeWebhookHandler implements WebhookHandlerInterface
{

}
23 changes: 23 additions & 0 deletions Helper/Webhook/ChargebackWebhookHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2024 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/

namespace Adyen\Payment\Helper\Webhook;

use Adyen\Payment\Helper\Order;
use Adyen\Payment\Model\Notification;
use Magento\Sales\Model\Order as MagentoOrder;


class ChargebackWebhookHandler extends AbstractDisputeWebhookHandler implements WebhookHandlerInterface
{

}
23 changes: 23 additions & 0 deletions Helper/Webhook/NotificationOfChargebackWebhookHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2024 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/

namespace Adyen\Payment\Helper\Webhook;

use Adyen\Payment\Helper\Order;
use Adyen\Payment\Model\Notification;
use Magento\Sales\Model\Order as MagentoOrder;


class NotificationOfChargebackWebhookHandler extends AbstractDisputeWebhookHandler implements WebhookHandlerInterface
{

}
23 changes: 23 additions & 0 deletions Helper/Webhook/RequestForInformationWebhookHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2024 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/

namespace Adyen\Payment\Helper\Webhook;

use Adyen\Payment\Helper\Order;
use Adyen\Payment\Model\Notification;
use Magento\Sales\Model\Order as MagentoOrder;


class RequestForInformationWebhookHandler extends AbstractDisputeWebhookHandler implements WebhookHandlerInterface
{

}
23 changes: 23 additions & 0 deletions Helper/Webhook/SecondChargebackWebhookHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2024 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/

namespace Adyen\Payment\Helper\Webhook;

use Adyen\Payment\Helper\Order;
use Adyen\Payment\Model\Notification;
use Magento\Sales\Model\Order as MagentoOrder;


class SecondChargebackWebhookHandler extends AbstractDisputeWebhookHandler implements WebhookHandlerInterface
{

}
26 changes: 25 additions & 1 deletion Helper/Webhook/WebhookHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class WebhookHandlerFactory
private CancelOrRefundWebhookHandler $cancelOrRefundWebhookHandler;
private OrderClosedWebhookHandler $orderClosedWebhookHandler;
private OrderOpenedWebhookHandler $orderOpenedWebhookHandler;
private ChargebackWebhookHandler $chargebackWebhookHandler;
private ChargebackReversedWebhookHandler $chargebackReversedWebhookHandler;
private NotificationOfChargebackWebhookHandler $notificationOfChargebackWebhookHandler;


public function __construct(
AdyenLogger $adyenLogger,
Expand All @@ -47,7 +51,12 @@ public function __construct(
CancellationWebhookHandler $cancellationWebhookHandler,
CancelOrRefundWebhookHandler $cancelOrRefundWebhookHandler,
OrderClosedWebhookHandler $orderClosedWebhookHandler,
OrderOpenedWebhookHandler $orderOpenedWebhookHandler
OrderOpenedWebhookHandler $orderOpenedWebhookHandler,
ChargebackWebhookHandler $chargebackWebhookHandler,
RequestForInformationWebhookHandler $requestForInformationWebhookHandler,
ChargebackReversedWebhookHandler $chargebackReversedWebhookHandler,
SecondChargebackWebhookHandler $secondChargebackWebhookHandler,
NotificationOfChargebackWebhookHandler $notificationOfChargebackWebhookHandler,
) {
$this->adyenLogger = $adyenLogger;
$this->authorisationWebhookHandler = $authorisationWebhookHandler;
Expand All @@ -63,6 +72,11 @@ public function __construct(
$this->cancelOrRefundWebhookHandler = $cancelOrRefundWebhookHandler;
$this->orderClosedWebhookHandler = $orderClosedWebhookHandler;
$this->orderOpenedWebhookHandler = $orderOpenedWebhookHandler;
$this->chargebackWebhookHandler = $chargebackWebhookHandler;
$this->requestForInformationWebhookHandler = $requestForInformationWebhookHandler;
$this->chargebackReversedWebhookHandler = $chargebackReversedWebhookHandler;
$this->secondChargebackWebhookHandler = $secondChargebackWebhookHandler;
$this->notificationOfChargebackWebhookHandler = $notificationOfChargebackWebhookHandler;
}

/**
Expand Down Expand Up @@ -98,6 +112,16 @@ public function create(string $eventCode): WebhookHandlerInterface
return $this->orderOpenedWebhookHandler;
case Notification::ORDER_CLOSED:
return $this->orderClosedWebhookHandler;
case Notification::CHARGEBACK:
return $this->chargebackWebhookHandler;
case Notification::NOTIFICATION_OF_CHARGEBACK:
return $this->notificationOfChargebackWebhookHandler;
case Notification::REQUEST_FOR_INFORMATION:
return $this->requestForInformationWebhookHandler;
case Notification::CHARGEBACK_REVERSED:
return $this->chargebackReversedWebhookHandler;
case Notification::SECOND_CHARGEBACK:
return $this->secondChargebackWebhookHandler;
}

$exceptionMessage = sprintf(
Expand Down
6 changes: 6 additions & 0 deletions Model/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class Notification extends AbstractModel implements NotificationInterface
const ORDER_OPENED = 'ORDER_OPENED';
const ORDER_CLOSED = "ORDER_CLOSED";
const OFFER_CLOSED = "OFFER_CLOSED";
const CHARGEBACK = "CHARGEBACK";
const SECOND_CHARGEBACK = "SECOND_CHARGEBACK";
const CHARGEBACK_REVERSED = "CHARGEBACK_REVERSED";
const REQUEST_FOR_INFORMATION = "REQUEST_FOR_INFORMATION";
const NOTIFICATION_OF_CHARGEBACK = "NOTIFICATION_OF_CHARGEBACK";
const STATE_ADYEN_AUTHORIZED = "adyen_authorized";
const MAX_ERROR_COUNT = 5;

public function __construct(
Expand Down
Loading
Loading