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

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