Skip to content

Commit

Permalink
MOL-1287: add technical name to payment methods
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Jan 4, 2024
1 parent 250a3d3 commit 9533950
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
</service>

<service id="Kiener\MolliePayments\Service\PaymentMethodService" class="Kiener\MolliePayments\Service\PaymentMethodService" public="true">
<argument>%kernel.shopware_version%</argument>
<argument type="service" id="Shopware\Core\Content\Media\MediaService"/>
<argument type="service" id="Kiener\MolliePayments\Repository\Media\MediaRepository"/>
<argument type="service" id="Kiener\MolliePayments\Repository\PaymentMethod\PaymentMethodRepository"/>
Expand Down
42 changes: 38 additions & 4 deletions src/Service/PaymentMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +46,12 @@

class PaymentMethodService
{

/**
*
*/
public const TECHNICAL_NAME_PREFIX = 'payment_mollie_';

/**
* @var MediaService
*/
Expand All @@ -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);
}


Expand Down Expand Up @@ -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(),
Expand All @@ -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)
Expand All @@ -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;
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPUnit/Service/PaymentMethodServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit 9533950

Please sign in to comment.