-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (50 loc) · 1.49 KB
/
server.js
File metadata and controls
65 lines (50 loc) · 1.49 KB
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
// sk_test_51MPlB6SGXIVKb2H5jy6sl9hrEVtmeUNJPvsz1sTdJK5WOq6uQ0qSG5vH5Gbdf4Rzrlc4FOHWT1awLM5keF8YbPPS00KSf6SziE
// Coffee : price_1MPlL9SGXIVKb2H5DfNUdni0
// Sunglasses: price_1MPlMiSGXIVKb2H5Uf9FYZjN
// Camera: price_1MPlNfSGXIVKb2H5bLJzAWfl
const express = require('express');
var cors = require('cors') // allows any id address to access the stripe server
const stripe = require('stripe')('sk_test_51MPlB6SGXIVKb2H5jy6sl9hrEVtmeUNJPvsz1sTdJK5WOq6uQ0qSG5vH5Gbdf4Rzrlc4FOHWT1awLM5keF8YbPPS00KSf6SziE'); // initialize stripe client for out account
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
[
{
id: 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!"));