-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PAYOSWXP-47: payolution: migrate payment-filter
… from event-listener to standardized payment-filter functionality
- Loading branch information
1 parent
c99228f
commit dc96a4e
Showing
7 changed files
with
221 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/Components/PaymentFilter/PayolutionPaymentMethodFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 0 additions & 85 deletions
85
src/EventListener/CheckoutConfirmPayolutionEventListener.php
This file was deleted.
Oops, something went wrong.
147 changes: 147 additions & 0 deletions
147
tests/Components/PaymentFilter/PayolutionPaymentFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
Oops, something went wrong.