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

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 Psr\Log\LoggerInterface;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Event\RouteRequest\HandlePaymentMethodRouteRequestEvent;
  14. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  16. use Swag\PayPal\Checkout\ExpressCheckout\SalesChannel\ExpressPrepareCheckoutRoute;
  17. use Swag\PayPal\Checkout\Payment\Handler\AbstractPaymentHandler;
  18. use Swag\PayPal\Checkout\Payment\Handler\EcsSpbHandler;
  19. use Swag\PayPal\Checkout\Payment\PayPalPaymentHandler;
  20. use Swag\PayPal\Checkout\SPBCheckout\Service\SPBCheckoutDataServiceInterface;
  21. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  22. use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
  23. use Swag\PayPal\Setting\Settings;
  24. use Swag\PayPal\Util\PaymentMethodUtil;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\Session\Session;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. class SPBCheckoutSubscriber implements EventSubscriberInterface
  30. {
  31.     public const PAYPAL_SMART_PAYMENT_BUTTONS_DATA_EXTENSION_ID 'payPalSpbButtonData';
  32.     private SettingsValidationServiceInterface $settingsValidationService;
  33.     private SystemConfigService $systemConfigService;
  34.     private SPBCheckoutDataServiceInterface $spbCheckoutDataService;
  35.     private PaymentMethodUtil $paymentMethodUtil;
  36.     private Session $session;
  37.     private TranslatorInterface $translator;
  38.     private LoggerInterface $logger;
  39.     public function __construct(
  40.         SettingsValidationServiceInterface $settingsValidationService,
  41.         SystemConfigService $systemConfigService,
  42.         SPBCheckoutDataServiceInterface $spbCheckoutDataService,
  43.         PaymentMethodUtil $paymentMethodUtil,
  44.         Session $session,
  45.         TranslatorInterface $translator,
  46.         LoggerInterface $logger
  47.     ) {
  48.         $this->settingsValidationService $settingsValidationService;
  49.         $this->systemConfigService $systemConfigService;
  50.         $this->spbCheckoutDataService $spbCheckoutDataService;
  51.         $this->paymentMethodUtil $paymentMethodUtil;
  52.         $this->session $session;
  53.         $this->translator $translator;
  54.         $this->logger $logger;
  55.     }
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             AccountEditOrderPageLoadedEvent::class => 'onAccountOrderEditLoaded',
  60.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  61.             HandlePaymentMethodRouteRequestEvent::class => 'addNecessaryRequestParameter',
  62.         ];
  63.     }
  64.     public function onAccountOrderEditLoaded(AccountEditOrderPageLoadedEvent $event): void
  65.     {
  66.         $request $event->getRequest();
  67.         if (!$this->checkSettings($event->getSalesChannelContext(), $event->getPage()->getPaymentMethods())) {
  68.             return;
  69.         }
  70.         if ($this->addSuccessMessage($request)) {
  71.             $this->logger->debug('Added success message');
  72.             return;
  73.         }
  74.         $this->logger->debug('Adding data');
  75.         $editOrderPage $event->getPage();
  76.         $buttonData $this->spbCheckoutDataService->buildCheckoutData(
  77.             $event->getSalesChannelContext(),
  78.             $editOrderPage->getOrder()->getId()
  79.         );
  80.         $currency $editOrderPage->getOrder()->getCurrency();
  81.         if ($currency === null) {
  82.             $currency $event->getSalesChannelContext()->getCurrency();
  83.         }
  84.         $buttonData->setDisabledAlternativePaymentMethods(
  85.             $this->spbCheckoutDataService->getDisabledAlternativePaymentMethods(
  86.                 $editOrderPage->getOrder()->getAmountTotal(),
  87.                 $currency->getIsoCode()
  88.             )
  89.         );
  90.         $this->changePaymentMethodDescription($editOrderPage->getPaymentMethods(), $event->getContext());
  91.         $editOrderPage->addExtension(self::PAYPAL_SMART_PAYMENT_BUTTONS_DATA_EXTENSION_ID$buttonData);
  92.         $this->logger->debug('Added data');
  93.     }
  94.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  95.     {
  96.         $request $event->getRequest();
  97.         if (!$this->checkSettings($event->getSalesChannelContext(), $event->getPage()->getPaymentMethods())) {
  98.             return;
  99.         }
  100.         $confirmPage $event->getPage();
  101.         if ($confirmPage->getCart()->getExtension(ExpressPrepareCheckoutRoute::PAYPAL_EXPRESS_CHECKOUT_CART_EXTENSION_ID) !== null) {
  102.             return;
  103.         }
  104.         if ($confirmPage->getCart()->getErrors()->blockOrder()) {
  105.             return;
  106.         }
  107.         if ($this->addSuccessMessage($request)) {
  108.             $this->logger->debug('Added success message');
  109.             return;
  110.         }
  111.         $this->logger->debug('Adding data');
  112.         $buttonData $this->spbCheckoutDataService->buildCheckoutData($event->getSalesChannelContext());
  113.         $buttonData->setDisabledAlternativePaymentMethods(
  114.             $this->spbCheckoutDataService->getDisabledAlternativePaymentMethods(
  115.                 $confirmPage->getCart()->getPrice()->getTotalPrice(),
  116.                 $event->getSalesChannelContext()->getCurrency()->getIsoCode()
  117.             )
  118.         );
  119.         $this->changePaymentMethodDescription($confirmPage->getPaymentMethods(), $event->getContext());
  120.         $confirmPage->addExtension(self::PAYPAL_SMART_PAYMENT_BUTTONS_DATA_EXTENSION_ID$buttonData);
  121.         $this->logger->debug('Added data');
  122.     }
  123.     public function addNecessaryRequestParameter(HandlePaymentMethodRouteRequestEvent $event): void
  124.     {
  125.         $this->logger->debug('Adding request parameter');
  126.         $storefrontRequest $event->getStorefrontRequest();
  127.         $storeApiRequest $event->getStoreApiRequest();
  128.         $originalRoute $storefrontRequest->attributes->get('_route');
  129.         if ($originalRoute !== 'frontend.account.edit-order.update-order') {
  130.             return;
  131.         }
  132.         $storeApiRequest->request->set(
  133.             PayPalPaymentHandler::PAYPAL_SMART_PAYMENT_BUTTONS_ID,
  134.             $storefrontRequest->request->get(PayPalPaymentHandler::PAYPAL_SMART_PAYMENT_BUTTONS_ID)
  135.         );
  136.         $storeApiRequest->request->set(
  137.             AbstractPaymentHandler::PAYPAL_PAYMENT_ORDER_ID_INPUT_NAME,
  138.             $storefrontRequest->request->get(AbstractPaymentHandler::PAYPAL_PAYMENT_ORDER_ID_INPUT_NAME)
  139.         );
  140.         $this->logger->debug('Added request parameter');
  141.     }
  142.     private function checkSettings(SalesChannelContext $salesChannelContextPaymentMethodCollection $paymentMethods): bool
  143.     {
  144.         if (!$this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext$paymentMethods)) {
  145.             return false;
  146.         }
  147.         $salesChannelId $salesChannelContext->getSalesChannelId();
  148.         try {
  149.             $this->settingsValidationService->validate($salesChannelId);
  150.         } catch (PayPalSettingsInvalidException $e) {
  151.             return false;
  152.         }
  153.         if (!$this->systemConfigService->getBool(Settings::SPB_CHECKOUT_ENABLED$salesChannelId)
  154.             || $this->systemConfigService->getString(Settings::MERCHANT_LOCATION$salesChannelId) === Settings::MERCHANT_LOCATION_GERMANY
  155.         ) {
  156.             return false;
  157.         }
  158.         return true;
  159.     }
  160.     private function addSuccessMessage(Request $request): bool
  161.     {
  162.         $requestQuery $request->query;
  163.         if ($requestQuery->has(EcsSpbHandler::PAYPAL_PAYMENT_ORDER_ID_INPUT_NAME)) {
  164.             $this->session->getFlashBag()->add('success'$this->translator->trans('paypal.smartPaymentButtons.confirmPageHint'));
  165.             return true;
  166.         }
  167.         return false;
  168.     }
  169.     private function changePaymentMethodDescription(PaymentMethodCollection $paymentMethodsContext $context): void
  170.     {
  171.         $payPalPaymentMethodId $this->paymentMethodUtil->getPayPalPaymentMethodId($context);
  172.         if ($payPalPaymentMethodId === null) {
  173.             return;
  174.         }
  175.         $paypalPaymentMethod $paymentMethods->get($payPalPaymentMethodId);
  176.         if ($paypalPaymentMethod === null) {
  177.             return;
  178.         }
  179.         $paypalPaymentMethod->addTranslated('description'$this->translator->trans('paypal.smartPaymentButtons.description'));
  180.     }
  181. }