-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
server.js
83 lines (68 loc) · 2.28 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const express = require("express");
const app = express();
const { resolve } = require("path");
const port = process.env.PORT || 3000;
// importing the dotenv module to use environment variables:
require("dotenv").config();
const api_key = process.env.SECRET_KEY;
const stripe = require("stripe")(api_key);
// ------------ Imports & necessary things here ------------
// Setting up the static folder:
// app.use(express.static(resolve(__dirname, "./client")));
app.use(express.static(resolve(__dirname, process.env.STATIC_DIR)));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.get("/", (req, res) => {
const path = resolve(process.env.STATIC_DIR + "/index.html");
res.sendFile(path);
});
// creating a route for success page:
app.get("/success", (req, res) => {
const path = resolve(process.env.STATIC_DIR + "/success.html");
res.sendFile(path);
});
// creating a route for cancel page:
app.get("/cancel", (req, res) => {
const path = resolve(process.env.STATIC_DIR + "/cancel.html");
res.sendFile(path);
});
// Workshop page routes:
app.get("/workshop1", (req, res) => {
const path = resolve(process.env.STATIC_DIR + "/workshops/workshop1.html");
res.sendFile(path);
});
app.get("/workshop2", (req, res) => {
const path = resolve(process.env.STATIC_DIR + "/workshops/workshop2.html");
res.sendFile(path);
});
app.get("/workshop3", (req, res) => {
const path = resolve(process.env.STATIC_DIR + "/workshops/workshop3.html");
res.sendFile(path);
});
// ____________________________________________________________________________________
const domainURL = process.env.DOMAIN;
app.post("/create-checkout-session/:pid", async (req, res) => {
const priceId = req.params.pid;
const session = await stripe.checkout.sessions.create({
mode: "payment",
success_url: `${domainURL}/success?id={CHECKOUT_SESSION_ID}`,
cancel_url: `${domainURL}/cancel`,
payment_method_types: ["card"],
line_items: [
{
price: priceId,
quantity: 1,
},
],
// allowing the use of promo-codes:
allow_promotion_codes: true,
});
res.json({
id: session.id,
});
});
// Server listening:
app.listen(port, () => {
console.log(`Server listening on port: ${port}`);
console.log(`You may access you app at: ${domainURL}`);
});