Skip to content

Commit

Permalink
fix: paypal 获取用户数据失败 (#278)
Browse files Browse the repository at this point in the history
* fix: paypal 获取用户数据失败

* fix: paypal 生产api地址
  • Loading branch information
izhiqiang authored Jun 28, 2024
1 parent af51102 commit de873da
Showing 1 changed file with 9 additions and 35 deletions.
44 changes: 9 additions & 35 deletions src/Providers/PayPal.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class PayPal extends Base

protected string $authUrl = 'https://www.paypal.com/signin/authorize';

protected string $tokenURL = 'https://api.sandbox.paypal.com/v1/oauth2/token';
protected string $tokenURL = 'https://api-m.paypal.com/v1/oauth2/token';

protected string $userinfoURL = 'https://api.paypal.com/v1/identity/openidconnect/userinfo';
protected string $userinfoURL = 'https://api.paypal.com/v1/identity/openidconnect/userinfo?schema=openid';

protected array $scopes = [
'openid', 'profile', 'email', 'address',
Expand All @@ -42,11 +42,11 @@ class PayPal extends Base
public function __construct(array $config)
{
parent::__construct($config);
$this->sandbox = (bool) $this->config->get('sandbox', false);
$this->sandbox = (bool)$this->config->get('sandbox', false);
if ($this->sandbox) {
$this->authUrl = 'https://www.sandbox.paypal.com/signin/authorize';
$this->tokenURL = 'https://api-m.sandbox.paypal.com/v1/oauth2/token';
$this->userinfoURL = 'https://api-m.sandbox.paypal.com/v1/identity/openidconnect/userinfo';
$this->userinfoURL = 'https://api-m.sandbox.paypal.com/v1/identity/openidconnect/userinfo?schema=openid';
}
}

Expand Down Expand Up @@ -97,49 +97,24 @@ protected function getTokenUrl(): string
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
*
* @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/#link-getaccesstoken
*
*/
public function tokenFromCode(string $code): array
{
$response = $this->getHttpClient()->post(
$this->getTokenUrl(),
[
'form_params' => [
Contracts\RFC6749_ABNF_GRANT_TYPE => Contracts\RFC6749_ABNF_AUTHORATION_CODE,
Contracts\RFC6749_ABNF_GRANT_TYPE => Contracts\RFC6749_ABNF_CLIENT_CREDENTIALS,
Contracts\RFC6749_ABNF_CODE => $code,
],
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Basic '.\base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())),
],
]
);

return $this->normalizeAccessTokenResponse((string) $response->getBody());
}

/**
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
*
* @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/#link-exchangerefreshtokenforaccesstoken
*/
public function refreshToken(string $refreshToken): mixed
{
$response = $this->getHttpClient()->post(
$this->getTokenUrl(),
[
'form_params' => [
Contracts\RFC6749_ABNF_GRANT_TYPE => Contracts\RFC6749_ABNF_REFRESH_TOKEN,
Contracts\RFC6749_ABNF_REFRESH_TOKEN => $refreshToken,
],
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Basic '.\base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())),
'Authorization' => 'Basic ' . \base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())),
],
]
);

return $this->normalizeAccessTokenResponse((string) $response->getBody());
return $this->normalizeAccessTokenResponse((string)$response->getBody());
}

/**
Expand All @@ -154,11 +129,10 @@ protected function getUserByToken(string $token): array
[
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer '.$token,
'Authorization' => 'Bearer ' . $token,
],
]
);

return $this->fromJsonBody($response);
}

Expand Down

0 comments on commit de873da

Please sign in to comment.