custom/plugins/SwagPayPal/src/Checkout/SPBCheckout/SPBMarksSubscriber.php line 53

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\SPBCheckout;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  11. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  12. use Swag\PayPal\Checkout\ExpressCheckout\ExpressCheckoutController;
  13. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  14. use Swag\PayPal\Setting\Service\SettingsServiceInterface;
  15. use Swag\PayPal\Setting\SwagPayPalSettingStruct;
  16. use Swag\PayPal\Util\PaymentMethodUtil;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class SPBMarksSubscriber implements EventSubscriberInterface
  19. {
  20.     public const PAYPAL_SMART_PAYMENT_MARKS_DATA_EXTENSION_ID 'payPalSpbMarksData';
  21.     /**
  22.      * @var SettingsServiceInterface
  23.      */
  24.     private $settingsService;
  25.     /**
  26.      * @var PaymentMethodUtil
  27.      */
  28.     private $paymentMethodUtil;
  29.     public function __construct(SettingsServiceInterface $settingsServicePaymentMethodUtil $paymentMethodUtil)
  30.     {
  31.         $this->settingsService $settingsService;
  32.         $this->paymentMethodUtil $paymentMethodUtil;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             AccountPaymentMethodPageLoadedEvent::class => 'addMarksExtension',
  38.             FooterPageletLoadedEvent::class => 'addMarksExtension',
  39.             CheckoutConfirmPageLoadedEvent::class => 'addMarksExtension',
  40.         ];
  41.     }
  42.     /**
  43.      * @param AccountPaymentMethodPageLoadedEvent|FooterPageletLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  44.      */
  45.     public function addMarksExtension($event): void
  46.     {
  47.         $spbMarksData $this->getSpbMarksData($event->getSalesChannelContext());
  48.         if ($spbMarksData === null) {
  49.             return;
  50.         }
  51.         if ($event instanceof CheckoutConfirmPageLoadedEvent) {
  52.             $confirmPage $event->getPage();
  53.             if ($confirmPage->getCart()->getExtension(ExpressCheckoutController::PAYPAL_EXPRESS_CHECKOUT_CART_EXTENSION_ID) !== null) {
  54.                 return;
  55.             }
  56.             $confirmPage->addExtension(self::PAYPAL_SMART_PAYMENT_MARKS_DATA_EXTENSION_ID$spbMarksData);
  57.             return;
  58.         }
  59.         if ($event instanceof AccountPaymentMethodPageLoadedEvent) {
  60.             $event->getPage()->addExtension(self::PAYPAL_SMART_PAYMENT_MARKS_DATA_EXTENSION_ID$spbMarksData);
  61.             return;
  62.         }
  63.         $event->getPagelet()->addExtension(self::PAYPAL_SMART_PAYMENT_MARKS_DATA_EXTENSION_ID$spbMarksData);
  64.     }
  65.     private function getSpbMarksData(SalesChannelContext $salesChannelContext): ?SPBMarksData
  66.     {
  67.         if (!$this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext)) {
  68.             return null;
  69.         }
  70.         try {
  71.             $settings $this->settingsService->getSettings($salesChannelContext->getSalesChannel()->getId());
  72.         } catch (PayPalSettingsInvalidException $e) {
  73.             return null;
  74.         }
  75.         if (!$settings->getSpbCheckoutEnabled()
  76.             || $settings->getMerchantLocation() === SwagPayPalSettingStruct::MERCHANT_LOCATION_GERMANY
  77.         ) {
  78.             return null;
  79.         }
  80.         return new SPBMarksData(
  81.             $settings->getSandbox() ? $settings->getClientIdSandbox() : $settings->getClientId(),
  82.             (string) $this->paymentMethodUtil->getPayPalPaymentMethodId($salesChannelContext->getContext()),
  83.             $settings->getSpbAlternativePaymentMethodsEnabled()
  84.         );
  85.     }
  86. }