custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmPrzelewy24EventListener.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\PaymentHandler\PayonePrzelewy24PaymentHandler;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  6. use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressEntity;
  7. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  8. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  9. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  10. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Shopware\Storefront\Page\PageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class CheckoutConfirmPrzelewy24EventListener implements EventSubscriberInterface
  15. {
  16.     private const ALLOWED_COUNTRIES = ['PL'];
  17.     private const ALLOWED_CURRENCIES = ['PLN'];
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             CheckoutConfirmPageLoadedEvent::class => 'hidePaymentMethod',
  22.             AccountPaymentMethodPageLoadedEvent::class => 'hidePaymentMethod',
  23.             AccountEditOrderPageLoadedEvent::class => 'hidePaymentMethod',
  24.         ];
  25.     }
  26.     /**
  27.      * @param AccountEditOrderPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  28.      */
  29.     public function hidePaymentMethod(PageLoadedEvent $event): void
  30.     {
  31.         $context $event->getSalesChannelContext();
  32.         $customer $context->getCustomer();
  33.         $page $event->getPage();
  34.         $billingAddress $customer $customer->getActiveBillingAddress() : null;
  35.         if ($this->isCurrencyAllowed($context->getCurrency()->getIsoCode())
  36.             && (!$billingAddress || $this->isAddressAllowed($billingAddress))
  37.         ) {
  38.             return;
  39.         }
  40.         $paymentMethods $this->removePaymentMethods($page->getPaymentMethods(), [PayonePrzelewy24PaymentHandler::class]);
  41.         $event->getPage()->setPaymentMethods($paymentMethods);
  42.     }
  43.     /**
  44.      * @param CustomerAddressEntity|OrderAddressEntity $address
  45.      */
  46.     private function isAddressAllowed($address): bool
  47.     {
  48.         $country $address->getCountry();
  49.         return $country && \in_array($country->getIso(), self::ALLOWED_COUNTRIEStrue);
  50.     }
  51.     private function isCurrencyAllowed(string $currencyCode): bool
  52.     {
  53.         return \in_array($currencyCodeself::ALLOWED_CURRENCIEStrue);
  54.     }
  55.     private function removePaymentMethods(PaymentMethodCollection $paymentMethods, array $paymentHandler): PaymentMethodCollection
  56.     {
  57.         return $paymentMethods->filter(
  58.             static function (PaymentMethodEntity $paymentMethod) use ($paymentHandler) {
  59.                 return !\in_array($paymentMethod->getHandlerIdentifier(), $paymentHandlertrue);
  60.             }
  61.         );
  62.     }
  63. }