8
8
9
9
/**
10
10
* Class PayPal
11
+ *
11
12
* @author zhiqiang
12
- * @package Overtrue\Socialite\Providers
13
+ *
13
14
* @see https://developer.paypal.com/docs/log-in-with-paypal/
14
15
*/
15
16
class PayPal extends Base
16
17
{
17
18
public const NAME = 'paypal ' ;
18
19
19
20
protected string $ scopeSeparator = ' ' ;
21
+
20
22
/**
21
23
* @var string|null
22
- * code or id_token
24
+ * code or id_token
23
25
*/
24
26
protected ?string $ responseType = Contracts \RFC6749_ABNF_CODE ;
25
- protected string $ flowEntry = 'static ' ;
26
27
27
- protected string $ authUrl = 'https://www.paypal.com/signin/authorize ' ;
28
- protected string $ tokenURL = "https://api.sandbox.paypal.com/v1/oauth2/token " ;
29
- protected string $ userinfoURL = "https://api.paypal.com/v1/identity/openidconnect/userinfo " ;
28
+ protected string $ flowEntry = 'static ' ;
29
+
30
+ protected string $ authUrl = 'https://www.paypal.com/signin/authorize ' ;
31
+
32
+ protected string $ tokenURL = 'https://api.sandbox.paypal.com/v1/oauth2/token ' ;
33
+
34
+ protected string $ userinfoURL = 'https://api.paypal.com/v1/identity/openidconnect/userinfo ' ;
30
35
31
36
protected array $ scopes = [
32
- 'openid ' , 'profile ' , 'email ' , 'address '
37
+ 'openid ' , 'profile ' , 'email ' , 'address ' ,
33
38
];
34
39
35
40
protected bool $ sandbox = true ;
36
41
37
42
public function __construct (array $ config )
38
43
{
39
44
parent ::__construct ($ config );
40
- $ this ->sandbox = (bool )$ this ->config ->get ('sandbox ' , false );
45
+ $ this ->sandbox = (bool ) $ this ->config ->get ('sandbox ' , false );
41
46
if ($ this ->sandbox ) {
42
- $ this ->authUrl = 'https://www.sandbox.paypal.com/signin/authorize ' ;
43
- $ this ->tokenURL = 'https://api-m.sandbox.paypal.com/v1/oauth2/token ' ;
47
+ $ this ->authUrl = 'https://www.sandbox.paypal.com/signin/authorize ' ;
48
+ $ this ->tokenURL = 'https://api-m.sandbox.paypal.com/v1/oauth2/token ' ;
44
49
$ this ->userinfoURL = 'https://api-m.sandbox.paypal.com/v1/identity/openidconnect/userinfo ' ;
45
50
}
46
51
}
47
52
48
53
/**
49
- * @param string|null $responseType
50
54
* @return $this
55
+ *
51
56
* @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/generate-button/
52
57
*/
53
58
public function withResponseType (?string $ responseType )
54
59
{
55
60
$ this ->responseType = $ responseType ;
61
+
56
62
return $ this ;
57
63
}
58
64
@@ -65,32 +71,31 @@ protected function getCodeFields(): array
65
71
{
66
72
$ fields = \array_merge (
67
73
[
68
- 'flowEntry ' => $ this ->flowEntry ,
69
- Contracts \RFC6749_ABNF_CLIENT_ID => $ this ->getClientId (),
74
+ 'flowEntry ' => $ this ->flowEntry ,
75
+ Contracts \RFC6749_ABNF_CLIENT_ID => $ this ->getClientId (),
70
76
Contracts \RFC6749_ABNF_RESPONSE_TYPE => $ this ->responseType ,
71
- Contracts \RFC6749_ABNF_SCOPE => $ this ->formatScopes ($ this ->scopes , $ this ->scopeSeparator ),
72
- Contracts \RFC6749_ABNF_REDIRECT_URI => $ this ->redirectUrl ,
77
+ Contracts \RFC6749_ABNF_SCOPE => $ this ->formatScopes ($ this ->scopes , $ this ->scopeSeparator ),
78
+ Contracts \RFC6749_ABNF_REDIRECT_URI => $ this ->redirectUrl ,
73
79
],
74
80
$ this ->parameters
75
81
);
76
82
77
83
if ($ this ->state ) {
78
84
$ fields [Contracts \RFC6749_ABNF_STATE ] = $ this ->state ;
79
85
}
86
+
80
87
return $ fields ;
81
88
}
82
89
83
-
84
90
protected function getTokenUrl (): string
85
91
{
86
92
return $ this ->tokenURL ;
87
93
}
88
94
89
95
/**
90
- * @param string $code
91
- * @return array
92
96
* @throws \GuzzleHttp\Exception\GuzzleException
93
97
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
98
+ *
94
99
* @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/#link-getaccesstoken
95
100
*/
96
101
public function tokenFromCode (string $ code ): array
@@ -100,46 +105,46 @@ public function tokenFromCode(string $code): array
100
105
[
101
106
'form_params ' => [
102
107
Contracts \RFC6749_ABNF_GRANT_TYPE => Contracts \RFC6749_ABNF_AUTHORATION_CODE ,
103
- Contracts \RFC6749_ABNF_CODE => $ code ,
108
+ Contracts \RFC6749_ABNF_CODE => $ code ,
104
109
],
105
- 'headers ' => [
106
- 'Accept ' => 'application/json ' ,
107
- 'Authorization ' => 'Basic ' . \base64_encode (\sprintf ('%s:%s ' , $ this ->getClientId (), $ this ->getClientSecret ())),
110
+ 'headers ' => [
111
+ 'Accept ' => 'application/json ' ,
112
+ 'Authorization ' => 'Basic ' . \base64_encode (\sprintf ('%s:%s ' , $ this ->getClientId (), $ this ->getClientSecret ())),
108
113
],
109
114
]
110
115
);
111
- return $ this ->normalizeAccessTokenResponse ((string )$ response ->getBody ());
116
+
117
+ return $ this ->normalizeAccessTokenResponse ((string ) $ response ->getBody ());
112
118
}
113
119
114
120
/**
115
- * @param string $refreshToken
116
- * @return mixed
117
121
* @throws \GuzzleHttp\Exception\GuzzleException
118
122
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
123
+ *
119
124
* @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/#link-exchangerefreshtokenforaccesstoken
120
125
*/
121
126
public function refreshToken (string $ refreshToken ): mixed
122
127
{
123
- $ response = $ this ->getHttpClient ()->post (
128
+ $ response = $ this ->getHttpClient ()->post (
124
129
$ this ->getTokenUrl (),
125
130
[
126
131
'form_params ' => [
127
- Contracts \RFC6749_ABNF_GRANT_TYPE => Contracts \RFC6749_ABNF_REFRESH_TOKEN ,
132
+ Contracts \RFC6749_ABNF_GRANT_TYPE => Contracts \RFC6749_ABNF_REFRESH_TOKEN ,
128
133
Contracts \RFC6749_ABNF_REFRESH_TOKEN => $ refreshToken ,
129
134
],
130
- 'headers ' => [
131
- 'Accept ' => 'application/json ' ,
132
- 'Authorization ' => 'Basic ' . \base64_encode (\sprintf ('%s:%s ' , $ this ->getClientId (), $ this ->getClientSecret ())),
135
+ 'headers ' => [
136
+ 'Accept ' => 'application/json ' ,
137
+ 'Authorization ' => 'Basic ' . \base64_encode (\sprintf ('%s:%s ' , $ this ->getClientId (), $ this ->getClientSecret ())),
133
138
],
134
139
]
135
140
);
136
- return $ this ->normalizeAccessTokenResponse ((string )$ response ->getBody ());
141
+
142
+ return $ this ->normalizeAccessTokenResponse ((string ) $ response ->getBody ());
137
143
}
138
144
139
145
/**
140
- * @param string $token
141
- * @return array
142
146
* @throws \GuzzleHttp\Exception\GuzzleException
147
+ *
143
148
* @see https://developer.paypal.com/docs/api/identity/v1/#userinfo_get
144
149
*/
145
150
protected function getUserByToken (string $ token ): array
@@ -148,22 +153,24 @@ protected function getUserByToken(string $token): array
148
153
$ this ->userinfoURL ,
149
154
[
150
155
'headers ' => [
151
- 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
152
- 'Authorization ' => 'Bearer ' . $ token ,
156
+ 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
157
+ 'Authorization ' => 'Bearer ' . $ token ,
153
158
],
154
159
]
155
160
);
161
+
156
162
return $ this ->fromJsonBody ($ response );
157
163
}
158
164
159
165
#[Pure]
160
166
protected function mapUserToObject (array $ user ): Contracts \UserInterface
161
167
{
162
- $ user [Contracts \ABNF_ID ] = $ user ['user_id ' ] ?? null ;
168
+ $ user [Contracts \ABNF_ID ] = $ user ['user_id ' ] ?? null ;
163
169
$ user [Contracts \ABNF_NICKNAME ] = $ user ['given_name ' ] ?? $ user ['family_name ' ] ?? $ user ['middle_name ' ] ?? null ;
164
- $ user [Contracts \ABNF_NAME ] = $ user ['name ' ] ?? '' ;
165
- $ user [Contracts \ABNF_EMAIL ] = $ user [Contracts \ABNF_EMAIL ] ?? null ;
166
- $ user [Contracts \ABNF_AVATAR ] = $ user ['picture ' ] ?? null ;
170
+ $ user [Contracts \ABNF_NAME ] = $ user ['name ' ] ?? '' ;
171
+ $ user [Contracts \ABNF_EMAIL ] = $ user [Contracts \ABNF_EMAIL ] ?? null ;
172
+ $ user [Contracts \ABNF_AVATAR ] = $ user ['picture ' ] ?? null ;
173
+
167
174
return new User ($ user );
168
175
}
169
176
}
0 commit comments