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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Kiener\MolliePayments\Subscriber;
  4. use Kiener\MolliePayments\Factory\MollieApiFactory;
  5. use Kiener\MolliePayments\Gateway\MollieGatewayInterface;
  6. use Kiener\MolliePayments\Handler\Method\CreditCardPayment;
  7. use Kiener\MolliePayments\Service\MandateServiceInterface;
  8. use Kiener\MolliePayments\Service\MollieLocaleService;
  9. use Kiener\MolliePayments\Service\SettingsService;
  10. use Kiener\MolliePayments\Setting\MollieSettingStruct;
  11. use Kiener\MolliePayments\Struct\PaymentMethod\PaymentMethodAttributes;
  12. use Mollie\Api\Exceptions\ApiException;
  13. use Mollie\Api\MollieApiClient;
  14. use Mollie\Api\Resources\Method;
  15. use Mollie\Api\Types\PaymentMethod;
  16. use Shopware\Core\Checkout\Customer\CustomerEntity;
  17. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  18. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  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 MollieLocaleService
  40.      */
  41.     private $mollieLocaleService;
  42.     /**
  43.      * @var MandateServiceInterface
  44.      */
  45.     private $mandateService;
  46.     /**
  47.      * @var MollieGatewayInterface
  48.      */
  49.     private $mollieGateway;
  50.     /**
  51.      * @var ?string
  52.      */
  53.     private $profileId;
  54.     public function __construct(MollieApiFactory $apiFactorySettingsService $settingsServiceMandateServiceInterface $mandateServiceMollieGatewayInterface $mollieGatewayMollieLocaleService $mollieLocaleService)
  55.     {
  56.         $this->apiFactory $apiFactory;
  57.         $this->settingsService $settingsService;
  58.         $this->mandateService $mandateService;
  59.         $this->mollieGateway $mollieGateway;
  60.         $this->mollieLocaleService $mollieLocaleService;
  61.     }
  62.     /**
  63.      * @return array<mixed>
  64.      */
  65.     public static function getSubscribedEvents(): array
  66.     {
  67.         return [
  68.             CheckoutConfirmPageLoadedEvent::class => [
  69.                 ['addDataToPage'10],
  70.             ],
  71.             AccountEditOrderPageLoadedEvent::class => ['addDataToPage'10],
  72.         ];
  73.     }
  74.     /**
  75.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  76.      *
  77.      * @throws \Mollie\Api\Exceptions\IncompatiblePlatform
  78.      */
  79.     public function addDataToPage($args): void
  80.     {
  81.         $scId $args->getSalesChannelContext()->getSalesChannel()->getId();
  82.         $currentSelectedPaymentMethod $args->getSalesChannelContext()->getPaymentMethod();
  83.         $mollieAttributes = new PaymentMethodAttributes($currentSelectedPaymentMethod);
  84.         // load additional data only for mollie payment methods
  85.         if (! $mollieAttributes->isMolliePayment()) {
  86.             return;
  87.         }
  88.         // load our settings for the
  89.         // current request
  90.         $this->settings $this->settingsService->getSettings($scId);
  91.         // now use our factory to get the correct
  92.         // client with the correct sales channel settings
  93.         $this->apiClient $this->apiFactory->getClient($scId);
  94.         $this->mollieGateway->switchClient($scId);
  95.         $this->addMollieLocaleVariableToPage($args);
  96.         $this->addMollieProfileIdVariableToPage($args);
  97.         $this->addMollieTestModeVariableToPage($args);
  98.         $this->addMollieComponentsVariableToPage($args);
  99.         $this->addMollieSingleClickPaymentDataToPage($args$mollieAttributes);
  100.         $this->addMolliePosTerminalsVariableToPage($args$mollieAttributes);
  101.     }
  102.     /**
  103.      * Adds the locale for Mollie components to the storefront.
  104.      *
  105.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  106.      */
  107.     private function addMollieLocaleVariableToPage($args): void
  108.     {
  109.         $salesChannelContext $args->getSalesChannelContext();
  110.         $locale $this->mollieLocaleService->getLocale($salesChannelContext);
  111.         $args->getPage()->assign([
  112.             'mollie_locale' => $locale,
  113.         ]);
  114.     }
  115.     /**
  116.      * Adds the test mode variable to the storefront.
  117.      *
  118.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  119.      */
  120.     private function addMollieTestModeVariableToPage($args): void
  121.     {
  122.         $args->getPage()->assign([
  123.             'mollie_test_mode' => $this->settings->isTestMode() ? 'true' 'false',
  124.         ]);
  125.     }
  126.     /**
  127.      * Adds the profile id to the storefront.
  128.      *
  129.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  130.      */
  131.     private function addMollieProfileIdVariableToPage($args): void
  132.     {
  133.         $mollieProfileId $this->loadMollieProfileId();
  134.         $args->getPage()->assign([
  135.             'mollie_profile_id' => $mollieProfileId,
  136.         ]);
  137.     }
  138.     private function loadMollieProfileId(): string
  139.     {
  140.         if ($this->profileId !== null) {
  141.             return $this->profileId;
  142.         }
  143.         $mollieProfileId '';
  144.         /*
  145.          * Fetches the profile id from Mollie's API for the current key.
  146.          */
  147.         try {
  148.             if ($this->apiClient->usesOAuth() === false) {
  149.                 $mollieProfile $this->apiClient->profiles->get('me');
  150.             } else {
  151.                 $mollieProfile $this->apiClient->profiles->page()->offsetGet(0);
  152.             }
  153.             if (isset($mollieProfile->id)) {
  154.                 $mollieProfileId $mollieProfile->id;
  155.             }
  156.         } catch (ApiException $e) {
  157.         }
  158.         $this->profileId $mollieProfileId;
  159.         return $this->profileId;
  160.     }
  161.     /**
  162.      * Adds the components variable to the storefront.
  163.      *
  164.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  165.      */
  166.     private function addMollieComponentsVariableToPage($args): void
  167.     {
  168.         $args->getPage()->assign([
  169.             'enable_credit_card_components' => $this->settings->getEnableCreditCardComponents(),
  170.             'enable_one_click_payments_compact_view' => $this->settings->isOneClickPaymentsCompactView(),
  171.         ]);
  172.     }
  173.     /**
  174.      * Adds ideal issuers variable to the storefront.
  175.      *
  176.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  177.      * @param PaymentMethodAttributes $selectedPayment
  178.      */
  179.     private function addMolliePosTerminalsVariableToPage($args$selectedPayment): void
  180.     {
  181.         // do not load terminals if not required
  182.         if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::POINT_OF_SALE) {
  183.             return;
  184.         }
  185.         try {
  186.             $terminalsArray = [];
  187.             $terminals $this->mollieGateway->getPosTerminals();
  188.             foreach ($terminals as $terminal) {
  189.                 $terminalsArray[] = [
  190.                     'id' => $terminal->id,
  191.                     'name' => $terminal->description,
  192.                 ];
  193.             }
  194.             $args->getPage()->assign(
  195.                 [
  196.                     'mollie_terminals' => $terminalsArray,
  197.                 ]
  198.             );
  199.         } catch (\Exception $e) {
  200.         }
  201.     }
  202.     /**
  203.      * Adds the components variable to the storefront.
  204.      *
  205.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
  206.      * @param PaymentMethodAttributes $selectedPayment
  207.      */
  208.     private function addMollieSingleClickPaymentDataToPage($args$selectedPayment): void
  209.     {
  210.         // do not load credit card mandate if not required
  211.         if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::CREDITCARD) {
  212.             return;
  213.         }
  214.         $args->getPage()->assign([
  215.             'enable_one_click_payments' => $this->settings->isOneClickPaymentsEnabled(),
  216.         ]);
  217.         if (! $this->settings->isOneClickPaymentsEnabled()) {
  218.             return;
  219.         }
  220.         try {
  221.             $salesChannelContext $args->getSalesChannelContext();
  222.             $loggedInCustomer $salesChannelContext->getCustomer();
  223.             if (! $loggedInCustomer instanceof CustomerEntity) {
  224.                 return;
  225.             }
  226.             // only load the list of mandates if the payment method is CreditCardPayment
  227.             if ($salesChannelContext->getPaymentMethod()->getHandlerIdentifier() !== CreditCardPayment::class) {
  228.                 return;
  229.             }
  230.             $mandates $this->mandateService->getCreditCardMandatesByCustomerId($loggedInCustomer->getId(), $salesChannelContext);
  231.             $args->getPage()->setExtensions([
  232.                 'MollieCreditCardMandateCollection' => $mandates,
  233.             ]);
  234.         } catch (\Exception $e) {
  235.         }
  236.     }
  237. }