From 1b20ec327fdc3737c191959859db56604d72fc93 Mon Sep 17 00:00:00 2001 From: Frederik Rommel Date: Mon, 8 Jan 2024 19:20:18 +0100 Subject: [PATCH] PAYOSWXP-47: payolution: migrate payment-filter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … from event-listener to standardized payment-filter functionality --- .../DefaultPaymentFilterService.php | 4 +- .../PayolutionPaymentMethodFilter.php | 37 ++++ src/DependencyInjection/listeners.xml | 6 - .../payment_method_filter.xml | 20 +++ ...CheckoutConfirmPayolutionEventListener.php | 85 ---------- .../PayolutionPaymentFilterTest.php | 90 ++++++++++ ...koutConfirmPayolutionEventListenerTest.php | 158 ------------------ 7 files changed, 149 insertions(+), 251 deletions(-) create mode 100644 src/Components/PaymentFilter/PayolutionPaymentMethodFilter.php delete mode 100644 src/EventListener/CheckoutConfirmPayolutionEventListener.php create mode 100644 tests/Components/PaymentFilter/PayolutionPaymentFilterTest.php delete mode 100644 tests/EventListener/CheckoutConfirmPayolutionEventListenerTest.php diff --git a/src/Components/PaymentFilter/DefaultPaymentFilterService.php b/src/Components/PaymentFilter/DefaultPaymentFilterService.php index 2c218cb0b..c25455727 100644 --- a/src/Components/PaymentFilter/DefaultPaymentFilterService.php +++ b/src/Components/PaymentFilter/DefaultPaymentFilterService.php @@ -21,8 +21,8 @@ class DefaultPaymentFilterService implements PaymentFilterServiceInterface * @param class-string $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, diff --git a/src/Components/PaymentFilter/PayolutionPaymentMethodFilter.php b/src/Components/PaymentFilter/PayolutionPaymentMethodFilter.php new file mode 100644 index 000000000..9f31d3054 --- /dev/null +++ b/src/Components/PaymentFilter/PayolutionPaymentMethodFilter.php @@ -0,0 +1,37 @@ +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()); + } +} diff --git a/src/DependencyInjection/listeners.xml b/src/DependencyInjection/listeners.xml index 42249a358..b5bd8b39d 100644 --- a/src/DependencyInjection/listeners.xml +++ b/src/DependencyInjection/listeners.xml @@ -42,12 +42,6 @@ - - - - - - diff --git a/src/DependencyInjection/payment_method_filter.xml b/src/DependencyInjection/payment_method_filter.xml index e804c9377..624e5d594 100644 --- a/src/DependencyInjection/payment_method_filter.xml +++ b/src/DependencyInjection/payment_method_filter.xml @@ -147,5 +147,25 @@ + + + PayonePayment\PaymentHandler\PayonePayolutionInvoicingPaymentHandler + + + + + + PayonePayment\PaymentHandler\PayonePayolutionDebitPaymentHandler + + + + + + PayonePayment\PaymentHandler\PayonePayolutionInstallmentPaymentHandler + + diff --git a/src/EventListener/CheckoutConfirmPayolutionEventListener.php b/src/EventListener/CheckoutConfirmPayolutionEventListener.php deleted file mode 100644 index 27f411e2e..000000000 --- a/src/EventListener/CheckoutConfirmPayolutionEventListener.php +++ /dev/null @@ -1,85 +0,0 @@ - 'hidePaymentMethods', - AccountPaymentMethodPageLoadedEvent::class => 'hidePaymentMethods', - AccountEditOrderPageLoadedEvent::class => 'hidePaymentMethods', - ]; - } - - public function hidePaymentMethods( - CheckoutConfirmPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|AccountEditOrderPageLoadedEvent $event - ): void { - $page = $event->getPage(); - - $paymentMethods = $page->getPaymentMethods(); - - if ($this->companyNameMissing($event->getSalesChannelContext(), PayonePayolutionInvoicingPaymentHandler::class)) { - $paymentMethods = $this->removePaymentMethod($paymentMethods, PayonePayolutionInvoicing::UUID); - } - - if ($this->companyNameMissing($event->getSalesChannelContext(), PayonePayolutionDebitPaymentHandler::class)) { - $paymentMethods = $this->removePaymentMethod($paymentMethods, PayonePayolutionDebit::UUID); - } - - if ($this->companyNameMissing($event->getSalesChannelContext(), PayonePayolutionInstallmentPaymentHandler::class)) { - $paymentMethods = $this->removePaymentMethod($paymentMethods, PayonePayolutionInstallment::UUID); - } - - if ($this->companyDataHandlingIsDisabled($event->getSalesChannelContext())) { - $paymentMethods = $this->removePaymentMethod($paymentMethods, PayonePayolutionInvoicing::UUID); - } - - $page->setPaymentMethods($paymentMethods); - } - - private function removePaymentMethod(PaymentMethodCollection $paymentMethods, string $paymentMethodId): PaymentMethodCollection - { - return $paymentMethods->filter( - static fn (PaymentMethodEntity $paymentMethod) => $paymentMethod->getId() !== $paymentMethodId - ); - } - - private function companyDataHandlingIsDisabled(SalesChannelContext $context): bool - { - return !($this->getConfiguration($context, 'payolutionInvoicingTransferCompanyData')); - } - - 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); - } -} diff --git a/tests/Components/PaymentFilter/PayolutionPaymentFilterTest.php b/tests/Components/PaymentFilter/PayolutionPaymentFilterTest.php new file mode 100644 index 000000000..66c5c2e9c --- /dev/null +++ b/tests/Components/PaymentFilter/PayolutionPaymentFilterTest.php @@ -0,0 +1,90 @@ +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, + ]; + } +} diff --git a/tests/EventListener/CheckoutConfirmPayolutionEventListenerTest.php b/tests/EventListener/CheckoutConfirmPayolutionEventListenerTest.php deleted file mode 100644 index af630521c..000000000 --- a/tests/EventListener/CheckoutConfirmPayolutionEventListenerTest.php +++ /dev/null @@ -1,158 +0,0 @@ -setPaymentMethods($page); - - $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - - $event = new CheckoutConfirmPageLoadedEvent($page, $salesChannelContext, new Request()); - $listener = $this->getContainer()->get(CheckoutConfirmPayolutionEventListener::class); - - $listener->hidePaymentMethods($event); - - static::assertSame(0, $event->getPage()->getPaymentMethods()->count()); - } - - public function testItHidesAnotherPaymentMethodsWithInvoicingOnCheckoutConfirmPage(): void - { - $page = new CheckoutConfirmPage(); - $this->setPaymentMethods($page); - - $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - - $this->setPayoneConfig( - $this->getContainer(), - ConfigInstaller::CONFIG_FIELD_PAYOLUTION_INVOICING_TRANSFER_COMPANY_DATA, - true - ); - - $this->setPayoneConfig( - $this->getContainer(), - ConfigurationPrefixes::CONFIGURATION_PREFIXES[PayonePayolutionInvoicingPaymentHandler::class] . 'CompanyName', - 'the-company' - ); - - $event = new CheckoutConfirmPageLoadedEvent($page, $salesChannelContext, new Request()); - $listener = $this->getContainer()->get(CheckoutConfirmPayolutionEventListener::class); - - $listener->hidePaymentMethods($event); - - static::assertSame(1, $event->getPage()->getPaymentMethods()->count()); - static::assertSame(PayonePayolutionInvoicing::UUID, $event->getPage()->getPaymentMethods()->first()->getId()); - } - - public function testItHidesAnotherPaymentMethodsWithDebitOnCheckoutConfirmPage(): void - { - $page = new CheckoutConfirmPage(); - $this->setPaymentMethods($page); - - $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - - $this->setPayoneConfig( - $this->getContainer(), - ConfigInstaller::CONFIG_FIELD_PAYOLUTION_INVOICING_TRANSFER_COMPANY_DATA, - true - ); - - $this->setPayoneConfig( - $this->getContainer(), - ConfigurationPrefixes::CONFIGURATION_PREFIXES[PayonePayolutionDebitPaymentHandler::class] . 'CompanyName', - 'the-company' - ); - - $event = new CheckoutConfirmPageLoadedEvent($page, $salesChannelContext, new Request()); - $listener = $this->getContainer()->get(CheckoutConfirmPayolutionEventListener::class); - - $listener->hidePaymentMethods($event); - - static::assertSame(1, $event->getPage()->getPaymentMethods()->count()); - static::assertSame(PayonePayolutionDebit::UUID, $event->getPage()->getPaymentMethods()->first()->getId()); - } - - public function testItHidesAnotherPaymentMethodsWithInstallmentOnCheckoutConfirmPage(): void - { - $page = new CheckoutConfirmPage(); - $this->setPaymentMethods($page); - - $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - - $this->setPayoneConfig( - $this->getContainer(), - ConfigInstaller::CONFIG_FIELD_PAYOLUTION_INVOICING_TRANSFER_COMPANY_DATA, - true - ); - - $this->setPayoneConfig( - $this->getContainer(), - ConfigurationPrefixes::CONFIGURATION_PREFIXES[PayonePayolutionInstallmentPaymentHandler::class] . 'CompanyName', - 'the-company' - ); - - $event = new CheckoutConfirmPageLoadedEvent($page, $salesChannelContext, new Request()); - $listener = $this->getContainer()->get(CheckoutConfirmPayolutionEventListener::class); - - $listener->hidePaymentMethods($event); - - static::assertSame(1, $event->getPage()->getPaymentMethods()->count()); - static::assertSame(PayonePayolutionInstallment::UUID, $event->getPage()->getPaymentMethods()->first()->getId()); - } - - protected function setPaymentMethods(Page $page): void - { - $paymentMethod1 = new PaymentMethodEntity(); - $paymentMethod2 = new PaymentMethodEntity(); - $paymentMethod3 = new PaymentMethodEntity(); - $paymentMethod4 = new PaymentMethodEntity(); - - $paymentMethod1->setId(PayonePayolutionInstallment::UUID); - $paymentMethod1->setHandlerIdentifier(PayonePayolutionInstallmentPaymentHandler::class); - - $paymentMethod2->setId(PayonePayolutionInvoicing::UUID); - $paymentMethod2->setHandlerIdentifier(PayonePayolutionInvoicingPaymentHandler::class); - - $paymentMethod3->setId(PayonePayolutionDebit::UUID); - $paymentMethod3->setHandlerIdentifier(PayonePayolutionDebitPaymentHandler::class); - - $paymentMethod4->setId(PayonePayolutionInstallment::UUID); - $paymentMethod4->setHandlerIdentifier(PayonePayolutionInstallmentPaymentHandler::class); - - $page->setPaymentMethods(new PaymentMethodCollection([ - $paymentMethod1, - $paymentMethod2, - $paymentMethod3, - $paymentMethod4, - ])); - } -}