-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpayment.controller.js
61 lines (52 loc) · 1.64 KB
/
payment.controller.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
const router = require("express").Router();
const Razorpay = require("razorpay");
const crypto = require("crypto");
//Saving the order id to use it for verification purpose
let order_id;
//To create order id
router.post("/order", async (req, res) => {
try {
const instance = new Razorpay({
key_id: process.env.RAZORPAY_KEY_ID,
key_secret: process.env.RAZORPAY_KEY_SECRET,
});
const options = {
amount: req.body.amount * 100,
currency: "INR",
receipt: crypto.randomBytes(10).toString("hex"),
};
instance.orders.create(options, (error, order) => {
if (error) {
console.log(error);
return res
.status(500)
.json({ message: "Something went wrong", status: "Failed" });
}
order_id = order.id;
res.status(201).json(order);
});
} catch (error) {
console.log(error);
return res.status(500).json({ message: "Internal server error" });
}
});
//To verify payment
router.post("/verify", async (req, res) => {
try {
const { razorpay_payment_id, razorpay_signature } = req.body;
const sign = order_id + "|" + razorpay_payment_id;
const expectedSign = crypto
.createHmac("sha256", process.env.RAZORPAY_KEY_SECRET)
.update(sign.toString())
.digest("hex");
if (razorpay_signature === expectedSign) {
return res.status(201).json({ message: "Payment verified successfully" });
} else {
return res.status(400).json({ message: "Invalid signature sent!" });
}
} catch (error) {
console.log(error);
return res.status(500).json({ message: "Internal server error" });
}
});
module.exports = router;