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

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\DataBag\RequestDataBag;
  15. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  16. use Shopware\Core\PlatformRequest;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. class CheckoutValidationSubscriber implements EventSubscriberInterface
  22. {
  23.     private RequestStack $requestStack;
  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 (!$request instanceof Request) {
  42.             return;
  43.         }
  44.         $salesChannelContext $this->getSalesContextFromRequest($request);
  45.         $paymentMethod $salesChannelContext->getPaymentMethod();
  46.         $paymentHandlerIdentifier $paymentMethod->getHandlerIdentifier();
  47.         if (strpos($paymentHandlerIdentifier'RpayPayments') !== false) {
  48.             /** @var AbstractPaymentHandler $paymentHandler */
  49.             $paymentHandler $this->paymentHandlerRegistry->getPaymentMethodHandler($paymentMethod->getId());
  50.             $validationDefinitions $paymentHandler->getValidationDefinitions(new RequestDataBag($request->request->all()), $salesChannelContext);
  51.             $definitions = new DataValidationDefinition();
  52.             DataValidationHelper::addSubConstraints($definitions$validationDefinitions);
  53.             $event->getDefinition()->addSub('ratepay'$definitions);
  54.         }
  55.     }
  56.     private function getSalesContextFromRequest(Request $request): SalesChannelContext
  57.     {
  58.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  59.     }
  60. }