diff --git a/lib/SellingPartnerOAuth.php b/lib/SellingPartnerOAuth.php index 5d8aace1..e43c7362 100644 --- a/lib/SellingPartnerOAuth.php +++ b/lib/SellingPartnerOAuth.php @@ -39,4 +39,42 @@ public static function getAccessTokenFromRefreshToken($refreshToken, $clientId, return $bodyAsJson['access_token']; } + + /** + * @param string $lwaAuthorizationCode + * @param string $clientId + * @param string $clientSecret + * @param string $redirectUri + * @return string|null + * @throws GuzzleException + */ + public static function getRefreshTokenFromLwaAuthorizationCode( + string $lwaAuthorizationCode, + string $clientId, + string $clientSecret, + string $redirectUri + ): ?string { + $client = new Client(); + $params = [ + 'grant_type' => 'authorization_code', + 'client_id' => $clientId, + 'client_secret' => $clientSecret, + 'code' => $lwaAuthorizationCode, + 'redirect_uri' => $redirectUri, + ]; + $options = array_merge([ + RequestOptions::HEADERS => ['Accept' => 'application/json'], + RequestOptions::HTTP_ERRORS => false, + 'curl' => [ + CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, + ], + ], $params ? [RequestOptions::FORM_PARAMS => $params] : []); + + $response = $client->request('POST', 'https://api.amazon.com/auth/o2/token', $options); + + $body = $response->getBody()->getContents(); + $bodyAsJson = json_decode($body, true); + + return $bodyAsJson['refresh_token']; + } }