-
Notifications
You must be signed in to change notification settings - Fork 12
/
StripeOAuth.class.php
408 lines (381 loc) · 13 KB
/
StripeOAuth.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
// include the Stripe version of the OAuth2 Client
require_once('StripeOAuth2Client.class.php');
/**
* StripeOAuth
*
* Library which helps perform an OAuth2 flow for Stripe, which is used when
* creating a Stripe application.
*
* Stripe applications allow merchants (with their own Stripe account) to
* give you permission to receive payments on their behalf.
*
* @see <https://github.com/quizlet/oauth2-php>
* @see <https://stripe.com/docs/apps/reference>
* @see <https://code.google.com/p/oauth/>
* @thanks Eric Ma <[email protected]>
* @author Oliver Nassar <[email protected]>
* @example
* <code>
* // Redirect Logic
* $oauth = (new StripeOAuth(
* 'ca_********************************',
* 'sk_*****************************'
* ));
* $url = $oauth->getAuthorizeUri();
* header('Location: ' . ($url));
* exit(0);
*
* // Callback: Access token and Publisher Key
* $oauth = (new StripeOAuth(
* 'ca_********************************',
* 'sk_*****************************'
* ));
* $token = $oauth->getAccessToken($_GET['code']);
* $key = $oauth->getPublishableKey($_GET['code']);
* echo 'Access token: ' . ($token) . '<br />Key: ' . ($key);
* exit(0);
* <code>
*/
class StripeOAuth
{
/**
* _body
*
* The response body, in json form, of the access-token request. OAuth2
* doesn't seem to generally provide access to this, so it's extended
* through the <StripeOAuth2Client> class to provide that helper.
*
* @var string
* @access private
*/
private $_body;
/**
* _client
*
* A reference to the <OAuth2Client> instance that Stripe is
* authenticating through.
*
* @var OAuth2Client
* @access private
*/
private $_client;
/**
* _cid
*
* The Client ID of the Stripe application that is initiating the OAuth2
* connection.
*
* @var string
* @access private
*/
private $_cid;
/**
* _endpoints
*
* The URI endpoints for the Stripe connection flow.
*
* @var array
* @access private
*/
private $_endpoints = array(
'base' => 'https://connect.stripe.com',
'access' => '/oauth/authorize',
'token' => '/oauth/token'
);
/**
* _secret
*
* The secret key of the account that the application was registered
* under.
*
* @var string
* @access private
*/
private $_secret;
/**
* _tokens
*
* An array of tokens (access and refresh) that were retrieved in the
* connection flow.
*
* @var array (default: array());
* @access private
*/
private $_tokens = array();
/**
* __construct
*
* Sets the Client ID of the application and the secret of the account
* that the application is registered under.
*
* @access public
* @param String $cid The client ID (testing or live) of the
* application. Can be found in the *Applications* section of
* your Stripe account's *Account Settings*
* @param String $secret The secret (either testing or live) for the
* *account* that the application was created through
* @return void
*/
public function __construct($cid, $secret)
{
$this->_cid = $cid;
$this->_secret = $secret;
}
/**
* _getClient
*
* Creates and returns the <OAuth2Client> resource that processes the
* connection flow. If it's already been created, a reference to it is
* returned (rather than re-created).
*
* @access private
* @return OAuth2Client
*/
private function _getClient()
{
if (!isset($this->_client)) {
$this->_client = (new StripeOAuth2Client(array(
'base_uri' => $this->_endpoints['base'],
'client_id' => $this->_cid,
'client_secret' => $this->_secret,
'access_token_uri' => $this->_endpoints['token']
)));
}
return $this->_client;
}
/**
* getAccessToken
*
* Generates and returns the access token for the connection flow. Also
* allows for the access token generation to be based off of a refresh
* token if <refresh> is set to <true>.
*
* By default, access tokens are retrieved using an authorization code,
* rather than a refresh token.
*
* @access public
* @param String $code
* @param Boolean $refresh (default: false)
* @return String
*/
public function getAccessToken($code, $refresh = false)
{
$tokens = $this->getTokens($code, $refresh);
return $tokens['access'];
}
/**
* getAuthorizeUri
*
* Returns the endpoint-URI that a user should be redirected to in order
* to authorize an application to act on their behalf.
*
* This is just part of the flow, however, as after they have authorized
* the application, an access token still needs to be generated and
* retrieved on their behalf.
*
* @todo include <state> parameter for CSRF consideration
* @todo add ability to specify default values, as listed below
* @access public
* @param Array $params (default: array())
* @param String $scope (default: read_write)
* @return String
*/
public function getAuthorizeUri(
$params = array(),
$scope = 'read_write'
) {
/*
Default values:
stripe_user[email]: the user's email address
stripe_user[url]: the URL where Stripe will be used, often on your own website
stripe_user[phone_number]: the business phone number
stripe_user[business_name]: the legal name of the business
stripe_user[first_name]: first name of the person applying
stripe_user[last_name]: last name of the person applying
stripe_user[street_address]: street address of the person applying
stripe_user[city]: city of the person applying
stripe_user[state]: two digit state code as a string
stripe_user[zip]: five digit zip code as a string
stripe_user[physical_product]: true if the user sells a physical product, false otherwise
*/
// default params to send a long
$defaults = array(
'response_type' => 'code',
'client_id' => ($this->_cid),
'scope' => $scope
);
// merge with passed in
$params = array_merge($defaults, $params);
// build the url
$base = $this->_endpoints['base'];
$access = $this->_endpoints['access'];
$query = http_build_query($params);
return ($base) . ($access) . '?' . ($query);
}
/**
* getBody
*
* @access public
* @return Array
*/
public function getBody()
{
if (empty($this->_tokens)) {
throw new Exception(
'Access token hasn\'t been generated yet.'
);
}
return $this->_body;
}
/**
* getPublishableKey
*
* @access public
* @return String
*/
public function getPublishableKey()
{
if (empty($this->_tokens)) {
throw new Exception(
'Access token hasn\'t been generated yet.'
);
}
return $this->_body->stripe_publishable_key;
}
/**
* getRefreshToken
*
* Generates and returns the refresh token for the connection flow. Also
* allows for the refresh token generation to be based off of a refresh
* token itself, if <refresh> is set to <true>.
*
* By default, refresh tokens are retrieved using an authorization code,
* rather than a refresh token.
*
* @access public
* @param String $code
* @param Boolean $refresh (default: false)
* @return String
*/
public function getRefreshToken($code, $refresh = true)
{
$tokens = $this->getTokens($code, $refresh);
return $tokens['refresh'];
}
/**
* getScope
*
* @access public
* @return String
*/
public function getScope()
{
if (empty($this->_tokens)) {
throw new Exception(
'Access token hasn\'t been generated yet.'
);
}
return $this->_body->scope;
}
/**
* getTokens
*
* Retrieves and stores the access and refresh tokens for the OAuth
* connection, depending on whether it's an authorization code being
* passed in, or a refresh token.
*
* This is distinguished via the the <refresh> boolean set to true.
*
* @access public
* @param String $code
* @param Boolean $refresh (default: false)
* @return Array
*/
public function getTokens($code, $refresh = false)
{
// if tokens haven't been stored yet
if (empty($this->_tokens)) {
// retrieve OAuth2 resource
$client = $this->_getClient();
/**
* Set the grant type and code depending on whether the code
* being supplied is an authorization token or a refresh token
*/
if ($refresh === true) {
$client->setVariable('grant_type', 'refresh_token');
$client->setVariable('refresh_token', $code);
} else {
$client->setVariable('grant_type', 'authorization_code');
$client->setVariable('code', $code);
}
// sign the request by setting a custom header
$header = 'Authorization: Bearer ' . ($this->_secret);
array_push($client::$CURL_OPTS[CURLOPT_HTTPHEADER], $header);
// get the session (initiates the request)
$session = $client->getSession();
$this->_body = json_decode(
$this->_getClient()->getLastResponse()
);
// if it bailed
if (isset($this->_body->code)) {
$body = $this->_body->message;
throw new Exception(
'Error making StripeOAuth request: (' .
($this->_body->code) . ') ' . ($body)
);
}
// store the tokens
$this->_tokens = array(
'access' => $session['access_token'],
'refresh' => $session['refresh_token']
);
}
return $this->_tokens;
}
/**
* getTokenType
*
* @access public
* @return String
*/
public function getTokenType()
{
if (empty($this->_tokens)) {
throw new Exception(
'Access token hasn\'t been generated yet.'
);
}
return $this->_body->token_type;
}
/**
* getUserId
*
* @access public
* @return String
*/
public function getUserId()
{
if (empty($this->_tokens)) {
throw new Exception(
'Access token hasn\'t been generated yet.'
);
}
return $this->_body->stripe_user_id;
}
/**
* isLive
*
* @access public
* @return Boolean
*/
public function isLive()
{
if (empty($this->_tokens)) {
throw new Exception(
'Access token hasn\'t been generated yet.'
);
}
return $this->_body->livemode === true;
}
}