custom/plugins/PayonePayment/src/EventListener/PaypalExpressAddressValidationEventListener.php line 41

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\PaymentHandler\PayonePaypalExpressPaymentHandler;
  5. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  6. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Validator\Constraints\Optional;
  11. class PaypalExpressAddressValidationEventListener implements EventSubscriberInterface
  12. {
  13.     /** @var RequestStack */
  14.     private $requestStack;
  15.     public function __construct(RequestStack $requestStack)
  16.     {
  17.         $this->requestStack $requestStack;
  18.     }
  19.     /**
  20.      * {@inheritdoc}
  21.      */
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             'framework.validation.address.create'  => 'disableAdditionalAddressValidation',
  26.             'framework.validation.address.update'  => 'disableAdditionalAddressValidation',
  27.             'framework.validation.customer.create' => 'disableBirthdayValidation',
  28.             BuildValidationEvent::class            => 'disableConfirmPageLoaderAddressValidation',
  29.         ];
  30.     }
  31.     /**
  32.      * This additional event listener is needed because of autoloading misbehaviour.
  33.      */
  34.     public function disableConfirmPageLoaderAddressValidation(BuildValidationEvent $event): void
  35.     {
  36.         $request $this->requestStack->getCurrentRequest();
  37.         if (null === $request) {
  38.             return;
  39.         }
  40.         $route $request->get('_route');
  41.         /** @var null|SalesChannelContext $salesChannelContext */
  42.         $salesChannelContext $request->get('sw-sales-channel-context');
  43.         if (null === $route || $route !== 'frontend.checkout.confirm.page') {
  44.             return;
  45.         }
  46.         if (null === $salesChannelContext) {
  47.             return;
  48.         }
  49.         if ($salesChannelContext->getPaymentMethod()->getHandlerIdentifier() !== PayonePaypalExpressPaymentHandler::class) {
  50.             return;
  51.         }
  52.         $this->markAddressFieldsAsOptional($event->getDefinition());
  53.     }
  54.     public function disableAdditionalAddressValidation(BuildValidationEvent $event): void
  55.     {
  56.         $request $this->requestStack->getCurrentRequest();
  57.         if ($request === null) {
  58.             return;
  59.         }
  60.         if (\mb_strpos($request->getPathInfo(), '/payone/paypal/redirect-handler') === false) {
  61.             return;
  62.         }
  63.         $this->markAddressFieldsAsOptional($event->getDefinition());
  64.     }
  65.     public function disableBirthdayValidation(BuildValidationEvent $event): void
  66.     {
  67.         $request $this->requestStack->getCurrentRequest();
  68.         if ($request === null) {
  69.             return;
  70.         }
  71.         if (\mb_strpos($request->getPathInfo(), '/payone/paypal/redirect-handler') === false) {
  72.             return;
  73.         }
  74.         $definition $event->getDefinition();
  75.         $definition->set('birthdayDay', new Optional());
  76.         $definition->set('birthdayMonth', new Optional());
  77.         $definition->set('birthdayYear', new Optional());
  78.     }
  79.     private function markAddressFieldsAsOptional(DataValidationDefinition $definition): void
  80.     {
  81.         $definition->set('additionalAddressLine1', new Optional());
  82.         $definition->set('additionalAddressLine2', new Optional());
  83.         $definition->set('phoneNumber', new Optional());
  84.     }
  85. }