From b9b26e7abdbf8f1ec83bb8d1afc026e9eddf9c52 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Wed, 14 Feb 2024 15:10:50 +0100 Subject: [PATCH] MOL-245: setup a timer --- .../PaypalExpress/PayPalExpress.php | 26 ++++++++++++++++--- .../PaypalExpressControllerBase.php | 17 +++++------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Components/PaypalExpress/PayPalExpress.php b/src/Components/PaypalExpress/PayPalExpress.php index f1b0c67dd..d246847c6 100644 --- a/src/Components/PaypalExpress/PayPalExpress.php +++ b/src/Components/PaypalExpress/PayPalExpress.php @@ -18,6 +18,15 @@ class PayPalExpress { + /** + * define how often we ask the session until we get the shipping address + */ + private const SESSION_MAX_RETRY = 5; + + /** + * define how long we will wait for the session response + */ + private const SESSION_BASE_TIMEOUT = 2000; /** * @var PaymentMethodRepository */ @@ -122,10 +131,21 @@ public function startSession(Cart $cart, SalesChannelContext $context): Session public function loadSession(string $sessionId, SalesChannelContext $context): Session { - //we need to wait here 2 seconds, until mollie get the data from paypal api - usleep(2000); $mollie = $this->mollieApiFactory->getClient($context->getSalesChannelId()); - return $mollie->sessions->get($sessionId); + /** + * if we load the session from mollie api, we dont get the shipping address at first time. usually it takes several seconds until the data from paypal is transfered to mollie + * so we try to load the session at least 5 times with increased waiting time. + */ + for ($i = 1; $i <= self::SESSION_MAX_RETRY; $i++) { + $sleepTimer = self::SESSION_BASE_TIMEOUT * $i; + usleep($sleepTimer); + $session = $mollie->sessions->get($sessionId); + if ($session->shippingAddress !== null) { + break; + } + } + + return $session; } diff --git a/src/Controller/Storefront/PaypalExpress/PaypalExpressControllerBase.php b/src/Controller/Storefront/PaypalExpress/PaypalExpressControllerBase.php index d45f158b0..5d7af01cc 100644 --- a/src/Controller/Storefront/PaypalExpress/PaypalExpressControllerBase.php +++ b/src/Controller/Storefront/PaypalExpress/PaypalExpressControllerBase.php @@ -83,7 +83,6 @@ public function startCheckout(Request $request, SalesChannelContext $context): R if ($oldSessionId !== null) { $session = $this->paypalExpress->loadSession($oldSessionId, $context); - ; } else { $session = $this->paypalExpress->startSession($cart, $context); @@ -134,16 +133,12 @@ public function finishCheckout(SalesChannelContext $context): Response $payPalExpressSession = $this->paypalExpress->loadSession($payPalExpressSessionId, $context); if ($payPalExpressSession->shippingAddress === null) { - # try 2nd time, to reduce redirects and customer complains - $payPalExpressSession = $this->paypalExpress->loadSession($payPalExpressSessionId, $context); - if ($payPalExpressSession->shippingAddress === null) { - $this->logger->error('Failed to finish checkout, got session without shipping address', [ - 'sessionId' => $payPalExpressSession->id, - 'status' => $payPalExpressSession->status - ]); - - return new RedirectResponse($returnUrl); - } + $this->logger->error('Failed to finish checkout, got session without shipping address', [ + 'sessionId' => $payPalExpressSession->id, + 'status' => $payPalExpressSession->status + ]); + + return new RedirectResponse($returnUrl); } $shippingAddress = AddressStruct::createFromApiResponse($payPalExpressSession->shippingAddress);