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

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 Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  11. use Swag\PayPal\Checkout\Plus\Service\PlusDataService;
  12. use Swag\PayPal\Payment\PayPalPaymentHandler;
  13. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  14. use Swag\PayPal\Setting\Service\SettingsServiceInterface;
  15. use Swag\PayPal\Setting\SwagPayPalSettingStruct;
  16. use Swag\PayPal\Util\PaymentMethodUtil;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class PlusSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var SettingsServiceInterface
  23.      */
  24.     private $settingsService;
  25.     /**
  26.      * @var PlusDataService
  27.      */
  28.     private $plusDataService;
  29.     /**
  30.      * @var PaymentMethodUtil
  31.      */
  32.     private $paymentMethodUtil;
  33.     /**
  34.      * @var TranslatorInterface
  35.      */
  36.     private $translator;
  37.     public function __construct(
  38.         SettingsServiceInterface $settingsService,
  39.         PlusDataService $plusDataService,
  40.         PaymentMethodUtil $paymentMethodUtil,
  41.         TranslatorInterface $translator
  42.     ) {
  43.         $this->settingsService $settingsService;
  44.         $this->plusDataService $plusDataService;
  45.         $this->paymentMethodUtil $paymentMethodUtil;
  46.         $this->translator $translator;
  47.     }
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  52.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishLoaded',
  53.         ];
  54.     }
  55.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  56.     {
  57.         $salesChannelContext $event->getSalesChannelContext();
  58.         if (!$this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($salesChannelContext)) {
  59.             return;
  60.         }
  61.         $isExpressCheckout $event->getRequest()->query->getBoolean(PayPalPaymentHandler::PAYPAL_EXPRESS_CHECKOUT_ID);
  62.         if ($isExpressCheckout) {
  63.             return;
  64.         }
  65.         try {
  66.             $settings $this->settingsService->getSettings($salesChannelContext->getSalesChannel()->getId());
  67.         } catch (PayPalSettingsInvalidException $e) {
  68.             return;
  69.         }
  70.         if (!$settings->getPlusCheckoutEnabled()
  71.             || $settings->getMerchantLocation() === SwagPayPalSettingStruct::MERCHANT_LOCATION_OTHER
  72.         ) {
  73.             return;
  74.         }
  75.         $page $event->getPage();
  76.         $plusData $this->plusDataService->getPlusData($page->getCart(), $salesChannelContext$settings);
  77.         if ($plusData === null) {
  78.             return;
  79.         }
  80.         $payPalPaymentId $plusData->getPaymentMethodId();
  81.         $payPalPaymentMethodFromCollection $page->getPaymentMethods()->get($payPalPaymentId);
  82.         if ($payPalPaymentMethodFromCollection !== null) {
  83.             $this->changePaymentMethod($payPalPaymentMethodFromCollection);
  84.         }
  85.         $currentSelectedPaymentMethod $salesChannelContext->getPaymentMethod();
  86.         if ($currentSelectedPaymentMethod->getId() !== $payPalPaymentId) {
  87.             return;
  88.         }
  89.         $this->changePaymentMethod($currentSelectedPaymentMethod);
  90.         $page->addExtension('payPalPlusData'$plusData);
  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.         try {
  100.             $settings $this->settingsService->getSettings($salesChannelContext->getSalesChannel()->getId());
  101.         } catch (PayPalSettingsInvalidException $e) {
  102.             return;
  103.         }
  104.         if (!$settings->getPlusCheckoutEnabled()
  105.             || $settings->getMerchantLocation() === SwagPayPalSettingStruct::MERCHANT_LOCATION_OTHER
  106.         ) {
  107.             return;
  108.         }
  109.         $transactions $event->getPage()->getOrder()->getTransactions();
  110.         if ($transactions === null) {
  111.             return;
  112.         }
  113.         $transaction $transactions->first();
  114.         if ($transaction === null) {
  115.             return;
  116.         }
  117.         $paymentMethod $transaction->getPaymentMethod();
  118.         if ($paymentMethod === null) {
  119.             return;
  120.         }
  121.         $payPalPaymentId $this->paymentMethodUtil->getPayPalPaymentMethodId($salesChannelContext->getContext());
  122.         if ($paymentMethod->getId() !== $payPalPaymentId) {
  123.             return;
  124.         }
  125.         $this->changePaymentMethod($paymentMethod);
  126.     }
  127.     private function changePaymentMethod(PaymentMethodEntity $paymentMethod): void
  128.     {
  129.         $paymentMethod->addTranslated('name'$this->translator->trans('payPalPlus.paymentNameOverwrite'));
  130.         $description $paymentMethod->getTranslation('description');
  131.         if ($description === null) {
  132.             $description $paymentMethod->getDescription();
  133.         }
  134.         $paymentMethod->addTranslated(
  135.             'description',
  136.             $description ' ' $this->translator->trans('payPalPlus.paymentDescriptionExtension')
  137.         );
  138.     }
  139. }