custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmTrustlyEventListener.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\PaymentMethod\PayoneTrustly;
  5. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  6. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  9. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. /**
  13.  * This event listener removes the Trustly payment method for customers
  14.  * which have a billing address that is outside from allowed country list.
  15.  */
  16. class CheckoutConfirmTrustlyEventListener implements EventSubscriberInterface
  17. {
  18.     protected const ALLOWED_BANK_COUNTRIES = [
  19.         'DE',
  20.         'DK',
  21.         'EE',
  22.         'ES',
  23.         'FI',
  24.         'IT',
  25.         'MT',
  26.         'NL',
  27.         'NO',
  28.         'PL',
  29.         'SE',
  30.     ];
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             CheckoutConfirmPageLoadedEvent::class      => 'hideTrustlyForDisallowedCountryCustomers',
  35.             AccountPaymentMethodPageLoadedEvent::class => 'hideTrustlyForDisallowedCountryCustomers',
  36.             AccountEditOrderPageLoadedEvent::class     => 'hideTrustlyForDisallowedCountryCustomers',
  37.         ];
  38.     }
  39.     /**
  40.      * @param AccountEditOrderPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  41.      */
  42.     public function hideTrustlyForDisallowedCountryCustomers($event): void
  43.     {
  44.         $paymentMethods $event->getPage()->getPaymentMethods();
  45.         if (
  46.             $this->isEuroCurrency($event->getSalesChannelContext()) &&
  47.             $this->isAllowedCountryCustomer($event->getSalesChannelContext())
  48.         ) {
  49.             return;
  50.         }
  51.         $paymentMethods $this->removePaymentMethod($paymentMethodsPayoneTrustly::UUID);
  52.         $event->getPage()->setPaymentMethods($paymentMethods);
  53.     }
  54.     /**
  55.      * Returns whether or not the currency is EUR.
  56.      */
  57.     private function isEuroCurrency(SalesChannelContext $context): bool
  58.     {
  59.         return $context->getCurrency()->getIsoCode() === 'EUR';
  60.     }
  61.     /**
  62.      * Returns whether or not the customer's billing address
  63.      * is inside allowed countries or not.
  64.      */
  65.     private function isAllowedCountryCustomer(SalesChannelContext $context): bool
  66.     {
  67.         $customer $context->getCustomer();
  68.         if (null === $customer) {
  69.             return false;
  70.         }
  71.         $billingAddress $customer->getActiveBillingAddress();
  72.         if (null === $billingAddress || null === $billingAddress->getCountry()) {
  73.             return false;
  74.         }
  75.         return in_array($billingAddress->getCountry()->getIso(), self::ALLOWED_BANK_COUNTRIEStrue);
  76.     }
  77.     private function removePaymentMethod(PaymentMethodCollection $paymentMethodsstring $paymentMethodId): PaymentMethodCollection
  78.     {
  79.         return $paymentMethods->filter(
  80.             static function (PaymentMethodEntity $paymentMethod) use ($paymentMethodId) {
  81.                 return $paymentMethod->getId() !== $paymentMethodId;
  82.             }
  83.         );
  84.     }
  85. }