-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
46 lines (41 loc) · 1.73 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
// sk_test_51MlsYIJatKHrupv6K1jpugHYafS21yPiCfiSzIcx1eGhdqSUr8aGt8ZUfip6CNbWedz4CXFM0SuCPPaRb82b58K400lMHcanY1;
// Logitech G502 HERO: price_1MlssHJatKHrupv6CjiaGMFQ
// MAD CATZ R.A.T.: price_1MlsvXJatKHrupv6IKVNv9xf
// STEELSERIES Aerox 5 RGB Optical Gaming Mouse: price_1MlsytJatKHrupv6C5Y0hrAI
const express = require("express");
var cors = require("cors");
const stripe = require("stripe")("sk_test_51MlsYIJatKHrupv6K1jpugHYafS21yPiCfiSzIcx1eGhdqSUr8aGt8ZUfip6CNbWedz4CXFM0SuCPPaRb82b58K400lMHcanY1");
const path = require("path");
// create express server
const app = express();
const port = process.env.PORT || 5000;
// use our middleware cors
app.use(cors());
// recommended by stripe docs as it wants our express app to use this
app.use(express.static(path.join(__dirname, "store/build")));
app.use(express.json());
app.post("/checkout", async (req, res) => {
console.log(req.body);
// converting our array into the lineItems array that Stripe wants
const items = req.body.items;
let lineItems = [];
// create a new array in the format that Stripe wants for us to process payments
items.forEach((item) => {
lineItems.push({ price: item.id, quantity: item.quantity });
});
// initiate session with stripe
const session = await stripe.checkout.sessions.create({
line_items: lineItems,
mode: "payment",
success_url: "https://gaming-mouse-app.herokuapp.com/success",
cancel_url: "https://gaming-mouse-app.herokuapp.com/cancel",
});
res.send(JSON.stringify({ url: session.url }));
});
app.get("/test", (req, res) => {
res.send("Api is working");
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/store/build/index.html"));
});
app.listen(port, () => console.log(`Listening on port ${port}!`));