custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmPayolutionEventListener.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\Components\ConfigReader\ConfigReaderInterface;
  5. use PayonePayment\PaymentMethod\PayonePayolutionInstallment;
  6. use PayonePayment\PaymentMethod\PayonePayolutionInvoicing;
  7. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  8. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  11. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  13. use Shopware\Storefront\Page\PageLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class CheckoutConfirmPayolutionEventListener implements EventSubscriberInterface
  16. {
  17.     private ConfigReaderInterface $configReader;
  18.     public function __construct(ConfigReaderInterface $configReader)
  19.     {
  20.         $this->configReader $configReader;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             CheckoutConfirmPageLoadedEvent::class => 'hidePaymentMethodsForCompanies',
  26.             AccountPaymentMethodPageLoadedEvent::class => 'hidePaymentMethodsForCompanies',
  27.             AccountEditOrderPageLoadedEvent::class => 'hidePaymentMethodsForCompanies',
  28.         ];
  29.     }
  30.     public function hidePaymentMethodsForCompanies(PageLoadedEvent $event): void
  31.     {
  32.         $page $event->getPage();
  33.         if (
  34.             !method_exists($page'getPaymentMethods')
  35.             || !method_exists($page'setPaymentMethods')
  36.         ) {
  37.             return;
  38.         }
  39.         if (!$this->customerHasCompanyAddress($event->getSalesChannelContext())) {
  40.             return;
  41.         }
  42.         $paymentMethods $page->getPaymentMethods();
  43.         $paymentMethods $this->removePaymentMethod($paymentMethodsPayonePayolutionInstallment::UUID);
  44.         if ($this->companyDataHandlingIsDisabled($event->getSalesChannelContext())) {
  45.             $paymentMethods $this->removePaymentMethod($paymentMethodsPayonePayolutionInvoicing::UUID);
  46.         }
  47.         $page->setPaymentMethods($paymentMethods);
  48.     }
  49.     private function removePaymentMethod(PaymentMethodCollection $paymentMethodsstring $paymentMethodId): PaymentMethodCollection
  50.     {
  51.         return $paymentMethods->filter(
  52.             static function (PaymentMethodEntity $paymentMethod) use ($paymentMethodId) {
  53.                 return $paymentMethod->getId() !== $paymentMethodId;
  54.             }
  55.         );
  56.     }
  57.     private function customerHasCompanyAddress(SalesChannelContext $context): bool
  58.     {
  59.         $customer $context->getCustomer();
  60.         if ($customer === null) {
  61.             return false;
  62.         }
  63.         $billingAddress $customer->getActiveBillingAddress();
  64.         if ($billingAddress === null) {
  65.             return false;
  66.         }
  67.         return !empty($billingAddress->getCompany());
  68.     }
  69.     private function companyDataHandlingIsDisabled(SalesChannelContext $context): bool
  70.     {
  71.         $configuration $this->configReader->read($context->getSalesChannel()->getId());
  72.         return !((bool) $configuration->get('payolutionInvoicingTransferCompanyData'));
  73.     }
  74. }