From f2b558534febf8c0dcc7733e5c9b93c8d06027bb Mon Sep 17 00:00:00 2001 From: ropczan Date: Fri, 19 Apr 2024 11:04:30 +0200 Subject: [PATCH] Fix cancel action - empty orderId in details (#34) --- src/Action/CancelAction.php | 9 ++-- tests/Unit/Action/CancelActionTest.php | 70 ++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/Action/CancelAction.php b/src/Action/CancelAction.php index 8a43a10..2ec2b28 100644 --- a/src/Action/CancelAction.php +++ b/src/Action/CancelAction.php @@ -36,12 +36,13 @@ public function execute($request): void $payment = PaymentHelper::ensurePayment($request->getFirstModel()); $orderId = PaymentHelper::getOrderId($model, $payment); Assert::notEmpty($orderId, 'OrderId must be set on cancel action.'); + $configKey = PaymentHelper::getConfigKey($model, $payment); - if (!$this->canCancelPayment($model, $payment)) { + if (!$this->canCancelPayment($orderId, $configKey)) { throw new CannotCancelPaymentException('Order status is final, cannot cancel payment.'); } - $this->orderRequestService->cancel($model->orderId(), PaymentHelper::getConfigKey($model, $payment)); + $this->orderRequestService->cancel($orderId, $configKey); } public function supports($request): bool @@ -55,9 +56,9 @@ public function supports($request): bool /** * @throws PayUException */ - private function canCancelPayment(Model $model, PaymentInterface $payment): bool + private function canCancelPayment(string $orderId, ?string $configKey): bool { - $response = $this->orderRequestService->retrieve($model->orderId(), PaymentHelper::getConfigKey($model, $payment)); + $response = $this->orderRequestService->retrieve($orderId, $configKey); return !$response->orders[0]->status->isFinal(); } diff --git a/tests/Unit/Action/CancelActionTest.php b/tests/Unit/Action/CancelActionTest.php index 5148c26..0f80e0d 100644 --- a/tests/Unit/Action/CancelActionTest.php +++ b/tests/Unit/Action/CancelActionTest.php @@ -35,6 +35,7 @@ public function successTest(): void ); $payment = new Payment(); + $payment->setConfigKey('pos2'); $payment->setDetails(FileTestUtil::decodeJsonFromFile(__DIR__ . '/../../Integration/Action/data/detailsWithOrderId.json')); $request = new Cancel($payment); @@ -61,6 +62,7 @@ public function orderHasFinalStatusTest(): void ); $payment = new Payment(); + $payment->setConfigKey('pos2'); $payment->setDetails(FileTestUtil::decodeJsonFromFile(__DIR__ . '/../../Integration/Action/data/detailsWithOrderId.json')); $request = new Cancel($payment); @@ -69,14 +71,74 @@ public function orderHasFinalStatusTest(): void $action->execute($request); } + /** + * @test + */ + public function emptyOrderIdInDetails(): void + { + $action = $this->getCancelAction( + OrderCanceledResponse::fromResponse( + FileTestUtil::decodeJsonFromFile( + __DIR__ . '/../../Integration/Request/data/orderCanceledResponse.json' + ) + ), + OrderRetrieveResponse::fromResponse( + FileTestUtil::decodeJsonFromFile( + __DIR__ . '/../../Integration/Request/data/retrieveOrderResponse.json' + ) + ) + ); + + $details = FileTestUtil::decodeJsonFromFile(__DIR__ . '/../../Integration/Action/data/detailsWithOrderId.json'); + $details['orderId'] = null; + + $payment = new Payment(); + $payment->setConfigKey('pos2'); + $payment->setDetails($details); + $payment->setOrderId('123'); + + $request = new Cancel($payment); + $request->setModel($payment->getDetails()); + + $action->execute($request); + } + + /** + * @test + */ + public function emptyOrderId(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('OrderId must be set on cancel action.'); + + $action = $this->getCancelAction(null, null); + + $details = FileTestUtil::decodeJsonFromFile(__DIR__ . '/../../Integration/Action/data/detailsWithOrderId.json'); + $details['orderId'] = null; + + $payment = new Payment(); + $payment->setConfigKey('pos2'); + $payment->setDetails($details); + + $request = new Cancel($payment); + $request->setModel($payment->getDetails()); + + $action->execute($request); + } + private function getCancelAction( ?OrderCanceledResponse $orderCanceledResponse, - OrderRetrieveResponse $retrieveOrderResponse + ?OrderRetrieveResponse $retrieveOrderResponse ): CancelAction { $orderRequestService = $this->createMock(OrderRequestService::class); - $orderRequestService->expects(self::once()) - ->method('retrieve') - ->willReturn($retrieveOrderResponse); + if (null === $retrieveOrderResponse) { + $orderRequestService->expects(self::never()) + ->method('retrieve'); + } else { + $orderRequestService->expects(self::once()) + ->method('retrieve') + ->willReturn($retrieveOrderResponse); + } if (null === $orderCanceledResponse) { $orderRequestService->expects(self::never())