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

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\PayonePayolutionInstallment;
  10. use PayonePayment\PaymentMethod\PayonePayolutionInvoicing;
  11. use PayonePayment\PaymentMethod\PayoneSecureInvoice;
  12. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  13. use Shopware\Core\PlatformRequest;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\Validator\Constraints\NotBlank;
  19. class OrderValidationEventListener implements EventSubscriberInterface
  20. {
  21.     /** @var RequestStack */
  22.     private $requestStack;
  23.     /** @var ConfigReaderInterface */
  24.     private $configReader;
  25.     public function __construct(RequestStack $requestStackConfigReaderInterface $configReader)
  26.     {
  27.         $this->requestStack $requestStack;
  28.         $this->configReader $configReader;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             'framework.validation.order.create' => 'validateOrderData',
  34.         ];
  35.     }
  36.     public function validateOrderData(BuildValidationEvent $event): void
  37.     {
  38.         $request $this->requestStack->getCurrentRequest();
  39.         if (null === $request) {
  40.             return;
  41.         }
  42.         // TODO: can be removed when https://github.com/shopware/platform/pull/226 is merged
  43.         $context $this->getContextFromRequest($request);
  44.         $customer $context->getCustomer();
  45.         if ($this->isSecureInvoicePayment($context) && $customer !== null) {
  46.             $activeBilling $customer->getActiveBillingAddress();
  47.             if ($activeBilling !== null && empty($activeBilling->getCompany())) {
  48.                 $event->getDefinition()->add(
  49.                     'secureInvoiceBirthday',
  50.                     new Birthday(['value' => $this->getMinimumDate()])
  51.                 );
  52.             }
  53.         }
  54.         if ($this->isPayonePayolutionInstallment($context) || $this->isPayonePayolutionInvoicing($context)) {
  55.             $event->getDefinition()->add(
  56.                 'payolutionConsent',
  57.                 new NotBlank()
  58.             );
  59.             $event->getDefinition()->add(
  60.                 'payolutionBirthday',
  61.                 new Birthday(['value' => $this->getMinimumDate()])
  62.             );
  63.             if ($this->isPayonePayolutionInstallment($context)) {
  64.                 if ($this->customerHasCompanyAddress($context)) {
  65.                     $event->getDefinition()->add(
  66.                         'payonePaymentMethod',
  67.                         new PaymentMethod(['value' => $context->getPaymentMethod()])
  68.                     );
  69.                 }
  70.             }
  71.             if ($this->isPayonePayolutionInvoicing($context) && $this->companyDataHandlingIsDisabled($context)) {
  72.                 if ($this->customerHasCompanyAddress($context)) {
  73.                     $event->getDefinition()->add(
  74.                         'payonePaymentMethod',
  75.                         new PaymentMethod(['value' => $context->getPaymentMethod()])
  76.                     );
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     private function getMinimumDate(): DateTimeInterface
  82.     {
  83.         return (new DateTime())->modify('-18 years')->setTime(00);
  84.     }
  85.     private function getContextFromRequest(Request $request): SalesChannelContext
  86.     {
  87.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  88.     }
  89.     private function isPayonePayolutionInstallment(SalesChannelContext $context): bool
  90.     {
  91.         return $context->getPaymentMethod()->getId() === PayonePayolutionInstallment::UUID;
  92.     }
  93.     private function isPayonePayolutionInvoicing(SalesChannelContext $context): bool
  94.     {
  95.         return $context->getPaymentMethod()->getId() === PayonePayolutionInvoicing::UUID;
  96.     }
  97.     private function isSecureInvoicePayment(SalesChannelContext $context): bool
  98.     {
  99.         return $context->getPaymentMethod()->getId() === PayoneSecureInvoice::UUID;
  100.     }
  101.     private function customerHasCompanyAddress(SalesChannelContext $context): bool
  102.     {
  103.         $customer $context->getCustomer();
  104.         if (null === $customer) {
  105.             return false;
  106.         }
  107.         $billingAddress $customer->getActiveBillingAddress();
  108.         if (null === $billingAddress) {
  109.             return false;
  110.         }
  111.         return !empty($billingAddress->getCompany());
  112.     }
  113.     private function companyDataHandlingIsDisabled(SalesChannelContext $context): bool
  114.     {
  115.         $configuration $this->configReader->read($context->getSalesChannel()->getId());
  116.         return !((bool) $configuration->get('payolutionInvoicingTransferCompanyData'));
  117.     }
  118. }