custom/plugins/SwagPayPal/src/Checkout/SPBCheckout/SPBCheckoutSubscriber.php line 76

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\Checkout\Payment\PaymentMethodCollection;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  11. use Swag\PayPal\Checkout\ExpressCheckout\ExpressCheckoutController;
  12. use Swag\PayPal\Checkout\SPBCheckout\Service\SPBCheckoutDataService;
  13. use Swag\PayPal\Payment\Handler\AbstractPaymentHandler;
  14. use Swag\PayPal\Payment\Handler\EcsSpbHandler;
  15. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  16. use Swag\PayPal\Setting\Service\SettingsServiceInterface;
  17. use Swag\PayPal\Setting\SwagPayPalSettingStruct;
  18. use Swag\PayPal\Util\PaymentMethodUtil;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\Session\Session;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. class SPBCheckoutSubscriber implements EventSubscriberInterface
  23. {
  24.     public const PAYPAL_SMART_PAYMENT_BUTTONS_DATA_EXTENSION_ID 'payPalSpbButtonData';
  25.     public const PAYPAL_SMART_PAYMENT_BUTTONS_ERROR_PARAMETER 'payPalSpbError';
  26.     /**
  27.      * @var SettingsServiceInterface
  28.      */
  29.     private $settingsService;
  30.     /**
  31.      * @var SPBCheckoutDataService
  32.      */
  33.     private $spbCheckoutDataService;
  34.     /**
  35.      * @var PaymentMethodUtil
  36.      */
  37.     private $paymentMethodUtil;
  38.     /**
  39.      * @var Session
  40.      */
  41.     private $session;
  42.     /**
  43.      * @var TranslatorInterface
  44.      */
  45.     private $translator;
  46.     public function __construct(
  47.         SettingsServiceInterface $settingsService,
  48.         SPBCheckoutDataService $spbCheckoutDataService,
  49.         PaymentMethodUtil $paymentMethodUtil,
  50.         Session $session,
  51.         TranslatorInterface $translator
  52.     ) {
  53.         $this->settingsService $settingsService;
  54.         $this->spbCheckoutDataService $spbCheckoutDataService;
  55.         $this->paymentMethodUtil $paymentMethodUtil;
  56.         $this->session $session;
  57.         $this->translator $translator;
  58.     }
  59.     public static function getSubscribedEvents(): array
  60.     {
  61.         return [
  62.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  63.         ];
  64.     }
  65.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  66.     {
  67.         if ($event->getRequest()->query->getBoolean(self::PAYPAL_SMART_PAYMENT_BUTTONS_ERROR_PARAMETER)) {
  68.             $this->session->getFlashBag()->add('danger'$this->translator->trans('smartPaymentButtons.confirmPageError'));
  69.         }
  70.         $salesChannelContext $event->getSalesChannelContext();
  71.         if (!$this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext)) {
  72.             return;
  73.         }
  74.         try {
  75.             $settings $this->settingsService->getSettings($salesChannelContext->getSalesChannel()->getId());
  76.         } catch (PayPalSettingsInvalidException $e) {
  77.             return;
  78.         }
  79.         if (!$settings->getSpbCheckoutEnabled()
  80.             || $settings->getMerchantLocation() === SwagPayPalSettingStruct::MERCHANT_LOCATION_GERMANY
  81.         ) {
  82.             return;
  83.         }
  84.         $confirmPage $event->getPage();
  85.         if ($confirmPage->getCart()->getExtension(ExpressCheckoutController::PAYPAL_EXPRESS_CHECKOUT_CART_EXTENSION_ID) !== null) {
  86.             return;
  87.         }
  88.         $requestQuery $event->getRequest()->query;
  89.         if ($requestQuery->has(EcsSpbHandler::PAYPAL_PAYER_ID_INPUT_NAME)
  90.             && $requestQuery->has(AbstractPaymentHandler::PAYPAL_PAYMENT_ID_INPUT_NAME)
  91.         ) {
  92.             $this->session->getFlashBag()->add('success'$this->translator->trans('smartPaymentButtons.confirmPageHint'));
  93.             return;
  94.         }
  95.         $buttonData $this->spbCheckoutDataService->getCheckoutData(
  96.             $event->getSalesChannelContext(),
  97.             $settings
  98.         );
  99.         $this->changePaymentMethodDescription($confirmPage->getPaymentMethods(), $event->getContext());
  100.         $confirmPage->addExtension(self::PAYPAL_SMART_PAYMENT_BUTTONS_DATA_EXTENSION_ID$buttonData);
  101.     }
  102.     private function changePaymentMethodDescription(PaymentMethodCollection $paymentMethodsContext $context): void
  103.     {
  104.         $payPalPaymentMethodId $this->paymentMethodUtil->getPayPalPaymentMethodId($context);
  105.         if ($payPalPaymentMethodId === null) {
  106.             return;
  107.         }
  108.         $paypalPaymentMethod $paymentMethods->get($payPalPaymentMethodId);
  109.         if ($paypalPaymentMethod === null) {
  110.             return;
  111.         }
  112.         $paypalPaymentMethod->addTranslated('description'$this->translator->trans('smartPaymentButtons.description'));
  113.     }
  114. }