-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathserver.js
41 lines (36 loc) · 1016 Bytes
/
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
import "dotenv/config";
import express from "express";
import * as paypal from "./paypal-api.js";
const app = express();
app.set("view engine", "ejs");
app.use(express.static("public"));
// render checkout page with client id & unique client token
app.get("/", async (req, res) => {
const clientId = process.env.CLIENT_ID;
try {
const clientToken = await paypal.generateClientToken();
res.render("checkout", { clientId, clientToken });
} catch (err) {
res.status(500).send(err.message);
}
});
// create order
app.post("/api/orders", async (req, res) => {
try {
const order = await paypal.createOrder();
res.json(order);
} catch (err) {
res.status(500).send(err.message);
}
});
// capture payment
app.post("/api/orders/:orderID/capture", async (req, res) => {
const { orderID } = req.params;
try {
const captureData = await paypal.capturePayment(orderID);
res.json(captureData);
} catch (err) {
res.status(500).send(err.message);
}
});
app.listen(8888);