Skip to content

Commit

Permalink
MOL-1263: load data for selected payment methods only (mollie#659)
Browse files Browse the repository at this point in the history
* MOL-1263: load data for selected payment methods only

* MOL-1263: remove unused repositories

* MOL-1263: check locale for null

---------

Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Nov 27, 2023
1 parent 214c933 commit 9dc38d8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 46 deletions.
1 change: 0 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@
<argument type="service" id="Kiener\MolliePayments\Factory\MollieApiFactory"/>
<argument type="service" id="Kiener\MolliePayments\Service\SettingsService"/>
<argument type="service" id="Kiener\MolliePayments\Repository\Language\LanguageRepository"/>
<argument type="service" id="Kiener\MolliePayments\Repository\Locale\LocaleRepository"/>
<argument type="service" id="Kiener\MolliePayments\Service\MandateService"/>
<argument type="service" id="Kiener\MolliePayments\Gateway\Mollie\MollieGateway"/>
<tag name="kernel.event_subscriber"/>
Expand Down
107 changes: 62 additions & 45 deletions src/Subscriber/CheckoutConfirmPageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
use Kiener\MolliePayments\Service\MandateServiceInterface;
use Kiener\MolliePayments\Service\SettingsService;
use Kiener\MolliePayments\Setting\MollieSettingStruct;
use Kiener\MolliePayments\Struct\PaymentMethod\PaymentMethodAttributes;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Method;
use Mollie\Api\Resources\Terminal;
use Mollie\Api\Types\PaymentMethod;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\Language\LanguageEntity;
use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -53,10 +56,6 @@ class CheckoutConfirmPageSubscriber implements EventSubscriberInterface
*/
private $repoLanguages;

/**
* @var LocaleRepositoryInterface
*/
private $repoLocales;

/**
* @var MandateServiceInterface
Expand All @@ -68,6 +67,10 @@ class CheckoutConfirmPageSubscriber implements EventSubscriberInterface
*/
private $mollieGateway;

/**
* @var ?string
*/
private $profileId = null;

/**
* @return array<mixed>>
Expand All @@ -86,16 +89,14 @@ public static function getSubscribedEvents(): array
* @param MollieApiFactory $apiFactory
* @param SettingsService $settingsService
* @param LanguageRepositoryInterface $languageRepositoryInterface
* @param LocaleRepositoryInterface $localeRepositoryInterface
* @param MandateServiceInterface $mandateService
* @param MollieGatewayInterface $mollieGateway
*/
public function __construct(MollieApiFactory $apiFactory, SettingsService $settingsService, LanguageRepositoryInterface $languageRepositoryInterface, LocaleRepositoryInterface $localeRepositoryInterface, MandateServiceInterface $mandateService, MollieGatewayInterface $mollieGateway)
public function __construct(MollieApiFactory $apiFactory, SettingsService $settingsService, LanguageRepositoryInterface $languageRepositoryInterface, MandateServiceInterface $mandateService, MollieGatewayInterface $mollieGateway)
{
$this->apiFactory = $apiFactory;
$this->settingsService = $settingsService;
$this->repoLanguages = $languageRepositoryInterface;
$this->repoLocales = $localeRepositoryInterface;
$this->mandateService = $mandateService;
$this->mollieGateway = $mollieGateway;
}
Expand All @@ -107,11 +108,19 @@ public function __construct(MollieApiFactory $apiFactory, SettingsService $setti
*/
public function addDataToPage($args): void
{
$scId = $args->getSalesChannelContext()->getSalesChannel()->getId();

$currentSelectedPaymentMethod = $args->getSalesChannelContext()->getPaymentMethod();
$mollieAttributes = new PaymentMethodAttributes($currentSelectedPaymentMethod);

# load additional data only for mollie payment methods
if (! $mollieAttributes->isMolliePayment()) {
return;
}

# load our settings for the
# current request
$this->settings = $this->settingsService->getSettings($args->getSalesChannelContext()->getSalesChannel()->getId());

$scId = $args->getSalesChannelContext()->getSalesChannel()->getId();
$this->settings = $this->settingsService->getSettings($scId);

# now use our factory to get the correct
# client with the correct sales channel settings
Expand All @@ -123,9 +132,9 @@ public function addDataToPage($args): void
$this->addMollieProfileIdVariableToPage($args);
$this->addMollieTestModeVariableToPage($args);
$this->addMollieComponentsVariableToPage($args);
$this->addMollieIdealIssuersVariableToPage($args);
$this->addMollieSingleClickPaymentDataToPage($args);
$this->addMolliePosTerminalsVariableToPage($args);
$this->addMollieIdealIssuersVariableToPage($args, $mollieAttributes);
$this->addMollieSingleClickPaymentDataToPage($args, $mollieAttributes);
$this->addMolliePosTerminalsVariableToPage($args, $mollieAttributes);
}

/**
Expand Down Expand Up @@ -166,7 +175,6 @@ private function addMollieLocaleVariableToPage($args): void
*/
$locale = '';

$context = $args->getContext();
$salesChannelContext = $args->getSalesChannelContext();


Expand All @@ -175,17 +183,16 @@ private function addMollieLocaleVariableToPage($args): void
$languageId = $salesChannel->getLanguageId();
if ($languageId !== null) {
$languageCriteria = new Criteria();
$languageCriteria->addAssociation('locale');
$languageCriteria->addFilter(new EqualsFilter('id', $languageId));

$languages = $this->repoLanguages->search($languageCriteria, $args->getContext());
$languagesResult = $this->repoLanguages->search($languageCriteria, $args->getContext());
/** @var LanguageEntity $language */
$language = $languagesResult->first();

$localeId = $languages->first()->getLocaleId();

$localeCriteria = new Criteria();
$localeCriteria->addFilter(new EqualsFilter('id', $localeId));

$locales = $this->repoLocales->search($localeCriteria, $args->getContext());
$locale = $locales->first()->getCode();
if ($language !== null && $language->getLocale() !== null) {
$locale = $language->getLocale()->getCode();
}
}
}

Expand Down Expand Up @@ -231,6 +238,18 @@ private function addMollieTestModeVariableToPage($args): void
*/
private function addMollieProfileIdVariableToPage($args): void
{
$mollieProfileId = $this->loadMollieProfileId();

$args->getPage()->assign([
'mollie_profile_id' => $mollieProfileId,
]);
}

private function loadMollieProfileId(): string
{
if ($this->profileId !== null) {
return $this->profileId;
}
$mollieProfileId = '';

/**
Expand All @@ -249,10 +268,9 @@ private function addMollieProfileIdVariableToPage($args): void
} catch (ApiException $e) {
//
}
$this->profileId = $mollieProfileId;

$args->getPage()->assign([
'mollie_profile_id' => $mollieProfileId,
]);
return $this->profileId;
}

/**
Expand All @@ -272,30 +290,19 @@ private function addMollieComponentsVariableToPage($args): void
* Adds ideal issuers variable to the storefront.
*
* @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
* @param PaymentMethodAttributes $selectedPayment
*/
private function addMollieIdealIssuersVariableToPage($args): void
private function addMollieIdealIssuersVariableToPage($args, $selectedPayment): void
{
// do not load ideal issuers if not required
if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::IDEAL) {
return;
}
$customFields = [];
$ideal = null;
$mollieProfileId = '';
$preferredIssuer = '';

/**
* Fetches the profile id from Mollie's API for the current key.
*/
try {
if ($this->apiClient->usesOAuth() === false) {
$mollieProfile = $this->apiClient->profiles->get('me');
} else {
$mollieProfile = $this->apiClient->profiles->page()->offsetGet(0);
}

if (isset($mollieProfile->id)) {
$mollieProfileId = $mollieProfile->id;
}
} catch (ApiException $e) {
//
}
$mollieProfileId = $this->loadMollieProfileId();

// Get custom fields from the customer in the sales channel context
if ($args->getSalesChannelContext()->getCustomer() !== null) {
Expand Down Expand Up @@ -340,9 +347,14 @@ private function addMollieIdealIssuersVariableToPage($args): void
* Adds ideal issuers variable to the storefront.
*
* @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
* @param PaymentMethodAttributes $selectedPayment
*/
private function addMolliePosTerminalsVariableToPage($args): void
private function addMolliePosTerminalsVariableToPage($args, $selectedPayment): void
{
//do not load terminals if not required
if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::POINT_OF_SALE) {
return;
}
try {
$terminalsArray = [];

Expand All @@ -368,9 +380,14 @@ private function addMolliePosTerminalsVariableToPage($args): void
* Adds the components variable to the storefront.
*
* @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $args
* @param PaymentMethodAttributes $selectedPayment
*/
private function addMollieSingleClickPaymentDataToPage($args): void
private function addMollieSingleClickPaymentDataToPage($args, $selectedPayment): void
{
// do not load credit card mandate if not required
if ($selectedPayment->getMollieIdentifier() !== PaymentMethod::CREDITCARD) {
return;
}
$args->getPage()->assign([
'enable_one_click_payments' => $this->settings->isOneClickPaymentsEnabled(),
]);
Expand Down

0 comments on commit 9dc38d8

Please sign in to comment.