diff --git a/src/Components/PaymentFilter/DefaultPaymentFilterService.php b/src/Components/PaymentFilter/DefaultPaymentFilterService.php index 3dc876dfe..d7e613849 100644 --- a/src/Components/PaymentFilter/DefaultPaymentFilterService.php +++ b/src/Components/PaymentFilter/DefaultPaymentFilterService.php @@ -18,7 +18,7 @@ class DefaultPaymentFilterService implements PaymentFilterServiceInterface * @param class-string $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, diff --git a/src/Components/PaymentFilter/PayolutionPaymentMethodFilter.php b/src/Components/PaymentFilter/PayolutionPaymentMethodFilter.php new file mode 100644 index 000000000..88f55c564 --- /dev/null +++ b/src/Components/PaymentFilter/PayolutionPaymentMethodFilter.php @@ -0,0 +1,53 @@ +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); + } +} 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 2ff1b60f6..5981f5a4a 100644 --- a/src/DependencyInjection/payment_method_filter.xml +++ b/src/DependencyInjection/payment_method_filter.xml @@ -139,5 +139,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..fdafd95fb --- /dev/null +++ b/tests/Components/PaymentFilter/PayolutionPaymentFilterTest.php @@ -0,0 +1,147 @@ +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, + ]; + } +} 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, - ])); - } -}