vendor/sylius/paypal-plugin/src/Controller/PayPalButtonsController.php line 58

  1. <?php
  2. declare(strict_types=1);
  3. namespace Sylius\PayPalPlugin\Controller;
  4. use Sylius\Component\Channel\Context\ChannelContextInterface;
  5. use Sylius\Component\Core\Model\ChannelInterface;
  6. use Sylius\Component\Core\Model\OrderInterface;
  7. use Sylius\Component\Core\Repository\OrderRepositoryInterface;
  8. use Sylius\Component\Locale\Context\LocaleContextInterface;
  9. use Sylius\PayPalPlugin\Processor\LocaleProcessorInterface;
  10. use Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface;
  11. use Sylius\PayPalPlugin\Provider\PayPalConfigurationProviderInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Twig\Environment;
  16. final class PayPalButtonsController
  17. {
  18.     private Environment $twig;
  19.     private UrlGeneratorInterface $router;
  20.     private ChannelContextInterface $channelContext;
  21.     private LocaleContextInterface $localeContext;
  22.     private PayPalConfigurationProviderInterface $payPalConfigurationProvider;
  23.     private OrderRepositoryInterface $orderRepository;
  24.     private AvailableCountriesProviderInterface $availableCountriesProvider;
  25.     private LocaleProcessorInterface $localeProcessor;
  26.     public function __construct(
  27.         Environment $twig,
  28.         UrlGeneratorInterface $router,
  29.         ChannelContextInterface $channelContext,
  30.         LocaleContextInterface $localeContext,
  31.         PayPalConfigurationProviderInterface $payPalConfigurationProvider,
  32.         OrderRepositoryInterface $orderRepository,
  33.         AvailableCountriesProviderInterface $availableCountriesProvider,
  34.         LocaleProcessorInterface $localeProcessor
  35.     ) {
  36.         $this->twig $twig;
  37.         $this->router $router;
  38.         $this->channelContext $channelContext;
  39.         $this->localeContext $localeContext;
  40.         $this->payPalConfigurationProvider $payPalConfigurationProvider;
  41.         $this->orderRepository $orderRepository;
  42.         $this->availableCountriesProvider $availableCountriesProvider;
  43.         $this->localeProcessor $localeProcessor;
  44.     }
  45.     public function renderProductPageButtonsAction(Request $request): Response
  46.     {
  47.         $productId $request->attributes->getInt('productId');
  48.         /** @var ChannelInterface $channel */
  49.         $channel $this->channelContext->getChannel();
  50.         try {
  51.             return new Response($this->twig->render('@SyliusPayPalPlugin/payFromProductPage.html.twig', [
  52.                 'available_countries' => $this->availableCountriesProvider->provide(),
  53.                 'clientId' => $this->payPalConfigurationProvider->getClientId($channel),
  54.                 'completeUrl' => $this->router->generate('sylius_shop_checkout_complete'),
  55.                 'createPayPalOrderFromProductUrl' => $this->router->generate('sylius_paypal_plugin_create_paypal_order_from_product', ['productId' => $productId]),
  56.                 'errorPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_payment_error'),
  57.                 'locale' => $this->localeProcessor->process($this->localeContext->getLocaleCode()),
  58.                 'processPayPalOrderUrl' => $this->router->generate('sylius_paypal_plugin_process_paypal_order'),
  59.             ]));
  60.         } catch (\InvalidArgumentException $exception) {
  61.             return new Response('');
  62.         }
  63.     }
  64.     public function renderCartPageButtonsAction(Request $request): Response
  65.     {
  66.         $orderId $request->attributes->getInt('orderId');
  67.         /** @var ChannelInterface $channel */
  68.         $channel $this->channelContext->getChannel();
  69.         /** @var OrderInterface $order */
  70.         $order $this->orderRepository->find($orderId);
  71.         try {
  72.             return new Response($this->twig->render('@SyliusPayPalPlugin/payFromCartPage.html.twig', [
  73.                 'available_countries' => $this->availableCountriesProvider->provide(),
  74.                 'clientId' => $this->payPalConfigurationProvider->getClientId($channel),
  75.                 'completeUrl' => $this->router->generate('sylius_shop_checkout_complete'),
  76.                 'createPayPalOrderFromCartUrl' => $this->router->generate('sylius_paypal_plugin_create_paypal_order_from_cart', ['id' => $orderId]),
  77.                 'currency' => $order->getCurrencyCode(),
  78.                 'errorPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_payment_error'),
  79.                 'locale' => $this->localeProcessor->process((string) $order->getLocaleCode()),
  80.                 'orderId' => $orderId,
  81.                 'partnerAttributionId' => $this->payPalConfigurationProvider->getPartnerAttributionId($channel),
  82.                 'processPayPalOrderUrl' => $this->router->generate('sylius_paypal_plugin_process_paypal_order'),
  83.             ]));
  84.         } catch (\InvalidArgumentException $exception) {
  85.             return new Response('');
  86.         }
  87.     }
  88.     public function renderPaymentPageButtonsAction(Request $request): Response
  89.     {
  90.         $orderId $request->attributes->getInt('orderId');
  91.         /** @var ChannelInterface $channel */
  92.         $channel $this->channelContext->getChannel();
  93.         /** @var OrderInterface $order */
  94.         $order $this->orderRepository->find($orderId);
  95.         try {
  96.             return new Response($this->twig->render('@SyliusPayPalPlugin/payFromPaymentPage.html.twig', [
  97.                 'available_countries' => $this->availableCountriesProvider->provide(),
  98.                 'cancelPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_cancel_payment'),
  99.                 'clientId' => $this->payPalConfigurationProvider->getClientId($channel),
  100.                 'currency' => $order->getCurrencyCode(),
  101.                 'completePayPalOrderFromPaymentPageUrl' => $this->router->generate('sylius_paypal_plugin_complete_paypal_order_from_payment_page', ['id' => $orderId]),
  102.                 'createPayPalOrderFromPaymentPageUrl' => $this->router->generate('sylius_paypal_plugin_create_paypal_order_from_payment_page', ['id' => $orderId]),
  103.                 'errorPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_payment_error'),
  104.                 'locale' => $this->localeProcessor->process((string) $order->getLocaleCode()),
  105.                 'orderId' => $orderId,
  106.                 'partnerAttributionId' => $this->payPalConfigurationProvider->getPartnerAttributionId($channel),
  107.             ]));
  108.         } catch (\InvalidArgumentException $exception) {
  109.             return new Response('');
  110.         }
  111.     }
  112. }