custom/plugins/BilliePaymentSW6/src/Components/Checkout/Subscriber/CheckoutValidationSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (c) Billie 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 Billie\BilliePayment\Components\Checkout\Subscriber;
  10. use Billie\BilliePayment\Components\PaymentMethod\Util\MethodHelper;
  11. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  12. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  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 CheckoutValidationSubscriber implements EventSubscriberInterface
  20. {
  21.     private RequestStack $requestStack;
  22.     public function __construct(RequestStack $requestStack)
  23.     {
  24.         $this->requestStack $requestStack;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             'framework.validation.order.create' => ['validateOrderData'10],
  30.         ];
  31.     }
  32.     public function validateOrderData(BuildValidationEvent $event): void
  33.     {
  34.         $request $this->requestStack->getCurrentRequest();
  35.         if (!$request instanceof Request) {
  36.             // should never occur. just to be save.
  37.             return;
  38.         }
  39.         $salesChannelContext $this->getSalesContextFromRequest($request);
  40.         if (MethodHelper::isBilliePayment($salesChannelContext->getPaymentMethod())) {
  41.             $definitions = new DataValidationDefinition();
  42.             $definitions->add('session-id', new NotBlank());
  43.             $event->getDefinition()->addSub('billie_payment'$definitions);
  44.         }
  45.     }
  46.     private function getSalesContextFromRequest(Request $request): SalesChannelContext
  47.     {
  48.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  49.     }
  50. }