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

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.     /** @var ConfigReaderInterface */
  18.     private $configReader;
  19.     public function __construct(ConfigReaderInterface $configReader)
  20.     {
  21.         $this->configReader $configReader;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             CheckoutConfirmPageLoadedEvent::class      => 'hidePaymentMethodsForCompanies',
  27.             AccountPaymentMethodPageLoadedEvent::class => 'hidePaymentMethodsForCompanies',
  28.             AccountEditOrderPageLoadedEvent::class     => 'hidePaymentMethodsForCompanies',
  29.         ];
  30.     }
  31.     public function hidePaymentMethodsForCompanies(PageLoadedEvent $event): void
  32.     {
  33.         $page $event->getPage();
  34.         if (
  35.             !method_exists($page'getPaymentMethods') ||
  36.             !method_exists($page'setPaymentMethods')
  37.         ) {
  38.             return;
  39.         }
  40.         if (!$this->customerHasCompanyAddress($event->getSalesChannelContext())) {
  41.             return;
  42.         }
  43.         $paymentMethods $page->getPaymentMethods();
  44.         $paymentMethods $this->removePaymentMethod($paymentMethodsPayonePayolutionInstallment::UUID);
  45.         if ($this->companyDataHandlingIsDisabled($event->getSalesChannelContext())) {
  46.             $paymentMethods $this->removePaymentMethod($paymentMethodsPayonePayolutionInvoicing::UUID);
  47.         }
  48.         $page->setPaymentMethods($paymentMethods);
  49.     }
  50.     private function removePaymentMethod(PaymentMethodCollection $paymentMethodsstring $paymentMethodId): PaymentMethodCollection
  51.     {
  52.         return $paymentMethods->filter(
  53.             static function (PaymentMethodEntity $paymentMethod) use ($paymentMethodId) {
  54.                 return $paymentMethod->getId() !== $paymentMethodId;
  55.             }
  56.         );
  57.     }
  58.     private function customerHasCompanyAddress(SalesChannelContext $context): bool
  59.     {
  60.         $customer $context->getCustomer();
  61.         if (null === $customer) {
  62.             return false;
  63.         }
  64.         $billingAddress $customer->getActiveBillingAddress();
  65.         if (null === $billingAddress) {
  66.             return false;
  67.         }
  68.         return !empty($billingAddress->getCompany());
  69.     }
  70.     private function companyDataHandlingIsDisabled(SalesChannelContext $context): bool
  71.     {
  72.         $configuration $this->configReader->read($context->getSalesChannel()->getId());
  73.         return !((bool) $configuration->get('payolutionInvoicingTransferCompanyData'));
  74.     }
  75. }