diff --git a/src/EventListener/CheckoutConfirmPayolutionEventListener.php b/src/EventListener/CheckoutConfirmPayolutionEventListener.php index 8c2f0ce9b..e609d63ed 100644 --- a/src/EventListener/CheckoutConfirmPayolutionEventListener.php +++ b/src/EventListener/CheckoutConfirmPayolutionEventListener.php @@ -5,6 +5,11 @@ namespace PayonePayment\EventListener; use PayonePayment\Components\ConfigReader\ConfigReaderInterface; +use PayonePayment\Configuration\ConfigurationPrefixes; +use PayonePayment\PaymentHandler\PayonePayolutionDebitPaymentHandler; +use PayonePayment\PaymentHandler\PayonePayolutionInstallmentPaymentHandler; +use PayonePayment\PaymentHandler\PayonePayolutionInvoicingPaymentHandler; +use PayonePayment\PaymentMethod\PayonePayolutionDebit; use PayonePayment\PaymentMethod\PayonePayolutionInstallment; use PayonePayment\PaymentMethod\PayonePayolutionInvoicing; use Shopware\Core\Checkout\Payment\PaymentMethodCollection; @@ -24,29 +29,30 @@ public function __construct(private readonly ConfigReaderInterface $configReader public static function getSubscribedEvents(): array { return [ - CheckoutConfirmPageLoadedEvent::class => 'hidePaymentMethodsForCompanies', - AccountPaymentMethodPageLoadedEvent::class => 'hidePaymentMethodsForCompanies', - AccountEditOrderPageLoadedEvent::class => 'hidePaymentMethodsForCompanies', + CheckoutConfirmPageLoadedEvent::class => 'hidePaymentMethods', + AccountPaymentMethodPageLoadedEvent::class => 'hidePaymentMethods', + AccountEditOrderPageLoadedEvent::class => 'hidePaymentMethods', ]; } - public function hidePaymentMethodsForCompanies( + public function hidePaymentMethods( CheckoutConfirmPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|AccountEditOrderPageLoadedEvent $event ): void { $page = $event->getPage(); - if (!$this->customerHasCompanyAddress($event->getSalesChannelContext())) { - return; - } - $paymentMethods = $page->getPaymentMethods(); - $paymentMethods = $this->removePaymentMethod($paymentMethods, PayonePayolutionInstallment::UUID); + /* Remove Payolution payment methods if company name is missing */ + $paymentMethods = $this->removePaymentMethodIfMissing($paymentMethods, $event->getSalesChannelContext(), PayonePayolutionInvoicingPaymentHandler::class, PayonePayolutionInvoicing::UUID); + $paymentMethods = $this->removePaymentMethodIfMissing($paymentMethods, $event->getSalesChannelContext(), PayonePayolutionDebitPaymentHandler::class, PayonePayolutionDebit::UUID); + $paymentMethods = $this->removePaymentMethodIfMissing($paymentMethods, $event->getSalesChannelContext(), PayonePayolutionInstallmentPaymentHandler::class, PayonePayolutionInstallment::UUID); + if ($this->companyDataHandlingIsDisabled($event->getSalesChannelContext())) { $paymentMethods = $this->removePaymentMethod($paymentMethods, PayonePayolutionInvoicing::UUID); } + $page->setPaymentMethods($paymentMethods); } @@ -57,27 +63,31 @@ private function removePaymentMethod(PaymentMethodCollection $paymentMethods, st ); } - private function customerHasCompanyAddress(SalesChannelContext $context): bool - { - $customer = $context->getCustomer(); - - if ($customer === null) { - return false; + private function removePaymentMethodIfMissing( + PaymentMethodCollection $paymentMethods, + SalesChannelContext $context, + string $paymentHandlerClass, + string $paymentMethodUUID + ): PaymentMethodCollection|array { + if ($this->companyNameMissing($context, $paymentHandlerClass)) { + return $this->removePaymentMethod($paymentMethods, $paymentMethodUUID); } - $billingAddress = $customer->getActiveBillingAddress(); - - if ($billingAddress === null) { - return false; - } - - return !empty($billingAddress->getCompany()); + return $paymentMethods; } private function companyDataHandlingIsDisabled(SalesChannelContext $context): bool { - $configuration = $this->configReader->read($context->getSalesChannel()->getId()); + return !($this->getConfiguration($context, 'payolutionInvoicingTransferCompanyData')); + } - return !($configuration->get('payolutionInvoicingTransferCompanyData')); + private function companyNameMissing(SalesChannelContext $context, string $paymentHandler): bool + { + return empty($this->getConfiguration($context, ConfigurationPrefixes::CONFIGURATION_PREFIXES[$paymentHandler] . 'CompanyName')); + } + + private function getConfiguration(SalesChannelContext $context, $configName): array|bool|int|string + { + return $this->configReader->read($context->getSalesChannel()->getId())->get($configName); } } diff --git a/src/Resources/snippet/en_GB/messages.en-GB.json b/src/Resources/snippet/en_GB/messages.en-GB.json index 83289573f..f25535503 100644 --- a/src/Resources/snippet/en_GB/messages.en-GB.json +++ b/src/Resources/snippet/en_GB/messages.en-GB.json @@ -101,7 +101,7 @@ }, "consentCheckbox": { "introduction": "With the transmission of the data necessary for the completion of the invoice purchase, an identity check and credit check to payolution GmbH, Am Euro Platz 2, 1120 Wien I agree.", - "textWithLink": "I can revoke my consent at any time with effect for the future." + "textWithLink": "I can revoke my consent at any time with effect for the future." }, "mandateCheckbox": { "textWithLink": "I hereby issue the SEPA direct debit mandate." @@ -290,4 +290,4 @@ "VIOLATION::PAYONE_INVALID_FORMAT_ERROR": "Please provide a valid IBAN.", "VIOLATION::PAYONE_NOT_SUPPORTED_COUNTRY_CODE_ERROR": "The country of the IBAN is not valid." } -} +} \ No newline at end of file