-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (37 loc) · 932 Bytes
/
index.js
File metadata and controls
43 lines (37 loc) · 932 Bytes
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
const express = require("express");
const cors = require("cors");
const dotenv = require("dotenv");
dotenv.config();
const stripe = require("stripe")(process.env.STRIPE_KEY);
const PORT = 5000;
const app = express();
app.use(cors({ origin: true }));
app.use(express.json());
app.get("/", (req, res) => {
res.status(200).json({
message: "Success",
});
});
app.post("/payment/create", async (req, res) => {
const total = parseInt(req.query.total);
if (total > 0) {
const paymentIntent = await stripe.paymentIntents.create({
amount: total,
currency: "usd",
});
res.status(201).json({
clientSecret: paymentIntent.client_secret,
});
} else {
res.status(403).json({
message: "total must be greater than 0",
});
}
});
app.listen(PORT, (error) => {
if (error) {
console.error(error.message);
} else {
console.log(" => Server Running successfully");
}
});