custom/plugins/SwagPayPal/src/Installment/Banner/InstallmentBannerSubscriber.php line 76

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\Installment\Banner;
  8. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPage;
  9. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPage;
  13. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPage;
  15. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  16. use Shopware\Storefront\Page\PageLoadedEvent;
  17. use Shopware\Storefront\Page\Product\ProductPage;
  18. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  19. use Shopware\Storefront\Pagelet\Footer\FooterPagelet;
  20. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  21. use Shopware\Storefront\Pagelet\PageletLoadedEvent;
  22. use Swag\CmsExtensions\Storefront\Pagelet\Quickview\QuickviewPagelet;
  23. use Swag\CmsExtensions\Storefront\Pagelet\Quickview\QuickviewPageletLoadedEvent;
  24. use Swag\PayPal\Installment\Banner\Service\BannerDataService;
  25. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  26. use Swag\PayPal\Setting\Service\SettingsServiceInterface;
  27. use Swag\PayPal\Util\PaymentMethodUtil;
  28. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  29. class InstallmentBannerSubscriber implements EventSubscriberInterface
  30. {
  31.     public const PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID 'payPalInstallmentBannerData';
  32.     public const PAYPAL_INSTALLMENT_BANNER_DATA_CART_PAGE_EXTENSION_ID 'payPalInstallmentBannerDataCheckoutCart';
  33.     /**
  34.      * @var SettingsServiceInterface
  35.      */
  36.     private $settingsService;
  37.     /**
  38.      * @var PaymentMethodUtil
  39.      */
  40.     private $paymentMethodUtil;
  41.     /**
  42.      * @var BannerDataService
  43.      */
  44.     private $bannerDataService;
  45.     public function __construct(
  46.         SettingsServiceInterface $settingsService,
  47.         PaymentMethodUtil $paymentMethodUtil,
  48.         BannerDataService $bannerDataService
  49.     ) {
  50.         $this->settingsService $settingsService;
  51.         $this->paymentMethodUtil $paymentMethodUtil;
  52.         $this->bannerDataService $bannerDataService;
  53.     }
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         return [
  57.             CheckoutCartPageLoadedEvent::class => 'addInstallmentBanner',
  58.             CheckoutConfirmPageLoadedEvent::class => 'addInstallmentBanner',
  59.             CheckoutRegisterPageLoadedEvent::class => 'addInstallmentBanner',
  60.             OffcanvasCartPageLoadedEvent::class => 'addInstallmentBanner',
  61.             ProductPageLoadedEvent::class => 'addInstallmentBanner',
  62.             FooterPageletLoadedEvent::class => 'addInstallmentBannerPagelet',
  63.             QuickviewPageletLoadedEvent::class => 'addInstallmentBannerPagelet',
  64.         ];
  65.     }
  66.     public function addInstallmentBanner(PageLoadedEvent $pageLoadedEvent): void
  67.     {
  68.         $salesChannelContext $pageLoadedEvent->getSalesChannelContext();
  69.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext) === false) {
  70.             return;
  71.         }
  72.         try {
  73.             $settings $this->settingsService->getSettings($salesChannelContext->getSalesChannel()->getId());
  74.         } catch (PayPalSettingsInvalidException $e) {
  75.             return;
  76.         }
  77.         if ($settings->getInstallmentBannerEnabled() === false) {
  78.             return;
  79.         }
  80.         /** @var CheckoutCartPage|CheckoutConfirmPage|CheckoutRegisterPage|OffcanvasCartPage|ProductPage $page */
  81.         $page $pageLoadedEvent->getPage();
  82.         $bannerData $this->bannerDataService->getInstallmentBannerData(
  83.             $page,
  84.             $salesChannelContext,
  85.             $settings
  86.         );
  87.         if ($page instanceof CheckoutCartPage) {
  88.             $productTableBannerData = new BannerData(
  89.                 $bannerData->getPaymentMethodId(),
  90.                 $bannerData->getClientId(),
  91.                 $bannerData->getAmount(),
  92.                 $bannerData->getCurrency(),
  93.                 'flex',
  94.                 'grey',
  95.                 '20x1'
  96.             );
  97.             $page->addExtension(self::PAYPAL_INSTALLMENT_BANNER_DATA_CART_PAGE_EXTENSION_ID$productTableBannerData);
  98.         }
  99.         $page->addExtension(
  100.             self::PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID,
  101.             $bannerData
  102.         );
  103.     }
  104.     public function addInstallmentBannerPagelet(PageletLoadedEvent $pageletLoadedEvent): void
  105.     {
  106.         $salesChannelContext $pageletLoadedEvent->getSalesChannelContext();
  107.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext) === false) {
  108.             return;
  109.         }
  110.         try {
  111.             $settings $this->settingsService->getSettings($salesChannelContext->getSalesChannel()->getId());
  112.         } catch (PayPalSettingsInvalidException $e) {
  113.             return;
  114.         }
  115.         if ($settings->getInstallmentBannerEnabled() === false) {
  116.             return;
  117.         }
  118.         /** @var FooterPagelet|QuickviewPagelet $pagelet */
  119.         $pagelet $pageletLoadedEvent->getPagelet();
  120.         $bannerData $this->bannerDataService->getInstallmentBannerData(
  121.             $pagelet,
  122.             $salesChannelContext,
  123.             $settings
  124.         );
  125.         $pagelet->addExtension(
  126.             self::PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID,
  127.             $bannerData
  128.         );
  129.     }
  130. }