Skip to content

Commit

Permalink
MOL-245: setup a timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Feb 14, 2024
1 parent 21d29dc commit b9b26e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
26 changes: 23 additions & 3 deletions src/Components/PaypalExpress/PayPalExpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b9b26e7

Please sign in to comment.