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

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\CustomerService;
  8. use Kiener\MolliePayments\Service\CustomFieldService;
  9. use Kiener\MolliePayments\Service\MandateServiceInterface;
  10. use Kiener\MolliePayments\Service\SalesChannel\SalesChannelLocale;
  11. use Kiener\MolliePayments\Service\SettingsService;
  12. use Kiener\MolliePayments\Setting\MollieSettingStruct;
  13. use Kiener\MolliePayments\Struct\PaymentMethod\PaymentMethodAttributes;
  14. use Mollie\Api\Exceptions\ApiException;
  15. use Mollie\Api\MollieApiClient;
  16. use Mollie\Api\Resources\Method;
  17. use Mollie\Api\Types\PaymentMethod;
  18. use Shopware\Core\Checkout\Customer\CustomerEntity;
  19. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  20. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. class CheckoutConfirmPageSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var MollieApiFactory
  26.      */
  27.     private $apiFactory;
  28.     /**
  29.      * @var MollieApiClient
  30.      */
  31.     private $apiClient;
  32.     /**
  33.      * @var SettingsService
  34.      */
  35.     private $settingsService;
  36.     /**
  37.      * @var MollieSettingStruct
  38.      */
  39.     private $settings;
  40.     /**
  41.      * @var SalesChannelLocale
  42.      */
  43.     private $salesChannelLocale;
  44.     /**
  45.      * @var MandateServiceInterface
  46.      */
  47.     private $mandateService;
  48.     /**
  49.      * @var MollieGatewayInterface
  50.      */
  51.     private $mollieGateway;
  52.     /**
  53.      * @var ?string
  54.      */
  55.     private $profileId null;
  56.     /**
  57.      * @return array<mixed>>
  58.      */
  59.     public static function getSubscribedEvents(): array
  60.     {
  61.         return [
  62.             CheckoutConfirmPageLoadedEvent::class => [
  63.                 ['addDataToPage'10],
  64.             ],
  65.             AccountEditOrderPageLoadedEvent::class => ['addDataToPage'10],
  66.         ];
  67.     }
  68.     /**
  69.      * @param MollieApiFactory $apiFactory
  70.      * @param SettingsService $settingsService
  71.      * @param MandateServiceInterface $mandateService
  72.      * @param MollieGatewayInterface $mollieGateway
  73.      * @param SalesChannelLocale $salesChannelLocale
  74.      */
  75.     public function __construct(MollieApiFactory $apiFactorySettingsService $settingsServiceMandateServiceInterface $mandateServiceMollieGatewayInterface $mollieGatewaySalesChannelLocale $salesChannelLocale)
  76.     {
  77.         $this->apiFactory $apiFactory;
  78.         $this->settingsService $settingsService;
  79.         $this->mandateService $mandateService;
  80.         $this->mollieGateway $mollieGateway;
  81.         $this->salesChannelLocale $salesChannelLocale;
  82.     }
  83.     /**
  84.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  85.      * @throws \Mollie\Api\Exceptions\IncompatiblePlatform
  86.      */
  87.     public function addDataToPage($args): void
  88.     {
  89.         $scId $args->getSalesChannelContext()->getSalesChannel()->getId();
  90.         $currentSelectedPaymentMethod $args->getSalesChannelContext()->getPaymentMethod();
  91.         $mollieAttributes = new PaymentMethodAttributes($currentSelectedPaymentMethod);
  92.         # load additional data only for mollie payment methods
  93.         if (!$mollieAttributes->isMolliePayment()) {
  94.             return;
  95.         }
  96.         # load our settings for the
  97.         # current request
  98.         $this->settings $this->settingsService->getSettings($scId);
  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$mollieAttributes);
  108.         $this->addMollieSingleClickPaymentDataToPage($args$mollieAttributes);
  109.         $this->addMolliePosTerminalsVariableToPage($args$mollieAttributes);
  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.         $salesChannelContext $args->getSalesChannelContext();
  119.         $locale $this->salesChannelLocale->getLocale($salesChannelContext);
  120.         $args->getPage()->assign([
  121.             'mollie_locale' => $locale,
  122.         ]);
  123.     }
  124.     /**
  125.      * Adds the test mode variable to the storefront.
  126.      *
  127.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  128.      */
  129.     private function addMollieTestModeVariableToPage($args): void
  130.     {
  131.         $args->getPage()->assign([
  132.             'mollie_test_mode' => $this->settings->isTestMode() ? 'true' 'false',
  133.         ]);
  134.     }
  135.     /**
  136.      * Adds the profile id to the storefront.
  137.      *
  138.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  139.      */
  140.     private function addMollieProfileIdVariableToPage($args): void
  141.     {
  142.         $mollieProfileId $this->loadMollieProfileId();
  143.         $args->getPage()->assign([
  144.             'mollie_profile_id' => $mollieProfileId,
  145.         ]);
  146.     }
  147.     private function loadMollieProfileId(): string
  148.     {
  149.         if ($this->profileId !== null) {
  150.             return $this->profileId;
  151.         }
  152.         $mollieProfileId '';
  153.         /**
  154.          * Fetches the profile id from Mollie's API for the current key.
  155.          */
  156.         try {
  157.             if ($this->apiClient->usesOAuth() === false) {
  158.                 $mollieProfile $this->apiClient->profiles->get('me');
  159.             } else {
  160.                 $mollieProfile $this->apiClient->profiles->page()->offsetGet(0);
  161.             }
  162.             if (isset($mollieProfile->id)) {
  163.                 $mollieProfileId $mollieProfile->id;
  164.             }
  165.         } catch (ApiException $e) {
  166.             //
  167.         }
  168.         $this->profileId $mollieProfileId;
  169.         return $this->profileId;
  170.     }
  171.     /**
  172.      * Adds the components variable to the storefront.
  173.      *
  174.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  175.      */
  176.     private function addMollieComponentsVariableToPage($args): void
  177.     {
  178.         $args->getPage()->assign([
  179.             'enable_credit_card_components' => $this->settings->getEnableCreditCardComponents(),
  180.             'enable_one_click_payments_compact_view' => $this->settings->isOneClickPaymentsCompactView(),
  181.         ]);
  182.     }
  183.     /**
  184.      * Adds ideal issuers variable to the storefront.
  185.      *
  186.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  187.      * @param PaymentMethodAttributes $selectedPayment
  188.      */
  189.     private function addMollieIdealIssuersVariableToPage($args$selectedPayment): void
  190.     {
  191.         // do not load ideal issuers if not required
  192.         if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::IDEAL) {
  193.             return;
  194.         }
  195.         $customFields = [];
  196.         $ideal null;
  197.         $preferredIssuer '';
  198.         $mollieProfileId $this->loadMollieProfileId();
  199.         // Get custom fields from the customer in the sales channel context
  200.         if ($args->getSalesChannelContext()->getCustomer() !== null) {
  201.             $customFields $args->getSalesChannelContext()->getCustomer()->getCustomFields();
  202.         }
  203.         // Get the preferred issuer from the custom fields
  204.         if (
  205.             is_array($customFields)
  206.             && isset($customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER])
  207.             && (string)$customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER] !== ''
  208.         ) {
  209.             $preferredIssuer $customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS][CustomerService::CUSTOM_FIELDS_KEY_PREFERRED_IDEAL_ISSUER];
  210.         }
  211.         $parameters = [
  212.             'include' => 'issuers',
  213.         ];
  214.         if ($this->apiClient->usesOAuth()) {
  215.             $parameters['profileId'] = $mollieProfileId;
  216.         }
  217.         // Get issuers from the API
  218.         try {
  219.             $ideal $this->apiClient->methods->get(PaymentMethod::IDEAL$parameters);
  220.         } catch (Exception $e) {
  221.             //
  222.         }
  223.         // Assign issuers to storefront
  224.         if ($ideal instanceof Method) {
  225.             $args->getPage()->assign([
  226.                 'ideal_issuers' => $ideal->issuers,
  227.                 'preferred_issuer' => $preferredIssuer,
  228.             ]);
  229.         }
  230.     }
  231.     /**
  232.      * Adds ideal issuers variable to the storefront.
  233.      *
  234.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  235.      * @param PaymentMethodAttributes $selectedPayment
  236.      */
  237.     private function addMolliePosTerminalsVariableToPage($args$selectedPayment): void
  238.     {
  239.         //do not load terminals if not required
  240.         if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::POINT_OF_SALE) {
  241.             return;
  242.         }
  243.         try {
  244.             $terminalsArray = [];
  245.             $terminals $this->mollieGateway->getPosTerminals();
  246.             foreach ($terminals as $terminal) {
  247.                 $terminalsArray[] = [
  248.                     'id' => $terminal->id,
  249.                     'name' => $terminal->description,
  250.                 ];
  251.             }
  252.             $args->getPage()->assign(
  253.                 [
  254.                     'mollie_terminals' => $terminalsArray
  255.                 ]
  256.             );
  257.         } catch (Exception $e) {
  258.         }
  259.     }
  260.     /**
  261.      * Adds the components variable to the storefront.
  262.      *
  263.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  264.      * @param PaymentMethodAttributes $selectedPayment
  265.      */
  266.     private function addMollieSingleClickPaymentDataToPage($args$selectedPayment): void
  267.     {
  268.         // do not load credit card mandate if not required
  269.         if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::CREDITCARD) {
  270.             return;
  271.         }
  272.         $args->getPage()->assign([
  273.             'enable_one_click_payments' => $this->settings->isOneClickPaymentsEnabled(),
  274.         ]);
  275.         if (!$this->settings->isOneClickPaymentsEnabled()) {
  276.             return;
  277.         }
  278.         try {
  279.             $salesChannelContext $args->getSalesChannelContext();
  280.             $loggedInCustomer $salesChannelContext->getCustomer();
  281.             if (!$loggedInCustomer instanceof CustomerEntity) {
  282.                 return;
  283.             }
  284.             // only load the list of mandates if the payment method is CreditCardPayment
  285.             if ($salesChannelContext->getPaymentMethod()->getHandlerIdentifier() !== CreditCardPayment::class) {
  286.                 return;
  287.             }
  288.             $mandates $this->mandateService->getCreditCardMandatesByCustomerId($loggedInCustomer->getId(), $salesChannelContext);
  289.             $args->getPage()->setExtensions([
  290.                 'MollieCreditCardMandateCollection' => $mandates
  291.             ]);
  292.         } catch (Exception $e) {
  293.             //
  294.         }
  295.     }
  296. }