vendor/sylius/sylius/src/Sylius/Bundle/PayumBundle/Controller/PayumController.php line 64

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\PayumBundle\Controller;
  12. use Payum\Core\Model\GatewayConfigInterface;
  13. use Payum\Core\Payum;
  14. use Payum\Core\Request\Generic;
  15. use Payum\Core\Request\GetStatusInterface;
  16. use Payum\Core\Security\GenericTokenFactoryInterface;
  17. use Payum\Core\Security\HttpRequestVerifierInterface;
  18. use Payum\Core\Security\TokenInterface;
  19. use Sylius\Bundle\PayumBundle\Factory\GetStatusFactoryInterface;
  20. use Sylius\Bundle\PayumBundle\Factory\ResolveNextRouteFactoryInterface;
  21. use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface;
  22. use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
  23. use Sylius\Component\Core\Model\OrderInterface;
  24. use Sylius\Component\Core\Model\PaymentInterface;
  25. use Sylius\Component\Core\Model\PaymentMethodInterface;
  26. use Sylius\Component\Order\Repository\OrderRepositoryInterface;
  27. use Sylius\Component\Resource\Metadata\MetadataInterface;
  28. use Symfony\Component\HttpFoundation\RedirectResponse;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpFoundation\Response;
  31. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  32. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  33. use Symfony\Component\Routing\RouterInterface;
  34. final class PayumController
  35. {
  36.     public function __construct(
  37.         private Payum $payum,
  38.         private OrderRepositoryInterface $orderRepository,
  39.         private MetadataInterface $orderMetadata,
  40.         private RequestConfigurationFactoryInterface $requestConfigurationFactory,
  41.         private ViewHandlerInterface $viewHandler,
  42.         private RouterInterface $router,
  43.         private GetStatusFactoryInterface $getStatusRequestFactory,
  44.         private ResolveNextRouteFactoryInterface $resolveNextRouteRequestFactory
  45.     ) {
  46.     }
  47.     public function prepareCaptureAction(Request $request$tokenValue): Response
  48.     {
  49.         $configuration $this->requestConfigurationFactory->create($this->orderMetadata$request);
  50.         /** @var OrderInterface|null $order */
  51.         $order $this->orderRepository->findOneByTokenValue($tokenValue);
  52.         if (null === $order) {
  53.             throw new NotFoundHttpException(sprintf('Order with token "%s" does not exist.'$tokenValue));
  54.         }
  55.         $request->getSession()->set('sylius_order_id'$order->getId());
  56.         $payment $order->getLastPayment(PaymentInterface::STATE_NEW);
  57.         if (null === $payment) {
  58.             $url $this->router->generate('sylius_shop_order_thank_you');
  59.             return new RedirectResponse($url);
  60.         }
  61.         $token $this->provideTokenBasedOnPayment($payment$configuration->getParameters()->get('redirect'));
  62.         return new RedirectResponse($token->getTargetUrl());
  63.     }
  64.     public function afterCaptureAction(Request $request): Response
  65.     {
  66.         $configuration $this->requestConfigurationFactory->create($this->orderMetadata$request);
  67.         $token $this->getHttpRequestVerifier()->verify($request);
  68.         /** @var Generic&GetStatusInterface $status */
  69.         $status $this->getStatusRequestFactory->createNewWithModel($token);
  70.         $this->payum->getGateway($token->getGatewayName())->execute($status);
  71.         $resolveNextRoute $this->resolveNextRouteRequestFactory->createNewWithModel($status->getFirstModel());
  72.         $this->payum->getGateway($token->getGatewayName())->execute($resolveNextRoute);
  73.         $this->getHttpRequestVerifier()->invalidate($token);
  74.         if (PaymentInterface::STATE_NEW !== $status->getValue()) {
  75.             /** @var FlashBagInterface $flashBag */
  76.             $flashBag $request->getSession()->getBag('flashes');
  77.             $flashBag->add('info'sprintf('sylius.payment.%s'$status->getValue()));
  78.         }
  79.         return new RedirectResponse($this->router->generate($resolveNextRoute->getRouteName(), $resolveNextRoute->getRouteParameters()));
  80.     }
  81.     private function getTokenFactory(): GenericTokenFactoryInterface
  82.     {
  83.         return $this->payum->getTokenFactory();
  84.     }
  85.     private function getHttpRequestVerifier(): HttpRequestVerifierInterface
  86.     {
  87.         return $this->payum->getHttpRequestVerifier();
  88.     }
  89.     private function provideTokenBasedOnPayment(PaymentInterface $payment, array $redirectOptions): TokenInterface
  90.     {
  91.         /** @var PaymentMethodInterface $paymentMethod */
  92.         $paymentMethod $payment->getMethod();
  93.         /** @var GatewayConfigInterface $gatewayConfig */
  94.         $gatewayConfig $paymentMethod->getGatewayConfig();
  95.         if (isset($gatewayConfig->getConfig()['use_authorize']) && true === (bool) $gatewayConfig->getConfig()['use_authorize']) {
  96.             $token $this->getTokenFactory()->createAuthorizeToken(
  97.                 $gatewayConfig->getGatewayName(),
  98.                 $payment,
  99.                 $redirectOptions['route']
  100.                     ?? null,
  101.                 $redirectOptions['parameters']
  102.                     ?? []
  103.             );
  104.         } else {
  105.             $token $this->getTokenFactory()->createCaptureToken(
  106.                 $gatewayConfig->getGatewayName(),
  107.                 $payment,
  108.                 $redirectOptions['route']
  109.                     ?? null,
  110.                 $redirectOptions['parameters']
  111.                     ?? []
  112.             );
  113.         }
  114.         return $token;
  115.     }
  116. }