custom/plugins/MolliePayments/src/Subscriber/CheckoutConfirmPageSubscriber.php line 99

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Exception;
  4. use Kiener\MolliePayments\Factory\MollieApiFactory;
  5. use Kiener\MolliePayments\Gateway\MollieGatewayInterface;
  6. use Kiener\MolliePayments\Handler\Method\CreditCardPayment;
  7. use Kiener\MolliePayments\Service\MandateServiceInterface;
  8. use Kiener\MolliePayments\Service\MollieLocaleService;
  9. use Kiener\MolliePayments\Service\SettingsService;
  10. use Kiener\MolliePayments\Setting\MollieSettingStruct;
  11. use Kiener\MolliePayments\Struct\PaymentMethod\PaymentMethodAttributes;
  12. use Mollie\Api\Exceptions\ApiException;
  13. use Mollie\Api\MollieApiClient;
  14. use Mollie\Api\Resources\Method;
  15. use Mollie\Api\Types\PaymentMethod;
  16. use Shopware\Core\Checkout\Customer\CustomerEntity;
  17. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  18. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class CheckoutConfirmPageSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var MollieApiFactory
  24.      */
  25.     private $apiFactory;
  26.     /**
  27.      * @var MollieApiClient
  28.      */
  29.     private $apiClient;
  30.     /**
  31.      * @var SettingsService
  32.      */
  33.     private $settingsService;
  34.     /**
  35.      * @var MollieSettingStruct
  36.      */
  37.     private $settings;
  38.     /**
  39.      * @var MollieLocaleService
  40.      */
  41.     private $mollieLocaleService;
  42.     /**
  43.      * @var MandateServiceInterface
  44.      */
  45.     private $mandateService;
  46.     /**
  47.      * @var MollieGatewayInterface
  48.      */
  49.     private $mollieGateway;
  50.     /**
  51.      * @var ?string
  52.      */
  53.     private $profileId null;
  54.     /**
  55.      * @return array<mixed>>
  56.      */
  57.     public static function getSubscribedEvents(): array
  58.     {
  59.         return [
  60.             CheckoutConfirmPageLoadedEvent::class => [
  61.                 ['addDataToPage'10],
  62.             ],
  63.             AccountEditOrderPageLoadedEvent::class => ['addDataToPage'10],
  64.         ];
  65.     }
  66.     /**
  67.      * @param MollieApiFactory $apiFactory
  68.      * @param SettingsService $settingsService
  69.      * @param MandateServiceInterface $mandateService
  70.      * @param MollieGatewayInterface $mollieGateway
  71.      * @param MollieLocaleService $mollieLocaleService
  72.      */
  73.     public function __construct(MollieApiFactory $apiFactorySettingsService $settingsServiceMandateServiceInterface $mandateServiceMollieGatewayInterface $mollieGatewayMollieLocaleService $mollieLocaleService)
  74.     {
  75.         $this->apiFactory $apiFactory;
  76.         $this->settingsService $settingsService;
  77.         $this->mandateService $mandateService;
  78.         $this->mollieGateway $mollieGateway;
  79.         $this->mollieLocaleService $mollieLocaleService;
  80.     }
  81.     /**
  82.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  83.      * @throws \Mollie\Api\Exceptions\IncompatiblePlatform
  84.      */
  85.     public function addDataToPage($args): void
  86.     {
  87.         $scId $args->getSalesChannelContext()->getSalesChannel()->getId();
  88.         $currentSelectedPaymentMethod $args->getSalesChannelContext()->getPaymentMethod();
  89.         $mollieAttributes = new PaymentMethodAttributes($currentSelectedPaymentMethod);
  90.         # load additional data only for mollie payment methods
  91.         if (!$mollieAttributes->isMolliePayment()) {
  92.             return;
  93.         }
  94.         # load our settings for the
  95.         # current request
  96.         $this->settings $this->settingsService->getSettings($scId);
  97.         # now use our factory to get the correct
  98.         # client with the correct sales channel settings
  99.         $this->apiClient $this->apiFactory->getClient($scId);
  100.         $this->mollieGateway->switchClient($scId);
  101.         $this->addMollieLocaleVariableToPage($args);
  102.         $this->addMollieProfileIdVariableToPage($args);
  103.         $this->addMollieTestModeVariableToPage($args);
  104.         $this->addMollieComponentsVariableToPage($args);
  105.         $this->addMollieSingleClickPaymentDataToPage($args$mollieAttributes);
  106.         $this->addMolliePosTerminalsVariableToPage($args$mollieAttributes);
  107.     }
  108.     /**
  109.      * Adds the locale for Mollie components to the storefront.
  110.      *
  111.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  112.      */
  113.     private function addMollieLocaleVariableToPage($args): void
  114.     {
  115.         $salesChannelContext $args->getSalesChannelContext();
  116.         $locale $this->mollieLocaleService->getLocale($salesChannelContext);
  117.         $args->getPage()->assign([
  118.             'mollie_locale' => $locale,
  119.         ]);
  120.     }
  121.     /**
  122.      * Adds the test mode variable to the storefront.
  123.      *
  124.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  125.      */
  126.     private function addMollieTestModeVariableToPage($args): void
  127.     {
  128.         $args->getPage()->assign([
  129.             'mollie_test_mode' => $this->settings->isTestMode() ? 'true' 'false',
  130.         ]);
  131.     }
  132.     /**
  133.      * Adds the profile id to the storefront.
  134.      *
  135.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  136.      */
  137.     private function addMollieProfileIdVariableToPage($args): void
  138.     {
  139.         $mollieProfileId $this->loadMollieProfileId();
  140.         $args->getPage()->assign([
  141.             'mollie_profile_id' => $mollieProfileId,
  142.         ]);
  143.     }
  144.     private function loadMollieProfileId(): string
  145.     {
  146.         if ($this->profileId !== null) {
  147.             return $this->profileId;
  148.         }
  149.         $mollieProfileId '';
  150.         /**
  151.          * Fetches the profile id from Mollie's API for the current key.
  152.          */
  153.         try {
  154.             if ($this->apiClient->usesOAuth() === false) {
  155.                 $mollieProfile $this->apiClient->profiles->get('me');
  156.             } else {
  157.                 $mollieProfile $this->apiClient->profiles->page()->offsetGet(0);
  158.             }
  159.             if (isset($mollieProfile->id)) {
  160.                 $mollieProfileId $mollieProfile->id;
  161.             }
  162.         } catch (ApiException $e) {
  163.             //
  164.         }
  165.         $this->profileId $mollieProfileId;
  166.         return $this->profileId;
  167.     }
  168.     /**
  169.      * Adds the components variable to the storefront.
  170.      *
  171.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  172.      */
  173.     private function addMollieComponentsVariableToPage($args): void
  174.     {
  175.         $args->getPage()->assign([
  176.             'enable_credit_card_components' => $this->settings->getEnableCreditCardComponents(),
  177.             'enable_one_click_payments_compact_view' => $this->settings->isOneClickPaymentsCompactView(),
  178.         ]);
  179.     }
  180.     /**
  181.      * Adds ideal issuers variable to the storefront.
  182.      *
  183.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  184.      * @param PaymentMethodAttributes $selectedPayment
  185.      */
  186.     private function addMolliePosTerminalsVariableToPage($args$selectedPayment): void
  187.     {
  188.         //do not load terminals if not required
  189.         if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::POINT_OF_SALE) {
  190.             return;
  191.         }
  192.         try {
  193.             $terminalsArray = [];
  194.             $terminals $this->mollieGateway->getPosTerminals();
  195.             foreach ($terminals as $terminal) {
  196.                 $terminalsArray[] = [
  197.                     'id' => $terminal->id,
  198.                     'name' => $terminal->description,
  199.                 ];
  200.             }
  201.             $args->getPage()->assign(
  202.                 [
  203.                     'mollie_terminals' => $terminalsArray
  204.                 ]
  205.             );
  206.         } catch (Exception $e) {
  207.         }
  208.     }
  209.     /**
  210.      * Adds the components variable to the storefront.
  211.      *
  212.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  213.      * @param PaymentMethodAttributes $selectedPayment
  214.      */
  215.     private function addMollieSingleClickPaymentDataToPage($args$selectedPayment): void
  216.     {
  217.         // do not load credit card mandate if not required
  218.         if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::CREDITCARD) {
  219.             return;
  220.         }
  221.         $args->getPage()->assign([
  222.             'enable_one_click_payments' => $this->settings->isOneClickPaymentsEnabled(),
  223.         ]);
  224.         if (!$this->settings->isOneClickPaymentsEnabled()) {
  225.             return;
  226.         }
  227.         try {
  228.             $salesChannelContext $args->getSalesChannelContext();
  229.             $loggedInCustomer $salesChannelContext->getCustomer();
  230.             if (!$loggedInCustomer instanceof CustomerEntity) {
  231.                 return;
  232.             }
  233.             // only load the list of mandates if the payment method is CreditCardPayment
  234.             if ($salesChannelContext->getPaymentMethod()->getHandlerIdentifier() !== CreditCardPayment::class) {
  235.                 return;
  236.             }
  237.             $mandates $this->mandateService->getCreditCardMandatesByCustomerId($loggedInCustomer->getId(), $salesChannelContext);
  238.             $args->getPage()->setExtensions([
  239.                 'MollieCreditCardMandateCollection' => $mandates
  240.             ]);
  241.         } catch (Exception $e) {
  242.             //
  243.         }
  244.     }
  245. }