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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\PaymentHandler\AbstractKlarnaPaymentHandler;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  6. use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressEntity;
  7. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
  8. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  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 Swag\CustomizedProducts\Core\Checkout\CustomizedProductsCartDataCollector;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class CheckoutConfirmKlarnaRestrictPaymentEventListener implements EventSubscriberInterface
  17. {
  18.     private const ALLOWED_COUNTRIES = ['AT''DK''FI''DE''NL''NO''SE''CH'];
  19.     private const ALLOWED_B2B_COUNTRIES = ['FI''DE''NO''SE'];
  20.     private const ALLOWED_CURRENCIES = ['EUR''DKK''NOK''SEKCHF'];
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             CheckoutConfirmPageLoadedEvent::class => 'hidePaymentMethod',
  25.             AccountPaymentMethodPageLoadedEvent::class => 'hidePaymentMethod',
  26.             AccountEditOrderPageLoadedEvent::class => 'hidePaymentMethod',
  27.         ];
  28.     }
  29.     /**
  30.      * @param AccountEditOrderPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  31.      */
  32.     public function hidePaymentMethod(PageLoadedEvent $event): void
  33.     {
  34.         $context $event->getSalesChannelContext();
  35.         $customer $context->getCustomer();
  36.         $billingAddress $customer $customer->getActiveBillingAddress() : null;
  37.         if ($this->isCurrencyAllowed($context->getCurrency()->getIsoCode())
  38.             && (!$billingAddress || $this->isAddressAllowed($billingAddress))
  39.             && !$this->hasCustomProducts($event)
  40.         ) {
  41.             return;
  42.         }
  43.         $event->getPage()->setPaymentMethods($this->removeKlarnaMethods($event->getPage()->getPaymentMethods()));
  44.     }
  45.     /**
  46.      * @param CustomerAddressEntity|OrderAddressEntity $address
  47.      */
  48.     private function isAddressAllowed($address): bool
  49.     {
  50.         $country $address->getCountry();
  51.         return $country && \in_array($country->getIso(), self::ALLOWED_COUNTRIEStrue)
  52.             && (!$address->getCompany() || \in_array($country->getIso(), self::ALLOWED_B2B_COUNTRIEStrue));
  53.     }
  54.     private function isCurrencyAllowed(string $currencyCode): bool
  55.     {
  56.         return \in_array($currencyCodeself::ALLOWED_CURRENCIEStrue);
  57.     }
  58.     /**
  59.      * @param AccountEditOrderPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  60.      */
  61.     private function hasCustomProducts(PageLoadedEvent $event): bool
  62.     {
  63.         // PAYOSWXP-50: we also added this check cause the SwagCustomProducts extension does have a few issues, which
  64.         // makes it very expensive to fix them in the Payone extension. So we exclude the payment method
  65.         // if there are any "custom product" within the order/cart
  66.         if ($event instanceof CheckoutConfirmPageLoadedEvent) {
  67.             foreach ($event->getPage()->getCart()->getLineItems() as $item) {
  68.                 if (class_exists('Swag\CustomizedProducts\Core\Checkout\CustomizedProductsCartDataCollector')
  69.                     && $item->getType() === CustomizedProductsCartDataCollector::CUSTOMIZED_PRODUCTS_TEMPLATE_LINE_ITEM_TYPE) {
  70.                     return true;
  71.                 }
  72.             }
  73.         } elseif ($event instanceof AccountEditOrderPageLoadedEvent) {
  74.             foreach ($event->getPage()->getOrder()->getLineItems() ?? new OrderLineItemCollection() as $item) {
  75.                 if (class_exists('Swag\CustomizedProducts\Core\Checkout\CustomizedProductsCartDataCollector')
  76.                     && $item->getType() === CustomizedProductsCartDataCollector::CUSTOMIZED_PRODUCTS_TEMPLATE_LINE_ITEM_TYPE) {
  77.                     return true;
  78.                 }
  79.             }
  80.         }
  81.         return false;
  82.     }
  83.     private function removeKlarnaMethods(PaymentMethodCollection $paymentMethodCollection): PaymentMethodCollection
  84.     {
  85.         return $paymentMethodCollection->filter(static function (PaymentMethodEntity $paymentMethod) {
  86.             return !is_subclass_of($paymentMethod->getHandlerIdentifier(), AbstractKlarnaPaymentHandler::class);
  87.         });
  88.     }
  89. }