vendor/store.shopware.com/swagpaypal/src/Installment/Banner/InstallmentBannerSubscriber.php line 137

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. class InstallmentBannerSubscriber implements EventSubscriberInterface
  34. {
  35.     public const PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID 'payPalInstallmentBannerData';
  36.     public const PAYPAL_INSTALLMENT_BANNER_DATA_CART_PAGE_EXTENSION_ID 'payPalInstallmentBannerDataCheckoutCart';
  37.     private SettingsValidationServiceInterface $settingsValidationService;
  38.     private SystemConfigService $systemConfigService;
  39.     private PaymentMethodUtil $paymentMethodUtil;
  40.     private BannerDataServiceInterface $bannerDataService;
  41.     private LoggerInterface $logger;
  42.     private ExcludedProductValidator $excludedProductValidator;
  43.     public function __construct(
  44.         SettingsValidationServiceInterface $settingsValidationService,
  45.         SystemConfigService $systemConfigService,
  46.         PaymentMethodUtil $paymentMethodUtil,
  47.         BannerDataServiceInterface $bannerDataService,
  48.         ExcludedProductValidator $excludedProductValidator,
  49.         LoggerInterface $logger
  50.     ) {
  51.         $this->settingsValidationService $settingsValidationService;
  52.         $this->systemConfigService $systemConfigService;
  53.         $this->paymentMethodUtil $paymentMethodUtil;
  54.         $this->bannerDataService $bannerDataService;
  55.         $this->excludedProductValidator $excludedProductValidator;
  56.         $this->logger $logger;
  57.     }
  58.     public static function getSubscribedEvents(): array
  59.     {
  60.         return [
  61.             CheckoutCartPageLoadedEvent::class => 'addInstallmentBanner',
  62.             CheckoutConfirmPageLoadedEvent::class => 'addInstallmentBanner',
  63.             CheckoutRegisterPageLoadedEvent::class => 'addInstallmentBanner',
  64.             OffcanvasCartPageLoadedEvent::class => 'addInstallmentBanner',
  65.             ProductPageLoadedEvent::class => 'addInstallmentBanner',
  66.             FooterPageletLoadedEvent::class => 'addInstallmentBannerPagelet',
  67.             QuickviewPageletLoadedEvent::class => 'addInstallmentBannerPagelet',
  68.         ];
  69.     }
  70.     public function addInstallmentBanner(PageLoadedEvent $pageLoadedEvent): void
  71.     {
  72.         $salesChannelContext $pageLoadedEvent->getSalesChannelContext();
  73.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext) === false) {
  74.             return;
  75.         }
  76.         try {
  77.             $this->settingsValidationService->validate($salesChannelContext->getSalesChannel()->getId());
  78.         } catch (PayPalSettingsInvalidException $e) {
  79.             return;
  80.         }
  81.         if (!$this->systemConfigService->getBool(Settings::INSTALLMENT_BANNER_ENABLED)) {
  82.             return;
  83.         }
  84.         /** @var CheckoutCartPage|CheckoutConfirmPage|CheckoutRegisterPage|OffcanvasCartPage|ProductPage $page */
  85.         $page $pageLoadedEvent->getPage();
  86.         if ($page instanceof ProductPage
  87.             && $this->excludedProductValidator->isProductExcluded($page->getProduct(), $pageLoadedEvent->getSalesChannelContext())) {
  88.             return;
  89.         }
  90.         if (!$page instanceof ProductPage
  91.             && $this->excludedProductValidator->cartContainsExcludedProduct($page->getCart(), $pageLoadedEvent->getSalesChannelContext())) {
  92.             return;
  93.         }
  94.         $bannerData $this->bannerDataService->getInstallmentBannerData($page$salesChannelContext);
  95.         if ($page instanceof CheckoutCartPage) {
  96.             $productTableBannerData = new BannerData(
  97.                 $bannerData->getPaymentMethodId(),
  98.                 $bannerData->getClientId(),
  99.                 $bannerData->getAmount(),
  100.                 $bannerData->getCurrency(),
  101.                 'flex',
  102.                 'grey',
  103.                 '20x1'
  104.             );
  105.             $page->addExtension(self::PAYPAL_INSTALLMENT_BANNER_DATA_CART_PAGE_EXTENSION_ID$productTableBannerData);
  106.         }
  107.         $page->addExtension(
  108.             self::PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID,
  109.             $bannerData
  110.         );
  111.         $this->logger->debug('Added data to {page}', ['page' => \get_class($pageLoadedEvent)]);
  112.     }
  113.     public function addInstallmentBannerPagelet(PageletLoadedEvent $pageletLoadedEvent): void
  114.     {
  115.         $salesChannelContext $pageletLoadedEvent->getSalesChannelContext();
  116.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext) === false) {
  117.             return;
  118.         }
  119.         try {
  120.             $this->settingsValidationService->validate($salesChannelContext->getSalesChannelId());
  121.         } catch (PayPalSettingsInvalidException $e) {
  122.             return;
  123.         }
  124.         if (!$this->systemConfigService->getBool(Settings::INSTALLMENT_BANNER_ENABLED$salesChannelContext->getSalesChannelId())) {
  125.             return;
  126.         }
  127.         if ($pageletLoadedEvent instanceof QuickviewPageletLoadedEvent
  128.             && $this->excludedProductValidator->isProductExcluded($pageletLoadedEvent->getPagelet()->getProduct(), $pageletLoadedEvent->getSalesChannelContext())) {
  129.             return;
  130.         }
  131.         /** @var FooterPagelet|QuickviewPagelet $pagelet */
  132.         $pagelet $pageletLoadedEvent->getPagelet();
  133.         $bannerData $this->bannerDataService->getInstallmentBannerData($pagelet$salesChannelContext);
  134.         $pagelet->addExtension(
  135.             self::PAYPAL_INSTALLMENT_BANNER_DATA_EXTENSION_ID,
  136.             $bannerData
  137.         );
  138.     }
  139. }