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

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