custom/plugins/SwagPayPal/src/Checkout/Plus/PlusSubscriber.php line 107

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