-
Notifications
You must be signed in to change notification settings - Fork 22
/
AuthorisationWebhookHandler.php
183 lines (165 loc) · 7.09 KB
/
AuthorisationWebhookHandler.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php declare(strict_types=1);
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* 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 <shopware@adyen.com>
*/
namespace Adyen\Shopware\ScheduledTask\Webhook;
use Adyen\AdyenException;
use Adyen\Shopware\Entity\Notification\NotificationEntity;
use Adyen\Shopware\Exception\CaptureException;
use Adyen\Shopware\Service\CaptureService;
use Adyen\Shopware\Service\AdyenPaymentService;
use Adyen\Shopware\Service\ConfigurationService;
use Adyen\Shopware\Service\PluginPaymentMethodsService;
use Adyen\Shopware\Util\Currency;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
use Shopware\Core\Framework\Context;
class AuthorisationWebhookHandler implements WebhookHandlerInterface
{
/** @var LoggerInterface */
private $logger;
/** @var CaptureService */
private $captureService;
/** @var AdyenPaymentService */
private $adyenPaymentService;
/** @var OrderTransactionStateHandler */
private $orderTransactionStateHandler;
/** @var PluginPaymentMethodsService */
private $pluginPaymentMethodsService;
/** @var ConfigurationService */
private $configurationService;
/**
* @param CaptureService $captureService
* @param AdyenPaymentService $adyenPaymentService
* @param OrderTransactionStateHandler $orderTransactionStateHandler
* @param PluginPaymentMethodsService $pluginPaymentMethodsService
* @param ConfigurationService $configurationService
* @param LoggerInterface $logger
*/
public function __construct(
CaptureService $captureService,
AdyenPaymentService $adyenPaymentService,
OrderTransactionStateHandler $orderTransactionStateHandler,
PluginPaymentMethodsService $pluginPaymentMethodsService,
ConfigurationService $configurationService,
LoggerInterface $logger
) {
$this->captureService = $captureService;
$this->adyenPaymentService = $adyenPaymentService;
$this->orderTransactionStateHandler = $orderTransactionStateHandler;
$this->pluginPaymentMethodsService = $pluginPaymentMethodsService;
$this->configurationService = $configurationService;
$this->logger = $logger;
}
/**
* @param OrderTransactionEntity $orderTransactionEntity
* @param NotificationEntity $notificationEntity
* @param string $state
* @param string $currentTransactionState
* @param Context $context
* @return void
* @throws CaptureException|AdyenException
*/
public function handleWebhook(
OrderTransactionEntity $orderTransactionEntity,
NotificationEntity $notificationEntity,
string $state,
string $currentTransactionState,
Context $context
): void {
if ($state !== $currentTransactionState) {
if ($notificationEntity->isSuccess()) {
$this->handleSuccessfulNotification($orderTransactionEntity, $notificationEntity, $context);
} else {
$this->handleFailedNotification($orderTransactionEntity, $context);
}
}
}
/**
* @param OrderTransactionEntity $orderTransaction
* @param NotificationEntity $notification
* @param Context $context
* @return void
* @throws CaptureException
*/
private function handleSuccessfulNotification(
OrderTransactionEntity $orderTransaction,
NotificationEntity $notification,
Context $context
): void {
$paymentMethodHandler = $this->pluginPaymentMethodsService->getGiftcardHandlerIdentifierFromTxVariant(
$notification->getPaymentMethod()
);
if (is_null($paymentMethodHandler)) {
$paymentMethodHandler = $orderTransaction->getPaymentMethod()->getHandlerIdentifier();
}
$isManualCapture = $this->captureService->isManualCapture($paymentMethodHandler);
$currencyUtil = new Currency();
$totalPrice = $orderTransaction->getAmount()->getTotalPrice();
$isoCode = $orderTransaction->getOrder()->getCurrency()->getIsoCode();
$transactionAmount = $currencyUtil->sanitize($totalPrice, $isoCode);
$adyenPayment = $this->adyenPaymentService->getAdyenPayment($notification->getPspreference());
if (is_null($adyenPayment)) {
$this->adyenPaymentService->insertAdyenPayment($notification, $orderTransaction, $isManualCapture);
}
// check for partial payments
$merchantOrderReference = isset(json_decode($notification->getAdditionalData())->merchantOrderReference);
if ($merchantOrderReference) {
return;
}
if ($transactionAmount === intval($notification->getAmountValue())) {
if ($isManualCapture) {
$this->logger->info(
'Manual capture required. Setting payment to `authorised` state.',
['notification' => $notification->getVars()]
);
$this->orderTransactionStateHandler->authorize($orderTransaction->getId(), $context);
if ($this->captureService->requiresCaptureOnShipment(
$paymentMethodHandler,
$orderTransaction->getOrder()->getSalesChannelId()
)) {
$this->logger->info(
'Attempting capture for open invoice payment.',
['notification' => $notification->getVars()]
);
$this->captureService->doOpenInvoiceCapture(
$notification->getMerchantReference(),
$notification->getAmountValue(),
$context
);
}
} else {
$this->orderTransactionStateHandler->paid($orderTransaction->getId(), $context);
}
}
}
/**
* @param OrderTransactionEntity $orderTransactionEntity
* @param Context $context
* @return void
*/
private function handleFailedNotification(OrderTransactionEntity $orderTransactionEntity, Context $context): void
{
$this->orderTransactionStateHandler->fail($orderTransactionEntity->getId(), $context);
}
}