custom/plugins/RpayPayments/src/Components/CreditworthinessPreCheck/Subscriber/CheckoutValidationSubscriber.php line 62

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\CreditworthinessPreCheck\Subscriber;
  10. use Ratepay\RpayPayments\Components\CreditworthinessPreCheck\Service\PaymentQueryValidatorService;
  11. use Ratepay\RpayPayments\Util\RequestHelper;
  12. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  13. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  14. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  15. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  16. use Shopware\Core\Framework\Validation\DataValidator;
  17. use Shopware\Core\PlatformRequest;
  18. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. class CheckoutValidationSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @decrecated use PaymentQueryValidatorService::CODE_METHOD_NOT_AVAILABLE
  26.      * @var string
  27.      */
  28.     public const CODE_METHOD_NOT_AVAILABLE PaymentQueryValidatorService::CODE_METHOD_NOT_AVAILABLE;
  29.     private RequestStack $requestStack;
  30.     private CartService $cartService;
  31.     private DataValidator $dataValidator;
  32.     private PaymentQueryValidatorService $validatorService;
  33.     public function __construct(
  34.         RequestStack $requestStack,
  35.         DataValidator $dataValidator,
  36.         CartService $cartService,
  37.         PaymentQueryValidatorService $validatorService
  38.     ) {
  39.         $this->requestStack $requestStack;
  40.         $this->cartService $cartService;
  41.         $this->dataValidator $dataValidator;
  42.         $this->validatorService $validatorService;
  43.     }
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             'framework.validation.order.create' => ['validatePaymentQuery'5],
  48.         ];
  49.     }
  50.     public function validatePaymentQuery(BuildValidationEvent $event): void
  51.     {
  52.         $request $this->requestStack->getCurrentRequest();
  53.         if (!$request instanceof Request) {
  54.             return;
  55.         }
  56.         $context $this->getContextFromRequest($request);
  57.         $paymentHandlerIdentifier $context->getPaymentMethod()->getHandlerIdentifier();
  58.         if (strpos($paymentHandlerIdentifier'RpayPayments') !== false) {
  59.             $ratepayData RequestHelper::getArray($request'ratepay', []);
  60.             // we must validate the data BEFORE we send the request to the gateway.
  61.             // this is not the good way, but we do not have another possibility.
  62.             // we just want to validate the ratepay-data to prevent unexpected behavior of third-party-plugins
  63.             $definition = new DataValidationDefinition();
  64.             $definition->addSub('ratepay'$event->getDefinition()->getSubDefinitions()['ratepay']);
  65.             $this->dataValidator->validate([
  66.                 'ratepay' => $ratepayData,
  67.             ], $definition);
  68.             $this->validatorService->validate(
  69.                 $this->cartService->getCart($context->getToken(), $context),
  70.                 $context,
  71.                 $ratepayData['transactionId'] ?? '--',
  72.                 new DataBag($request->request->all())
  73.             );
  74.         }
  75.     }
  76.     private function getContextFromRequest(Request $request): SalesChannelContext
  77.     {
  78.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  79.     }
  80. }