custom/plugins/PayonePayment/src/EventListener/OrderValidationEventListener.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use DateTime;
  5. use DateTimeInterface;
  6. use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
  7. use PayonePayment\Components\Validator\Birthday;
  8. use PayonePayment\Components\Validator\PaymentMethod;
  9. use PayonePayment\PaymentMethod\PayoneOpenInvoice;
  10. use PayonePayment\PaymentMethod\PayonePayolutionInstallment;
  11. use PayonePayment\PaymentMethod\PayonePayolutionInvoicing;
  12. use PayonePayment\PaymentMethod\PayoneSecureInvoice;
  13. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  14. use Shopware\Core\PlatformRequest;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\Validator\Constraints\NotBlank;
  20. class OrderValidationEventListener implements EventSubscriberInterface
  21. {
  22.     /** @var RequestStack */
  23.     private $requestStack;
  24.     /** @var ConfigReaderInterface */
  25.     private $configReader;
  26.     public function __construct(RequestStack $requestStackConfigReaderInterface $configReader)
  27.     {
  28.         $this->requestStack $requestStack;
  29.         $this->configReader $configReader;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             'framework.validation.order.create' => 'validateOrderData',
  35.         ];
  36.     }
  37.     public function validateOrderData(BuildValidationEvent $event): void
  38.     {
  39.         $request $this->requestStack->getCurrentRequest();
  40.         if (null === $request) {
  41.             return;
  42.         }
  43.         $context $this->getContextFromRequest($request);
  44.         $this->addInvoiceValidationDefinitions($context$event);
  45.         $this->addPayolutionInvoicingValidationDefinitions($context$event);
  46.         $this->addPayolutionInstallmentValidationDefinitions($context$event);
  47.     }
  48.     private function addInvoiceValidationDefinitions(SalesChannelContext $salesChannelContextBuildValidationEvent $event): void
  49.     {
  50.         $customer $salesChannelContext->getCustomer();
  51.         if (null === $customer) {
  52.             return;
  53.         }
  54.         if ($this->isInvoicePayment($salesChannelContext) === false) {
  55.             return;
  56.         }
  57.         $activeBilling $customer->getActiveBillingAddress();
  58.         if ($activeBilling !== null && empty($activeBilling->getCompany())) {
  59.             $event->getDefinition()->add(
  60.                 'payoneInvoiceBirthday',
  61.                 new Birthday(['value' => $this->getMinimumDate()])
  62.             );
  63.         }
  64.     }
  65.     private function addPayolutionInvoicingValidationDefinitions(SalesChannelContext $salesChannelContextBuildValidationEvent $event): void
  66.     {
  67.         if ($this->isPayonePayolutionInvoicing($salesChannelContext) === false) {
  68.             return;
  69.         }
  70.         $event->getDefinition()->add(
  71.             'payolutionConsent',
  72.             new NotBlank()
  73.         );
  74.         $event->getDefinition()->add(
  75.             'payolutionBirthday',
  76.             new Birthday(['value' => $this->getMinimumDate()])
  77.         );
  78.         if ($this->companyDataHandlingIsDisabled($salesChannelContext) === false) {
  79.             return;
  80.         }
  81.         if ($this->customerHasCompanyAddress($salesChannelContext)) {
  82.             $event->getDefinition()->add(
  83.                 'payonePaymentMethod',
  84.                 new PaymentMethod(['value' => $salesChannelContext->getPaymentMethod()])
  85.             );
  86.         }
  87.     }
  88.     private function addPayolutionInstallmentValidationDefinitions(SalesChannelContext $salesChannelContextBuildValidationEvent $event): void
  89.     {
  90.         if ($this->isPayonePayolutionInstallment($salesChannelContext) === false) {
  91.             return;
  92.         }
  93.         $event->getDefinition()->add(
  94.             'payolutionConsent',
  95.             new NotBlank()
  96.         );
  97.         $event->getDefinition()->add(
  98.             'payolutionBirthday',
  99.             new Birthday(['value' => $this->getMinimumDate()])
  100.         );
  101.         if ($this->customerHasCompanyAddress($salesChannelContext)) {
  102.             $event->getDefinition()->add(
  103.                 'payonePaymentMethod',
  104.                 new PaymentMethod(['value' => $salesChannelContext->getPaymentMethod()])
  105.             );
  106.         }
  107.     }
  108.     private function getMinimumDate(): DateTimeInterface
  109.     {
  110.         return (new DateTime())->modify('-18 years')->setTime(00);
  111.     }
  112.     private function getContextFromRequest(Request $request): SalesChannelContext
  113.     {
  114.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  115.     }
  116.     private function isPayonePayolutionInstallment(SalesChannelContext $context): bool
  117.     {
  118.         return $context->getPaymentMethod()->getId() === PayonePayolutionInstallment::UUID;
  119.     }
  120.     private function isPayonePayolutionInvoicing(SalesChannelContext $context): bool
  121.     {
  122.         return $context->getPaymentMethod()->getId() === PayonePayolutionInvoicing::UUID;
  123.     }
  124.     private function isInvoicePayment(SalesChannelContext $context): bool
  125.     {
  126.         return in_array($context->getPaymentMethod()->getId(), [PayoneSecureInvoice::UUIDPayoneOpenInvoice::UUID]);
  127.     }
  128.     private function customerHasCompanyAddress(SalesChannelContext $context): bool
  129.     {
  130.         $customer $context->getCustomer();
  131.         if (null === $customer) {
  132.             return false;
  133.         }
  134.         $billingAddress $customer->getActiveBillingAddress();
  135.         if (null === $billingAddress) {
  136.             return false;
  137.         }
  138.         return !empty($billingAddress->getCompany());
  139.     }
  140.     private function companyDataHandlingIsDisabled(SalesChannelContext $context): bool
  141.     {
  142.         $configuration $this->configReader->read($context->getSalesChannel()->getId());
  143.         return !((bool) $configuration->get('payolutionInvoicingTransferCompanyData'));
  144.     }
  145. }