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

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\Account\Order\AccountEditOrderPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  17. use Shopware\Storefront\Page\PageLoadedEvent;
  18. use Stripe\ShopwarePayment\Config\StripePluginConfigService;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class StripeSdkSubscriber implements EventSubscriberInterface
  21. {
  22.     private EntityRepositoryInterface $languageRepository;
  23.     private StripePluginConfigService $stripePluginConfigService;
  24.     public function __construct(
  25.         StripePluginConfigService $stripePluginConfigService,
  26.         EntityRepositoryInterface $languageRepository
  27.     ) {
  28.         $this->stripePluginConfigService $stripePluginConfigService;
  29.         $this->languageRepository $languageRepository;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             CheckoutConfirmPageLoadedEvent::class => 'onPageWithPaymentSelectionLoaded',
  35.             AccountEditOrderPageLoadedEvent::class => 'onPageWithPaymentSelectionLoaded',
  36.         ];
  37.     }
  38.     public function onPageWithPaymentSelectionLoaded(PageLoadedEvent $event): void
  39.     {
  40.         $salesChannelContext $event->getSalesChannelContext();
  41.         $salesChannel $salesChannelContext->getSalesChannel();
  42.         $salesChannelLocale $this->getSalesChannelLocale($salesChannel$salesChannelContext->getContext());
  43.         $stripePluginConfig $this->stripePluginConfigService->getStripePluginConfigForSalesChannel(
  44.             $salesChannel->getId()
  45.         );
  46.         $stripeSdkPageExtension = new StripeSdkPageExtension();
  47.         $stripeSdkPageExtension->assign([
  48.             'stripePublicKey' => $stripePluginConfig->getStripePublicKey(),
  49.             'salesChannelLocale' => $salesChannelLocale,
  50.             'stripeAccountCountryIso' => $stripePluginConfig->getStripeAccountCountryIso(),
  51.         ]);
  52.         $event->getPage()->addExtension(StripeSdkPageExtension::PAGE_EXTENSION_NAME$stripeSdkPageExtension);
  53.     }
  54.     private function getSalesChannelLocale(SalesChannelEntity $salesChannelContext $context): ?string
  55.     {
  56.         $salesChannelLanguageId $salesChannel->getLanguageId();
  57.         $criteria = new Criteria([$salesChannelLanguageId]);
  58.         $criteria->addAssociation('locale');
  59.         $salesChannelLanguage $this->languageRepository->search(
  60.             $criteria,
  61.             $context
  62.         )->get($salesChannelLanguageId);
  63.         return $salesChannelLanguage $salesChannelLanguage->getLocale()->getCode() : null;
  64.     }
  65. }