custom/plugins/SwagPayPal/src/Checkout/CheckoutSubscriber.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal\Checkout;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  11. use Shopware\Storefront\Page\PageLoadedEvent;
  12. use Swag\PayPal\Checkout\Cart\Service\CartPriceService;
  13. use Swag\PayPal\Util\Lifecycle\Method\PaymentMethodDataRegistry;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class CheckoutSubscriber implements EventSubscriberInterface
  16. {
  17.     private PaymentMethodDataRegistry $methodDataRegistry;
  18.     private LoggerInterface $logger;
  19.     private CartPriceService $cartPriceService;
  20.     public function __construct(
  21.         PaymentMethodDataRegistry $methodDataRegistry,
  22.         LoggerInterface $logger,
  23.         CartPriceService $cartPriceService
  24.     ) {
  25.         $this->methodDataRegistry $methodDataRegistry;
  26.         $this->logger $logger;
  27.         $this->cartPriceService $cartPriceService;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             //CheckoutConfirmPageLoadedEvent::class => ['onConfirmPageLoaded', 1],
  33.             AccountEditOrderPageLoadedEvent::class => ['onEditOrderPageLoaded'1],
  34.         ];
  35.     }
  36.     /**
  37.      * @deprecated tag:v6.0.0 - will be removed, functionality has been moved to Swag\PayPal\Checkout\SalesChannel\FilteredPaymentMethodRoute
  38.      */
  39.     public function onConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  40.     {
  41.         if (!$this->cartPriceService->isZeroValueCart($event->getPage()->getCart())) {
  42.             return;
  43.         }
  44.         $this->removePayPalPaymentMethodsFromPage($event);
  45.     }
  46.     public function onEditOrderPageLoaded(AccountEditOrderPageLoadedEvent $event): void
  47.     {
  48.         $order $event->getPage()->getOrder();
  49.         if ($order->getPrice()->getTotalPrice() === 0.0) {
  50.             $this->logger->info('PayPal is removed from the available payment methods, because the amount of the order is zero');
  51.             $this->removePayPalPaymentMethodsFromPage($event);
  52.         }
  53.     }
  54.     /**
  55.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  56.      */
  57.     private function removePayPalPaymentMethodsFromPage(PageLoadedEvent $event): void
  58.     {
  59.         foreach ($event->getPage()->getPaymentMethods() as $paymentMethod) {
  60.             if ($this->methodDataRegistry->isPayPalPaymentMethod($paymentMethod)) {
  61.                 $event->getPage()->getPaymentMethods()->remove($paymentMethod->getId());
  62.             }
  63.         }
  64.     }
  65. }