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

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