Skip to content

Commit

Permalink
PAYOSWXP-47: payolution: migrate payment-filter
Browse files Browse the repository at this point in the history
… from event-listener to standardized payment-filter functionality
  • Loading branch information
rommelfreddy committed Mar 21, 2024
1 parent a5cd02e commit 1b20ec3
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 251 deletions.
4 changes: 2 additions & 2 deletions src/Components/PaymentFilter/DefaultPaymentFilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class DefaultPaymentFilterService implements PaymentFilterServiceInterface
* @param class-string<AbstractPayonePaymentHandler> $paymentHandlerClass
*/
public function __construct(
private readonly SystemConfigService $systemConfigService,
private readonly string $paymentHandlerClass,
protected readonly SystemConfigService $systemConfigService,
protected readonly string $paymentHandlerClass,
private readonly ?array $allowedCountries = null,
private readonly ?array $allowedB2bCountries = null,
private readonly ?array $allowedCurrencies = null,
Expand Down
37 changes: 37 additions & 0 deletions src/Components/PaymentFilter/PayolutionPaymentMethodFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\PaymentFilter;

use PayonePayment\Components\ConfigReader\ConfigReader;
use PayonePayment\Components\PaymentFilter\Exception\PaymentMethodNotAllowedException;
use PayonePayment\PaymentHandler\PayonePayolutionInvoicingPaymentHandler;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

class PayolutionPaymentMethodFilter extends DefaultPaymentFilterService
{
protected function additionalChecks(PaymentMethodCollection $methodCollection, PaymentFilterContext $filterContext): void
{
if (empty($this->getPaymentHandlerConfiguration($filterContext->getSalesChannelContext(), 'CompanyName'))) {
throw new PaymentMethodNotAllowedException('Payolution: missing merchant-company name.');
}

if ($this->paymentHandlerClass === PayonePayolutionInvoicingPaymentHandler::class
&& !($this->getPaymentHandlerConfiguration($filterContext->getSalesChannelContext(), 'TransferCompanyData'))
) {
throw new PaymentMethodNotAllowedException('Payolution Invoicing: Missing configuration.');
}
}

private function getPaymentHandlerConfiguration(SalesChannelContext $context, string $configKey): mixed
{
return $this->getConfiguration($context, ConfigReader::getConfigKeyByPaymentHandler($this->paymentHandlerClass, $configKey));
}

private function getConfiguration(SalesChannelContext $context, string $configName): mixed
{
return $this->systemConfigService->get($configName, $context->getSalesChannel()->getId());
}
}
6 changes: 0 additions & 6 deletions src/DependencyInjection/listeners.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@
<tag name="kernel.event_subscriber"/>
</service>

<service id="PayonePayment\EventListener\CheckoutConfirmPayolutionEventListener">
<argument type="service" id="PayonePayment\Components\ConfigReader\ConfigReader" />

<tag name="kernel.event_subscriber"/>
</service>

<service id="PayonePayment\EventListener\CheckoutConfirmTemplateEventListener">
<tag name="kernel.event_subscriber"/>
</service>
Expand Down
20 changes: 20 additions & 0 deletions src/DependencyInjection/payment_method_filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,25 @@
<tag name="payone.payment_method.filter"/>
</service>

<service id="payone.payment_filter_method.payolution.invoice"
class="PayonePayment\Components\PaymentFilter\PayolutionPaymentMethodFilter">
<argument key="$systemConfigService" type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService" />
<argument key="$paymentHandlerClass">PayonePayment\PaymentHandler\PayonePayolutionInvoicingPaymentHandler</argument>
<tag name="payone.payment_method.filter"/>
</service>

<service id="payone.payment_filter_method.payolution.debit"
class="PayonePayment\Components\PaymentFilter\PayolutionPaymentMethodFilter">
<argument key="$systemConfigService" type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService" />
<argument key="$paymentHandlerClass">PayonePayment\PaymentHandler\PayonePayolutionDebitPaymentHandler</argument>
<tag name="payone.payment_method.filter"/>
</service>

<service id="payone.payment_filter_method.payolution.installment"
class="PayonePayment\Components\PaymentFilter\PayolutionPaymentMethodFilter">
<argument key="$systemConfigService" type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService" />
<argument key="$paymentHandlerClass">PayonePayment\PaymentHandler\PayonePayolutionInstallmentPaymentHandler</argument>
<tag name="payone.payment_method.filter"/>
</service>
</services>
</container>
85 changes: 0 additions & 85 deletions src/EventListener/CheckoutConfirmPayolutionEventListener.php

This file was deleted.

90 changes: 90 additions & 0 deletions tests/Components/PaymentFilter/PayolutionPaymentFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\PaymentFilter;

use PayonePayment\Configuration\ConfigurationPrefixes;
use PayonePayment\PaymentHandler\PayonePayolutionDebitPaymentHandler;
use PayonePayment\PaymentHandler\PayonePayolutionInstallmentPaymentHandler;
use PayonePayment\PaymentHandler\PayonePayolutionInvoicingPaymentHandler;
use PayonePayment\TestCaseBase\ConfigurationHelper;
use Shopware\Core\System\Currency\CurrencyEntity;

/**
* @covers \PayonePayment\Components\PaymentFilter\PayolutionPaymentMethodFilter
*/
class PayolutionPaymentFilterTest extends AbstractPaymentFilterTest
{
use ConfigurationHelper;

protected function setUp(): void
{
foreach ($this->getPaymentHandlerClasses() as $handlerClass) {
$this->setPayoneConfig($this->getContainer(), ConfigurationPrefixes::CONFIGURATION_PREFIXES[$handlerClass] . 'CompanyName', 'the-company');
}
$this->setPayoneConfig($this->getContainer(), 'payolutionInvoicingTransferCompanyData', true);
}

protected function getFilterService(?string $paymentHandlerClass = null): PaymentFilterServiceInterface
{
$serviceId = match ($paymentHandlerClass) {
PayonePayolutionInvoicingPaymentHandler::class => 'payone.payment_filter_method.payolution.invoice',
PayonePayolutionDebitPaymentHandler::class => 'payone.payment_filter_method.payolution.debit',
PayonePayolutionInstallmentPaymentHandler::class => 'payone.payment_filter_method.payolution.installment',
};

if (!$serviceId) {
throw new \RuntimeException('unknown payment-handler: ' . $paymentHandlerClass);
}

return $this->getContainer()->get($serviceId);
}

protected function getDisallowedBillingCountry(): ?string
{
return null;
}

protected function getAllowedBillingCountry(): string
{
return 'DE';
}

protected function getDisallowedCurrency(): ?CurrencyEntity
{
return null;
}

protected function getAllowedCurrency(): CurrencyEntity
{
$currency = $this->createMock(CurrencyEntity::class);
$currency->method('getIsoCode')->willReturn('EUR');

return $currency;
}

protected function getTooLowValue(): ?float
{
return null;
}

protected function getTooHighValue(): ?float
{
return null;
}

protected function getAllowedValue(): float
{
return 100.0;
}

protected function getPaymentHandlerClasses(): array
{
return [
PayonePayolutionInvoicingPaymentHandler::class,
PayonePayolutionDebitPaymentHandler::class,
PayonePayolutionInstallmentPaymentHandler::class,
];
}
}
Loading

0 comments on commit 1b20ec3

Please sign in to comment.