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

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 Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Swag\AmazonPay\Installer\PaymentMethodInstaller;
  11. use Swag\AmazonPay\Storefront\Page\Extension\AmazonPayConfirmExtension;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. class AmazonPayConfirmEventListener implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var SessionInterface
  18.      */
  19.     private $session;
  20.     public function __construct(SessionInterface $session)
  21.     {
  22.         $this->session $session;
  23.     }
  24.     /**
  25.      * {@inheritdoc}
  26.      */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             CheckoutConfirmPageLoadedEvent::class => 'onLoadConfirmPage',
  31.         ];
  32.     }
  33.     public function onLoadConfirmPage(CheckoutConfirmPageLoadedEvent $event): void
  34.     {
  35.         // Only for AmazonPay!
  36.         if ($event->getSalesChannelContext()->getPaymentMethod()->getId() !== PaymentMethodInstaller::AMAZON_PAYMENT_ID) {
  37.             // Remove the "on hold" checkout session id from the session just to clear things up.
  38.             // Will be automatically set again if Amazon Pay is active
  39.             $this->session->remove('swag-amazon-pay.checkout-session-id');
  40.             return;
  41.         }
  42.         // Use URL param for checkout determination first and fallback to the value inside the session.
  43.         $checkoutSessionId = (string) $event->getRequest()->query->get('amazonPayCheckoutId'$this->session->get('swag-amazon-pay.checkout-session-id'));
  44.         $isOneClickCheckout $event->getRequest()->query->getBoolean('oneClickCheckout');
  45.         // No session id but one click checkout requested?
  46.         if (!$checkoutSessionId && $isOneClickCheckout) {
  47.             return;
  48.         }
  49.         // 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.
  50.         if ($isOneClickCheckout) {
  51.             $this->session->set('swag-amazon-pay.checkout-session-id'$checkoutSessionId);
  52.             $this->addConfirmPageExtension($event$checkoutSessionIdtrue);
  53.             return;
  54.         }
  55.         // Reset from One-Click checkout to the regular Amazon Pay checkout
  56.         $this->session->remove('swag-amazon-pay.checkout-session-id');
  57.     }
  58.     private function addConfirmPageExtension(
  59.         CheckoutConfirmPageLoadedEvent $event,
  60.         ?string $checkoutSessionId,
  61.         bool $isOneClickCheckout
  62.     ): void {
  63.         $confirmExtension = new AmazonPayConfirmExtension();
  64.         $confirmExtension->setCheckoutSessionId($checkoutSessionId);
  65.         $confirmExtension->setIsOneClickCheckout($isOneClickCheckout);
  66.         $event->getPage()->addExtension(AmazonPayConfirmExtension::EXTENSION_NAME$confirmExtension);
  67.     }
  68. }