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

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