-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (34 loc) · 1.09 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
const express = require('express');
const keys = require('./config/keys');
const stripe = require('stripe')(keys.stripeSecretKey);
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 3000;
console.log(`Public: ${keys.stripePublishableKey}`);
console.log(`Secret: ${keys.stripeSecretKey}`);
// Middlewares
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
// ejs engine
app.set('view engine', 'ejs');
// Static
app.use('/public', express.static('public'));
// Routes
app.get('/', (req, res) => {
res.render('index', {stripePublishableKey: keys.stripePublishableKey});
});
app.post('/charge', (req, res) => {
const amount = 2500;
stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
}).then(customer => stripe.charges.create({
amount,
description: 'Web development ebook',
currency: 'usd',
customer: customer.id
})).then(charge => res.render('success'));
});
app.listen(port, err => {
console.log(err ? `Error on port ${port}` : `App running on port ${port}`);
});