-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (49 loc) · 1.69 KB
/
index.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
require('dotenv').config()
const express = require('express');
const request = require('request-promise-native');
const stripe = require('stripe')(process.env.STRIPE_KEY);
const rateLimit = require('express-rate-limit');
const app = express();
const apiLimiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 5
});
app.use(express.static('public'));
app.use(express.json());
app.post('/', apiLimiter, async (req, res) => {
const { email, token, fullName, membership, additional, plusOne } = req.body;
if (!email || !token || !fullName || !membership) {
return res.status(400).json({ message: 'Invalid body' });
}
try {
const options = {
method: 'POST',
uri: process.env.FORM_LINK,
form: {
"entry.717698538": fullName,
"entry.799749242": additional,
"entry.924011423": membership,
"entry.1233294446": email,
"entry.1577292230": plusOne
}
}
const results = await stripe.charges.create({
amount: plusOne ? 1100 : 550,
currency: 'cad',
description: `UBCAni Spring Gala 2019 RSVP`,
source: token,
receipt_email: email
});
console.log(`Charge successful for ${email}`);
await request(options);
console.log(`Submitted payment of ${results.amount} from ${email} to Google Forms`);
res.json({ message: 'success' });
} catch (err) {
console.log('An unexpected error occurred');
console.log(err);
res.status(500).json(err);
}
});
app.listen(3000, () => {
console.log('App listening on port 3000');
});