vendor/store.shopware.com/molliepayments/src/Subscriber/AccountPaymentMethodPageSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Exception;
  4. use Kiener\MolliePayments\Service\CustomerServiceInterface;
  5. use Kiener\MolliePayments\Service\MandateServiceInterface;
  6. use Kiener\MolliePayments\Service\SettingsService;
  7. use Kiener\MolliePayments\Setting\MollieSettingStruct;
  8. use Shopware\Core\Checkout\Customer\CustomerEntity;
  9. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class AccountPaymentMethodPageSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var SettingsService
  15.      */
  16.     private $settingsService;
  17.     /**
  18.      * @var MollieSettingStruct
  19.      */
  20.     private $settings;
  21.     /**
  22.      * @var MandateServiceInterface
  23.      */
  24.     private $mandateService;
  25.     /**
  26.      * @return array<mixed>>
  27.      */
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             AccountPaymentMethodPageLoadedEvent::class => ['addDataToPage'10],
  32.         ];
  33.     }
  34.     /**
  35.      * @param SettingsService $settingsService
  36.      * @param MandateServiceInterface $mandateService
  37.      */
  38.     public function __construct(
  39.         SettingsService $settingsService,
  40.         MandateServiceInterface $mandateService
  41.     ) {
  42.         $this->settingsService $settingsService;
  43.         $this->mandateService $mandateService;
  44.     }
  45.     /**
  46.      * @param AccountPaymentMethodPageLoadedEvent $args
  47.      * @return void
  48.      */
  49.     public function addDataToPage(AccountPaymentMethodPageLoadedEvent $args): void
  50.     {
  51.         # load our settings for the
  52.         # current request
  53.         $this->settings $this->settingsService->getSettings($args->getSalesChannelContext()->getSalesChannel()->getId());
  54.         $this->addMollieSingleClickPaymentDataToPage($args);
  55.     }
  56.     /**
  57.      * Adds the components variable to the storefront.
  58.      *
  59.      * @param AccountPaymentMethodPageLoadedEvent $args
  60.      */
  61.     private function addMollieSingleClickPaymentDataToPage(AccountPaymentMethodPageLoadedEvent $args): void
  62.     {
  63.         $args->getPage()->assign([
  64.             'enable_one_click_payments' => $this->settings->isOneClickPaymentsEnabled(),
  65.         ]);
  66.         if (!$this->settings->isOneClickPaymentsEnabled()) {
  67.             return;
  68.         }
  69.         try {
  70.             $salesChannelContext $args->getSalesChannelContext();
  71.             $loggedInCustomer $salesChannelContext->getCustomer();
  72.             if (!$loggedInCustomer instanceof CustomerEntity) {
  73.                 return;
  74.             }
  75.             $mandates $this->mandateService->getCreditCardMandatesByCustomerId($loggedInCustomer->getId(), $salesChannelContext);
  76.             $args->getPage()->setExtensions([
  77.                 'MollieCreditCardMandateCollection' => $mandates
  78.             ]);
  79.         } catch (Exception $e) {
  80.             //
  81.         }
  82.     }
  83. }