custom/plugins/StripeShopwarePayment/src/Payment/Subscriber/StripeSdkSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Stripe\ShopwarePayment\Payment\Subscriber;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  15. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  16. use Stripe\ShopwarePayment\Config\StripePluginConfigService;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class StripeSdkSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var EntityRepositoryInterface
  22.      */
  23.     private $languageRepository;
  24.     /**
  25.      * @var StripePluginConfigService
  26.      */
  27.     private $stripePluginConfigService;
  28.     public function __construct(
  29.         StripePluginConfigService $stripePluginConfigService,
  30.         EntityRepositoryInterface $languageRepository
  31.     ) {
  32.         $this->stripePluginConfigService $stripePluginConfigService;
  33.         $this->languageRepository $languageRepository;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  39.         ];
  40.     }
  41.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  42.     {
  43.         $salesChannelContext $event->getSalesChannelContext();
  44.         $salesChannel $salesChannelContext->getSalesChannel();
  45.         $salesChannelLocale $this->getSalesChannelLocale($salesChannel$salesChannelContext->getContext());
  46.         $stripePluginConfig $this->stripePluginConfigService->getStripePluginConfigForSalesChannel(
  47.             $salesChannel->getId()
  48.         );
  49.         $stripeSdkPageExtension = new StripeSdkPageExtension();
  50.         $stripeSdkPageExtension->assign([
  51.             'stripePublicKey' => $stripePluginConfig->getStripePublicKey(),
  52.             'salesChannelLocale' => $salesChannelLocale,
  53.         ]);
  54.         $event->getPage()->addExtension(StripeSdkPageExtension::PAGE_EXTENSION_NAME$stripeSdkPageExtension);
  55.     }
  56.     private function getSalesChannelLocale(SalesChannelEntity $salesChannelContext $context): ?string
  57.     {
  58.         $salesChannelLanguageId $salesChannel->getLanguageId();
  59.         $criteria = new Criteria([$salesChannelLanguageId]);
  60.         $criteria->addAssociation('locale');
  61.         $salesChannelLanguage $this->languageRepository->search(
  62.             $criteria,
  63.             $context
  64.         )->get($salesChannelLanguageId);
  65.         return $salesChannelLanguage $salesChannelLanguage->getLocale()->getCode() : null;
  66.     }
  67. }