Skip to content

Commit 8e85c0f

Browse files
LucaGallinariNoResponseMate
authored andcommitted
Cancel payment only if it's possible
1 parent 96c6077 commit 8e85c0f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/Controller/CancelPayPalPaymentAction.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,24 @@ public function __invoke(Request $request): Response
5959
* @var string $content
6060
*/
6161
$content = $request->getContent();
62-
6362
$content = (array) json_decode($content, true);
6463

6564
$payment = $this->paymentProvider->getByPayPalOrderId((string) $content['payPalOrderId']);
6665

6766
/** @var OrderInterface $order */
6867
$order = $payment->getOrder();
6968

70-
$this->getStateMachine()->apply($payment, PaymentTransitions::GRAPH, PaymentTransitions::TRANSITION_CANCEL);
69+
$paymentStateMachine = $this->getStateMachine();
70+
if ($paymentStateMachine->can($payment, PaymentTransitions::GRAPH, PaymentTransitions::TRANSITION_CANCEL)) {
71+
$paymentStateMachine->apply($payment, PaymentTransitions::GRAPH, PaymentTransitions::TRANSITION_CANCEL);
7172

72-
$this->orderPaymentProcessor->process($order);
73-
$this->objectManager->flush();
73+
$this->orderPaymentProcessor->process($order);
74+
$this->objectManager->flush();
7475

75-
FlashBagProvider::getFlashBag($this->flashBagOrRequestStack)
76-
->add('success', 'sylius.pay_pal.payment_cancelled')
77-
;
76+
FlashBagProvider::getFlashBag($this->flashBagOrRequestStack)
77+
->add('success', 'sylius.pay_pal.payment_cancelled')
78+
;
79+
}
7880

7981
return new Response('', Response::HTTP_NO_CONTENT);
8082
}

0 commit comments

Comments
 (0)