-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (58 loc) · 1.78 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
import express from "express";
import dotenv from "dotenv";
import stripe from "stripe";
// Load variables
dotenv.config();
// Start Server
const app = express();
app.use(express.static("public"));
app.use(express.json());
// Home Route
app.get("/", (req,res) => {
res.sendFile("index.html", {root: "public"});
});
//Sucessful Order
app.get("/success", (req,res) => {
res.sendFile("SuccessPayment.html", {root: "public"});
});
// Failed Order
app.get("/cancel", (req,res) => {
res.sendFile("orderfailed.html", {root: "public"});
});
// Stripe
let stripeGateway = stripe(process.env.stripe_api);
let DOMAIN = process.env.DOMAIN;
app.post('/stripe-checkout', async (req,res) => {
const lineItems = req.body.items.map((item) => {
const unitAmount = parseInt(item.price.replace(/[^0-9.-]+/g, '') + 100);
console.log("item-price:", item.price);
console.log("unitAmount:", unitAmount);
return {
price_data: {
currency: 'usd',
product_data: {
name: item.title,
images: [item.productImg]
},
unit_amount: unitAmount,
},
quantity: item.quantity,
};
});
console.log("lineItems:" , lineItems);
// Create Checkout Session
const session = await stripeGateway.checkout.sessions.create({
payment_method_types: ["card"],
mode: "payment",
success_url: `${DOMAIN}/success`,
cancel_url: `${DOMAIN}/cancel`,
line_items: lineItems,
// Asking Address in Stripe Checkout Page
billing_address_collection: 'required'
});
res.json(session.url);
});
// Create Checkout Session
app.listen(3000, () => {
console.log("listening on port 3000;");
});