forked from mtermoul/crowd-pitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (58 loc) · 2.01 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
57
58
59
60
61
62
63
64
const express = require('express');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const session = require('express-session');
var Cosmic = require('cosmicjs');
require('dotenv').config();
const PORT = process.env.PORT || 3000
var app = express();
const stripeKey = process.env.STRIPE_KEY;
const stripePublishKey = process.env.STRIPE_PUBLISH_KEY;
const stripe = require('stripe')(stripeKey);
const bucket_slug = process.env.COSMIC_BUCKET || 'crowd-pitch';
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true}));
app.use(session({
secret: 'crowd pitch keep secret',
resave: false,
saveUninitialized: true,
cookie: {}
}))
const api = Cosmic();
const bucket = api.bucket({
slug: bucket_slug
});
app.get('/', async function(req, res) {
res.locals.stripePublishKey = stripePublishKey;
const data = (await bucket.getObjects({type: 'pages'})).objects;
res.locals.cosmic = data.find((item) => {
return item.slug === 'google-cash';
});
if (req.session.pageVariant === undefined) {
req.session.pageVariant = Math.floor(Math.random() * 2);
}
res.locals.pageB = (req.session.pageVariant === 1 ? true : false);
res.render('home');
});
app.post('/pay', function(req, res) {
stripe.charges.create({
amount: Number(req.body.amount) * 100, // amount in cents
currency: 'usd',
description: `${req.body.firstName} ${req.body.lastName} (${req.body.email}) - ${req.body.orderSummary} - Source: ${req.body.pageSource}`,
source: req.body.stripeToken,
statement_descriptor: 'CROWD-PITHC GOOG CASHM'
})
.then(charge => {
res.send(charge);
})
.catch(error => {
console.log('--- Error:', error);
res.status(500).send(error);
});
});
app.listen(PORT, () => {
console.log(`🚀 Server ready at http://localhost:${PORT}`);
});