-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (42 loc) · 1.36 KB
/
server.js
File metadata and controls
53 lines (42 loc) · 1.36 KB
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
'use strict';
var CONFIG = require('./config');
var app = require('./lib/server');
var stripe = require('stripe')(CONFIG.secretKey);
var OAuth = require('client-oauth2');
// Configure the OAuth 2.0 Client
var oauth = new OAuth({
clientId: CONFIG.clientId,
clientSecret: CONFIG.secretKey,
scopes: ['read_write'],
redirectUri: CONFIG.redirectUri,
authorizationUri: 'https://connect.stripe.com/oauth/authorize',
accessTokenUri: 'https://connect.stripe.com/oauth/token'
});
// Get the home page:
app.get('/', function(req, res) {
res.render('index', {
redirectUri: oauth.code.getUri()
});
});
// Handle the redirect - this should match your CONFIG redirect's path:
app.get('/connected', function(req, res) {
if (req.query.error) return response.render('connected', {
error: req.query.error
});
// Use the Authorization Code to get a Token
oauth.code.getToken(req.url).then(handleToken);
// Go fetch the Account from the Token
function handleToken(token) {
stripe.account.retrieve(token.data.stripe_user_id, onAccount);
}
// Render the Account information
function onAccount(error, account) {
res.render('connected', {
error: error,
account: account
});
}
});
app.listen(CONFIG.port, function() {
console.log('Listening on port', CONFIG.port);
});