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

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\Extension\TemplateData\ExpressDataExtension;
  6. use KlarnaPayment\Components\Helper\PaymentHelper\PaymentHelperInterface;
  7. use KlarnaPayment\Installer\Modules\PaymentMethodInstaller;
  8. use Shopware\Core\Checkout\Customer\Event\GuestCustomerRegisterEvent;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  13. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  14. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  15. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  16. use Shopware\Core\System\SalesChannel\SalesChannel\AbstractContextSwitchRoute;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  19. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  20. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  21. use Shopware\Storefront\Page\PageLoadedEvent;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. class ExpressButtonEventListener implements EventSubscriberInterface
  24. {
  25.     private const ENVIRONMENT_PLAYGROUND 'playground';
  26.     private const ENVIRONMENT_PRODUCTION 'production';
  27.     /** @var PaymentHelperInterface */
  28.     private $paymentHelper;
  29.     /** @var ConfigReaderInterface */
  30.     private $configReader;
  31.     /** @var AbstractContextSwitchRoute */
  32.     private $contextSwitchRoute;
  33.     /** @var SalesChannelRepositoryInterface */
  34.     private $paymentMethodsRepository;
  35.     public function __construct(
  36.         PaymentHelperInterface $paymentHelper,
  37.         ConfigReaderInterface $configReader,
  38.         AbstractContextSwitchRoute $contextSwitchRoute,
  39.         SalesChannelRepositoryInterface $paymentMethodsRepository
  40.     ) {
  41.         $this->paymentHelper            $paymentHelper;
  42.         $this->configReader             $configReader;
  43.         $this->contextSwitchRoute       $contextSwitchRoute;
  44.         $this->paymentMethodsRepository $paymentMethodsRepository;
  45.     }
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             CheckoutRegisterPageLoadedEvent::class => 'addExpressTemplateData',
  50.             CheckoutCartPageLoadedEvent::class     => 'addExpressTemplateData',
  51.             OffcanvasCartPageLoadedEvent::class    => 'addExpressTemplateData',
  52.             //GuestCustomerRegisterEvent::class      => 'changeDefaultPaymentMethod',
  53.         ];
  54.     }
  55.     /**
  56.      * @param CheckoutCartPageLoadedEvent|CheckoutRegisterPageLoadedEvent|OffcanvasCartPageLoadedEvent $event
  57.      */
  58.     public function addExpressTemplateData(PageLoadedEvent $event): void
  59.     {
  60.         $salesChannelContext $event->getSalesChannelContext();
  61.         if (!$this->paymentHelper->isKlarnaPaymentsEnabled($salesChannelContext)) {
  62.             return;
  63.         }
  64.         $configuration $this->configReader->read($salesChannelContext->getSalesChannel()->getId());
  65.         if (!$configuration->get('isKlarnaExpressActive'false)) {
  66.             return;
  67.         }
  68.         $locale  $this->paymentHelper->getSalesChannelLocale($salesChannelContext);
  69.         $country $this->paymentHelper->getShippingCountry($salesChannelContext);
  70.         $environment self::ENVIRONMENT_PLAYGROUND;
  71.         $user        $configuration->get('testApiUsername');
  72.         if (!$configuration->get('testMode')) {
  73.             $environment self::ENVIRONMENT_PRODUCTION;
  74.             $user        $configuration->get('apiUsername');
  75.         }
  76.         $underscorePosition strpos($user'_');
  77.         if ($underscorePosition === false) {
  78.             return;
  79.         }
  80.         $templateData = new ExpressDataExtension(
  81.             substr($user0$underscorePosition),
  82.             $environment,
  83.             substr_replace($locale->getCode(), (string) $country->getIso(), 32),
  84.             $configuration->get('klarnaExpressTheme''default'),
  85.             $configuration->get('klarnaExpressLabel''default'),
  86.             $configuration->get('klarnaExpressCssClass'''),
  87.             $configuration->get('klarnaExpressShape''default')
  88.         );
  89.         $event->getPage()->addExtension(ExpressDataExtension::EXTENSION_NAME$templateData);
  90.     }
  91.     public function changeDefaultPaymentMethod(GuestCustomerRegisterEvent $event): void
  92.     {
  93.         $context $event->getSalesChannelContext();
  94.         $paymentMethod $this->getFirstKlarnaPaymentMethodId($context);
  95.         if (null === $paymentMethod) {
  96.             return;
  97.         }
  98.         $customer $context->getCustomer();
  99.         if (null === $customer) {
  100.             return;
  101.         }
  102.         $this->contextSwitchRoute->switchContext(
  103.             new RequestDataBag([
  104.                 SalesChannelContextService::PAYMENT_METHOD_ID => $paymentMethod,
  105.             ]),
  106.             $context
  107.         );
  108.     }
  109.     private function getFirstKlarnaPaymentMethodId(SalesChannelContext $context): ?string
  110.     {
  111.         $validPaymentMethods = [
  112.             PaymentMethodInstaller::KLARNA_PAY_LATER,
  113.             PaymentMethodInstaller::KLARNA_FINANCING,
  114.             PaymentMethodInstaller::KLARNA_DIRECT_DEBIT,
  115.             PaymentMethodInstaller::KLARNA_DIRECT_BANK_TRANSFER,
  116.             PaymentMethodInstaller::KLARNA_CREDIT_CARD,
  117.             PaymentMethodInstaller::KLARNA_PAY_NOW,
  118.         ];
  119.         $criteria = (new Criteria())
  120.             ->addFilter(new EqualsFilter('active'true))
  121.             ->addSorting(new FieldSorting('position'));
  122.         /** @var PaymentMethodCollection $availablePaymentMethods */
  123.         $availablePaymentMethods $this->paymentMethodsRepository
  124.             ->search($criteria$context)
  125.             ->getEntities();
  126.         $availablePaymentMethods $availablePaymentMethods->filterByActiveRules($context);
  127.         foreach ($validPaymentMethods as $validPaymentMethod) {
  128.             if ($availablePaymentMethods->has($validPaymentMethod)) {
  129.                 return $validPaymentMethod;
  130.             }
  131.         }
  132.         return null;
  133.     }
  134. }