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

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\MollieLocaleService;
  11. use Kiener\MolliePayments\Service\SalesChannel\SalesChannelLocale;
  12. use Kiener\MolliePayments\Service\SettingsService;
  13. use Kiener\MolliePayments\Setting\MollieSettingStruct;
  14. use Kiener\MolliePayments\Struct\PaymentMethod\PaymentMethodAttributes;
  15. use Mollie\Api\Exceptions\ApiException;
  16. use Mollie\Api\MollieApiClient;
  17. use Mollie\Api\Resources\Method;
  18. use Mollie\Api\Types\PaymentMethod;
  19. use Shopware\Core\Checkout\Customer\CustomerEntity;
  20. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  21. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. class CheckoutConfirmPageSubscriber implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var MollieApiFactory
  27.      */
  28.     private $apiFactory;
  29.     /**
  30.      * @var MollieApiClient
  31.      */
  32.     private $apiClient;
  33.     /**
  34.      * @var SettingsService
  35.      */
  36.     private $settingsService;
  37.     /**
  38.      * @var MollieSettingStruct
  39.      */
  40.     private $settings;
  41.     /**
  42.      * @var MollieLocaleService
  43.      */
  44.     private $mollieLocaleService;
  45.     /**
  46.      * @var MandateServiceInterface
  47.      */
  48.     private $mandateService;
  49.     /**
  50.      * @var MollieGatewayInterface
  51.      */
  52.     private $mollieGateway;
  53.     /**
  54.      * @var ?string
  55.      */
  56.     private $profileId null;
  57.     /**
  58.      * @return array<mixed>>
  59.      */
  60.     public static function getSubscribedEvents(): array
  61.     {
  62.         return [
  63.             CheckoutConfirmPageLoadedEvent::class => [
  64.                 ['addDataToPage'10],
  65.             ],
  66.             AccountEditOrderPageLoadedEvent::class => ['addDataToPage'10],
  67.         ];
  68.     }
  69.     /**
  70.      * @param MollieApiFactory $apiFactory
  71.      * @param SettingsService $settingsService
  72.      * @param MandateServiceInterface $mandateService
  73.      * @param MollieGatewayInterface $mollieGateway
  74.      * @param MollieLocaleService $mollieLocaleService
  75.      */
  76.     public function __construct(MollieApiFactory $apiFactorySettingsService $settingsServiceMandateServiceInterface $mandateServiceMollieGatewayInterface $mollieGatewayMollieLocaleService $mollieLocaleService)
  77.     {
  78.         $this->apiFactory $apiFactory;
  79.         $this->settingsService $settingsService;
  80.         $this->mandateService $mandateService;
  81.         $this->mollieGateway $mollieGateway;
  82.         $this->mollieLocaleService $mollieLocaleService;
  83.     }
  84.     /**
  85.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  86.      * @throws \Mollie\Api\Exceptions\IncompatiblePlatform
  87.      */
  88.     public function addDataToPage($args): void
  89.     {
  90.         $scId $args->getSalesChannelContext()->getSalesChannel()->getId();
  91.         $currentSelectedPaymentMethod $args->getSalesChannelContext()->getPaymentMethod();
  92.         $mollieAttributes = new PaymentMethodAttributes($currentSelectedPaymentMethod);
  93.         # load additional data only for mollie payment methods
  94.         if (!$mollieAttributes->isMolliePayment()) {
  95.             return;
  96.         }
  97.         # load our settings for the
  98.         # current request
  99.         $this->settings $this->settingsService->getSettings($scId);
  100.         # now use our factory to get the correct
  101.         # client with the correct sales channel settings
  102.         $this->apiClient $this->apiFactory->getClient($scId);
  103.         $this->mollieGateway->switchClient($scId);
  104.         $this->addMollieLocaleVariableToPage($args);
  105.         $this->addMollieProfileIdVariableToPage($args);
  106.         $this->addMollieTestModeVariableToPage($args);
  107.         $this->addMollieComponentsVariableToPage($args);
  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->mollieLocaleService->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 addMolliePosTerminalsVariableToPage($args$selectedPayment): void
  190.     {
  191.         //do not load terminals if not required
  192.         if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::POINT_OF_SALE) {
  193.             return;
  194.         }
  195.         try {
  196.             $terminalsArray = [];
  197.             $terminals $this->mollieGateway->getPosTerminals();
  198.             foreach ($terminals as $terminal) {
  199.                 $terminalsArray[] = [
  200.                     'id' => $terminal->id,
  201.                     'name' => $terminal->description,
  202.                 ];
  203.             }
  204.             $args->getPage()->assign(
  205.                 [
  206.                     'mollie_terminals' => $terminalsArray
  207.                 ]
  208.             );
  209.         } catch (Exception $e) {
  210.         }
  211.     }
  212.     /**
  213.      * Adds the components variable to the storefront.
  214.      *
  215.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  216.      * @param PaymentMethodAttributes $selectedPayment
  217.      */
  218.     private function addMollieSingleClickPaymentDataToPage($args$selectedPayment): void
  219.     {
  220.         // do not load credit card mandate if not required
  221.         if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::CREDITCARD) {
  222.             return;
  223.         }
  224.         $args->getPage()->assign([
  225.             'enable_one_click_payments' => $this->settings->isOneClickPaymentsEnabled(),
  226.         ]);
  227.         if (!$this->settings->isOneClickPaymentsEnabled()) {
  228.             return;
  229.         }
  230.         try {
  231.             $salesChannelContext $args->getSalesChannelContext();
  232.             $loggedInCustomer $salesChannelContext->getCustomer();
  233.             if (!$loggedInCustomer instanceof CustomerEntity) {
  234.                 return;
  235.             }
  236.             // only load the list of mandates if the payment method is CreditCardPayment
  237.             if ($salesChannelContext->getPaymentMethod()->getHandlerIdentifier() !== CreditCardPayment::class) {
  238.                 return;
  239.             }
  240.             $mandates $this->mandateService->getCreditCardMandatesByCustomerId($loggedInCustomer->getId(), $salesChannelContext);
  241.             $args->getPage()->setExtensions([
  242.                 'MollieCreditCardMandateCollection' => $mandates
  243.             ]);
  244.         } catch (Exception $e) {
  245.             //
  246.         }
  247.     }
  248. }