-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
84 lines (70 loc) · 2.31 KB
/
run.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
'use strict';
const productLine = require('./public/js/productLine.js');
const configuration = {
keyPublishable: process.env.PUBLISHABLE_KEY || 'pk_test_DZkclA7Lk0U2szChf0u7RV8U',
keySecret: process.env.SECRET_KEY || 'sk_test_JvHrW5FfszUh49X5eMW5ZKU5',
port: process.env.PORT || 1212,
forceHttps: process.env.FORCE_HTTPS || false,
forceWww: process.env.FORCE_WWW || false,
};
const express = require('express');
const app = express();
app.set('view engine', 'pug');
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
const stripe = require("stripe")(configuration.keySecret);
const router = express.Router();
app.use(express.static('public'));
app.use((req, res, next) => {
let desiredHost = req.get('host');
let redirect = false;
if (configuration.forceWww && (req.subdomains.length !== 1 || req.subdomains[0] !== 'www')) {
desiredHost = 'www.' + desiredHost;
redirect = true;
}
if (configuration.forceHttps && !req.get('x-arr-ssl')) {
redirect = true;
}
if (redirect) {
return res.redirect("https://" + desiredHost + req.url);
}
next();
});
app.get('/', function(req, res) {
res.render('index', { keyPublishable: configuration.keyPublishable });
});
app.post("/charge", (req, res) => {
let amount = (getPriceFromProducts(productLine.products, req.body) * 100).toFixed(0);
stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken,
})
.then(customer =>
stripe.charges.create({
amount,
currency: 'cad',
description: 'Website Charge',
customer: customer.id,
metadata: req.body,
}))
.then(charge => {
res.status(200);
res.send({ });
})
.catch(charge => {
res.status(500);
res.send({ });
});
});
app.listen(configuration.port);
console.log(`Started on port: ${configuration.port}`);
function getPriceFromProducts(products, source) {
const productNames = Object.keys(products);
const deliveryFee = source['pickupLocation'] === 'delivery' ? 5 : 0;
return productNames.map(p => {
const units = source[p] || 0;
const unitPrice = Math.ceil((products[p].price * productLine.priceMultiplier + productLine.priceAddition) * 10) / 10;
return units * unitPrice;
}).reduce((a, b) => a + b) * productLine.gst + deliveryFee;
}