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

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