1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
6
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
7
|
|
|
use OAuth\Common\Http\Uri\Uri; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* PayPal service. |
11
|
|
|
* |
12
|
|
|
* @author Flávio Heleno <[email protected]> |
13
|
|
|
* @link https://developer.paypal.com/webapps/developer/docs/integration/direct/log-in-with-paypal/detailed/ |
14
|
|
|
*/ |
15
|
|
|
class Paypal extends AbstractService |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Defined scopes |
19
|
|
|
* @link https://developer.paypal.com/webapps/developer/docs/integration/direct/log-in-with-paypal/detailed/ |
20
|
|
|
* @see #attributes |
21
|
|
|
*/ |
22
|
|
|
const SCOPE_OPENID = 'openid'; |
23
|
|
|
const SCOPE_PROFILE = 'profile'; |
24
|
|
|
const SCOPE_PAYPALATTRIBUTES = 'https://uri.paypal.com/services/paypalattributes'; |
25
|
|
|
const SCOPE_EMAIL = 'email'; |
26
|
|
|
const SCOPE_ADDRESS = 'address'; |
27
|
|
|
const SCOPE_PHONE = 'phone'; |
28
|
|
|
const SCOPE_EXPRESSCHECKOUT = 'https://uri.paypal.com/services/expresscheckout'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function init() |
34
|
|
|
{ |
35
|
|
|
if( $this->baseApiUri === null ) { |
|
|
|
|
36
|
|
|
$this->baseApiUri = new Uri('https://api.paypal.com/v1/'); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function getAuthorizationEndpoint() |
44
|
|
|
{ |
45
|
|
|
return new Uri('https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getAccessTokenEndpoint() |
52
|
|
|
{ |
53
|
|
|
return new Uri('https://api.paypal.com/v1/identity/openidconnect/tokenservice'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
protected function getAuthorizationMethod() |
60
|
|
|
{ |
61
|
|
|
return static::AUTHORIZATION_METHOD_HEADER_BEARER; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
protected function parseAccessTokenResponse($responseBody) |
68
|
|
|
{ |
69
|
|
|
$data = json_decode($responseBody, true); |
70
|
|
|
|
71
|
|
|
if (null === $data || !is_array($data)) { |
72
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
73
|
|
|
} elseif (isset($data['message'])) { |
74
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['message'] . '"'); |
75
|
|
|
} elseif (isset($data['name'])) { |
76
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['name'] . '"'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$token = new StdOAuth2Token(); |
80
|
|
|
$token->setAccessToken($data['access_token']); |
81
|
|
|
$token->setLifeTime($data['expires_in']); |
82
|
|
|
|
83
|
|
|
if (isset($data['refresh_token'])) { |
84
|
|
|
$token->setRefreshToken($data['refresh_token']); |
85
|
|
|
unset($data['refresh_token']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
unset($data['access_token']); |
89
|
|
|
unset($data['expires_in']); |
90
|
|
|
|
91
|
|
|
$token->setExtraParams($data); |
92
|
|
|
|
93
|
|
|
return $token; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|