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

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\Core\Checkout\Payment\Cart\Error\PaymentMethodBlockedError;
  10. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Shopware\Storefront\Page\PageLoadedEvent;
  13. use Swag\PayPal\Checkout\Cart\Service\CartPriceService;
  14. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  15. use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
  16. use Swag\PayPal\Util\PaymentMethodUtil;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class CheckoutSubscriber implements EventSubscriberInterface
  19. {
  20.     private SettingsValidationServiceInterface $settingsValidationService;
  21.     private PaymentMethodUtil $paymentMethodUtil;
  22.     private LoggerInterface $logger;
  23.     private CartPriceService $cartPriceService;
  24.     public function __construct(
  25.         SettingsValidationServiceInterface $settingsValidationService,
  26.         PaymentMethodUtil $paymentMethodUtil,
  27.         LoggerInterface $logger,
  28.         CartPriceService $cartPriceService
  29.     ) {
  30.         $this->settingsValidationService $settingsValidationService;
  31.         $this->paymentMethodUtil $paymentMethodUtil;
  32.         $this->logger $logger;
  33.         $this->cartPriceService $cartPriceService;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             CheckoutConfirmPageLoadedEvent::class => ['onConfirmPageLoaded'1],
  39.             AccountEditOrderPageLoadedEvent::class => ['onEditOrderPageLoaded'1],
  40.         ];
  41.     }
  42.     public function onConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  43.     {
  44.         $this->checkForMissingSettings($event);
  45.         $this->checkForCartValue($event);
  46.     }
  47.     public function onEditOrderPageLoaded(AccountEditOrderPageLoadedEvent $event): void
  48.     {
  49.         $this->checkForMissingSettings($event);
  50.         $this->checkForOrderValue($event);
  51.     }
  52.     /**
  53.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  54.      */
  55.     private function checkForMissingSettings(PageLoadedEvent $event): void
  56.     {
  57.         try {
  58.             $this->settingsValidationService->validate($event->getSalesChannelContext()->getSalesChannel()->getId());
  59.         } catch (PayPalSettingsInvalidException $e) {
  60.             $this->logger->info('PayPal is removed from the available payment methods: {message}', ['message' => $e->getMessage()]);
  61.             $this->removePayPalPaymentMethodFromConfirmPage($event);
  62.         }
  63.     }
  64.     private function checkForCartValue(CheckoutConfirmPageLoadedEvent $event): void
  65.     {
  66.         if ($this->cartPriceService->isZeroValueCart($event->getPage()->getCart())) {
  67.             $this->logger->info('PayPal is removed from the available payment methods, because the amount of the cart is zero');
  68.             $this->removePayPalPaymentMethodFromConfirmPage($event);
  69.         }
  70.     }
  71.     private function checkForOrderValue(AccountEditOrderPageLoadedEvent $event): void
  72.     {
  73.         $order $event->getPage()->getOrder();
  74.         if ($order->getPrice()->getTotalPrice() === 0.0) {
  75.             $this->logger->info('PayPal is removed from the available payment methods, because the amount of the order is zero');
  76.             $this->removePayPalPaymentMethodFromConfirmPage($event);
  77.         }
  78.     }
  79.     /**
  80.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  81.      */
  82.     private function removePayPalPaymentMethodFromConfirmPage(PageLoadedEvent $event): void
  83.     {
  84.         $paymentMethods $event->getPage()->getPaymentMethods();
  85.         $payPalPaymentMethodId $this->paymentMethodUtil->getPayPalPaymentMethodId($event->getContext());
  86.         if ($payPalPaymentMethodId === null) {
  87.             return;
  88.         }
  89.         if ($event->getSalesChannelContext()->getPaymentMethod()->getId() === $payPalPaymentMethodId) {
  90.             $paymentMethod $paymentMethods->get($payPalPaymentMethodId);
  91.             if ($paymentMethod !== null && $event instanceof CheckoutConfirmPageLoadedEvent) {
  92.                 $event->getPage()->getCart()->addErrors(new PaymentMethodBlockedError((string) $paymentMethod->getTranslation('name')));
  93.             }
  94.         }
  95.         $paymentMethods->remove($payPalPaymentMethodId);
  96.     }
  97. }