-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathOfferClosedWebhookHandler.php
More file actions
126 lines (109 loc) · 4.59 KB
/
OfferClosedWebhookHandler.php
File metadata and controls
126 lines (109 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?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\AdyenException;
use Adyen\Payment\Api\CleanupAdditionalInformationInterface;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\Order;
use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\Notification;
use Adyen\Payment\Model\ResourceModel\Order\Payment as OrderPaymentResourceModel;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\Order as MagentoOrder;
class OfferClosedWebhookHandler implements WebhookHandlerInterface
{
/**
* @param PaymentMethods $paymentMethodsHelper
* @param AdyenLogger $adyenLogger
* @param Config $configHelper
* @param Order $orderHelper
* @param OrderPaymentResourceModel $orderPaymentResourceModel
* @param CleanupAdditionalInformationInterface $cleanupAdditionalInformation
*/
public function __construct(
private readonly PaymentMethods $paymentMethodsHelper,
private readonly AdyenLogger $adyenLogger,
private readonly Config $configHelper,
private readonly Order $orderHelper,
private readonly OrderPaymentResourceModel $orderPaymentResourceModel,
private readonly CleanupAdditionalInformationInterface $cleanupAdditionalInformation
) { }
/**
* @throws LocalizedException
* @throws AdyenException
*/
public function handleWebhook(MagentoOrder $order, Notification $notification, string $transitionState): MagentoOrder
{
//Do not process OfferClosed for Pay by Link payments
if($order->getPayment()->getMethod() == 'adyen_pay_by_link')
return $order;
$capturedAdyenOrderPayments = $this->orderPaymentResourceModel->getLinkedAdyenOrderPayments(
$order->getPayment()->getEntityId()
);
if (!empty($capturedAdyenOrderPayments)) {
$order->addCommentToStatusHistory(
__('Order is not cancelled because this order was fully/partially authorised.')
);
$this->adyenLogger->addAdyenNotification(
'Order is not cancelled because this order was fully/partially authorised.',
[
'pspReference' => $notification->getPspreference(),
'merchantReference' => $notification->getMerchantReference()
]
);
return $order;
}
$identicalPaymentMethods = $this->paymentMethodsHelper->compareOrderAndWebhookPaymentMethods(
$order,
$notification
);
if (!$identicalPaymentMethods) {
$paymentMethodInstance = $order->getPayment()->getMethodInstance();
$this->adyenLogger->addAdyenNotification(sprintf(
'Payment method of notification %s (%s) does not match the payment method (%s) of order %s',
$notification->getId(),
$notification->getPaymentMethod(),
$order->getIncrementId(),
$this->paymentMethodsHelper->getAlternativePaymentMethodTxVariant(
$paymentMethodInstance)
),
[
'pspReference' => $notification->getPspreference(),
'merchantReference' => $notification->getMerchantReference()
]
);
return $order;
}
if ($order->isCanceled()) {
$message = __('The order has already been cancelled. Skipping the %1 webhook.',
$notification->getEventCode());
$this->adyenLogger->addAdyenNotification($message, [
'pspReference' => $notification->getPspreference(),
'merchantReference' => $notification->getMerchantReference()
]);
$order->addCommentToStatusHistory($message);
} else {
// Move the order from PAYMENT_REVIEW to NEW, so that it can be cancelled
if (!$order->isCanceled()
&& !$order->canCancel()
&& $this->configHelper->getNotificationsCanCancel($order->getStoreId())
) {
$order->setState(MagentoOrder::STATE_NEW);
}
$this->orderHelper->holdCancelOrder($order, true);
}
// Clean-up the data temporarily stored in `additional_information`
$this->cleanupAdditionalInformation->execute($order->getPayment());
return $order;
}
}