Skip to content

Commit

Permalink
Cancel action now sets cancel status in details so that it will be ca…
Browse files Browse the repository at this point in the history
…ncelled by the Payum Bundle
  • Loading branch information
LucaGallinari committed Sep 11, 2024
1 parent 07c7e44 commit 8737d5e
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 deletions src/Payum/Action/CancelAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

namespace Webgriffe\SyliusPausePayPlugin\Payum\Action;

use Doctrine\Persistence\ObjectManager;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Reply\HttpRedirect;
use Payum\Core\Request\Cancel;
use Psr\Log\LoggerInterface;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface as SyliusPaymentInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
use Sylius\Component\Payment\PaymentTransitions;
use Symfony\Component\Routing\RouterInterface;
use Webgriffe\SyliusPausePayPlugin\Client\PaymentState;
use Webgriffe\SyliusPausePayPlugin\Helper\PaymentDetailsHelper;
use Webmozart\Assert\Assert;
Expand All @@ -25,9 +23,7 @@ final class CancelAction implements ActionInterface
{
public function __construct(
private LoggerInterface $logger,
private ObjectManager $objectManager,
private FactoryInterface $stateMachineFactory,
private OrderProcessorInterface $orderPaymentProcessor,
private RouterInterface $router,
) {
}

Expand All @@ -47,39 +43,27 @@ public function execute($request): void

$this->logInfo($payment, 'Start cancel action');

$paymentStateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH);
if (!$paymentStateMachine->can(PaymentTransitions::TRANSITION_CANCEL)) {
$this->logInfo($payment, 'Payment cannot be cancelled.');

return;
}

/** @var PaymentDetails $paymentDetails */
$paymentDetails = $payment->getDetails();
PaymentDetailsHelper::assertPaymentDetailsAreValid($paymentDetails);

$paymentStateMachine->apply(PaymentTransitions::TRANSITION_CANCEL);
$this->logInfo($payment, 'Cancelled payment. Start processing order.');

/** @var SyliusPaymentInterface $lastPayment */
$lastPayment = $order->getLastPayment();
if ($lastPayment->getState() === SyliusPaymentInterface::STATE_NEW) {
$this->objectManager->flush();
$this->logInfo($payment, 'Order flushed.');

return;
}
$this->logger->info('Redirecting the user to the Sylius Pagolight waiting page.');

$this->orderPaymentProcessor->process($order);
$this->objectManager->flush();
$order = $payment->getOrder();
Assert::isInstanceOf($order, OrderInterface::class);

$paymentDetails = PaymentDetailsHelper::addPaymentStatus(
$paymentDetails,
PaymentState::CANCELLED,
);
$payment->setDetails($paymentDetails);

$this->logInfo($payment, 'Order processed and flushed.');
throw new HttpRedirect(
$this->router->generate('webgriffe_sylius_pagolight_plugin_payment_process', [
'tokenValue' => $order->getTokenValue(),
'_locale' => $order->getLocaleCode(),
]),
);
}

public function supports($request): bool
Expand All @@ -89,6 +73,6 @@ public function supports($request): bool

private function logInfo(SyliusPaymentInterface $payment, string $message, array $context = []): void
{
$this->logger->info(sprintf('[Payment #%s]: %s.', (string) $payment->getId(), $message, ), $context);
$this->logger->info(sprintf('[Payment #%s]: %s.', (string) $payment->getId(), $message,), $context);
}
}

0 comments on commit 8737d5e

Please sign in to comment.