-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
213 lines (193 loc) · 5.64 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require('dotenv').config();
// Gets the API key from process.env
const stripe = require('stripe')(process.env.STRIPE_API_TOKEN);
const express = require('express');
const app = express();
// ! Need to require CORS when you're fetching API from 3rd party services
const cors = require("cors");
const path = require('path');
const port = process.env.PORT || 3000;
// * Viewing ejs files
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
// ! Put this down to make express accept JSON data in POST create checkout
// ! session, i.e. req.body
app.use(express.json());
app.use(
cors({
origin: ["${YOUR_DOMAIN}", "https://checkout.stripe.com"],
})
);
/**
* This endpoint is used with ejs to create product detail page for all
* the products listed in the Shop.
*/
// Sample id: prod_LvXOq7aYRQfCXZ
app.get("/product", async (req, res) => {
let id = req.query.id;
if (id.length > 0) {
let productObject = await stripe.products.retrieve(id);
let default_price = productObject.default_price;
let unit_amount = await getUnitAmount(default_price);
let thisTitle = productObject.name;
let thisImages = productObject.images;
let thisDescription = productObject.description;
let thisPrice = '$' + centsToDollars(unit_amount);
res.render("product-detail", {
title: thisTitle,
images: thisImages,
description: thisDescription,
price: thisPrice,
product_id: id
});
}
});
/**
* Converts the unit_amount from cents to dollars
* @param {Number} The unit_amount in cents
* @returns {String} The unit_amount in dollars, rounded by 2 decimal places
*/
function centsToDollars(cents) {
return (parseFloat(cents) / 100).toFixed(2);
}
/**
* ! Unused endpoint (for now)
*/
app.post('/create-product', async (req, res) => {
try {
console.log('name = ' + req.body.name);
console.log('id = ' + req.body.id);
console.log('description = ' + req.body.description);
let product = await stripe.products.create({
name: req.body.name,
id: req.body.id,
description: req.body.description
})
// let product = await stripe.products.create({
// name: "pad thai",
// id: "2",
// description: "this is a pad thai"
// })
console.log('product created');
res.json(product);
} catch (error) {
console.log(error);
res.send(error);
}
})
/**
* Retrieves the details of an existing product. Supply the unique product ID
* from either a product creation request or the product list, and
* Stripe will return the corresponding product information.
*/
app.get('/retrieve-product', async (req, res) => {
try {
let id = req.query.id;
console.log('id = ' + id);
let product = await stripe.products.retrieve(id);
res.json(product);
console.log('success');
} catch (error) {
console.log(error);
res.send(error);
}
})
/**
* List the only active products in Stripe
*/
app.get('/list-product', async (req, res) => {
try {
let productList = await stripe.products.list({
active: true,
limit: 100,
});
console.log("productList length in app.js = ", productList.data.length);
res.json(productList);
} catch (error) {
console.log(error);
res.send(error);
}
})
/**
* Takes in the 'default_price' from the query and outputs the unit_amount
* (or the price) of the product in dollars.
*/
app.get('/retrieve-price', async (req, res) => {
try {
// let priceObj = await stripe.prices.retrieve(req.query.default_price);
// let unit_amount = priceObj.unit_amount;
// res.type('text').send(centsToDollars(unit_amount));
let unit_amount = await getUnitAmount(req.query.default_price);
res.type('text').send(centsToDollars(unit_amount));
} catch (error) {
console.log(error);
res.send(error);
}
})
/**
* ! Unused endpoint (for now)
*/
app.delete('/delete-product', async (req, res) => {
try {
await stripe.products.del(req.query.id);
} catch (error) {
console.log(error);
res.send(error);
}
})
/**
* ! Unused endpoint (for now)
*/
app.delete('/delete-all-product', async (req, res) => {
try {
let productList = await stripe.products.list();
for (let i = 0; i < productList.data.length; i++) {
await stripe.products.del(productList.data[i].id);
}
} catch (error) {
console.log(error);
res.send(error);
}
})
/**
* Takes in a JSON Array of the items in the shopping_cart and sends
* the corresponding Stripe checkout URL to the client side.
*/
app.post('/create-checkout-session', async (req, res) => {
try {
if (req.body.length > 0) {
console.log('req body: ' + JSON.stringify(req.body));
const session = await stripe.checkout.sessions.create({
line_items: req.body,
mode: 'payment',
success_url: `${YOUR_DOMAIN}/success.html`,
cancel_url: `${YOUR_DOMAIN}/checkout.html`,
});
console.log(session.url);
res.send(session.url);
} else {
console.log('EMPTY CART');
}
} catch (error) {
console.log(error);
}
});
// ~The 404 Route (ALWAYS Keep this as the last route)
app.get('*', (req, res) => {
res.render('error');
});
/**
*
* @param {id} default_price
* @returns Takes in the default_price and returns the unit_amount of the
* Stripe price object in cents.
*/
async function getUnitAmount(default_price) {
let priceObj = await stripe.prices.retrieve(default_price);
let unit_amount = priceObj.unit_amount;
return unit_amount;
}
app.use(express.static('public'));
// TODO: Put in the website domain once it's published
const YOUR_DOMAIN = 'https://mark-home-made.onrender.com/';
app.listen(port, () => console.log('Running on port ' + port));