Skip to content

Commit

Permalink
fix: remove refresh access token if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyDeHaas committed Oct 8, 2024
1 parent 937c98c commit 8370151
Showing 1 changed file with 25 additions and 41 deletions.
66 changes: 25 additions & 41 deletions src/Service/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PlugAndPay\Sdk\Contract\ClientInterface;
use PlugAndPay\Sdk\Entity\Response;
use PlugAndPay\Sdk\Exception\ExceptionFactory;
use PlugAndPay\Sdk\Exception\InvalidTokenException;
use PlugAndPay\Sdk\Exception\NotFoundException;
use PlugAndPay\Sdk\Exception\ValidationException;
use PlugAndPay\Sdk\Support\Str;
Expand All @@ -23,9 +22,9 @@
class Client implements ClientInterface
{
private const METHOD_DELETE = 'DELETE';
private const METHOD_GET = 'GET';
private const METHOD_PATCH = 'PATCH';
private const METHOD_POST = 'POST';
private const METHOD_GET = 'GET';
private const METHOD_PATCH = 'PATCH';
private const METHOD_POST = 'POST';

private const BASE_API_URL_PRODUCTION = 'https://api.plugandpay.nl';

Expand All @@ -40,13 +39,14 @@ class Client implements ClientInterface
private TokenService $tokenService;

public function __construct(
?string $accessToken = null,
?string $refreshToken = null,
string $baseUrl = null,
?int $clientId = null,
?string $accessToken = null,
?string $refreshToken = null,
string $baseUrl = null,
?int $clientId = null,
?GuzzleClient $guzzleClient = null,
TokenService $tokenService = null
) {
TokenService $tokenService = null
)
{
$this->baseUrl = $baseUrl ?? self::BASE_API_URL_PRODUCTION;
$this->accessToken = $accessToken;
$this->refreshToken = $refreshToken;
Expand All @@ -56,10 +56,11 @@ public function __construct(
}

private function createGuzzleClient(
string $baseUrl,
?string $accessToken,
string $baseUrl,
?string $accessToken,
?GuzzleClient $guzzleClient
): void {
): void
{
$headers = ['Accept' => 'application/json'];

if ($accessToken) {
Expand Down Expand Up @@ -218,33 +219,6 @@ public function getCredentials(string $code, string $codeVerifier, string $redir
return $this->fromGuzzleResponse($response);
}

/**
* @throws GuzzleException
* @throws NotFoundException
* @throws ValidationException
* @throws JsonException
* @throws InvalidTokenException
*/
public function refreshAccessTokenIfNeeded(): void
{
if ($this->tokenService->isValid($this->accessToken)) {
return;
}

$response = $this->refreshAccessToken($this->refreshToken, $this->clientId);

$responseData = $response->body();

// Update the Guzzle client with the new access token
$this->createGuzzleClient(
$this->baseUrl,
$responseData['access_token'],
$this->guzzleClient
);

$this->accessToken = $responseData['access_token'];
}

/**
* Exchange refresh token for a new access token.
*
Expand All @@ -261,6 +235,16 @@ public function refreshAccessToken(string $refreshToken, int $clientId): Respons
'refresh_token' => $refreshToken,
]);

return $this->fromGuzzleResponse($response);
$guzzleResponse = $this->fromGuzzleResponse($response);
$responseData = $guzzleResponse->body();

// Update the Guzzle client with the new access token
$this->createGuzzleClient(
$this->baseUrl,
$responseData['access_token'],
$this->guzzleClient
);

return $guzzleResponse;
}
}

0 comments on commit 8370151

Please sign in to comment.