-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
101 lines (83 loc) · 2.27 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require("dotenv").config({ path: ".env" });
const express = require("express");
var bodyParser = require("body-parser");
const app = express();
const { resolve } = require("path");
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const port = process.env.PORT | 4242;
app.use(bodyParser.json());
app.use(express.static("static"));
app.get("/", (req, res) => {
const path = resolve("static/index.html");
res.sendFile(path);
});
app.get("/config", (req, res) => {
res.send({
publicKey: process.env.STRIPE_PUBLISHABLE_KEY,
basePrice: process.env.BASE_PRICE,
currency: process.env.CURRENCY
});
});
app.get("/checkout-session", async (req, res) => {
const { sessionId } = req.query;
const session = await stripe.checkout.sessions.retrieve(sessionId);
res.send(session);
});
app.post("/checkout-session", async (req, res) => {
try {
const domainURL = req.headers.origin;
let currency = "USD";
const { locale } = req.body;
let paymentMethods = ["card"];
if (locale === "nl") {
paymentMethods.push("ideal");
currency = "EUR";
}
if (locale === "uk") {
paymentMethods.push("bacs_debit");
currency = "GBP";
}
if (locale === "de") {
paymentMethods.push("giropay");
currency = "EUR";
}
if (locale === "fr") {
paymentMethods.push("bancontact");
currency = "EUR";
}
if (locale === "ms") {
currency = "MYR";
}
if (locale === "pl") {
paymentMethods.push("p24");
currency = "PLN";
}
const quantity = 2;
const session = await stripe.checkout.sessions.create({
payment_method_types: paymentMethods,
locale: locale,
line_items: [
{
name: "Kitchen counter stools",
images: ["https://stripe.com/img/v3/checkout/chairs.jpg"],
quantity: quantity,
currency: currency,
amount: 8900
}
],
success_url: `${domainURL}/success.html?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${domainURL}/canceled.html`
});
res.send({
sessionId: session.id
});
} catch (err) {
res.status(500);
res.send({
error: err.message
});
}
});
app.listen(port, () =>
console.log(`Server listening on http://localhost:${port}!`)
);