custom/plugins/SwagPayPal/src/Checkout/ExpressCheckout/ExpressCheckoutSubscriber.php line 90

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\ExpressCheckout;
  8. use Shopware\Core\Content\Cms\CmsPageCollection;
  9. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  14. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  15. use Shopware\Storefront\Page\PageLoadedEvent;
  16. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  17. use Swag\CmsExtensions\Storefront\Pagelet\Quickview\QuickviewPageletLoadedEvent;
  18. use Swag\PayPal\Checkout\ExpressCheckout\Service\PayPalExpressCheckoutDataService;
  19. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  20. use Swag\PayPal\Setting\Service\SettingsServiceInterface;
  21. use Swag\PayPal\Setting\SwagPayPalSettingStruct;
  22. use Swag\PayPal\Util\PaymentMethodUtil;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. class ExpressCheckoutSubscriber implements EventSubscriberInterface
  25. {
  26.     public const PAYPAL_EXPRESS_CHECKOUT_BUTTON_DATA_EXTENSION_ID 'payPalEcsButtonData';
  27.     /**
  28.      * @var PayPalExpressCheckoutDataService
  29.      */
  30.     private $expressCheckoutDataService;
  31.     /**
  32.      * @var SettingsServiceInterface
  33.      */
  34.     private $settingsService;
  35.     /**
  36.      * @var PaymentMethodUtil
  37.      */
  38.     private $paymentMethodUtil;
  39.     public function __construct(
  40.         PayPalExpressCheckoutDataService $service,
  41.         SettingsServiceInterface $settingsService,
  42.         PaymentMethodUtil $paymentMethodUtil
  43.     ) {
  44.         $this->expressCheckoutDataService $service;
  45.         $this->settingsService $settingsService;
  46.         $this->paymentMethodUtil $paymentMethodUtil;
  47.     }
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             CheckoutCartPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  52.             CheckoutRegisterPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  53.             NavigationPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  54.             OffcanvasCartPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  55.             ProductPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  56.             CmsPageLoadedEvent::class => 'addExpressCheckoutDataToCmsPage',
  57.             QuickviewPageletLoadedEvent::class => 'addExpressCheckoutDataToPagelet',
  58.         ];
  59.     }
  60.     public function addExpressCheckoutDataToPage(PageLoadedEvent $event): void
  61.     {
  62.         $salesChannelContext $event->getSalesChannelContext();
  63.         $eventName = \get_class($event);
  64.         $addProductToCart $event instanceof ProductPageLoadedEvent || $event instanceof NavigationPageLoadedEvent;
  65.         $expressCheckoutButtonData $this->getExpressCheckoutButtonData($salesChannelContext$eventName$addProductToCart);
  66.         if ($expressCheckoutButtonData === null) {
  67.             return;
  68.         }
  69.         $event->getPage()->addExtension(
  70.             self::PAYPAL_EXPRESS_CHECKOUT_BUTTON_DATA_EXTENSION_ID,
  71.             $expressCheckoutButtonData
  72.         );
  73.     }
  74.     public function addExpressCheckoutDataToCmsPage(CmsPageLoadedEvent $event): void
  75.     {
  76.         $salesChannelContext $event->getSalesChannelContext();
  77.         $expressCheckoutButtonData $this->getExpressCheckoutButtonData($salesChannelContext, \get_class($event), true);
  78.         if ($expressCheckoutButtonData === null) {
  79.             return;
  80.         }
  81.         /** @var CmsPageCollection $pages */
  82.         $pages $event->getResult();
  83.         $cmsPage $pages->first();
  84.         if ($cmsPage === null) {
  85.             return;
  86.         }
  87.         $cmsPage->addExtension(
  88.             self::PAYPAL_EXPRESS_CHECKOUT_BUTTON_DATA_EXTENSION_ID,
  89.             $expressCheckoutButtonData
  90.         );
  91.     }
  92.     public function addExpressCheckoutDataToPagelet(QuickviewPageletLoadedEvent $event): void
  93.     {
  94.         $salesChannelContext $event->getSalesChannelContext();
  95.         $expressCheckoutButtonData $this->getExpressCheckoutButtonData($salesChannelContext, \get_class($event), true);
  96.         if ($expressCheckoutButtonData === null) {
  97.             return;
  98.         }
  99.         $quickviewPagelet $event->getPagelet();
  100.         $quickviewPagelet->addExtension(
  101.             self::PAYPAL_EXPRESS_CHECKOUT_BUTTON_DATA_EXTENSION_ID,
  102.             $expressCheckoutButtonData
  103.         );
  104.     }
  105.     private function getExpressCheckoutButtonData(
  106.         SalesChannelContext $salesChannelContext,
  107.         string $eventName,
  108.         bool $addProductToCart false
  109.     ): ?ExpressCheckoutButtonData {
  110.         $settings $this->checkSettings($salesChannelContext$eventName);
  111.         if ($settings === null) {
  112.             return null;
  113.         }
  114.         return $this->expressCheckoutDataService->getExpressCheckoutButtonData(
  115.             $salesChannelContext,
  116.             $settings,
  117.             $addProductToCart
  118.         );
  119.     }
  120.     private function checkSettings(SalesChannelContext $contextstring $eventName): ?SwagPayPalSettingStruct
  121.     {
  122.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($context) === false) {
  123.             return null;
  124.         }
  125.         try {
  126.             $settings $this->settingsService->getSettings($context->getSalesChannel()->getId());
  127.         } catch (PayPalSettingsInvalidException $e) {
  128.             return null;
  129.         }
  130.         if ($this->expressOptionForEventEnabled($settings$eventName) === false) {
  131.             return null;
  132.         }
  133.         return $settings;
  134.     }
  135.     private function expressOptionForEventEnabled(SwagPayPalSettingStruct $settingsstring $eventName): bool
  136.     {
  137.         switch ($eventName) {
  138.             case ProductPageLoadedEvent::class:
  139.             case QuickviewPageletLoadedEvent::class:
  140.                 return $settings->getEcsDetailEnabled();
  141.             case OffcanvasCartPageLoadedEvent::class:
  142.                 return $settings->getEcsOffCanvasEnabled();
  143.             case CheckoutRegisterPageLoadedEvent::class:
  144.                 return $settings->getEcsLoginEnabled();
  145.             case CheckoutCartPageLoadedEvent::class:
  146.                 return $settings->getEcsCartEnabled();
  147.             case NavigationPageLoadedEvent::class:
  148.             case CmsPageLoadedEvent::class:
  149.                 return $settings->getEcsListingEnabled();
  150.             default:
  151.                 return false;
  152.         }
  153.     }
  154. }