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

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\PayPalPaymentMethodProviderInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Webmozart\Assert\Assert;
  14. final class PayPalPaymentMethodListener
  15. {
  16.     private OnboardingInitiatorInterface $onboardingInitiator;
  17.     private UrlGeneratorInterface $urlGenerator;
  18.     private FlashBagInterface $flashBag;
  19.     private PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider;
  20.     public function __construct(
  21.         OnboardingInitiatorInterface $onboardingInitiator,
  22.         UrlGeneratorInterface $urlGenerator,
  23.         FlashBagInterface $flashBag,
  24.         PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider
  25.     ) {
  26.         $this->onboardingInitiator $onboardingInitiator;
  27.         $this->urlGenerator $urlGenerator;
  28.         $this->flashBag $flashBag;
  29.         $this->payPalPaymentMethodProvider $payPalPaymentMethodProvider;
  30.     }
  31.     public function initializeCreate(ResourceControllerEvent $event): void
  32.     {
  33.         $paymentMethod $event->getSubject();
  34.         /** @var PaymentMethodInterface $paymentMethod */
  35.         Assert::isInstanceOf($paymentMethodPaymentMethodInterface::class);
  36.         if (!$this->isNewPaymentMethodPayPal($paymentMethod)) {
  37.             return;
  38.         }
  39.         if ($this->isTherePayPalPaymentMethod()) {
  40.             $this->flashBag->add('error''sylius.pay_pal.more_than_one_seller_not_allowed');
  41.             $event->setResponse(new RedirectResponse($this->urlGenerator->generate('sylius_admin_payment_method_index')));
  42.             return;
  43.         }
  44.         if (!$this->onboardingInitiator->supports($paymentMethod)) {
  45.             return;
  46.         }
  47.         $event->setResponse(new RedirectResponse($this->onboardingInitiator->initiate($paymentMethod)));
  48.     }
  49.     private function isNewPaymentMethodPayPal(PaymentMethodInterface $paymentMethod): bool
  50.     {
  51.         /** @var GatewayConfigInterface $gatewayConfig */
  52.         $gatewayConfig $paymentMethod->getGatewayConfig();
  53.         return $gatewayConfig->getFactoryName() === 'sylius.pay_pal';
  54.     }
  55.     private function isTherePayPalPaymentMethod(): bool
  56.     {
  57.         try {
  58.             $this->payPalPaymentMethodProvider->provide();
  59.         } catch (PayPalPaymentMethodNotFoundException $exception) {
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64. }