custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmPaypalExpressEventListener.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\PaymentMethod\PayonePaypalExpress;
  5. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  6. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  7. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  8. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Shopware\Storefront\Page\PageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CheckoutConfirmPaypalExpressEventListener implements EventSubscriberInterface
  13. {
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             CheckoutConfirmPageLoadedEvent::class => 'hideInternalPaymentMethods',
  18.             AccountPaymentMethodPageLoadedEvent::class => 'hideInternalPaymentMethods',
  19.             AccountEditOrderPageLoadedEvent::class => 'hideInternalPaymentMethods',
  20.         ];
  21.     }
  22.     /**
  23.      * @param AccountEditOrderPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  24.      */
  25.     public function hideInternalPaymentMethods(PageLoadedEvent $event): void
  26.     {
  27.         $page $event->getPage();
  28.         if ($event instanceof AccountEditOrderPageLoadedEvent) {
  29.             return;
  30.         }
  31.         $activePaymentMethod $event->getSalesChannelContext()->getPaymentMethod();
  32.         $page->setPaymentMethods(
  33.             $this->filterPaymentMethods(
  34.                 $page->getPaymentMethods(),
  35.                 $activePaymentMethod
  36.             )
  37.         );
  38.     }
  39.     private function filterPaymentMethods(PaymentMethodCollection $paymentMethodsPaymentMethodEntity $activePaymentMethod): PaymentMethodCollection
  40.     {
  41.         $internalPaymentMethods = [
  42.             PayonePaypalExpress::UUID,
  43.         ];
  44.         return $paymentMethods->filter(
  45.             static function (PaymentMethodEntity $paymentMethod) use ($internalPaymentMethods$activePaymentMethod) {
  46.                 if ($activePaymentMethod->getId() === $paymentMethod->getId()) {
  47.                     return true;
  48.                 }
  49.                 return !\in_array($paymentMethod->getId(), $internalPaymentMethodstrue);
  50.             }
  51.         );
  52.     }
  53. }