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

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.     /** @param AccountEditOrderPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event */
  23.     public function hideInternalPaymentMethods(PageLoadedEvent $event): void
  24.     {
  25.         $page $event->getPage();
  26.         if ($event instanceof AccountEditOrderPageLoadedEvent) {
  27.             return;
  28.         }
  29.         $activePaymentMethod $event->getSalesChannelContext()->getPaymentMethod();
  30.         $page->setPaymentMethods(
  31.             $this->filterPaymentMethods(
  32.                 $page->getPaymentMethods(),
  33.                 $activePaymentMethod
  34.             )
  35.         );
  36.     }
  37.     private function filterPaymentMethods(PaymentMethodCollection $paymentMethodsPaymentMethodEntity $activePaymentMethod): PaymentMethodCollection
  38.     {
  39.         $internalPaymentMethods = [
  40.             PayonePaypalExpress::UUID,
  41.         ];
  42.         return $paymentMethods->filter(
  43.             static function (PaymentMethodEntity $paymentMethod) use ($internalPaymentMethods$activePaymentMethod) {
  44.                 if ($activePaymentMethod->getId() === $paymentMethod->getId()) {
  45.                     return true;
  46.                 }
  47.                 return !in_array($paymentMethod->getId(), $internalPaymentMethodstrue);
  48.             }
  49.         );
  50.     }
  51. }