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 Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  9. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  10. use Swag\PayPal\Setting\Service\SettingsServiceInterface;
  11. use Swag\PayPal\Util\PaymentMethodUtil;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class CheckoutSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var SettingsServiceInterface
  17.      */
  18.     private $settingsService;
  19.     /**
  20.      * @var PaymentMethodUtil
  21.      */
  22.     private $paymentMethodUtil;
  23.     public function __construct(SettingsServiceInterface $settingsServicePaymentMethodUtil $paymentMethodUtil)
  24.     {
  25.         $this->settingsService $settingsService;
  26.         $this->paymentMethodUtil $paymentMethodUtil;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             CheckoutConfirmPageLoadedEvent::class => ['onConfirmPageLoaded'1],
  32.         ];
  33.     }
  34.     public function onConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  35.     {
  36.         try {
  37.             $this->settingsService->getSettings($event->getSalesChannelContext()->getSalesChannel()->getId());
  38.         } catch (PayPalSettingsInvalidException $e) {
  39.             $this->removePayPalPaymentMethodFromConfirmPage($event);
  40.         }
  41.     }
  42.     private function removePayPalPaymentMethodFromConfirmPage(CheckoutConfirmPageLoadedEvent $event): void
  43.     {
  44.         $paymentMethodCollection $event->getPage()->getPaymentMethods();
  45.         $paymentMethodCollection->remove($this->paymentMethodUtil->getPayPalPaymentMethodId($event->getContext()));
  46.     }
  47. }