custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmEpsEventListener.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\PaymentMethod\PayoneEps;
  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 EPS payment method for customers
  14.  * which have a billing address that is outside from AT.
  15.  */
  16. class CheckoutConfirmEpsEventListener implements EventSubscriberInterface
  17. {
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             CheckoutConfirmPageLoadedEvent::class      => 'hideEpsForNonAtCustomers',
  22.             AccountPaymentMethodPageLoadedEvent::class => 'hideEpsForNonAtCustomers',
  23.             AccountEditOrderPageLoadedEvent::class     => 'hideEpsForNonAtCustomers',
  24.         ];
  25.     }
  26.     /**
  27.      * @param AccountEditOrderPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  28.      */
  29.     public function hideEpsForNonAtCustomers($event): void
  30.     {
  31.         $paymentMethods $event->getPage()->getPaymentMethods();
  32.         if (
  33.             $this->isEuroCurrency($event->getSalesChannelContext()) &&
  34.             $this->isAtCustomer($event->getSalesChannelContext())
  35.         ) {
  36.             return;
  37.         }
  38.         $paymentMethods $this->removePaymentMethod($paymentMethodsPayoneEps::UUID);
  39.         $event->getPage()->setPaymentMethods($paymentMethods);
  40.     }
  41.     /**
  42.      * Returns whether or not the currency is EUR.
  43.      */
  44.     private function isEuroCurrency(SalesChannelContext $context): bool
  45.     {
  46.         return $context->getCurrency()->getIsoCode() === 'EUR';
  47.     }
  48.     /**
  49.      * Returns whether or not the customer's billing address
  50.      * is inside AT or not. Or false if no customer or billing
  51.      * address is given.
  52.      */
  53.     private function isAtCustomer(SalesChannelContext $context): bool
  54.     {
  55.         $customer $context->getCustomer();
  56.         if (null === $customer) {
  57.             return false;
  58.         }
  59.         $billingAddress $customer->getActiveBillingAddress();
  60.         if (null === $billingAddress || null === $billingAddress->getCountry()) {
  61.             return false;
  62.         }
  63.         return $billingAddress->getCountry()->getIso() === 'AT';
  64.     }
  65.     private function removePaymentMethod(PaymentMethodCollection $paymentMethodsstring $paymentMethodId): PaymentMethodCollection
  66.     {
  67.         return $paymentMethods->filter(
  68.             static function (PaymentMethodEntity $paymentMethod) use ($paymentMethodId) {
  69.                 return $paymentMethod->getId() !== $paymentMethodId;
  70.             }
  71.         );
  72.     }
  73. }