-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (50 loc) · 1.39 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
// sk_test_51LnUKJDM1jwCEz8OJG69szv032rIo4X0WrFMaXrqxu9g8fdohsL1y54JEUhFUKrqoBquVjN3AzpIFyrbN915bgcd00O5hqoGCJ
// Coffee: price_1LnUTFDM1jwCEz8OGoOSXiSM
// Sunglasses: price_1LnUTxDM1jwCEz8OAqHYTwKQ
// Camera: price_1LnUUoDM1jwCEz8OvxIcJ7to
const express = require('express');
var cors = require('cors');
const stripe = require('stripe')('sk_test_51LnUKJDM1jwCEz8OJG69szv032rIo4X0WrFMaXrqxu9g8fdohsL1y54JEUhFUKrqoBquVjN3AzpIFyrbN915bgcd00O5hqoGCJ');
const app = express();
app.use(cors());
app.use(express.static("public"));
app.use(express.json());
app.post("/checkout", async (req, res) => {
/*
req.body.items
[
{
id: 1,
quantity: 3
}
]
stripe wants
[
{
price: 1,
quantity: 3
}
]
*/
console.log(req.body);
const items = req.body.items;
let lineItems = [];
items.forEach((item)=> {
lineItems.push(
{
price: item.id,
quantity: item.quantity
}
)
});
const session = await stripe.checkout.sessions.create({
line_items: lineItems,
mode: 'payment',
success_url: "http://localhost:3000/success",
cancel_url: "http://localhost:3000/cancel"
});
res.send(JSON.stringify({
url: session.url
}));
});
app.listen(4000, () => console.log("Listening on port 4000!"));