custom/plugins/MolliePayments/src/Subscriber/AccountPaymentMethodPageSubscriber.php line 57

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