Skip to content

Commit

Permalink
Fix cancel action - empty orderId in details (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ropczan authored Apr 19, 2024
1 parent 78a0bf1 commit f2b5585
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/Action/CancelAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
70 changes: 66 additions & 4 deletions tests/Unit/Action/CancelActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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())
Expand Down

0 comments on commit f2b5585

Please sign in to comment.