From cfb4830d671a3377ada05235e2e47a99343f7c2c Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Thu, 29 Feb 2024 14:44:18 +0100 Subject: [PATCH] NTR: Fix #704 (#718) * NTR: Fix #704 * NTR: check subscription with custom routes * NTR: fix translation problem --------- Co-authored-by: Vitalij Mik --- .../Api/PluginConfig/ConfigControllerBase.php | 12 ++++++++++++ .../api/mollie-payments-config.service.js | 15 +++++++++++++++ .../src/module/mollie-payments/index.js | 16 +++++++++++----- src/Service/PaymentMethodService.php | 18 +++++++++++++++++- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/Controller/Api/PluginConfig/ConfigControllerBase.php b/src/Controller/Api/PluginConfig/ConfigControllerBase.php index 88a1a5a9f..a3b65a153 100644 --- a/src/Controller/Api/PluginConfig/ConfigControllerBase.php +++ b/src/Controller/Api/PluginConfig/ConfigControllerBase.php @@ -206,6 +206,18 @@ public function getRefundManagerConfigLegacy(Request $request, Context $context) return $this->getRefundManagerConfig($request, $context); } + /** + * @Route("/api/_action/mollie/config/subscription", name="api.action.mollie.config.subscription", methods={"POST"}) + * @return JsonResponse + */ + public function getSubscriptionConfig(): JsonResponse + { + $config = $this->settings->getSettings(); + return new JsonResponse([ + 'enabled' => $config->isSubscriptionsEnabled() + ]); + } + /** * @param string $snippetName * @param string $locale diff --git a/src/Resources/app/administration/src/core/service/api/mollie-payments-config.service.js b/src/Resources/app/administration/src/core/service/api/mollie-payments-config.service.js index f11017177..946037192 100644 --- a/src/Resources/app/administration/src/core/service/api/mollie-payments-config.service.js +++ b/src/Resources/app/administration/src/core/service/api/mollie-payments-config.service.js @@ -56,6 +56,21 @@ export default class MolliePaymentsConfigService extends ApiService { }); } + getSubscriptionConfig(){ + return this.httpClient + .post( + `_action/${this.getApiBasePath()}/config/subscription`, + { + locale: this.currentLocale, + }, + { + headers: this.getBasicHeaders(), + } + ).then((response) => { + return ApiService.handleResponse(response); + }); + } + /** * * @param salesChannelId diff --git a/src/Resources/app/administration/src/module/mollie-payments/index.js b/src/Resources/app/administration/src/module/mollie-payments/index.js index 0fad54643..433f91e1a 100644 --- a/src/Resources/app/administration/src/module/mollie-payments/index.js +++ b/src/Resources/app/administration/src/module/mollie-payments/index.js @@ -19,19 +19,24 @@ import './page/mollie-subscriptions-detail'; import defaultSearchConfiguration from './default-search-configuration'; + // eslint-disable-next-line no-undef -const {Module, ApiService, Plugin} = Shopware; +const {Module,Plugin,Service} = Shopware; // Tell Shopware to wait loading until we call resolve. const resolve = Plugin.addBootPromise(); -// Because we first have to load our config from the database -const systemConfig = ApiService.getByName('systemConfigApiService') -systemConfig.getValues('MolliePayments').then(config => { +/** + * + * @type {MolliePaymentsConfigService} + */ +const configService = Service('MolliePaymentsConfigService'); +// Because we first have to check if subscription is enabled or not +configService.getSubscriptionConfig().then(result => { const navigation = []; - if(config['MolliePayments.config.subscriptionsEnabled']) { + if (result.enabled === true) { navigation.push({ id: 'mollie-subscriptions', label: 'mollie-payments.subscriptions.navigation.title', @@ -83,6 +88,7 @@ systemConfig.getValues('MolliePayments').then(config => { defaultSearchConfiguration, }); +}).finally(()=>{ // Now tell Shopware it's okay to load the administration resolve(); }); diff --git a/src/Service/PaymentMethodService.php b/src/Service/PaymentMethodService.php index 5d9c1e85d..446d4d703 100644 --- a/src/Service/PaymentMethodService.php +++ b/src/Service/PaymentMethodService.php @@ -37,6 +37,7 @@ use Shopware\Core\Checkout\Payment\PaymentMethodEntity; use Shopware\Core\Content\Media\MediaCollection; use Shopware\Core\Content\Media\MediaService; +use Shopware\Core\Defaults; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent; use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException; @@ -175,8 +176,17 @@ public function addPaymentMethods(array $paymentMethods, Context $context): void # unfortunately some fields are required (*sigh) # so we need to provide those with the value of # the existing method!!! - 'name' => $existingPaymentMethod->getName(), + 'name' => $existingPaymentMethod->getName() ]; + $translations = $existingPaymentMethod->getTranslations(); + + if ($translations !== null) { + foreach ($translations as $translation) { + $paymentMethodData['translations'][$translation->getLanguageId()]=[ + 'name' => $translation->getName() + ]; + } + } if ($this->versionCompare->gte('6.5.7.0')) { # we do a string cast here, since getTechnicalName will be not nullable in the future @@ -193,6 +203,11 @@ public function addPaymentMethods(array $paymentMethods, Context $context): void 'description' => '', 'mediaId' => $mediaId, 'afterOrderEnabled' => true, + 'translations'=>[ + Defaults::LANGUAGE_SYSTEM=>[ + 'name' => $paymentMethod['description'] + ] + ] ]; } @@ -377,6 +392,7 @@ private function getPaymentMethod($handlerIdentifier, Context $context): ?Paymen // Fetch ID for update $paymentCriteria = new Criteria(); $paymentCriteria->addFilter(new EqualsFilter('handlerIdentifier', $handlerIdentifier)); + $paymentCriteria->addAssociation('translations'); // Get payment IDs $paymentMethods = $this->paymentRepository->search($paymentCriteria, $context);