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

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Exception;
  4. use Kiener\MolliePayments\Factory\MollieApiFactory;
  5. use Kiener\MolliePayments\Service\CustomerService;
  6. use Kiener\MolliePayments\Service\CustomFieldService;
  7. use Kiener\MolliePayments\Service\Payment\Provider\ActivePaymentMethodsProvider;
  8. use Kiener\MolliePayments\Service\PaymentMethodService;
  9. use Kiener\MolliePayments\Service\SettingsService;
  10. use Kiener\MolliePayments\Setting\MollieSettingStruct;
  11. use Mollie\Api\Exceptions\ApiException;
  12. use Mollie\Api\MollieApiClient;
  13. use Mollie\Api\Resources\Method;
  14. use Mollie\Api\Types\PaymentMethod;
  15. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  16. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  19. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  20. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  21. use Shopware\Storefront\Page\PageLoadedEvent;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Throwable;
  24. class CheckoutConfirmPageSubscriber implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var MollieApiFactory
  28.      */
  29.     private $apiFactory;
  30.     /**
  31.      * @var MollieApiClient
  32.      */
  33.     private $apiClient;
  34.     /**
  35.      * @var SettingsService
  36.      */
  37.     private $settingsService;
  38.     /**
  39.      * @var MollieSettingStruct
  40.      */
  41.     private $settings;
  42.     /**
  43.      * @var EntityRepositoryInterface
  44.      */
  45.     private $languageRepositoryInterface;
  46.     /**
  47.      * @var EntityRepositoryInterface
  48.      */
  49.     private $localeRepositoryInterface;
  50.     /**
  51.      * @return array<mixed>>
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             CheckoutConfirmPageLoadedEvent::class => [
  57.                 ['addDataToPage'10],
  58.             ],
  59.             AccountEditOrderPageLoadedEvent::class => ['addDataToPage'10],
  60.         ];
  61.     }
  62.     /**
  63.      * @param MollieApiFactory $apiFactory
  64.      * @param SettingsService $settingsService
  65.      * @param EntityRepositoryInterface $languageRepositoryInterface
  66.      * @param EntityRepositoryInterface $localeRepositoryInterface
  67.      */
  68.     public function __construct(MollieApiFactory $apiFactorySettingsService $settingsServiceEntityRepositoryInterface $languageRepositoryInterfaceEntityRepositoryInterface $localeRepositoryInterface)
  69.     {
  70.         $this->apiFactory $apiFactory;
  71.         $this->settingsService $settingsService;
  72.         $this->languageRepositoryInterface $languageRepositoryInterface;
  73.         $this->localeRepositoryInterface $localeRepositoryInterface;
  74.     }
  75.     /**
  76.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  77.      * @throws \Mollie\Api\Exceptions\IncompatiblePlatform
  78.      */
  79.     public function addDataToPage($args): void
  80.     {
  81.         # load our settings for the
  82.         # current request
  83.         $this->settings $this->settingsService->getSettings($args->getSalesChannelContext()->getSalesChannel()->getId());
  84.         # now use our factory to get the correct
  85.         # client with the correct sales channel settings
  86.         $this->apiClient $this->apiFactory->getClient(
  87.             $args->getSalesChannelContext()->getSalesChannel()->getId()
  88.         );
  89.         $this->addMollieLocaleVariableToPage($args);
  90.         $this->addMollieProfileIdVariableToPage($args);
  91.         $this->addMollieTestModeVariableToPage($args);
  92.         $this->addMollieComponentsVariableToPage($args);
  93.         $this->addMollieIdealIssuersVariableToPage($args);
  94.     }
  95.     /**
  96.      * Adds the locale for Mollie components to the storefront.
  97.      *
  98.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  99.      */
  100.     private function addMollieLocaleVariableToPage($args): void
  101.     {
  102.         /**
  103.          * Build an array of available locales.
  104.          */
  105.         $availableLocales = [
  106.             'en_US',
  107.             'en_GB',
  108.             'nl_NL',
  109.             'fr_FR',
  110.             'it_IT',
  111.             'de_DE',
  112.             'de_AT',
  113.             'de_CH',
  114.             'es_ES',
  115.             'ca_ES',
  116.             'nb_NO',
  117.             'pt_PT',
  118.             'sv_SE',
  119.             'fi_FI',
  120.             'da_DK',
  121.             'is_IS',
  122.             'hu_HU',
  123.             'pl_PL',
  124.             'lv_LV',
  125.             'lt_LT'
  126.         ];
  127.         /**
  128.          * Get the language object from the sales channel context.
  129.          */
  130.         $locale '';
  131.         $context $args->getContext();
  132.         $salesChannelContext $args->getSalesChannelContext();
  133.         $salesChannel $salesChannelContext->getSalesChannel();
  134.         if ($salesChannel !== null) {
  135.             $languageId $salesChannel->getLanguageId();
  136.             if ($languageId !== null) {
  137.                 $languageCriteria = new Criteria();
  138.                 $languageCriteria->addFilter(new EqualsFilter('id'$languageId));
  139.                 $languages $this->languageRepositoryInterface->search($languageCriteria$args->getContext());
  140.                 $localeId $languages->first()->getLocaleId();
  141.                 $localeCriteria = new Criteria();
  142.                 $localeCriteria->addFilter(new EqualsFilter('id'$localeId));
  143.                 $locales $this->localeRepositoryInterface->search($localeCriteria$args->getContext());
  144.                 $locale $locales->first()->getCode();
  145.             }
  146.         }
  147.         /**
  148.          * Set the locale based on the current storefront.
  149.          */
  150.         if ($locale !== null && $locale !== '') {
  151.             $locale str_replace('-''_'$locale);
  152.         }
  153.         /**
  154.          * Check if the shop locale is available.
  155.          */
  156.         if ($locale === '' || !in_array($locale$availableLocalestrue)) {
  157.             $locale 'en_GB';
  158.         }
  159.         $args->getPage()->assign([
  160.             'mollie_locale' => $locale,
  161.         ]);
  162.     }
  163.     /**
  164.      * Adds the test mode variable to the storefront.
  165.      *
  166.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  167.      */
  168.     private function addMollieTestModeVariableToPage($args): void
  169.     {
  170.         $args->getPage()->assign([
  171.             'mollie_test_mode' => $this->settings->isTestMode() ? 'true' 'false',
  172.         ]);
  173.     }
  174.     /**
  175.      * Adds the profile id to the storefront.
  176.      *
  177.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  178.      */
  179.     private function addMollieProfileIdVariableToPage($args): void
  180.     {
  181.         $mollieProfileId '';
  182.         /**
  183.          * Fetches the profile id from Mollie's API for the current key.
  184.          */
  185.         try {
  186.             if ($this->apiClient->usesOAuth() === false) {
  187.                 $mollieProfile $this->apiClient->profiles->get('me');
  188.             } else {
  189.                 $mollieProfile $this->apiClient->profiles->page()->offsetGet(0);
  190.             }
  191.             if (isset($mollieProfile->id)) {
  192.                 $mollieProfileId $mollieProfile->id;
  193.             }
  194.         } catch (ApiException $e) {
  195.             //
  196.         }
  197.         $args->getPage()->assign([
  198.             'mollie_profile_id' => $mollieProfileId,
  199.         ]);
  200.     }
  201.     /**
  202.      * Adds the components variable to the storefront.
  203.      *
  204.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  205.      */
  206.     private function addMollieComponentsVariableToPage($args): void
  207.     {
  208.         $args->getPage()->assign([
  209.             'enable_credit_card_components' => $this->settings->getEnableCreditCardComponents(),
  210.         ]);
  211.     }
  212.     /**
  213.      * Adds ideal issuers variable to the storefront.
  214.      *
  215.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  216.      */
  217.     private function addMollieIdealIssuersVariableToPage($args): void
  218.     {
  219.         $customFields = [];
  220.         $ideal null;
  221.         $mollieProfileId '';
  222.         $preferredIssuer '';
  223.         /**
  224.          * Fetches the profile id from Mollie's API for the current key.
  225.          */
  226.         try {
  227.             if ($this->apiClient->usesOAuth() === false) {
  228.                 $mollieProfile $this->apiClient->profiles->get('me');
  229.             } else {
  230.                 $mollieProfile $this->apiClient->profiles->page()->offsetGet(0);
  231.             }
  232.             if (isset($mollieProfile->id)) {
  233.                 $mollieProfileId $mollieProfile->id;
  234.             }
  235.         } catch (ApiException $e) {
  236.             //
  237.         }
  238.         // Get custom fields from the customer in the sales channel context
  239.         if ($args->getSalesChannelContext()->getCustomer() !== null) {
  240.             $customFields $args->getSalesChannelContext()->getCustomer()->getCustomFields();
  241.         }
  242.         // Get the preferred issuer from the custom fields
  243.         if (
  244.             is_array($customFields)
  245.             && isset($customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER])
  246.             && (string)$customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER] !== ''
  247.         ) {
  248.             $preferredIssuer $customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER];
  249.         }
  250.         $parameters = [
  251.             'include' => 'issuers',
  252.         ];
  253.         if ($this->apiClient->usesOAuth()) {
  254.             $parameters['profileId'] = $mollieProfileId;
  255.         }
  256.         // Get issuers from the API
  257.         try {
  258.             $ideal $this->apiClient->methods->get(PaymentMethod::IDEAL$parameters);
  259.         } catch (Exception $e) {
  260.             //
  261.         }
  262.         // Assign issuers to storefront
  263.         if ($ideal instanceof Method) {
  264.             $args->getPage()->assign([
  265.                 'ideal_issuers' => $ideal->issuers,
  266.                 'preferred_issuer' => $preferredIssuer,
  267.             ]);
  268.         }
  269.     }
  270. }