Skip to content

Commit

Permalink
Cancel payment only if it's possible
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaGallinari authored and lruozzi9 committed Oct 18, 2023
1 parent 6862ed6 commit 2277fec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/Controller/CancelLastPayPalPaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Controller/CancelPayPalPaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 2277fec

Please sign in to comment.