-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
56 lines (47 loc) · 1.56 KB
/
server.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
var EventEmitter = require('events').EventEmitter;
var emitter = new EventEmitter;
var express = require('express');
var app = express.createServer();
var stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
app.use(express.static(__dirname));
app.use(express.bodyParser())
app.post('/test-payment', function(request, response){
stripe.charges.create({
currency: 'usd',
amount: Math.floor(request.body.amount*100) || 100,
description: request.body.description || "",
card: {
number: '4242424242424242',
exp_month: '12',
exp_year: '2018'
}
}, function(err, charge){
response.send(JSON.stringify(charge));
})
});
app.post('/stripe-webhook', function(request, response){
console.log(request.body)
if (request.body.type === 'charge.succeeded') {
console.log(request.body.data.object);
emitter.emit('chargeSucceeded', request.body.data.object);
}
response.send('OK');
});
function chargeServer(client, con) {
con.on('ready', function () {
if (typeof client['chargeSucceeded'] === 'function') {
emitter.on('chargeSucceeded', client['chargeSucceeded']);
}
});
con.on('end', function () {
if (typeof client['chargeSucceeded'] === 'function') {
emitter.removeListener('chargeSucceeded', client['chargeSucceeded']);
}
});
}
console.log("Starting on port: "+process.env.PORT)
app.listen(process.env.PORT || 8080);
// then just pass the server app handle to .listen()!
var dnode = require('dnode');
var server = dnode(chargeServer);
server.listen(app, {"io":{"transports":['xhr-polling'], "polling duration":10}});