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

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.      * @var ActivePaymentMethodsProvider
  52.      */
  53.     private $activePaymentMethodsProvider;
  54.     /**
  55.      * Returns an array of event names this subscriber wants to listen to.
  56.      *
  57.      * The array keys are event names and the value can be:
  58.      *
  59.      * * The method name to call (priority defaults to 0)
  60.      * * An array composed of the method name to call and the priority
  61.      * * An array of arrays composed of the method names to call and respective
  62.      *   priorities, or 0 if unset
  63.      *
  64.      * For instance:
  65.      *
  66.      * * array('eventName' => 'methodName')
  67.      * * array('eventName' => array('methodName', $priority))
  68.      * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  69.      *
  70.      * @return array The event names to listen to
  71.      */
  72.     public static function getSubscribedEvents(): array
  73.     {
  74.         return [
  75.             CheckoutConfirmPageLoadedEvent::class => [
  76.                 ['addDataToPage'10],
  77.             ],
  78.             AccountEditOrderPageLoadedEvent::class => ['addDataToPage'10],
  79.         ];
  80.     }
  81.     /**
  82.      * CheckoutConfirmPageSubscriber constructor.
  83.      * @param MollieApiFactory $apiFactory
  84.      * @param SettingsService $settingsService
  85.      * @param EntityRepositoryInterface $languageRepositoryInterface
  86.      * @param EntityRepositoryInterface $localeRepositoryInterface
  87.      * @param ActivePaymentMethodsProvider $activePaymentMethodsProvider
  88.      */
  89.     public function __construct(MollieApiFactory $apiFactorySettingsService $settingsServiceEntityRepositoryInterface $languageRepositoryInterfaceEntityRepositoryInterface $localeRepositoryInterfaceActivePaymentMethodsProvider $activePaymentMethodsProvider)
  90.     {
  91.         $this->apiFactory $apiFactory;
  92.         $this->settingsService $settingsService;
  93.         $this->languageRepositoryInterface $languageRepositoryInterface;
  94.         $this->localeRepositoryInterface $localeRepositoryInterface;
  95.         $this->activePaymentMethodsProvider $activePaymentMethodsProvider;
  96.     }
  97.     /**
  98.      * @param CheckoutConfirmPageLoadedEvent|AccountEditOrderPageLoadedEvent $args
  99.      * @throws \Mollie\Api\Exceptions\IncompatiblePlatform
  100.      */
  101.     public function addDataToPage($args): void
  102.     {
  103.         # load our settings for the
  104.         # current request
  105.         $this->settings $this->settingsService->getSettings($args->getSalesChannelContext()->getSalesChannel()->getId());
  106.         # now use our factory to get the correct
  107.         # client with the correct sales channel settings
  108.         $this->apiClient $this->apiFactory->getClient(
  109.             $args->getSalesChannelContext()->getSalesChannel()->getId()
  110.         );
  111.         $this->addMollieLocaleVariableToPage($args);
  112.         $this->addMollieProfileIdVariableToPage($args);
  113.         $this->addMollieTestModeVariableToPage($args);
  114.         $this->addMollieComponentsVariableToPage($args);
  115.         $this->addMollieIdealIssuersVariableToPage($args);
  116.     }
  117.     /**
  118.      * Adds the locale for Mollie components to the storefront.
  119.      *
  120.      * @param CheckoutConfirmPageLoadedEvent|AccountEditOrderPageLoadedEvent $args
  121.      */
  122.     private function addMollieLocaleVariableToPage($args): void
  123.     {
  124.         /**
  125.          * Build an array of available locales.
  126.          */
  127.         $availableLocales = [
  128.             'en_US',
  129.             'en_GB',
  130.             'nl_NL',
  131.             'fr_FR',
  132.             'it_IT',
  133.             'de_DE',
  134.             'de_AT',
  135.             'de_CH',
  136.             'es_ES',
  137.             'ca_ES',
  138.             'nb_NO',
  139.             'pt_PT',
  140.             'sv_SE',
  141.             'fi_FI',
  142.             'da_DK',
  143.             'is_IS',
  144.             'hu_HU',
  145.             'pl_PL',
  146.             'lv_LV',
  147.             'lt_LT'
  148.         ];
  149.         /**
  150.          * Get the language object from the sales channel context.
  151.          */
  152.         $locale '';
  153.         $context $args->getContext();
  154.         $salesChannelContext $args->getSalesChannelContext();
  155.         if ($context !== null && $salesChannelContext !== null) {
  156.             $salesChannel $salesChannelContext->getSalesChannel();
  157.             if ($salesChannel !== null) {
  158.                 $languageId $salesChannel->getLanguageId();
  159.                 if ($languageId !== null) {
  160.                     $languageCriteria = new Criteria();
  161.                     $languageCriteria->addFilter(new EqualsFilter('id'$languageId));
  162.                     $languages $this->languageRepositoryInterface->search($languageCriteria$args->getContext());
  163.                     $localeId $languages->first()->getLocaleId();
  164.                     $localeCriteria = new Criteria();
  165.                     $localeCriteria->addFilter(new EqualsFilter('id'$localeId));
  166.                     $locales $this->localeRepositoryInterface->search($localeCriteria$args->getContext());
  167.                     $locale $locales->first()->getCode();
  168.                 }
  169.             }
  170.         }
  171.         /**
  172.          * Set the locale based on the current storefront.
  173.          */
  174.         if ($locale !== null && $locale !== '') {
  175.             $locale str_replace('-''_'$locale);
  176.         }
  177.         /**
  178.          * Check if the shop locale is available.
  179.          */
  180.         if ($locale === '' || !in_array($locale$availableLocalestrue)) {
  181.             $locale 'en_GB';
  182.         }
  183.         $args->getPage()->assign([
  184.             'mollie_locale' => $locale,
  185.         ]);
  186.     }
  187.     /**
  188.      * Adds the test mode variable to the storefront.
  189.      *
  190.      * @param CheckoutConfirmPageLoadedEvent|AccountEditOrderPageLoadedEvent $args
  191.      */
  192.     private function addMollieTestModeVariableToPage($args): void
  193.     {
  194.         $args->getPage()->assign([
  195.             'mollie_test_mode' => $this->settings->isTestMode() ? 'true' 'false',
  196.         ]);
  197.     }
  198.     /**
  199.      * Adds the profile id to the storefront.
  200.      *
  201.      * @param CheckoutConfirmPageLoadedEvent|AccountEditOrderPageLoadedEvent $args
  202.      */
  203.     private function addMollieProfileIdVariableToPage($args): void
  204.     {
  205.         /** @var string $mollieProfileId */
  206.         $mollieProfileId '';
  207.         /**
  208.          * Fetches the profile id from Mollie's API for the current key.
  209.          */
  210.         try {
  211.             if ($this->apiClient->usesOAuth() === false) {
  212.                 $mollieProfile $this->apiClient->profiles->get('me');
  213.             } else {
  214.                 $mollieProfile $this->apiClient->profiles->page()->offsetGet(0);
  215.             }
  216.             if (isset($mollieProfile->id)) {
  217.                 $mollieProfileId $mollieProfile->id;
  218.             }
  219.         } catch (ApiException $e) {
  220.             //
  221.         }
  222.         $args->getPage()->assign([
  223.             'mollie_profile_id' => $mollieProfileId,
  224.         ]);
  225.     }
  226.     /**
  227.      * Adds the components variable to the storefront.
  228.      *
  229.      * @param CheckoutConfirmPageLoadedEvent|AccountEditOrderPageLoadedEvent $args
  230.      */
  231.     private function addMollieComponentsVariableToPage($args)
  232.     {
  233.         $args->getPage()->assign([
  234.             'enable_credit_card_components' => $this->settings->getEnableCreditCardComponents(),
  235.         ]);
  236.     }
  237.     /**
  238.      * Adds ideal issuers variable to the storefront.
  239.      *
  240.      * @param CheckoutConfirmPageLoadedEvent|AccountEditOrderPageLoadedEvent $args
  241.      */
  242.     private function addMollieIdealIssuersVariableToPage($args)
  243.     {
  244.         /** @var array $customFields */
  245.         $customFields = [];
  246.         /** @var Method $ideal */
  247.         $ideal null;
  248.         /** @var string $mollieProfileId */
  249.         $mollieProfileId '';
  250.         /** @var string $preferredIssuer */
  251.         $preferredIssuer '';
  252.         /**
  253.          * Fetches the profile id from Mollie's API for the current key.
  254.          */
  255.         try {
  256.             if ($this->apiClient->usesOAuth() === false) {
  257.                 $mollieProfile $this->apiClient->profiles->get('me');
  258.             } else {
  259.                 $mollieProfile $this->apiClient->profiles->page()->offsetGet(0);
  260.             }
  261.             if (isset($mollieProfile->id)) {
  262.                 $mollieProfileId $mollieProfile->id;
  263.             }
  264.         } catch (ApiException $e) {
  265.             //
  266.         }
  267.         // Get custom fields from the customer in the sales channel context
  268.         if ($args->getSalesChannelContext()->getCustomer() !== null) {
  269.             $customFields $args->getSalesChannelContext()->getCustomer()->getCustomFields();
  270.         }
  271.         // Get the preferred issuer from the custom fields
  272.         if (
  273.             is_array($customFields)
  274.             && isset($customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER])
  275.             && (string)$customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER] !== ''
  276.         ) {
  277.             $preferredIssuer $customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER];
  278.         }
  279.         /** @var array $parameters */
  280.         $parameters = [
  281.             'include' => 'issuers',
  282.         ];
  283.         if ($this->apiClient->usesOAuth()) {
  284.             $parameters['profileId'] = $mollieProfileId;
  285.         }
  286.         // Get issuers from the API
  287.         try {
  288.             $ideal $this->apiClient->methods->get(PaymentMethod::IDEAL$parameters);
  289.         } catch (Exception $e) {
  290.             //
  291.         }
  292.         // Assign issuers to storefront
  293.         if ($ideal !== null) {
  294.             $args->getPage()->assign([
  295.                 'ideal_issuers' => $ideal->issuers,
  296.                 'preferred_issuer' => $preferredIssuer,
  297.             ]);
  298.         }
  299.     }
  300. }