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 Jan 8, 2024
1 parent c99228f commit dc96a4e
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DefaultPaymentFilterService implements PaymentFilterServiceInterface
* @param class-string<AbstractPayonePaymentHandler> $paymentHandlerClass
*/
public function __construct(
private readonly string $paymentHandlerClass,
protected readonly string $paymentHandlerClass,
private readonly ?array $allowedCountries = null,
private readonly ?array $allowedB2bCountries = null,
private readonly ?array $allowedCurrencies = null,
Expand Down
53 changes: 53 additions & 0 deletions src/Components/PaymentFilter/PayolutionPaymentMethodFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\PaymentFilter;

use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
use PayonePayment\Configuration\ConfigurationPrefixes;
use PayonePayment\PaymentHandler\PayonePayolutionInvoicingPaymentHandler;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

class PayolutionPaymentMethodFilter extends DefaultPaymentFilterService
{
public function __construct(
private readonly ConfigReaderInterface $configReader,
string $paymentHandlerClass,
?array $allowedCountries = null,
?array $allowedB2bCountries = null,
?array $allowedCurrencies = null,
float $allowedMinValue = 0.0,
?float $allowedMaxValue = null
) {
parent::__construct($paymentHandlerClass, $allowedCountries, $allowedB2bCountries, $allowedCurrencies, $allowedMinValue, $allowedMaxValue);
}

public function filterPaymentMethods(PaymentMethodCollection $methodCollection, PaymentFilterContext $filterContext): PaymentMethodCollection
{
$methodCollection = parent::filterPaymentMethods($methodCollection, $filterContext);

if ($this->companyNameMissing($filterContext->getSalesChannelContext(), $this->paymentHandlerClass)) {
$methodCollection = $this->removePaymentMethod($methodCollection);
}

if ($this->paymentHandlerClass === PayonePayolutionInvoicingPaymentHandler::class
&& !($this->getConfiguration($filterContext->getSalesChannelContext(), 'payolutionInvoicingTransferCompanyData'))
) {
$methodCollection = $this->removePaymentMethod($methodCollection);
}

return $methodCollection;
}

private function companyNameMissing(SalesChannelContext $context, string $paymentHandler): bool
{
return empty($this->getConfiguration($context, ConfigurationPrefixes::CONFIGURATION_PREFIXES[$paymentHandler] . 'CompanyName'));
}

private function getConfiguration(SalesChannelContext $context, string $configName): array|bool|int|string|null
{
return $this->configReader->read($context->getSalesChannel()->getId())->get($configName);
}
}
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 @@ -139,5 +139,25 @@
<tag name="payone.payment_method.filter"/>
</service>

<service id="payone.payment_filter_method.payolution.invoice"
class="PayonePayment\Components\PaymentFilter\PayolutionPaymentMethodFilter">
<argument key="$configReader" type="service" id="PayonePayment\Components\ConfigReader\ConfigReader"/>
<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="$configReader" type="service" id="PayonePayment\Components\ConfigReader\ConfigReader"/>
<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="$configReader" type="service" id="PayonePayment\Components\ConfigReader\ConfigReader"/>
<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.

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

declare(strict_types=1);

namespace PayonePayment\EventListener;

use PayonePayment\Components\PaymentFilter\PaymentFilterContext;
use PayonePayment\Components\PaymentFilter\PaymentFilterServiceInterface;
use PayonePayment\Configuration\ConfigurationPrefixes;
use PayonePayment\PaymentHandler\PayonePayolutionDebitPaymentHandler;
use PayonePayment\PaymentHandler\PayonePayolutionInstallmentPaymentHandler;
use PayonePayment\PaymentHandler\PayonePayolutionInvoicingPaymentHandler;
use PayonePayment\TestCaseBase\ConfigurationHelper;
use PayonePayment\TestCaseBase\Mock\PaymentHandler\PaymentHandlerMock;
use Shopware\Core\System\Country\CountryEntity;
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);
}

/**
* @dataProvider dataProviderPaymentHandlerClasses
*/
public function testIfMethodsAreNotAvailableIfCompanyIsNotSet(string $paymentHandlerClass): void
{
$this->setPayoneConfig($this->getContainer(), ConfigurationPrefixes::CONFIGURATION_PREFIXES[$paymentHandlerClass] . 'CompanyName', null);

$methods = $this->getPaymentMethods($paymentHandlerClass);

$country = new CountryEntity();
$country->setIso($this->getAllowedBillingCountry());

$salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation();
$salesChannelContext->getCustomer()->getActiveBillingAddress()->setCountry($country);

$filterContext = new PaymentFilterContext(
$salesChannelContext,
$salesChannelContext->getCustomer()->getActiveBillingAddress(),
$salesChannelContext->getCustomer()->getActiveShippingAddress(),
$this->getAllowedCurrency()
);

$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertNotInPaymentCollection($paymentHandlerClass, $result, 'payment handler should be not available, because company value has not been set');
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
}

public function testIfInvoiceIsNotAvailableIfTransferCompanyDataIsDisabled(): void
{
$this->setPayoneConfig($this->getContainer(), 'payolutionInvoicingTransferCompanyData', false);

$country = new CountryEntity();
$country->setIso($this->getAllowedBillingCountry());

$salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation();
$salesChannelContext->getCustomer()->getActiveBillingAddress()->setCountry($country);

$filterContext = new PaymentFilterContext(
$salesChannelContext,
$salesChannelContext->getCustomer()->getActiveBillingAddress(),
$salesChannelContext->getCustomer()->getActiveShippingAddress(),
$this->getAllowedCurrency()
);

$result = $this->getFilterService(PayonePayolutionInvoicingPaymentHandler::class)
->filterPaymentMethods($this->getPaymentMethods(PayonePayolutionInvoicingPaymentHandler::class), $filterContext);


static::assertNotInPaymentCollection(PayonePayolutionInvoicingPaymentHandler::class, $result, 'payment handler should be not available, because configuration is disabled');
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
}

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 dc96a4e

Please sign in to comment.