custom/plugins/KlarnaPayment/src/Components/EventListener/CheckoutEventListener.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace KlarnaPayment\Components\EventListener;
  4. use KlarnaPayment\Components\Extension\TemplateData\CheckoutDataExtension;
  5. use KlarnaPayment\Components\Helper\PaymentHelper\PaymentHelperInterface;
  6. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  7. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  8. use Shopware\Storefront\Page\PageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class CheckoutEventListener implements EventSubscriberInterface
  11. {
  12.     /** @var PaymentHelperInterface */
  13.     private $paymentHelper;
  14.     public function __construct(PaymentHelperInterface $paymentHelper)
  15.     {
  16.         $this->paymentHelper $paymentHelper;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             CheckoutConfirmPageLoadedEvent::class  => 'addKlarnaTemplateData',
  22.             AccountEditOrderPageLoadedEvent::class => 'addKlarnaTemplateData',
  23.         ];
  24.     }
  25.     public function addKlarnaTemplateData(PageLoadedEvent $event): void
  26.     {
  27.         if (!($event instanceof CheckoutConfirmPageLoadedEvent) && !($event instanceof AccountEditOrderPageLoadedEvent)) {
  28.             return;
  29.         }
  30.         if ($this->paymentHelper->isKlarnaPaymentsEnabled($event->getSalesChannelContext())) {
  31.             $type CheckoutDataExtension::TYPE_PAYMENTS;
  32.         } elseif ($this->paymentHelper->isKlarnaCheckoutEnabled($event->getSalesChannelContext())) {
  33.             $type CheckoutDataExtension::TYPE_CHECKOUT;
  34.         } else {
  35.             return;
  36.         }
  37.         $templateData = new CheckoutDataExtension();
  38.         $templateData->assign([
  39.             'klarnaType' => $type,
  40.         ]);
  41.         $event->getPage()->addExtension(CheckoutDataExtension::EXTENSION_NAME$templateData);
  42.     }
  43. }