-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
54 lines (50 loc) · 1.32 KB
/
api.js
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
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
let createCustomer = function(req, res){
if(req.method !== 'POST'){
res.writeHead(400, 'bad request');
res.end();
return;
}
let jsonString = '';
let post;
req.on('data', data => {
jsonString += data;
});
req.on('end', () => {
post = JSON.parse(jsonString);
if(!post.email){
res.setHeader('Content-Type', 'application/json');
res.writeHead(400);
res.write(JSON.stringify({
error: 'Invalid email'
}));
res.end();
return;
}
console.log('communicating with stripe customer api...');
stripe.customers.create({
name: post.name,
email: post.email,
address: {
line1: post.line1,
city: post.city,
state: post.state,
postal_code: post.postal_code
}
}, function(err, customer) {
console.log('stripe customer api responded...');
if(err){
console.log('stripe customer api raised error', err);
res.writeHead(500, err);
res.end();
return;
}
console.log('stripe created customer', customer);
res.setHeader('Content-Type', 'application/json');
res.writeHead(200);
res.write(JSON.stringify(customer));
res.end();
});
});
};
module.exports = {createCustomer};