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

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\Util\Lifecycle\Method\PaymentMethodDataRegistry;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. /**
  15.  * @internal
  16.  */
  17. class CheckoutSubscriber implements EventSubscriberInterface
  18. {
  19.     private PaymentMethodDataRegistry $methodDataRegistry;
  20.     private LoggerInterface $logger;
  21.     public function __construct(
  22.         PaymentMethodDataRegistry $methodDataRegistry,
  23.         LoggerInterface $logger
  24.     ) {
  25.         $this->methodDataRegistry $methodDataRegistry;
  26.         $this->logger $logger;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             AccountEditOrderPageLoadedEvent::class => ['onEditOrderPageLoaded'1],
  32.         ];
  33.     }
  34.     public function onEditOrderPageLoaded(AccountEditOrderPageLoadedEvent $event): void
  35.     {
  36.         $order $event->getPage()->getOrder();
  37.         if ($order->getPrice()->getTotalPrice() === 0.0) {
  38.             $this->logger->info('PayPal is removed from the available payment methods, because the amount of the order is zero');
  39.             $this->removePayPalPaymentMethodsFromPage($event);
  40.         }
  41.     }
  42.     /**
  43.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  44.      */
  45.     private function removePayPalPaymentMethodsFromPage(PageLoadedEvent $event): void
  46.     {
  47.         foreach ($event->getPage()->getPaymentMethods() as $paymentMethod) {
  48.             if ($this->methodDataRegistry->isPayPalPaymentMethod($paymentMethod)) {
  49.                 $event->getPage()->getPaymentMethods()->remove($paymentMethod->getId());
  50.             }
  51.         }
  52.     }
  53. }