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

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