custom/plugins/KlarnaPayment/src/Components/EventListener/ExpressButtonEventListener.php line 98

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace KlarnaPayment\Components\EventListener;
  4. use KlarnaPayment\Components\ConfigReader\ConfigReaderInterface;
  5. use KlarnaPayment\Components\Controller\Storefront\KlarnaExpressCheckoutController;
  6. use KlarnaPayment\Components\Extension\TemplateData\ExpressDataExtension;
  7. use KlarnaPayment\Components\Helper\PaymentHelper\PaymentHelperInterface;
  8. use KlarnaPayment\Installer\Modules\PaymentMethodInstaller;
  9. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  10. use Shopware\Core\Checkout\Customer\Event\GuestCustomerRegisterEvent;
  11. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  12. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  13. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  14. use Shopware\Core\System\SalesChannel\SalesChannel\AbstractContextSwitchRoute;
  15. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  17. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  18. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  19. use Shopware\Storefront\Page\PageLoadedEvent;
  20. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\HttpFoundation\RequestStack;
  23. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  24. class ExpressButtonEventListener implements EventSubscriberInterface
  25. {
  26.     /** @var PaymentHelperInterface */
  27.     private $paymentHelper;
  28.     /** @var ConfigReaderInterface */
  29.     private $configReader;
  30.     /** @var AbstractContextSwitchRoute */
  31.     private $contextSwitchRoute;
  32.     /** @var null|SessionInterface */
  33.     private $session;
  34.     /** @var RequestStack */
  35.     private $requestStack;
  36.     public function __construct(
  37.         PaymentHelperInterface $paymentHelper,
  38.         ConfigReaderInterface $configReader,
  39.         AbstractContextSwitchRoute $contextSwitchRoute,
  40.         ?SessionInterface $session,
  41.         RequestStack $requestStack
  42.     ) {
  43.         $this->paymentHelper      $paymentHelper;
  44.         $this->configReader       $configReader;
  45.         $this->contextSwitchRoute $contextSwitchRoute;
  46.         $this->session            $session;
  47.         $this->requestStack       $requestStack;
  48.     }
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             CheckoutRegisterPageLoadedEvent::class => 'addExpressTemplateData',
  53.             CheckoutCartPageLoadedEvent::class     => 'addExpressTemplateData',
  54.             OffcanvasCartPageLoadedEvent::class    => 'addExpressTemplateData',
  55.             ProductPageLoadedEvent::class          => 'addExpressTemplateData',
  56.             GuestCustomerRegisterEvent::class      => 'changeDefaultPaymentMethod',
  57.             CheckoutConfirmPageLoadedEvent::class  => 'changePaymentMethod',
  58.             CheckoutOrderPlacedEvent::class        => 'resetSession',
  59.         ];
  60.     }
  61.     /**
  62.      * @param CheckoutCartPageLoadedEvent|CheckoutRegisterPageLoadedEvent|OffcanvasCartPageLoadedEvent $event
  63.      */
  64.     public function addExpressTemplateData(PageLoadedEvent $event): void
  65.     {
  66.         $salesChannelContext $event->getSalesChannelContext();
  67.         if (!$this->paymentHelper->isKlarnaExpressCheckoutEnabled($salesChannelContext)) {
  68.             return;
  69.         }
  70.         $configuration $this->configReader->read($salesChannelContext->getSalesChannel()->getId());
  71.         if (!$configuration->get('isKlarnaExpressCheckoutActive'false)) {
  72.             return;
  73.         }
  74.         $templateData = new ExpressDataExtension(
  75.             $configuration->get('klarnaExpressCheckoutClientKey'''),
  76.             $configuration->get('klarnaExpressTheme''default'),
  77.             $configuration->get('klarnaExpressShape''default')
  78.         );
  79.         $event->getPage()->addExtension(ExpressDataExtension::EXTENSION_NAME$templateData);
  80.     }
  81.     public function changePaymentMethod(CheckoutConfirmPageLoadedEvent $event): void
  82.     {
  83.         $context $event->getSalesChannelContext();
  84.         if (!$this->getSession()->has(KlarnaExpressCheckoutController::KLARNA_EXPRESS_SESSION_KEY)) {
  85.             return;
  86.         }
  87.         $customer $context->getCustomer();
  88.         if ($customer === null) {
  89.             return;
  90.         }
  91.         $confirmPage    $event->getPage();
  92.         $paymentMethods $confirmPage->getPaymentMethods();
  93.         /** @var PaymentMethodCollection $filtered */
  94.         $filtered $paymentMethods->filterByProperty('id'PaymentMethodInstaller::KLARNA_EXPRESS_CHECKOUT);
  95.         $confirmPage->setPaymentMethods($filtered);
  96.     }
  97.     public function changeDefaultPaymentMethod(GuestCustomerRegisterEvent $event): void
  98.     {
  99.         if (!$this->getSession()->has(KlarnaExpressCheckoutController::KLARNA_EXPRESS_SESSION_KEY)) {
  100.             return;
  101.         }
  102.         $this->contextSwitchRoute->switchContext(
  103.             new RequestDataBag([
  104.                 SalesChannelContextService::PAYMENT_METHOD_ID => PaymentMethodInstaller::KLARNA_EXPRESS_CHECKOUT,
  105.             ]),
  106.             $event->getSalesChannelContext()
  107.         );
  108.     }
  109.     public function resetSession(): void
  110.     {
  111.         $this->getSession()->remove(KlarnaExpressCheckoutController::KLARNA_EXPRESS_SESSION_KEY);
  112.     }
  113.     // TODO: Remove me if compatibility is at least 6.4.2.0
  114.     private function getSession(): SessionInterface
  115.     {
  116.         /** @phpstan-ignore-next-line */
  117.         return $this->session ?? $this->requestStack->getSession();
  118.     }
  119. }