From f411812bcdb237a975cc81e5dea5ad0f90008103 Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 5 Jan 2024 10:13:43 +0100 Subject: [PATCH] MOL-1287: add technical name to payment methods (#675) --- src/Resources/config/services.xml | 1 + src/Service/PaymentMethodService.php | 42 +++++++++++++++++-- .../Service/PaymentMethodServiceTest.php | 13 ++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index f426f9038..b79e98b45 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -86,6 +86,7 @@ + %kernel.shopware_version% diff --git a/src/Service/PaymentMethodService.php b/src/Service/PaymentMethodService.php index cd68ecc1a..e1f2da903 100644 --- a/src/Service/PaymentMethodService.php +++ b/src/Service/PaymentMethodService.php @@ -2,6 +2,7 @@ namespace Kiener\MolliePayments\Service; +use Kiener\MolliePayments\Compatibility\VersionCompare; use Kiener\MolliePayments\Handler\Method\ApplePayPayment; use Kiener\MolliePayments\Handler\Method\BanContactPayment; use Kiener\MolliePayments\Handler\Method\BankTransferPayment; @@ -45,6 +46,12 @@ class PaymentMethodService { + + /** + * + */ + public const TECHNICAL_NAME_PREFIX = 'payment_mollie_'; + /** * @var MediaService */ @@ -70,21 +77,29 @@ class PaymentMethodService */ private $httpClient; + /** + * @var VersionCompare + */ + private $versionCompare; + /** + * @param string $shopwareVersion * @param MediaService $mediaService * @param MediaRepositoryInterface $mediaRepository * @param PaymentMethodRepositoryInterface $paymentRepository * @param PluginIdProvider $pluginIdProvider * @param HttpClientInterface $httpClient */ - public function __construct(MediaService $mediaService, MediaRepositoryInterface $mediaRepository, PaymentMethodRepositoryInterface $paymentRepository, PluginIdProvider $pluginIdProvider, HttpClientInterface $httpClient) + public function __construct(string $shopwareVersion, MediaService $mediaService, MediaRepositoryInterface $mediaRepository, PaymentMethodRepositoryInterface $paymentRepository, PluginIdProvider $pluginIdProvider, HttpClientInterface $httpClient) { $this->mediaService = $mediaService; $this->mediaRepository = $mediaRepository; $this->paymentRepository = $paymentRepository; $this->pluginIdProvider = $pluginIdProvider; $this->httpClient = $httpClient; + + $this->versionCompare = new VersionCompare($shopwareVersion); } @@ -146,6 +161,10 @@ public function addPaymentMethods(array $paymentMethods, Context $context): void if ($existingPaymentMethod instanceof PaymentMethodEntity) { + + # extract the existing name + $mollieName = $paymentMethod['name']; + $paymentMethodData = [ # ALWAYS ADD THE ID, otherwise upsert would create NEW entries! 'id' => $existingPaymentMethod->getId(), @@ -155,7 +174,7 @@ public function addPaymentMethods(array $paymentMethods, Context $context): void # so that Mollie does always work for our wonderful customers :) 'pluginId' => $pluginId, 'customFields' => [ - 'mollie_payment_method_name' => $paymentMethod['name'], + 'mollie_payment_method_name' => $mollieName, ], # ------------------------------------------ # unfortunately some fields are required (*sigh) @@ -164,24 +183,39 @@ public function addPaymentMethods(array $paymentMethods, Context $context): void 'name' => $existingPaymentMethod->getName(), ]; + # starting with Shopware 6.5.7.0 this has to be filled out + # so that you can still save the payment method in the administration + if ($this->versionCompare->gte('6.5.7.0')) { + $paymentMethodData['technicalName'] = self::TECHNICAL_NAME_PREFIX . $mollieName; + } + $upsertData[] = $paymentMethodData; } else { # let's create a full parameter list of everything # that our new payment method needs to have + + $mollieName = $paymentMethod['description']; + $paymentMethodData = [ 'handlerIdentifier' => $paymentMethod['handler'], 'pluginId' => $pluginId, # ------------------------------------------ - 'name' => $paymentMethod['description'], + 'name' => $mollieName, 'description' => '', 'mediaId' => $mediaId, 'afterOrderEnabled' => true, # ------------------------------------------ 'customFields' => [ - 'mollie_payment_method_name' => $paymentMethod['name'], + 'mollie_payment_method_name' => $mollieName ], ]; + # starting with Shopware 6.5.7.0 this has to be filled out + # so that you can still save the payment method in the administration + if ($this->versionCompare->gte('6.5.7.0')) { + $paymentMethodData['technicalName'] = self::TECHNICAL_NAME_PREFIX . $mollieName; + } + $upsertData[] = $paymentMethodData; } } diff --git a/tests/PHPUnit/Service/PaymentMethodServiceTest.php b/tests/PHPUnit/Service/PaymentMethodServiceTest.php index ab4b30848..4dbd0061c 100644 --- a/tests/PHPUnit/Service/PaymentMethodServiceTest.php +++ b/tests/PHPUnit/Service/PaymentMethodServiceTest.php @@ -76,6 +76,7 @@ protected function setUp(): void $this->paymentMethodRepository = new FakePaymentMethodRepository($paymentMethod); $this->paymentMethodService = new PaymentMethodService( + '6.5.6.0', $this->createMock(MediaService::class), $this->mediaRepository, $this->paymentMethodRepository, @@ -84,6 +85,18 @@ protected function setUp(): void ); } + /** + * Starting with Shopware 6.5.7.0 a new technical name is + * required for a payment method. + * This test verifies that our used prefix is always the same. + * + * @return void + */ + public function testTechnicalPaymentMethodPrefix(): void + { + $this->assertEquals('payment_mollie_', PaymentMethodService::TECHNICAL_NAME_PREFIX); + } + /** * This test verifies that our list of officially supported payment * methods is not touched without recognizing it.