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

  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 ClientProvider $clientProvider;
  15. public function __construct(ClientProvider $clientProvider)
  16. {
  17. $this->clientProvider = $clientProvider;
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function getSubscribedEvents(): array
  23. {
  24. return [
  25. CheckoutConfirmPageLoadedEvent::class => 'onLoadConfirmPage',
  26. ];
  27. }
  28. public function onLoadConfirmPage(CheckoutConfirmPageLoadedEvent $event): void
  29. {
  30. $session = $event->getRequest()->getSession();
  31. // Only for AmazonPay!
  32. if ($event->getSalesChannelContext()->getPaymentMethod()->getId() !== PaymentMethodInstaller::AMAZON_PAYMENT_ID && $event->getRequest()->get('amazonPayAction') === 'reset') {
  33. // Remove the "on hold" checkout session id from the session just to clear things up.
  34. // Will be automatically set again if Amazon Pay is active
  35. $session->remove('swag-amazon-pay.checkout-session-id');
  36. return;
  37. }
  38. // Use URL param for checkout determination first and fallback to the value inside the session.
  39. $checkoutSessionId = (string)$event->getRequest()->query->get('amazonPayCheckoutId', $session->get('swag-amazon-pay.checkout-session-id'));
  40. $isOneClickCheckout = $event->getRequest()->query->getBoolean('oneClickCheckout');
  41. // No session id but one click checkout requested?
  42. if (!$checkoutSessionId && $isOneClickCheckout) {
  43. return;
  44. }
  45. // 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.
  46. if ($isOneClickCheckout) {
  47. $checkoutSession = $this->clientProvider->getClient($event->getSalesChannelContext()->getSalesChannelId())->getCheckoutSession($checkoutSessionId);
  48. //TODO handle wrong status
  49. $session->set('swag-amazon-pay.checkout-session-id', $checkoutSessionId);
  50. $this->addConfirmPageExtension($event, $checkoutSession);
  51. $this->removeOtherPaymentOptions($event);
  52. return;
  53. }
  54. // Reset from One-Click checkout to the regular Amazon Pay checkout
  55. $session->remove('swag-amazon-pay.checkout-session-id');
  56. }
  57. private function addConfirmPageExtension(
  58. CheckoutConfirmPageLoadedEvent $event,
  59. CheckoutSession $checkoutSession
  60. ): void
  61. {
  62. $confirmExtension = new AmazonPayConfirmExtension();
  63. $confirmExtension->setCheckoutSessionId($checkoutSession->getCheckoutSessionId());
  64. foreach ($checkoutSession->getPaymentPreferences() as $paymentPreference) {
  65. if (is_array($paymentPreference) && isset($paymentPreference['paymentDescriptor'])) {
  66. $confirmExtension->setPaymentDescriptor($paymentPreference['paymentDescriptor']);
  67. $amazonPayPaymentMethod = $event->getPage()->getPaymentMethods()->get(PaymentMethodInstaller::AMAZON_PAYMENT_ID);
  68. if ($amazonPayPaymentMethod) {
  69. $translations = $amazonPayPaymentMethod->getTranslated();
  70. $translations['description'] = $paymentPreference['paymentDescriptor'];
  71. $amazonPayPaymentMethod->setTranslated($translations);
  72. }
  73. break;
  74. }
  75. }
  76. $confirmExtension->setIsOneClickCheckout(true);
  77. $event->getPage()->addExtension(AmazonPayConfirmExtension::EXTENSION_NAME, $confirmExtension);
  78. }
  79. private function removeOtherPaymentOptions(CheckoutConfirmPageLoadedEvent $event): void
  80. {
  81. $filtered = $event->getPage()->getPaymentMethods()->filter(function ($paymentMethod) {
  82. return $paymentMethod->getId() === PaymentMethodInstaller::AMAZON_PAYMENT_ID;
  83. });
  84. if ($filtered->count() > 0) {
  85. $event->getPage()->setPaymentMethods($filtered);
  86. }
  87. }
  88. }