vendor/store.shopware.com/swagpaypal/src/Checkout/Plus/PlusSubscriber.php line 109

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\Plus;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  10. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPage;
  14. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  16. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  17. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  18. use Shopware\Storefront\Page\Page;
  19. use Swag\PayPal\Checkout\Payment\PayPalPaymentHandler;
  20. use Swag\PayPal\Checkout\Plus\Service\PlusDataService;
  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\Contracts\Translation\TranslatorInterface;
  27. /**
  28.  * @deprecated tag:v7.0.0 - Will be removed without replacement.
  29.  *
  30.  * @internal
  31.  */
  32. class PlusSubscriber implements EventSubscriberInterface
  33. {
  34.     public const PAYPAL_PLUS_DATA_EXTENSION_ID 'payPalPlusData';
  35.     private SettingsValidationServiceInterface $settingsValidationService;
  36.     private SystemConfigService $systemConfigService;
  37.     private PlusDataService $plusDataService;
  38.     private PaymentMethodUtil $paymentMethodUtil;
  39.     private TranslatorInterface $translator;
  40.     private LoggerInterface $logger;
  41.     public function __construct(
  42.         SettingsValidationServiceInterface $settingsValidationService,
  43.         SystemConfigService $systemConfigService,
  44.         PlusDataService $plusDataService,
  45.         PaymentMethodUtil $paymentMethodUtil,
  46.         TranslatorInterface $translator,
  47.         LoggerInterface $logger
  48.     ) {
  49.         $this->settingsValidationService $settingsValidationService;
  50.         $this->systemConfigService $systemConfigService;
  51.         $this->plusDataService $plusDataService;
  52.         $this->paymentMethodUtil $paymentMethodUtil;
  53.         $this->translator $translator;
  54.         $this->logger $logger;
  55.     }
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             AccountEditOrderPageLoadedEvent::class => 'onAccountEditOrderLoaded',
  60.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  61.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishLoaded',
  62.         ];
  63.     }
  64.     public function onAccountEditOrderLoaded(AccountEditOrderPageLoadedEvent $event): void
  65.     {
  66.         $salesChannelContext $event->getSalesChannelContext();
  67.         if (!$this->checkSettings($salesChannelContext$event->getPage()->getPaymentMethods())) {
  68.             return;
  69.         }
  70.         $this->logger->debug('Adding data');
  71.         $page $event->getPage();
  72.         $plusData $this->plusDataService->getPlusDataFromOrder($page->getOrder(), $salesChannelContext);
  73.         $this->addPlusExtension($plusData$page$salesChannelContext);
  74.         $this->logger->debug('Added data');
  75.     }
  76.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  77.     {
  78.         $isExpressCheckout $event->getRequest()->query->getBoolean(PayPalPaymentHandler::PAYPAL_EXPRESS_CHECKOUT_ID);
  79.         if ($isExpressCheckout) {
  80.             return;
  81.         }
  82.         $salesChannelContext $event->getSalesChannelContext();
  83.         if (!$this->checkSettings($salesChannelContext$event->getPage()->getPaymentMethods())) {
  84.             return;
  85.         }
  86.         $this->logger->debug('Adding data');
  87.         $page $event->getPage();
  88.         $plusData $this->plusDataService->getPlusData($page->getCart(), $salesChannelContext);
  89.         $this->addPlusExtension($plusData$page$salesChannelContext);
  90.         $this->logger->debug('Added data');
  91.     }
  92.     public function onCheckoutFinishLoaded(CheckoutFinishPageLoadedEvent $event): void
  93.     {
  94.         $isPlus $event->getRequest()->query->getBoolean(PayPalPaymentHandler::PAYPAL_PLUS_CHECKOUT_ID);
  95.         if ($isPlus === false) {
  96.             return;
  97.         }
  98.         $salesChannelContext $event->getSalesChannelContext();
  99.         $salesChannelId $salesChannelContext->getSalesChannelId();
  100.         try {
  101.             $this->settingsValidationService->validate($salesChannelId);
  102.         } catch (PayPalSettingsInvalidException $e) {
  103.             return;
  104.         }
  105.         if (!$this->systemConfigService->getBool(Settings::PLUS_CHECKOUT_ENABLED$salesChannelId)
  106.             || $this->systemConfigService->getString(Settings::MERCHANT_LOCATION$salesChannelId) === Settings::MERCHANT_LOCATION_OTHER
  107.         ) {
  108.             return;
  109.         }
  110.         $transactions $event->getPage()->getOrder()->getTransactions();
  111.         if ($transactions === null) {
  112.             return;
  113.         }
  114.         $payPalPaymentId $this->paymentMethodUtil->getPayPalPaymentMethodId($salesChannelContext->getContext());
  115.         if ($payPalPaymentId === null) {
  116.             return;
  117.         }
  118.         $transaction $transactions->filterByPaymentMethodId($payPalPaymentId)->first();
  119.         if ($transaction === null) {
  120.             return;
  121.         }
  122.         $paymentMethod $transaction->getPaymentMethod();
  123.         if ($paymentMethod === null) {
  124.             return;
  125.         }
  126.         $this->logger->debug('Changing payment method data');
  127.         $this->changePaymentMethod($paymentMethod);
  128.         $this->logger->debug('Changed payment method data');
  129.     }
  130.     private function checkSettings(SalesChannelContext $salesChannelContextPaymentMethodCollection $paymentMethods): bool
  131.     {
  132.         if (!$this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext$paymentMethods)) {
  133.             return false;
  134.         }
  135.         $salesChannelId $salesChannelContext->getSalesChannelId();
  136.         try {
  137.             $this->settingsValidationService->validate($salesChannelId);
  138.         } catch (PayPalSettingsInvalidException $e) {
  139.             return false;
  140.         }
  141.         if (!$this->systemConfigService->getBool(Settings::PLUS_CHECKOUT_ENABLED$salesChannelId)
  142.             || $this->systemConfigService->getString(Settings::MERCHANT_LOCATION$salesChannelId) === Settings::MERCHANT_LOCATION_OTHER
  143.         ) {
  144.             return false;
  145.         }
  146.         return true;
  147.     }
  148.     /**
  149.      * @param AccountEditOrderPage|CheckoutConfirmPage $page
  150.      */
  151.     private function addPlusExtension(
  152.         ?PlusData $plusData,
  153.         Page $page,
  154.         SalesChannelContext $salesChannelContext
  155.     ): void {
  156.         if ($plusData === null) {
  157.             return;
  158.         }
  159.         $payPalPaymentId $plusData->getPaymentMethodId();
  160.         $payPalPaymentMethodFromCollection $page->getPaymentMethods()->get($payPalPaymentId);
  161.         if ($payPalPaymentMethodFromCollection !== null) {
  162.             $this->changePaymentMethod($payPalPaymentMethodFromCollection);
  163.         }
  164.         $currentSelectedPaymentMethod $salesChannelContext->getPaymentMethod();
  165.         if ($currentSelectedPaymentMethod->getId() !== $payPalPaymentId) {
  166.             return;
  167.         }
  168.         $this->changePaymentMethod($currentSelectedPaymentMethod);
  169.         $page->addExtension(self::PAYPAL_PLUS_DATA_EXTENSION_ID$plusData);
  170.     }
  171.     private function changePaymentMethod(PaymentMethodEntity $paymentMethod): void
  172.     {
  173.         $paymentMethod->addTranslated('name'$this->translator->trans('paypal.plus.paymentNameOverwrite'));
  174.         $description $paymentMethod->getTranslation('description');
  175.         if ($description === null) {
  176.             $description $paymentMethod->getDescription();
  177.         }
  178.         $paymentMethod->addTranslated(
  179.             'description',
  180.             $description ' ' $this->translator->trans('paypal.plus.paymentDescriptionExtension')
  181.         );
  182.     }
  183. }