custom/plugins/RpayPayments/src/Components/Checkout/Subscriber/CheckoutValidationSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (c) Ratepay GmbH
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace Ratepay\RpayPayments\Components\Checkout\Subscriber;
  10. use Ratepay\RpayPayments\Components\PaymentHandler\AbstractPaymentHandler;
  11. use Ratepay\RpayPayments\Util\DataValidationHelper;
  12. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\PaymentHandlerRegistry;
  13. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  14. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  15. use Shopware\Core\PlatformRequest;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. class CheckoutValidationSubscriber implements EventSubscriberInterface
  21. {
  22.     private RequestStack $requestStack;
  23.     private ContainerInterface $container;
  24.     private PaymentHandlerRegistry $paymentHandlerRegistry;
  25.     public function __construct(
  26.         RequestStack $requestStack,
  27.         PaymentHandlerRegistry $paymentHandlerRegistry
  28.     ) {
  29.         $this->requestStack $requestStack;
  30.         $this->paymentHandlerRegistry $paymentHandlerRegistry;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             'framework.validation.order.create' => ['validateOrderData'10],
  36.         ];
  37.     }
  38.     public function validateOrderData(BuildValidationEvent $event): void
  39.     {
  40.         $request $this->requestStack->getCurrentRequest();
  41.         if (null === $request) {
  42.             return;
  43.         }
  44.         $salesChannelContext $this->getSalesContextFromRequest($request);
  45.         $paymentHandlerIdentifier $salesChannelContext->getPaymentMethod()->getHandlerIdentifier();
  46.         if (strpos($paymentHandlerIdentifier'RpayPayments') !== false) {
  47.             /** @var $paymentHandler AbstractPaymentHandler */
  48.             $paymentHandler $this->paymentHandlerRegistry->getHandler($paymentHandlerIdentifier);
  49.             $validationDefinitions $paymentHandler->getValidationDefinitions($request$salesChannelContext);
  50.             $definitions = new DataValidationDefinition();
  51.             DataValidationHelper::addSubConstraints($definitions$validationDefinitions);
  52.             $event->getDefinition()->addSub('ratepay'$definitions);
  53.         }
  54.     }
  55.     private function getSalesContextFromRequest($request): SalesChannelContext
  56.     {
  57.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  58.     }
  59. }