Skip to content

Commit

Permalink
Update Module-Payment to handle dispute notifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulritter committed Mar 8, 2024
1 parent ba281cb commit 1bf1fd4
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 4 deletions.
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
4 changes: 3 additions & 1 deletion 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
84 changes: 84 additions & 0 deletions Helper/Webhook/AbstractDisputeWebhookHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2022 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) 2022 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) 2022 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) 2022 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) 2022 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) 2022 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
6 changes: 6 additions & 0 deletions etc/adminhtml/system/adyen_testing_performance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/adyen_abstract/ignore_refund_notification</config_path>
</field>
<field id="ignore_dispute_notification" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Ignore dispute webhook</label>
<tooltip>If you set this setting to 'Yes', this will not process any of the DISPUTE webhooks that is received.</tooltip>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/adyen_abstract/ignore_dispute_notification</config_path>
</field>
<field id="debug" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1"
showInStore="0">
<label>Debug logging</label>
Expand Down

0 comments on commit 1bf1fd4

Please sign in to comment.