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

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.     private RequestStack $requestStack;
  14.     public function __construct(RequestStack $requestStack)
  15.     {
  16.         $this->requestStack $requestStack;
  17.     }
  18.     /**
  19.      * {@inheritdoc}
  20.      */
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             'framework.validation.address.create' => 'disableAdditionalAddressValidation',
  25.             'framework.validation.address.update' => 'disableAdditionalAddressValidation',
  26.             'framework.validation.customer.create' => 'disableBirthdayValidation',
  27.             BuildValidationEvent::class => 'disableConfirmPageLoaderAddressValidation',
  28.         ];
  29.     }
  30.     /**
  31.      * This additional event listener is needed because of autoloading misbehaviour.
  32.      */
  33.     public function disableConfirmPageLoaderAddressValidation(BuildValidationEvent $event): void
  34.     {
  35.         $request $this->requestStack->getCurrentRequest();
  36.         if ($request === null) {
  37.             return;
  38.         }
  39.         $route $request->get('_route');
  40.         /** @var SalesChannelContext|null $salesChannelContext */
  41.         $salesChannelContext $request->get('sw-sales-channel-context');
  42.         if ($route === null || $route !== 'frontend.checkout.confirm.page') {
  43.             return;
  44.         }
  45.         if ($salesChannelContext === null) {
  46.             return;
  47.         }
  48.         if ($salesChannelContext->getPaymentMethod()->getHandlerIdentifier() !== PayonePaypalExpressPaymentHandler::class) {
  49.             return;
  50.         }
  51.         $this->markAddressFieldsAsOptional($event->getDefinition());
  52.     }
  53.     public function disableAdditionalAddressValidation(BuildValidationEvent $event): void
  54.     {
  55.         $request $this->requestStack->getCurrentRequest();
  56.         if ($request === null) {
  57.             return;
  58.         }
  59.         if (\mb_strpos($request->getPathInfo(), '/payone/paypal/redirect-handler') === false) {
  60.             return;
  61.         }
  62.         $this->markAddressFieldsAsOptional($event->getDefinition());
  63.     }
  64.     public function disableBirthdayValidation(BuildValidationEvent $event): void
  65.     {
  66.         $request $this->requestStack->getCurrentRequest();
  67.         if ($request === null) {
  68.             return;
  69.         }
  70.         if (\mb_strpos($request->getPathInfo(), '/payone/paypal/redirect-handler') === false) {
  71.             return;
  72.         }
  73.         $definition $event->getDefinition();
  74.         $definition->set('birthdayDay', new Optional());
  75.         $definition->set('birthdayMonth', new Optional());
  76.         $definition->set('birthdayYear', new Optional());
  77.     }
  78.     private function markAddressFieldsAsOptional(DataValidationDefinition $definition): void
  79.     {
  80.         $definition->set('additionalAddressLine1', new Optional());
  81.         $definition->set('additionalAddressLine2', new Optional());
  82.         $definition->set('phoneNumber', new Optional());
  83.     }
  84. }