Skip to content

Commit 7ea3811

Browse files
authored
[ECP-9562] Fix the quote staying active on authorised payments (#2921)
* [ECP-9562] Disable the quote on Authorisation webhook handler * [ECP-9562] Update and add unit tests
1 parent b693557 commit 7ea3811

File tree

2 files changed

+84
-55
lines changed

2 files changed

+84
-55
lines changed

Helper/Webhook/AuthorisationWebhookHandler.php

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
namespace Adyen\Payment\Helper\Webhook;
1414

15-
1615
use Adyen\Payment\Helper\AdyenOrderPayment;
1716
use Adyen\Payment\Helper\CaseManagement;
1817
use Adyen\Payment\Helper\ChargedCurrency;
@@ -26,59 +25,35 @@
2625
use Adyen\Webhook\PaymentStates;
2726
use Magento\Framework\Exception\LocalizedException;
2827
use Magento\Framework\Serialize\SerializerInterface;
28+
use Magento\Quote\Api\CartRepositoryInterface;
2929
use Magento\Sales\Model\Order;
3030

3131
class AuthorisationWebhookHandler implements WebhookHandlerInterface
3232
{
33-
/** @var AdyenOrderPayment */
34-
private $adyenOrderPaymentHelper;
35-
36-
/** @var OrderHelper */
37-
private $orderHelper;
38-
39-
/** @var CaseManagement */
40-
private $caseManagementHelper;
41-
42-
/** @var SerializerInterface */
43-
private $serializer;
44-
45-
/** @var AdyenLogger */
46-
private $adyenLogger;
47-
48-
/** @var ChargedCurrency */
49-
private $chargedCurrency;
50-
51-
/** @var Config */
52-
private $configHelper;
53-
54-
/** @var Invoice */
55-
private $invoiceHelper;
56-
57-
/** @var PaymentMethods */
58-
private $paymentMethodsHelper;
59-
33+
/**
34+
* @param AdyenOrderPayment $adyenOrderPaymentHelper
35+
* @param OrderHelper $orderHelper
36+
* @param CaseManagement $caseManagementHelper
37+
* @param SerializerInterface $serializer
38+
* @param AdyenLogger $adyenLogger
39+
* @param ChargedCurrency $chargedCurrency
40+
* @param Config $configHelper
41+
* @param Invoice $invoiceHelper
42+
* @param PaymentMethods $paymentMethodsHelper
43+
* @param CartRepositoryInterface $cartRepository
44+
*/
6045
public function __construct(
61-
AdyenOrderPayment $adyenOrderPayment,
62-
OrderHelper $orderHelper,
63-
CaseManagement $caseManagementHelper,
64-
SerializerInterface $serializer,
65-
AdyenLogger $adyenLogger,
66-
ChargedCurrency $chargedCurrency,
67-
Config $configHelper,
68-
Invoice $invoiceHelper,
69-
PaymentMethods $paymentMethodsHelper
70-
)
71-
{
72-
$this->adyenOrderPaymentHelper = $adyenOrderPayment;
73-
$this->orderHelper = $orderHelper;
74-
$this->caseManagementHelper = $caseManagementHelper;
75-
$this->serializer = $serializer;
76-
$this->adyenLogger = $adyenLogger;
77-
$this->chargedCurrency = $chargedCurrency;
78-
$this->configHelper = $configHelper;
79-
$this->invoiceHelper = $invoiceHelper;
80-
$this->paymentMethodsHelper = $paymentMethodsHelper;
81-
}
46+
private readonly AdyenOrderPayment $adyenOrderPaymentHelper,
47+
private readonly OrderHelper $orderHelper,
48+
private readonly CaseManagement $caseManagementHelper,
49+
private readonly SerializerInterface $serializer,
50+
private readonly AdyenLogger $adyenLogger,
51+
private readonly ChargedCurrency $chargedCurrency,
52+
private readonly Config $configHelper,
53+
private readonly Invoice $invoiceHelper,
54+
private readonly PaymentMethods $paymentMethodsHelper,
55+
private readonly CartRepositoryInterface $cartRepository
56+
) { }
8257

8358
/**
8459
* @param Order $order
@@ -149,6 +124,13 @@ private function handleSuccessfulAuthorisation(Order $order, Notification $notif
149124
$this->orderHelper->createShipment($order);
150125
}
151126

127+
// Disable the quote if it's still active
128+
$quote = $this->cartRepository->get($order->getQuoteId());
129+
if ($quote->getIsActive()) {
130+
$quote->setIsActive(false);
131+
$this->cartRepository->save($quote);
132+
}
133+
152134
return $order;
153135
}
154136

Test/Unit/Helper/Webhook/AuthorisationWebhookHandlerTest.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Adyen\Payment\Model\Notification;
99
use Adyen\Webhook\PaymentStates;
1010
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Quote\Api\CartRepositoryInterface;
12+
use Magento\Quote\Model\Quote;
1113
use Magento\Sales\Model\Order;
1214
use PHPUnit\Framework\MockObject\MockObject;
1315
use Adyen\Payment\Helper\Order as OrderHelper;
@@ -27,6 +29,7 @@ class AuthorisationWebhookHandlerTest extends AbstractAdyenTestCase
2729
private AdyenAmountCurrency|MockObject $orderAmountCurrency;
2830
private Notification|MockObject $notificationMock;
2931
private Order|MockObject $orderMock;
32+
private Quote|MockObject $quoteMock;
3033

3134
protected function setUp(): void
3235
{
@@ -54,6 +57,7 @@ protected function setUp(): void
5457
'getPaymentMethod' => $paymentMethod,
5558
'isSuccessful' => true
5659
]);
60+
$this->quoteMock = $this->createMock(Quote::class);
5761
}
5862

5963
/**
@@ -89,6 +93,9 @@ public function testHandleWebhook(): void
8993

9094
$transitionState = PaymentStates::STATE_PAID;
9195

96+
$cartRepositoryMock = $this->createMock(CartRepositoryInterface::class);
97+
$cartRepositoryMock->expects($this->once())->method('get')->willReturn($this->quoteMock);
98+
9299
$handler = $this->createAuthorisationWebhookHandler(
93100
null,
94101
$this->orderHelperMock,
@@ -98,7 +105,8 @@ public function testHandleWebhook(): void
98105
$mockChargedCurrency,
99106
null,
100107
null,
101-
$paymentMethodsHelperMock
108+
$paymentMethodsHelperMock,
109+
$cartRepositoryMock
102110
);
103111

104112
// Call the function to be tested
@@ -160,6 +168,9 @@ public function testHandleSuccessfulAuthorisation($isAutoCapture): void
160168
]
161169
);
162170

171+
$cartRepositoryMock = $this->createMock(CartRepositoryInterface::class);
172+
$cartRepositoryMock->expects($this->once())->method('get')->willReturn($this->quoteMock);
173+
163174
$authorisationWebhookHandler = $this->createAuthorisationWebhookHandler(
164175
$this->adyenOrderPaymentMock,
165176
$this->orderHelperMock,
@@ -169,7 +180,8 @@ public function testHandleSuccessfulAuthorisation($isAutoCapture): void
169180
$mockChargedCurrency,
170181
null,
171182
null,
172-
$paymentMethodsMock
183+
$paymentMethodsMock,
184+
$cartRepositoryMock
173185
);
174186

175187
// Invoke the private method
@@ -257,6 +269,36 @@ public function testHandleAutoCapture(): void
257269
$this->assertInstanceOf(Order::class, $result);
258270
}
259271

272+
public function testDisableQuote()
273+
{
274+
$this->orderMock->expects($this->any())->method('getPayment')->willReturn($this->orderMock);
275+
$this->orderMock->expects($this->any())->method('getConfig')->willReturnSelf();
276+
277+
$this->quoteMock->expects($this->any())->method('getIsActive')->willReturn(true);
278+
$this->quoteMock->expects($this->any())->method('setIsActive')->with(false);
279+
280+
$cartRepositoryMock = $this->createMock(CartRepositoryInterface::class);
281+
$cartRepositoryMock->expects($this->once())->method('get')->willReturn($this->quoteMock);
282+
$cartRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
283+
284+
// Create an instance of AuthorisationWebhookHandler
285+
$webhookHandler = $this->createAuthorisationWebhookHandler(
286+
null,
287+
null,
288+
null,
289+
null,
290+
null,
291+
null,
292+
null,
293+
null,
294+
null,
295+
$cartRepositoryMock
296+
);
297+
298+
$result = $webhookHandler->handleWebhook($this->orderMock, $this->notificationMock, 'paid');
299+
$this->assertInstanceOf(Order::class, $result);
300+
}
301+
260302
/**
261303
* @throws ReflectionExceptionAlias
262304
*/
@@ -358,9 +400,9 @@ protected function createAuthorisationWebhookHandler(
358400
$mockChargedCurrency = null,
359401
$mockConfigHelper = null,
360402
$mockInvoiceHelper = null,
361-
$mockPaymentMethodsHelper = null
362-
): AuthorisationWebhookHandler
363-
{
403+
$mockPaymentMethodsHelper = null,
404+
$mockCartRepositoryMock = null
405+
): AuthorisationWebhookHandler {
364406
if (is_null($mockAdyenOrderPayment)) {
365407
$mockAdyenOrderPayment = $this->createMock(AdyenOrderPayment::class);
366408
}
@@ -397,6 +439,10 @@ protected function createAuthorisationWebhookHandler(
397439
$mockPaymentMethodsHelper = $this->createMock(PaymentMethods::class);
398440
}
399441

442+
if (is_null($mockCartRepositoryMock)) {
443+
$mockCartRepositoryMock = $this->createMock(CartRepositoryInterface::class);
444+
}
445+
400446
return new AuthorisationWebhookHandler(
401447
$mockAdyenOrderPayment,
402448
$mockOrderHelper,
@@ -406,7 +452,8 @@ protected function createAuthorisationWebhookHandler(
406452
$mockChargedCurrency,
407453
$mockConfigHelper,
408454
$mockInvoiceHelper,
409-
$mockPaymentMethodsHelper
455+
$mockPaymentMethodsHelper,
456+
$mockCartRepositoryMock
410457
);
411458
}
412459
}

0 commit comments

Comments
 (0)