-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
116 lines (100 loc) · 2.68 KB
/
router.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
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
var express = require('express');
var router = express.Router();
var path = require('path');
var async = require('async');
var stripe = require('stripe')('sk_test_eBBR4DkvJFUA4sqw8Vfaja0N');
router.get('/', function (req, res) {
res.sendFile(path.join(__dirname+'/index.html'));
});
router.get('/stripe/:email/:cardNumber/:mounth/:year/:cvc/:amount', function (req, res) {
var params = {
email: req.params.email,
cardNumber: req.params.cardNumber,
mounth: req.params.mounth,
year: req.params.year,
amount: req.params.amount,
cvc: req.params.cvc
};
console.log('params = ', params);
async.waterfall([
createCustomer,
createPlan,
createSubscribtion
], function (err, done) {
if (err) {
return res.send(err);
}
res.json(done);
});
function createCustomer(callback) {
stripe.customers.create({
source: {
object: "card",
exp_month: params.mounth,
exp_year: params.year,
number: params.cardNumber
},
email: params.email,
account_balance: params.amount,
description: 'Customer ' + params.email + ' is devTest account'
}, function (err, customer) {
console.log('customer = ', customer);
if (err) { return callback(err); }
callback(null, customer);
});
}
function createPlan(customer, callback) {
stripe.plans.create({
amount: 100,
interval: 'day',
name: '1$',
currency: 'usd',
id: '1$'
}, function (err, plan) {
console.log('plan = ', plan);
if (err) { return callback(err); }
callback(null, customer, plan)
});
}
function createSubscribtion(customer, plan, callback) {
stripe.subscriptions.create({
customer: customer.id,
plan: plan.id
}, function (err, ok) {
console.log('ok = ',ok);
if (err) { return callback(err); }
callback(null, ok);
});
}
});
router.get('/stripe/charges', function (req, res) {
stripe.charges.create({
amount: 2000,
currency: "usd",
source: {
number: '4242424242424242',
exp_month: 12,
exp_year: 2017,
cvc: '123'
},
description: "Charge for [email protected]"
}, function(err, charge) {
if (err) { res.send(err); }
res.json(charge);
});
})
router.post('/stripe/webhooks', function (req, res) {
res.json({ "webhooks": "ok" });
})
router.post('/charge', function(req, res) {
stripe.charges.create({
amount: req.body.paymentDetails.amount,
currency: 'usd',
source: req.body.token.id,
description: req.body.paymentDetails.description
}, function(err, charge) {
if (err) { res.send(err); }
res.json(charge);
});
})
module.exports = router;