custom/plugins/SwagAmazonPay/src/Storefront/EventListeners/AmazonPayConfirmEventListener.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) shopware AG <info@shopware.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Swag\AmazonPay\Storefront\EventListeners;
  9. use AmazonPayApiSdkExtension\Struct\CheckoutSession;
  10. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  11. use Swag\AmazonPay\Components\Client\ClientProvider;
  12. use Swag\AmazonPay\Installer\PaymentMethodInstaller;
  13. use Swag\AmazonPay\Storefront\Page\Extension\AmazonPayConfirmExtension;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. class AmazonPayConfirmEventListener implements EventSubscriberInterface
  18. {
  19.     private SessionInterface $session;
  20.     private ClientProvider $clientProvider;
  21.     public function __construct(?SessionInterface $session, ?RequestStack $requestStackClientProvider $clientProvider)
  22.     {
  23.         if ($session === null) {
  24.             $this->session $requestStack->getSession();  //SW65 style
  25.         } else {
  26.             $this->session $session//SW64 style
  27.         }
  28.         $this->clientProvider $clientProvider;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             CheckoutConfirmPageLoadedEvent::class => 'onLoadConfirmPage',
  37.         ];
  38.     }
  39.     public function onLoadConfirmPage(CheckoutConfirmPageLoadedEvent $event): void
  40.     {
  41.         $session $this->session;
  42.         // Only for AmazonPay!
  43.         if ($event->getSalesChannelContext()->getPaymentMethod()->getId() !== PaymentMethodInstaller::AMAZON_PAYMENT_ID && $event->getRequest()->get('amazonPayAction') === 'reset') {
  44.             // Remove the "on hold" checkout session id from the session just to clear things up.
  45.             // Will be automatically set again if Amazon Pay is active
  46.             $session->remove('swag-amazon-pay.checkout-session-id');
  47.             return;
  48.         }
  49.         // Use URL param for checkout determination first and fallback to the value inside the session.
  50.         $checkoutSessionId = (string)$event->getRequest()->query->get('amazonPayCheckoutId'$session->get('swag-amazon-pay.checkout-session-id'));
  51.         $isOneClickCheckout $event->getRequest()->query->getBoolean('oneClickCheckout');
  52.         // No session id but one click checkout requested?
  53.         if (!$checkoutSessionId && $isOneClickCheckout) {
  54.             return;
  55.         }
  56.         // Keep the checkoutSessionId on hold for the case that the customer e.g changes the shipping method on the confirm page or returns to confirm page later.
  57.         if ($isOneClickCheckout) {
  58.             $checkoutSession $this->clientProvider->getClient($event->getSalesChannelContext()->getSalesChannelId())->getCheckoutSession($checkoutSessionId);
  59.             //TODO handle wrong status
  60.             $session->set('swag-amazon-pay.checkout-session-id'$checkoutSessionId);
  61.             $this->addConfirmPageExtension($event$checkoutSession);
  62.             $this->removeOtherPaymentOptions($event);
  63.             return;
  64.         }
  65.         // Reset from One-Click checkout to the regular Amazon Pay checkout
  66.         $session->remove('swag-amazon-pay.checkout-session-id');
  67.     }
  68.     private function addConfirmPageExtension(
  69.         CheckoutConfirmPageLoadedEvent $event,
  70.         CheckoutSession                $checkoutSession,
  71.         bool                           $isOneClickCheckout true
  72.     ): void
  73.     {
  74.         $confirmExtension = new AmazonPayConfirmExtension();
  75.         $confirmExtension->setCheckoutSessionId($checkoutSession->getCheckoutSessionId());
  76.         foreach ($checkoutSession->getPaymentPreferences() as $paymentPreference) {
  77.             if (is_array($paymentPreference) && isset($paymentPreference['paymentDescriptor'])) {
  78.                 $confirmExtension->setPaymentDescriptor($paymentPreference['paymentDescriptor']);
  79.                 $filtered $event->getPage()->getPaymentMethods()->filter(function ($paymentMethod) {
  80.                     return $paymentMethod->getId() === PaymentMethodInstaller::AMAZON_PAYMENT_ID;
  81.                 });
  82.                 $amazonPayPaymentMethod $event->getPage()->getPaymentMethods()->get(PaymentMethodInstaller::AMAZON_PAYMENT_ID);
  83.                 $translations $amazonPayPaymentMethod->getTranslated();
  84.                 $translations['description'] = $paymentPreference['paymentDescriptor'];
  85.                 $amazonPayPaymentMethod->setTranslated($translations);
  86.                 break;
  87.             }
  88.         }
  89.         $confirmExtension->setIsOneClickCheckout($isOneClickCheckout);
  90.         $event->getPage()->addExtension(AmazonPayConfirmExtension::EXTENSION_NAME$confirmExtension);
  91.     }
  92.     private function removeOtherPaymentOptions(CheckoutConfirmPageLoadedEvent $event)
  93.     {
  94.         $filtered $event->getPage()->getPaymentMethods()->filter(function ($paymentMethod) {
  95.             return $paymentMethod->getId() === PaymentMethodInstaller::AMAZON_PAYMENT_ID;
  96.         });
  97.         if ($filtered->count() > 0) {
  98.             $event->getPage()->setPaymentMethods($filtered);
  99.         }
  100.     }
  101. }