vendor/store.shopware.com/swagpaypal/src/Checkout/ExpressCheckout/ExpressCheckoutSubscriber.php line 133

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 Psr\Log\LoggerInterface;
  9. use Shopware\Core\Checkout\Customer\CustomerEvents;
  10. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  11. use Shopware\Core\Content\Product\ProductCollection;
  12. use Shopware\Core\Framework\Event\DataMappingEvent;
  13. use Shopware\Core\Framework\Struct\ArrayStruct;
  14. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  15. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntitySearchResultLoadedEvent;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. use Shopware\Storefront\Event\SwitchBuyBoxVariantEvent;
  19. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  20. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  21. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  22. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  23. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  24. use Shopware\Storefront\Page\PageLoadedEvent;
  25. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  26. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  27. use Shopware\Storefront\Pagelet\PageletLoadedEvent;
  28. use Shopware\Storefront\Pagelet\Wishlist\GuestWishlistPageletLoadedEvent;
  29. use Swag\CmsExtensions\Storefront\Pagelet\Quickview\QuickviewPageletLoadedEvent;
  30. use Swag\PayPal\Checkout\Cart\Service\ExcludedProductValidator;
  31. use Swag\PayPal\Checkout\ExpressCheckout\Service\ExpressCheckoutDataServiceInterface;
  32. use Swag\PayPal\Checkout\ExpressCheckout\Service\ExpressCustomerService;
  33. use Swag\PayPal\Checkout\Payment\PayPalPaymentHandler;
  34. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  35. use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
  36. use Swag\PayPal\Setting\Settings;
  37. use Swag\PayPal\Util\PaymentMethodUtil;
  38. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  39. class ExpressCheckoutSubscriber implements EventSubscriberInterface
  40. {
  41.     public const PAYPAL_EXPRESS_CHECKOUT_BUTTON_DATA_EXTENSION_ID 'payPalEcsButtonData';
  42.     private ExpressCheckoutDataServiceInterface $expressCheckoutDataService;
  43.     private SettingsValidationServiceInterface $settingsValidationService;
  44.     private SystemConfigService $systemConfigService;
  45.     private PaymentMethodUtil $paymentMethodUtil;
  46.     private ExcludedProductValidator $excludedProductValidator;
  47.     private LoggerInterface $logger;
  48.     public function __construct(
  49.         ExpressCheckoutDataServiceInterface $service,
  50.         SettingsValidationServiceInterface $settingsValidationService,
  51.         SystemConfigService $systemConfigService,
  52.         PaymentMethodUtil $paymentMethodUtil,
  53.         ExcludedProductValidator $excludedProductValidator,
  54.         LoggerInterface $logger
  55.     ) {
  56.         $this->expressCheckoutDataService $service;
  57.         $this->settingsValidationService $settingsValidationService;
  58.         $this->systemConfigService $systemConfigService;
  59.         $this->paymentMethodUtil $paymentMethodUtil;
  60.         $this->excludedProductValidator $excludedProductValidator;
  61.         $this->logger $logger;
  62.     }
  63.     public static function getSubscribedEvents(): array
  64.     {
  65.         return [
  66.             CheckoutCartPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  67.             CheckoutRegisterPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  68.             NavigationPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  69.             OffcanvasCartPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  70.             ProductPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  71.             SearchPageLoadedEvent::class => 'addExpressCheckoutDataToPage',
  72.             'sales_channel.product.search.result.loaded' => 'addExcludedProductsToSearchResult',
  73.             QuickviewPageletLoadedEvent::class => 'addExpressCheckoutDataToPagelet',
  74.             GuestWishlistPageletLoadedEvent::class => 'addExpressCheckoutDataToPagelet',
  75.             SwitchBuyBoxVariantEvent::class => 'addExpressCheckoutDataToBuyBoxSwitch',
  76.             'framework.validation.address.create' => 'disableAddressValidation',
  77.             'framework.validation.customer.create' => 'disableCustomerValidation',
  78.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  79.             CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'addPayerIdToCustomer',
  80.         ];
  81.     }
  82.     /**
  83.      * @param CheckoutCartPageLoadedEvent|CheckoutRegisterPageLoadedEvent|NavigationPageLoadedEvent|OffcanvasCartPageLoadedEvent|ProductPageLoadedEvent|SearchPageLoadedEvent $event
  84.      */
  85.     public function addExpressCheckoutDataToPage(PageLoadedEvent $event): void
  86.     {
  87.         $addProductToCart $event instanceof ProductPageLoadedEvent
  88.             || $event instanceof NavigationPageLoadedEvent
  89.             || $event instanceof SearchPageLoadedEvent;
  90.         $expressCheckoutButtonData $this->getExpressCheckoutButtonData($event->getSalesChannelContext(), \get_class($event), $addProductToCart);
  91.         if ($expressCheckoutButtonData === null) {
  92.             return;
  93.         }
  94.         if ($event instanceof ProductPageLoadedEvent
  95.             && $this->excludedProductValidator->isProductExcluded($event->getPage()->getProduct(), $event->getSalesChannelContext())) {
  96.             return;
  97.         }
  98.         if (!$addProductToCart
  99.             && \method_exists($event->getPage(), 'getCart')
  100.             && $this->excludedProductValidator->cartContainsExcludedProduct($event->getPage()->getCart(), $event->getSalesChannelContext())) {
  101.             return;
  102.         }
  103.         $event->getPage()->addExtension(
  104.             self::PAYPAL_EXPRESS_CHECKOUT_BUTTON_DATA_EXTENSION_ID,
  105.             $expressCheckoutButtonData
  106.         );
  107.         $this->logger->debug('Added data to page {page}', ['page' => \get_class($event)]);
  108.     }
  109.     public function addExpressCheckoutDataToPagelet(PageletLoadedEvent $event): void
  110.     {
  111.         $expressCheckoutButtonData $this->getExpressCheckoutButtonData($event->getSalesChannelContext(), \get_class($event), true);
  112.         if ($expressCheckoutButtonData === null) {
  113.             return;
  114.         }
  115.         if ($event instanceof QuickviewPageletLoadedEvent
  116.             && $this->excludedProductValidator->isProductExcluded($event->getPagelet()->getProduct(), $event->getSalesChannelContext())) {
  117.             return;
  118.         }
  119.         $event->getPagelet()->addExtension(
  120.             self::PAYPAL_EXPRESS_CHECKOUT_BUTTON_DATA_EXTENSION_ID,
  121.             $expressCheckoutButtonData
  122.         );
  123.     }
  124.     public function addExpressCheckoutDataToBuyBoxSwitch(SwitchBuyBoxVariantEvent $event): void
  125.     {
  126.         $salesChannelContext $event->getSalesChannelContext();
  127.         $expressCheckoutButtonData $this->getExpressCheckoutButtonData($salesChannelContext\get_class($event), true);
  128.         if ($expressCheckoutButtonData === null) {
  129.             return;
  130.         }
  131.         $event->getProduct()->addExtension(
  132.             self::PAYPAL_EXPRESS_CHECKOUT_BUTTON_DATA_EXTENSION_ID,
  133.             $expressCheckoutButtonData
  134.         );
  135.     }
  136.     public function addExcludedProductsToSearchResult(SalesChannelEntitySearchResultLoadedEvent $event): void
  137.     {
  138.         if (!$this->checkSettings($event->getSalesChannelContext(), \get_class($event))) {
  139.             return;
  140.         }
  141.         $productIds = [];
  142.         /** @var ProductCollection $products */
  143.         $products $event->getResult()->getEntities();
  144.         foreach ($products as $product) {
  145.             $productIds[] = $product->getId();
  146.             $productIds[] = $product->getParentId();
  147.         }
  148.         $excluded $this->excludedProductValidator->findExcludedProducts(\array_filter($productIds), $event->getSalesChannelContext());
  149.         foreach ($products as $product) {
  150.             if (\in_array($product->getId(), $excludedtrue) || ($product->getParentId() && \in_array($product->getParentId(), $excludedtrue))) {
  151.                 $product->addExtension(ExcludedProductValidator::PRODUCT_EXCLUDED_FOR_PAYPAL, new ArrayStruct());
  152.             }
  153.         }
  154.     }
  155.     public function disableAddressValidation(BuildValidationEvent $event): void
  156.     {
  157.         if (!$event->getContext()->hasExtension(ExpressCustomerService::EXPRESS_CHECKOUT_ACTIVE)) {
  158.             return;
  159.         }
  160.         $event->getDefinition()->set('additionalAddressLine1')
  161.                                ->set('additionalAddressLine2')
  162.                                ->set('phoneNumber');
  163.     }
  164.     public function disableCustomerValidation(BuildValidationEvent $event): void
  165.     {
  166.         if (!$event->getContext()->hasExtension(ExpressCustomerService::EXPRESS_CHECKOUT_ACTIVE)) {
  167.             return;
  168.         }
  169.         $event->getDefinition()->set('birthdayDay')
  170.                                ->set('birthdayMonth')
  171.                                ->set('birthdayYear');
  172.     }
  173.     public function addPayerIdToCustomer(DataMappingEvent $event): void
  174.     {
  175.         if (!$event->getContext()->hasExtension(ExpressCustomerService::EXPRESS_CHECKOUT_ACTIVE)) {
  176.             return;
  177.         }
  178.         $input $event->getInput();
  179.         $output $event->getOutput();
  180.         $output['customFields'][ExpressCustomerService::EXPRESS_PAYER_ID] = $input->get(ExpressCustomerService::EXPRESS_PAYER_ID);
  181.         $event->setOutput($output);
  182.     }
  183.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  184.     {
  185.         if ($event->getRequest()->query->has(PayPalPaymentHandler::PAYPAL_EXPRESS_CHECKOUT_ID) === false) {
  186.             return;
  187.         }
  188.         $confirmPage $event->getPage();
  189.         $payPalPaymentMethodId $this->paymentMethodUtil->getPayPalPaymentMethodId($event->getContext());
  190.         if ($payPalPaymentMethodId === null) {
  191.             return;
  192.         }
  193.         $paymentMethods $confirmPage->getPaymentMethods();
  194.         if ($paymentMethods->has($payPalPaymentMethodId) === false) {
  195.             return;
  196.         }
  197.         $filtered $paymentMethods->filterByProperty('id'$payPalPaymentMethodId);
  198.         $confirmPage->setPaymentMethods($filtered);
  199.         $this->logger->debug('Removed other payment methods from selection for Express Checkout');
  200.     }
  201.     private function getExpressCheckoutButtonData(
  202.         SalesChannelContext $salesChannelContext,
  203.         string $eventName,
  204.         bool $addProductToCart false
  205.     ): ?ExpressCheckoutButtonData {
  206.         if (!$this->checkSettings($salesChannelContext$eventName)) {
  207.             return null;
  208.         }
  209.         return $this->expressCheckoutDataService->buildExpressCheckoutButtonData(
  210.             $salesChannelContext,
  211.             $addProductToCart
  212.         );
  213.     }
  214.     private function checkSettings(SalesChannelContext $contextstring $eventName): bool
  215.     {
  216.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($context) === false) {
  217.             return false;
  218.         }
  219.         try {
  220.             $this->settingsValidationService->validate($context->getSalesChannelId());
  221.         } catch (PayPalSettingsInvalidException $e) {
  222.             return false;
  223.         }
  224.         if ($this->expressOptionForEventEnabled($context->getSalesChannelId(), $eventName) === false) {
  225.             return false;
  226.         }
  227.         return true;
  228.     }
  229.     private function expressOptionForEventEnabled(string $salesChannelIdstring $eventName): bool
  230.     {
  231.         switch ($eventName) {
  232.             case ProductPageLoadedEvent::class:
  233.             case QuickviewPageletLoadedEvent::class:
  234.                 return $this->systemConfigService->getBool(Settings::ECS_DETAIL_ENABLED$salesChannelId);
  235.             case OffcanvasCartPageLoadedEvent::class:
  236.                 return $this->systemConfigService->getBool(Settings::ECS_OFF_CANVAS_ENABLED$salesChannelId);
  237.             case CheckoutRegisterPageLoadedEvent::class:
  238.                 return $this->systemConfigService->getBool(Settings::ECS_LOGIN_ENABLED$salesChannelId);
  239.             case CheckoutCartPageLoadedEvent::class:
  240.                 return $this->systemConfigService->getBool(Settings::ECS_CART_ENABLED$salesChannelId);
  241.             case NavigationPageLoadedEvent::class:
  242.             case CmsPageLoadedEvent::class:
  243.             case SearchPageLoadedEvent::class:
  244.             case GuestWishlistPageletLoadedEvent::class:
  245.             case SwitchBuyBoxVariantEvent::class:
  246.             case SalesChannelEntitySearchResultLoadedEvent::class:
  247.                 return $this->systemConfigService->getBool(Settings::ECS_LISTING_ENABLED$salesChannelId);
  248.             default:
  249.                 return false;
  250.         }
  251.     }
  252. }