custom/plugins/StripeShopwarePayment/src/PaymentMethods/Card/Subscriber/CreditCardSubscriber.php line 48

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\PaymentMethods\Card\Subscriber;
  11. use Shopware\Core\Checkout\Customer\CustomerEntity;
  12. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Shopware\Storefront\Page\PageLoadedEvent;
  15. use Stripe\ShopwarePayment\Config\StripePluginConfigService;
  16. use Stripe\ShopwarePayment\Session\StripePaymentMethodSettings;
  17. use Stripe\ShopwarePayment\StripeApi\StripeApi;
  18. use Stripe\ShopwarePayment\StripeApi\StripeApiFactory;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class CreditCardSubscriber implements EventSubscriberInterface
  21. {
  22.     private StripeApiFactory $stripeApiFactory;
  23.     private StripePluginConfigService $stripePluginConfigService;
  24.     private StripePaymentMethodSettings $stripePaymentMethodSettings;
  25.     public function __construct(
  26.         StripePluginConfigService $stripePluginConfigService,
  27.         StripeApiFactory $stripeApiFactory,
  28.         StripePaymentMethodSettings $stripePaymentMethodSettings
  29.     ) {
  30.         $this->stripePluginConfigService $stripePluginConfigService;
  31.         $this->stripeApiFactory $stripeApiFactory;
  32.         $this->stripePaymentMethodSettings $stripePaymentMethodSettings;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             CheckoutConfirmPageLoadedEvent::class => 'onPageWithPaymentSelectionLoaded',
  38.             AccountEditOrderPageLoadedEvent::class => 'onPageWithPaymentSelectionLoaded',
  39.         ];
  40.     }
  41.     public function onPageWithPaymentSelectionLoaded(PageLoadedEvent $event): void
  42.     {
  43.         $salesChannelContext $event->getSalesChannelContext();
  44.         $salesChannel $salesChannelContext->getSalesChannel();
  45.         $salesChannelId $salesChannel->getId();
  46.         $stripeApi $this->stripeApiFactory->createStripeApiForSalesChannel(
  47.             $salesChannelContext->getContext(),
  48.             $salesChannelId
  49.         );
  50.         $availableCards $this->fetchAvailableCards($stripeApi$salesChannelContext->getCustomer());
  51.         $stripePluginConfig $this->stripePluginConfigService->getStripePluginConfigForSalesChannel(
  52.             $salesChannel->getId()
  53.         );
  54.         $creditCardPageExtension = new CreditCardPageExtension();
  55.         $creditCardPageExtension->assign([
  56.             'isSavingCreditCardsAllowed' => $stripePluginConfig->isSavingCreditCardsAllowed(),
  57.             'availableCards' => $availableCards,
  58.             'selectedCard' => $this->stripePaymentMethodSettings->getSelectedCard(),
  59.         ]);
  60.         $event->getPage()->addExtension(CreditCardPageExtension::PAGE_EXTENSION_NAME$creditCardPageExtension);
  61.     }
  62.     private function fetchAvailableCards(StripeApi $stripeApi, ?CustomerEntity $customer): array
  63.     {
  64.         $availableCards = [];
  65.         if ($customer && $customer->getCustomFields() && isset($customer->getCustomFields()['stripeCustomerId'])) {
  66.             $availableCards $stripeApi->getSavedCardsOfStripeCustomer(
  67.                 $customer->getCustomFields()['stripeCustomerId']
  68.             );
  69.         }
  70.         $selectedCard $this->stripePaymentMethodSettings->getSelectedCard();
  71.         if ($selectedCard) {
  72.             // Ensure the selected card is part of the list of available cards
  73.             $cardExists false;
  74.             foreach ($availableCards as $card) {
  75.                 if ($card['id'] === $selectedCard['id']) {
  76.                     $cardExists true;
  77.                     break;
  78.                 }
  79.             }
  80.             if (!$cardExists) {
  81.                 $availableCards[] = $selectedCard;
  82.             }
  83.         }
  84.         return $availableCards;
  85.     }
  86. }