vendor/sylius/paypal-plugin/src/Listener/PayPalPaymentMethodListener.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sylius\PayPalPlugin\Listener;
  4. use Payum\Core\Model\GatewayConfigInterface;
  5. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  6. use Sylius\Component\Core\Model\PaymentMethodInterface;
  7. use Sylius\PayPalPlugin\Exception\PayPalPaymentMethodNotFoundException;
  8. use Sylius\PayPalPlugin\Onboarding\Initiator\OnboardingInitiatorInterface;
  9. use Sylius\PayPalPlugin\Provider\FlashBagProvider;
  10. use Sylius\PayPalPlugin\Provider\PayPalPaymentMethodProviderInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Webmozart\Assert\Assert;
  16. final class PayPalPaymentMethodListener
  17. {
  18.     private OnboardingInitiatorInterface $onboardingInitiator;
  19.     private UrlGeneratorInterface $urlGenerator;
  20.     private FlashBagInterface|RequestStack $flashBagOrRequestStack;
  21.     private PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider;
  22.     public function __construct(
  23.         OnboardingInitiatorInterface $onboardingInitiator,
  24.         UrlGeneratorInterface $urlGenerator,
  25.         FlashBagInterface|RequestStack $flashBagOrRequestStack,
  26.         PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider
  27.     ) {
  28.         if ($flashBagOrRequestStack instanceof FlashBagInterface) {
  29.             trigger_deprecation('sylius/paypal-plugin''1.5'sprintf('Passing an instance of %s as constructor argument for %s is deprecated as of PayPalPlugin 1.5 and will be removed in 2.0. Pass an instance of %s instead.'FlashBagInterface::class, self::class, RequestStack::class));
  30.         }
  31.         $this->onboardingInitiator $onboardingInitiator;
  32.         $this->urlGenerator $urlGenerator;
  33.         $this->flashBagOrRequestStack $flashBagOrRequestStack;
  34.         $this->payPalPaymentMethodProvider $payPalPaymentMethodProvider;
  35.     }
  36.     public function initializeCreate(ResourceControllerEvent $event): void
  37.     {
  38.         /** @var object $paymentMethod */
  39.         $paymentMethod $event->getSubject();
  40.         /** @var PaymentMethodInterface $paymentMethod */
  41.         Assert::isInstanceOf($paymentMethodPaymentMethodInterface::class);
  42.         if (!$this->isNewPaymentMethodPayPal($paymentMethod)) {
  43.             return;
  44.         }
  45.         if ($this->isTherePayPalPaymentMethod()) {
  46.             FlashBagProvider::getFlashBag($this->flashBagOrRequestStack)
  47.                 ->add('error''sylius.pay_pal.more_than_one_seller_not_allowed')
  48.             ;
  49.             $event->setResponse(new RedirectResponse($this->urlGenerator->generate('sylius_admin_payment_method_index')));
  50.             return;
  51.         }
  52.         if (!$this->onboardingInitiator->supports($paymentMethod)) {
  53.             return;
  54.         }
  55.         $event->setResponse(new RedirectResponse($this->onboardingInitiator->initiate($paymentMethod)));
  56.     }
  57.     private function isNewPaymentMethodPayPal(PaymentMethodInterface $paymentMethod): bool
  58.     {
  59.         /** @var GatewayConfigInterface $gatewayConfig */
  60.         $gatewayConfig $paymentMethod->getGatewayConfig();
  61.         return $gatewayConfig->getFactoryName() === 'sylius.pay_pal';
  62.     }
  63.     private function isTherePayPalPaymentMethod(): bool
  64.     {
  65.         try {
  66.             $this->payPalPaymentMethodProvider->provide();
  67.         } catch (PayPalPaymentMethodNotFoundException $exception) {
  68.             return false;
  69.         }
  70.         return true;
  71.     }
  72. }