-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
114 lines (93 loc) · 3.01 KB
/
app.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
require('dotenv-safe').load({silent: true});
const stripeAccoutKey = process.env.STRIPE_SECRET_KEY;
const morgan = require('morgan');
const bodyParser = require('body-parser');
const chalk = require('chalk');
const express = require('express');
const _ = require('lodash')
const stripe = require('stripe')(stripeAccoutKey);
const port = process.env.PORT || 3000;
const host = process.env.HOST || '0.0.0.0';
const pj = require('./package.json');
const app = express();
const info = chalk.cyan;
const error = chalk.red;
const success = chalk.green;
app.use(morgan('dev'));
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.set('views', './views')
app.set('view engine', 'jade');
app
.get('/config', getConfig)
.get('/', getIndex)
.post('/api/stripe/payment', postPayment)
;
const server = app.listen(port, host, function () {
const host = server.address().address;
const port = server.address().port;
console.log(info(`${pj.name}@${pj.version} running on ${host}:${port}`));
});
function getConfig(req, res) {
return res.json({
serviceRatePercent: parseFloat(process.env.STRIPE_TRANSACTION_RATE, 10),
serviceFlatFee: parseFloat(process.env.STRIPE_TRANSACTION_FLAT_FEE, 10)
});
}
function getIndex(req, res) {
return res.render('index');
}
function postPayment(req, res) {
// Get the credit card details submitted by the form
const paymentData = parsePaymentData(req.body);
const chargeInfo = {
amount: paymentData.amount, // amount in cents, again
currency: paymentData.currency,
source: paymentData.token,
description: paymentData.description,
receipt_email: paymentData.email,
metadata: {
email: paymentData.email
}
};
if (paymentData.customer) {
chargeInfo.metadata.customerFirstName = paymentData.customer.firstName;
chargeInfo.metadata.customerLastName = paymentData.customer.lastName;
}
if (paymentData.metadata && paymentData.metadata.length) {
paymentData.metadata.forEach((m) => {
const key = _.keys(m)[0];
if(key) {
chargeInfo.metadata[key] = m[key];
}
});
// console.log('chargeInfo', JSON.stringify(chargeInfo, null, 2));
}
const charge = stripe.charges.create(chargeInfo, function(err, charge) {
if (err) {
console.log(error(JSON.stringify(err, null, 2)));
return res.status(500).send('Sorry. Could not process your payment at this time.');
}
return res.status(200).json({message: 'OK'});
});
}
function parsePaymentData(body) {
const data = {
token: null
};
const props = [,
{ key: 'email', default: null },
{ key: 'amount', default: 0 },
{ key: 'currency', default: 'usd' },
{ key: 'token', default: null },
{ key: 'description', default: '' },
{ key: 'customer', default: null },
{ key: 'metadata', default: null }
];
if (body) {
props.forEach(function(p) {
data[p.key] = body[p.key] || p.default;
});
}
return data;
}