-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
140 lines (111 loc) · 3.47 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const express = require('express');
const bodyParser = require('body-parser');
const nunjucks = require('nunjucks');
const app = express();
const port = 3000;
const router = express.Router();
const STRIPE_API = require('./api/stripe-functions.js');
// webhook secret
const endpointSecret = '**********';
const stripe = require('stripe')(process.env.STRIPE_API_KEY);
/* Set up Express to serve HTML files using "res.render" with help of Nunjucks */
app.set('view engine', 'html');
app.engine('html', nunjucks.render);
nunjucks.configure('views', { noCache: true });
app.use(express.static(__dirname));
app.use('/styles', express.static('styles'));
app.use(bodyParser());
app.use('/', router);
/* Place all routes here */
router.get('/', (req, res) => {
STRIPE_API.getAllProductsAndPlans().then(products => {
res.render('adminView.html', {products: products});
});
});
/* Create Product */
router.get('/createProduct', (req, res) => {
res.render('createProduct.html');
});
router.post('/createProduct', (req, res) => {
STRIPE_API.createProduct(req.body).then(() => {
res.render('createProduct.html', { success: true });
});
});
/* Create Plan */
router.post('/createPlan', (req, res) => {
res.render('createPlan.html', {
productId: req.body.productId,
productName: req.body.productName
});
});
router.post('/createPlanForReal', (req, res) => {
STRIPE_API.createPlan(req.body).then(() => {
res.render('createPlan.html', { success: true });
});
});
router.get('/customerView', (req, res) => {
STRIPE_API.getAllProductsAndPlans().then(products => {
products = products.filter(product => {
return product.plans.length > 0;
});
res.render('customerView.html', {products: products});
});
});
router.post('/signUp', (req, res) => {
var product = {
name: req.body.productName
};
var plan = {
id: req.body.planId,
name: req.body.planName,
amount: req.body.planAmount,
interval: req.body.planInterval,
interval_count: req.body.planIntervalCount
}
res.render('signUp.html', {product: product, plan: plan});
});
router.post('/processPayment', (req, res) => {
var product = {
name: req.body.productName
};
var plan = {
id: req.body.planId,
name: req.body.planName,
amount: req.body.planAmount,
interval: req.body.planInterval,
interval_count: req.body.planIntervalCount
}
STRIPE_API.createCustomerAndSubscription(req,res,product,plan,req.body)
// .then(() => {
// res.render('signup.html', {product: product, plan: plan, success: true});
// }).catch(err => {
// res.render('signup.html', {product: product, plan: plan, error: true});
// });
});
app.post('/api/paymentsuccess', bodyParser.raw({type: 'application/json'}), (request, response) => {
const sig = request.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
}
catch (err) {
response.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
const paymentIntent = event.data.object;
break;
case 'payment_method.attached':
const paymentMethod = event.data.object;
handlePaymentMethodAttached(paymentMethod);
break;
default:
return response.status(400).end();
}
response.json({received: true});
});
/* Start listening on specified port */
app.listen(port, () => {
console.info('Example app listening on port', port)
});