-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (58 loc) · 1.68 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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const stripe = require("stripe")(process.env.SK);
const app = express();
app.use(cors());
app.use(express.json());
const path = require("path");
if (process.env.NODE_ENV === "production") {
app.use(express.static("build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "build", "index.html"));
});
}
app.get("/", (req, res) => {
res.send("Welcome to eShop website.");
});
const array = [];
const calculateOrderAmount = (items) => {
items.map((item) => {
const { price, cartQuantity } = item;
const cartItemAmount = price * cartQuantity;
return array.push(cartItemAmount);
});
const totalAmount = array.reduce((a, b) => {
return a + b;
}, 0);
return totalAmount * 100;
};
app.post("/create-payment-intent", async (req, res) => {
const { items, shipping, description } = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: "INR",
automatic_payment_methods: {
enabled: true,
},
description,
shipping: {
address: {
line1: shipping.line1,
line2: shipping.line2,
city: shipping.city,
country: shipping.country,
postal_code: shipping.postal_code,
},
name: shipping.name,
phone: shipping.phone,
},
// receipt_email: customerEmail
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
const PORT = process.env.PORT || 4242;
app.listen(PORT, () => console.log(`Node server listening on port ${PORT}`));