Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel payment action #33

Merged
merged 3 commits into from
Mar 15, 2024
Merged

Cancel payment action #33

merged 3 commits into from
Mar 15, 2024

Conversation

ropczan
Copy link
Contributor

@ropczan ropczan commented Mar 12, 2024

No description provided.

@ropczan ropczan marked this pull request as draft March 12, 2024 09:30
@ropczan ropczan marked this pull request as ready for review March 12, 2024 20:30
@ropczan ropczan requested a review from lukasz-falda March 12, 2024 20:30
Copy link
Member

@lukasz-falda lukasz-falda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the README file

private const FINAL_STATUSES = [
OrderStatus::Completed,
OrderStatus::Canceled,
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move it to OrderStatus class,
or create a public method like OrderStatus->isFinal()

$model = Model::ensureArrayObject($request->getModel());

$payment = PaymentHelper::ensurePayment($request->getFirstModel());
Assert::notNull($payment, 'Payment must be set on cancel action.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensurePayment method returns PaymentInterface, not null. PHPStan does not report this to you? Maybe we need to update PHPStan 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, PHPStan didn't report anything :(


try {
if (!$this->canCancelPayment($model, $payment)) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we can't cancel Payment we nned to know about it. Throw custom exception here like CannotCancelPaymentException

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


$this->orderRequestService->cancel($model->orderId(), PaymentHelper::getConfigKey($model, $payment));
} catch (\Throwable $throwable) {
$this->logger->critical('Cannot cancel payment.', ['exception' => $throwable]);
Copy link
Member

@lukasz-falda lukasz-falda Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't catch whole exceptions and only log it, we need to know if something happened. Throw custom exception CancelPaymentException and pass parent to it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the try catch completely, it is not needed here

$response = $this->orderRequestService->retrieve($model->orderId(), PaymentHelper::getConfigKey($model, $payment));
$status = $response->orders[0]->status;

return !in_array($status, self::FINAL_STATUSES, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create OrderStatus enum from $status and use method ->isFinal() as I mention above

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some doubts about checking this on CancelAction method. Maybe we need to check it before run CancelAction but this action should proceed only cancel request 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Api always returns a status code of 200, regardless of whether the payment was successfully canceled or not. For example, when you send a transaction cancellation request for a transaction already paid, you get a status of 200, but after checking the status it is still paid. For this reason, I would prefer to check the status before sending the cancellation and throw a custom exception.

$payment = new Payment();
$payment->setDetails(FileTestUtil::decodeJsonFromFile(__DIR__ . '/data/detailsWithOrderId.json'));
$expectedBaseDetails = $this->getExpectedBaseDetails();
self::assertSame($expectedBaseDetails, $payment->getDetails());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you just set the details. We need to check it again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not needed, removed


declare(strict_types=1);

namespace Answear\Payum\PayU\Tests\Integration\Action;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test seems like the unit test, not integration

$this->expectException(PayUAuthorizationException::class);
$this->expectExceptionCode(401);
$this->getOrderRequestService()->cancel('WZHF5FFDRJ140731GUEST000P01', null);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test with 400-500 code from PayU is missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added missing tests

@ropczan ropczan requested a review from lukasz-falda March 13, 2024 14:02
}

public static function isFinal(OrderStatus $status): bool
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe not static will be better? #whatever

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

@ropczan ropczan merged commit 78a0bf1 into main Mar 15, 2024
3 checks passed
@ropczan ropczan deleted the cancel-payment branch March 15, 2024 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants