From 2277fecd6e6e7de749f739cd34489bfd2c545361 Mon Sep 17 00:00:00 2001 From: Luca Gallinari Date: Fri, 8 Apr 2022 17:00:11 +0200 Subject: [PATCH] Cancel payment only if it's possible --- src/Controller/CancelLastPayPalPaymentAction.php | 8 +++++--- src/Controller/CancelPayPalPaymentAction.php | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Controller/CancelLastPayPalPaymentAction.php b/src/Controller/CancelLastPayPalPaymentAction.php index 9d587293..597d9500 100644 --- a/src/Controller/CancelLastPayPalPaymentAction.php +++ b/src/Controller/CancelLastPayPalPaymentAction.php @@ -49,10 +49,12 @@ public function __invoke(Request $request): Response $payment = $order->getLastPayment(); $paymentStateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH); - $paymentStateMachine->apply(PaymentTransitions::TRANSITION_CANCEL); + if ($paymentStateMachine->can(PaymentTransitions::TRANSITION_CANCEL)) { + $paymentStateMachine->apply(PaymentTransitions::TRANSITION_CANCEL); - $this->orderPaymentProcessor->process($order); - $this->objectManager->flush(); + $this->orderPaymentProcessor->process($order); + $this->objectManager->flush(); + } return new Response('', Response::HTTP_NO_CONTENT); } diff --git a/src/Controller/CancelPayPalPaymentAction.php b/src/Controller/CancelPayPalPaymentAction.php index b8e7c06d..7f635ecb 100644 --- a/src/Controller/CancelPayPalPaymentAction.php +++ b/src/Controller/CancelPayPalPaymentAction.php @@ -50,12 +50,14 @@ public function __invoke(Request $request): Response $order = $payment->getOrder(); $paymentStateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH); - $paymentStateMachine->apply(PaymentTransitions::TRANSITION_CANCEL); + if ($paymentStateMachine->can(PaymentTransitions::TRANSITION_CANCEL)) { + $paymentStateMachine->apply(PaymentTransitions::TRANSITION_CANCEL); - $this->orderPaymentProcessor->process($order); - $this->objectManager->flush(); + $this->orderPaymentProcessor->process($order); + $this->objectManager->flush(); - $this->flashBag->add('success', 'sylius.pay_pal.payment_cancelled'); + $this->flashBag->add('success', 'sylius.pay_pal.payment_cancelled'); + } return new Response('', Response::HTTP_NO_CONTENT); }