custom/plugins/KlarnaPayment/src/Components/EventListener/OrderValidationEventListener.php line 52

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace KlarnaPayment\Components\EventListener;
  4. use KlarnaPayment\Components\CartHasher\CartHasherInterface;
  5. use KlarnaPayment\Components\Helper\PaymentHelper\PaymentHelperInterface;
  6. use KlarnaPayment\Components\Validator\CartHash;
  7. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  8. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  9. use Shopware\Core\PlatformRequest;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Validator\Constraints\NotBlank;
  15. class OrderValidationEventListener implements EventSubscriberInterface
  16. {
  17.     /** @var RequestStack */
  18.     private $requestStack;
  19.     /** @var PaymentHelperInterface */
  20.     private $paymentHelper;
  21.     /** @var CartService */
  22.     private $cartService;
  23.     /** @var CartHasherInterface */
  24.     private $cartHasher;
  25.     public function __construct(
  26.         RequestStack $requestStack,
  27.         PaymentHelperInterface $paymentHelper,
  28.         CartService $cartService,
  29.         CartHasherInterface $cartHasher
  30.     ) {
  31.         $this->requestStack  $requestStack;
  32.         $this->paymentHelper $paymentHelper;
  33.         $this->cartService   $cartService;
  34.         $this->cartHasher    $cartHasher;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             'framework.validation.order.create' => 'validateOrderData',
  40.         ];
  41.     }
  42.     public function validateOrderData(BuildValidationEvent $event): void
  43.     {
  44.         $request $this->requestStack->getCurrentRequest();
  45.         if ($request === null) {
  46.             return;
  47.         }
  48.         $context $this->getContextFromRequest($request);
  49.         if ($this->paymentHelper->isKlarnaPaymentsSelected($context)) {
  50.             $cart $this->cartService->getCart($context->getToken(), $context);
  51.             $hash $this->cartHasher->generate($cart$context);
  52.             $event->getDefinition()->add(
  53.                 'klarnaAuthorizationToken',
  54.                 new NotBlank()
  55.             );
  56.             $event->getDefinition()->add(
  57.                 'klarnaCartHash',
  58.                 new CartHash(['value' => $hash])
  59.             );
  60.         }
  61.     }
  62.     private function getContextFromRequest(Request $request): SalesChannelContext
  63.     {
  64.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  65.     }
  66. }