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

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\Handler\Method\PosPayment;
  8. use Kiener\MolliePayments\Repository\Language\LanguageRepositoryInterface;
  9. use Kiener\MolliePayments\Repository\Locale\LocaleRepositoryInterface;
  10. use Kiener\MolliePayments\Service\CustomerService;
  11. use Kiener\MolliePayments\Service\CustomFieldService;
  12. use Kiener\MolliePayments\Service\MandateServiceInterface;
  13. use Kiener\MolliePayments\Service\SettingsService;
  14. use Kiener\MolliePayments\Setting\MollieSettingStruct;
  15. use Mollie\Api\Exceptions\ApiException;
  16. use Mollie\Api\MollieApiClient;
  17. use Mollie\Api\Resources\Method;
  18. use Mollie\Api\Resources\Terminal;
  19. use Mollie\Api\Types\PaymentMethod;
  20. use Shopware\Core\Checkout\Customer\CustomerEntity;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  23. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  24. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. class CheckoutConfirmPageSubscriber implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var MollieApiFactory
  30.      */
  31.     private $apiFactory;
  32.     /**
  33.      * @var MollieApiClient
  34.      */
  35.     private $apiClient;
  36.     /**
  37.      * @var SettingsService
  38.      */
  39.     private $settingsService;
  40.     /**
  41.      * @var MollieSettingStruct
  42.      */
  43.     private $settings;
  44.     /**
  45.      * @var LanguageRepositoryInterface
  46.      */
  47.     private $repoLanguages;
  48.     /**
  49.      * @var LocaleRepositoryInterface
  50.      */
  51.     private $repoLocales;
  52.     /**
  53.      * @var MandateServiceInterface
  54.      */
  55.     private $mandateService;
  56.     /**
  57.      * @var MollieGatewayInterface
  58.      */
  59.     private $mollieGateway;
  60.     /**
  61.      * @return array<mixed>>
  62.      */
  63.     public static function getSubscribedEvents(): array
  64.     {
  65.         return [
  66.             CheckoutConfirmPageLoadedEvent::class => [
  67.                 ['addDataToPage'10],
  68.             ],
  69.             AccountEditOrderPageLoadedEvent::class => ['addDataToPage'10],
  70.         ];
  71.     }
  72.     /**
  73.      * @param MollieApiFactory $apiFactory
  74.      * @param SettingsService $settingsService
  75.      * @param LanguageRepositoryInterface $languageRepositoryInterface
  76.      * @param LocaleRepositoryInterface $localeRepositoryInterface
  77.      * @param MandateServiceInterface $mandateService
  78.      * @param MollieGatewayInterface $mollieGateway
  79.      */
  80.     public function __construct(MollieApiFactory $apiFactorySettingsService $settingsServiceLanguageRepositoryInterface $languageRepositoryInterfaceLocaleRepositoryInterface $localeRepositoryInterfaceMandateServiceInterface $mandateServiceMollieGatewayInterface $mollieGateway)
  81.     {
  82.         $this->apiFactory $apiFactory;
  83.         $this->settingsService $settingsService;
  84.         $this->repoLanguages $languageRepositoryInterface;
  85.         $this->repoLocales $localeRepositoryInterface;
  86.         $this->mandateService $mandateService;
  87.         $this->mollieGateway $mollieGateway;
  88.     }
  89.     /**
  90.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  91.      * @throws \Mollie\Api\Exceptions\IncompatiblePlatform
  92.      */
  93.     public function addDataToPage($args): void
  94.     {
  95.         # load our settings for the
  96.         # current request
  97.         $this->settings $this->settingsService->getSettings($args->getSalesChannelContext()->getSalesChannel()->getId());
  98.         $scId $args->getSalesChannelContext()->getSalesChannel()->getId();
  99.         # now use our factory to get the correct
  100.         # client with the correct sales channel settings
  101.         $this->apiClient $this->apiFactory->getClient($scId);
  102.         $this->mollieGateway->switchClient($scId);
  103.         $this->addMollieLocaleVariableToPage($args);
  104.         $this->addMollieProfileIdVariableToPage($args);
  105.         $this->addMollieTestModeVariableToPage($args);
  106.         $this->addMollieComponentsVariableToPage($args);
  107.         $this->addMollieIdealIssuersVariableToPage($args);
  108.         $this->addMollieSingleClickPaymentDataToPage($args);
  109.         $this->addMolliePosTerminalsVariableToPage($args);
  110.     }
  111.     /**
  112.      * Adds the locale for Mollie components to the storefront.
  113.      *
  114.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  115.      */
  116.     private function addMollieLocaleVariableToPage($args): void
  117.     {
  118.         /**
  119.          * Build an array of available locales.
  120.          */
  121.         $availableLocales = [
  122.             'en_US',
  123.             'en_GB',
  124.             'nl_NL',
  125.             'fr_FR',
  126.             'it_IT',
  127.             'de_DE',
  128.             'de_AT',
  129.             'de_CH',
  130.             'es_ES',
  131.             'ca_ES',
  132.             'nb_NO',
  133.             'pt_PT',
  134.             'sv_SE',
  135.             'fi_FI',
  136.             'da_DK',
  137.             'is_IS',
  138.             'hu_HU',
  139.             'pl_PL',
  140.             'lv_LV',
  141.             'lt_LT'
  142.         ];
  143.         /**
  144.          * Get the language object from the sales channel context.
  145.          */
  146.         $locale '';
  147.         $context $args->getContext();
  148.         $salesChannelContext $args->getSalesChannelContext();
  149.         $salesChannel $salesChannelContext->getSalesChannel();
  150.         if ($salesChannel !== null) {
  151.             $languageId $salesChannel->getLanguageId();
  152.             if ($languageId !== null) {
  153.                 $languageCriteria = new Criteria();
  154.                 $languageCriteria->addFilter(new EqualsFilter('id'$languageId));
  155.                 $languages $this->repoLanguages->search($languageCriteria$args->getContext());
  156.                 $localeId $languages->first()->getLocaleId();
  157.                 $localeCriteria = new Criteria();
  158.                 $localeCriteria->addFilter(new EqualsFilter('id'$localeId));
  159.                 $locales $this->repoLocales->search($localeCriteria$args->getContext());
  160.                 $locale $locales->first()->getCode();
  161.             }
  162.         }
  163.         /**
  164.          * Set the locale based on the current storefront.
  165.          */
  166.         if ($locale !== null && $locale !== '') {
  167.             $locale str_replace('-''_'$locale);
  168.         }
  169.         /**
  170.          * Check if the shop locale is available.
  171.          */
  172.         if ($locale === '' || !in_array($locale$availableLocalestrue)) {
  173.             $locale 'en_GB';
  174.         }
  175.         $args->getPage()->assign([
  176.             'mollie_locale' => $locale,
  177.         ]);
  178.     }
  179.     /**
  180.      * Adds the test mode variable to the storefront.
  181.      *
  182.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  183.      */
  184.     private function addMollieTestModeVariableToPage($args): void
  185.     {
  186.         $args->getPage()->assign([
  187.             'mollie_test_mode' => $this->settings->isTestMode() ? 'true' 'false',
  188.         ]);
  189.     }
  190.     /**
  191.      * Adds the profile id to the storefront.
  192.      *
  193.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  194.      */
  195.     private function addMollieProfileIdVariableToPage($args): void
  196.     {
  197.         $mollieProfileId '';
  198.         /**
  199.          * Fetches the profile id from Mollie's API for the current key.
  200.          */
  201.         try {
  202.             if ($this->apiClient->usesOAuth() === false) {
  203.                 $mollieProfile $this->apiClient->profiles->get('me');
  204.             } else {
  205.                 $mollieProfile $this->apiClient->profiles->page()->offsetGet(0);
  206.             }
  207.             if (isset($mollieProfile->id)) {
  208.                 $mollieProfileId $mollieProfile->id;
  209.             }
  210.         } catch (ApiException $e) {
  211.             //
  212.         }
  213.         $args->getPage()->assign([
  214.             'mollie_profile_id' => $mollieProfileId,
  215.         ]);
  216.     }
  217.     /**
  218.      * Adds the components variable to the storefront.
  219.      *
  220.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  221.      */
  222.     private function addMollieComponentsVariableToPage($args): void
  223.     {
  224.         $args->getPage()->assign([
  225.             'enable_credit_card_components' => $this->settings->getEnableCreditCardComponents(),
  226.             'enable_one_click_payments_compact_view' => $this->settings->isOneClickPaymentsCompactView(),
  227.         ]);
  228.     }
  229.     /**
  230.      * Adds ideal issuers variable to the storefront.
  231.      *
  232.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  233.      */
  234.     private function addMollieIdealIssuersVariableToPage($args): void
  235.     {
  236.         $customFields = [];
  237.         $ideal null;
  238.         $mollieProfileId '';
  239.         $preferredIssuer '';
  240.         /**
  241.          * Fetches the profile id from Mollie's API for the current key.
  242.          */
  243.         try {
  244.             if ($this->apiClient->usesOAuth() === false) {
  245.                 $mollieProfile $this->apiClient->profiles->get('me');
  246.             } else {
  247.                 $mollieProfile $this->apiClient->profiles->page()->offsetGet(0);
  248.             }
  249.             if (isset($mollieProfile->id)) {
  250.                 $mollieProfileId $mollieProfile->id;
  251.             }
  252.         } catch (ApiException $e) {
  253.             //
  254.         }
  255.         // Get custom fields from the customer in the sales channel context
  256.         if ($args->getSalesChannelContext()->getCustomer() !== null) {
  257.             $customFields $args->getSalesChannelContext()->getCustomer()->getCustomFields();
  258.         }
  259.         // Get the preferred issuer from the custom fields
  260.         if (
  261.             is_array($customFields)
  262.             && isset($customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER])
  263.             && (string)$customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER] !== ''
  264.         ) {
  265.             $preferredIssuer $customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER];
  266.         }
  267.         $parameters = [
  268.             'include' => 'issuers',
  269.         ];
  270.         if ($this->apiClient->usesOAuth()) {
  271.             $parameters['profileId'] = $mollieProfileId;
  272.         }
  273.         // Get issuers from the API
  274.         try {
  275.             $ideal $this->apiClient->methods->get(PaymentMethod::IDEAL$parameters);
  276.         } catch (Exception $e) {
  277.             //
  278.         }
  279.         // Assign issuers to storefront
  280.         if ($ideal instanceof Method) {
  281.             $args->getPage()->assign([
  282.                 'ideal_issuers' => $ideal->issuers,
  283.                 'preferred_issuer' => $preferredIssuer,
  284.             ]);
  285.         }
  286.     }
  287.     /**
  288.      * Adds ideal issuers variable to the storefront.
  289.      *
  290.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  291.      */
  292.     private function addMolliePosTerminalsVariableToPage($args): void
  293.     {
  294.         try {
  295.             $terminalsArray = [];
  296.             $terminals $this->mollieGateway->getPosTerminals();
  297.             foreach ($terminals as $terminal) {
  298.                 $terminalsArray[] = [
  299.                     'id' => $terminal->id,
  300.                     'name' => $terminal->description,
  301.                 ];
  302.             }
  303.             $args->getPage()->assign(
  304.                 [
  305.                     'mollie_terminals' => $terminalsArray
  306.                 ]
  307.             );
  308.         } catch (Exception $e) {
  309.         }
  310.     }
  311.     /**
  312.      * Adds the components variable to the storefront.
  313.      *
  314.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  315.      */
  316.     private function addMollieSingleClickPaymentDataToPage($args): void
  317.     {
  318.         $args->getPage()->assign([
  319.             'enable_one_click_payments' => $this->settings->isOneClickPaymentsEnabled(),
  320.         ]);
  321.         if (!$this->settings->isOneClickPaymentsEnabled()) {
  322.             return;
  323.         }
  324.         try {
  325.             $salesChannelContext $args->getSalesChannelContext();
  326.             $loggedInCustomer $salesChannelContext->getCustomer();
  327.             if (!$loggedInCustomer instanceof CustomerEntity) {
  328.                 return;
  329.             }
  330.             // only load the list of mandates if the payment method is CreditCardPayment
  331.             if ($salesChannelContext->getPaymentMethod()->getHandlerIdentifier() !== CreditCardPayment::class) {
  332.                 return;
  333.             }
  334.             $mandates $this->mandateService->getCreditCardMandatesByCustomerId($loggedInCustomer->getId(), $salesChannelContext);
  335.             $args->getPage()->setExtensions([
  336.                 'MollieCreditCardMandateCollection' => $mandates
  337.             ]);
  338.         } catch (Exception $e) {
  339.             //
  340.         }
  341.     }
  342. }