-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
192 lines (176 loc) · 5.46 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const express = require("express");
const Razorpay = require("razorpay");
const fs = require("fs");
const {
validateWebhookSignature
} = require("razorpay/dist/utils/razorpay-utils");
const {
RAZORPAY_KEY_ID,
RAZORPAY_KEY_SECRET,
RAZORPAY_WEBHOOK_SECRET
} = require("./config");
const app = express();
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + "/public"));
// Utility functions
const readData = () =>
fs.existsSync("orders.json")
? JSON.parse(fs.readFileSync("orders.json"))
: [];
const writeData = (data) =>
fs.writeFileSync("orders.json", JSON.stringify(data, null, 2));
if (!fs.existsSync("orders.json")) writeData([]); // Initialize file if not exists
const razorpay = new Razorpay({
key_id: RAZORPAY_KEY_ID,
key_secret: RAZORPAY_KEY_SECRET
});
// Signature validation function
const validateSignature = (body, signature, secret, type = "webhook") => {
console.log(`Validating signature for ${type}`);
try {
if (type === "webhook") {
return validateWebhookSignature(JSON.stringify(body), signature, secret);
}
return validateWebhookSignature(body, signature, secret);
} catch (error) {
console.error("Error validating signature", error);
return false;
}
};
// Generic payment verification handler
const handlePaymentVerification = (
orderId,
paymentId,
status = "paid",
notes = []
) => {
console.log("Verifying payment:", orderId, paymentId, status, notes);
const orders = readData();
const order = orders.find((o) => o.order_id === orderId);
if (order) {
order.status = status;
order.payment_id = paymentId;
writeData(orders);
return true;
}
return false;
};
// Routes
app.post("/create-order", async (req, res) => {
try {
const { amount, currency } = req.body;
const options = {
amount: amount * 100,
currency,
receipt: "order_rcptid_11",
notes: {
userId: "123",
plan: "basic",
email: "[email protected]"
}
};
const response = await razorpay.orders.create(options);
console.log("Order created:", response);
const orders = readData();
orders.push({
order_id: response.id,
amount: response.amount,
currency: response.currency,
receipt: response.receipt,
notes: response.notes,
createdBy: "123"
});
writeData(orders);
res.status(200).send(response);
} catch (error) {
console.error("Error creating order:", error);
res.status(500).send(error);
}
});
app.post("/verify-payment", async (req, res) => {
console.log("Verifying payment in callback route", req.body);
const { razorpay_order_id, razorpay_payment_id, razorpay_signature } =
req.body;
const body = razorpay_order_id + "|" + razorpay_payment_id;
const isValidSignature = validateSignature(
body,
razorpay_signature,
RAZORPAY_KEY_SECRET,
"callback"
);
if (isValidSignature) {
const isPaymentVerified = handlePaymentVerification(
razorpay_order_id,
razorpay_payment_id
);
if (isPaymentVerified) {
console.log("Payment verification complete in callback route");
res.status(200).json({ status: "ok" });
} else {
console.log("Payment verification failed in callback route");
res.status(400).json({ status: "payment verification_failed" });
}
} else {
console.log("Signature verification failed in callback route");
res.status(400).json({ status: "signature verification_failed" });
}
});
app.post("/payment-webhook", async (req, res) => {
console.log("Received webhook payload:", req.body);
const signature = req.headers["x-razorpay-signature"];
console.log("Received webhook signature:", signature);
const isValidSignature = validateSignature(
req.body,
signature,
RAZORPAY_WEBHOOK_SECRET,
"webhook"
);
console.log("Signature verification status:", isValidSignature);
if (isValidSignature) {
const { event, payload } = req.body;
switch (event) {
case "payment.captured":
console.log("Captured payment entity:", payload.payment.entity);
const isPaymentVerified = handlePaymentVerification(
payload.payment.entity.order_id,
payload.payment.entity.id,
"paid",
payload.payment.entity.notes
);
if (isPaymentVerified) {
console.log("Payment verification complete in webhook route");
res.status(200).json({ status: "ok" });
} else {
console.log("Payment verification failed in webhook route");
res.status(400).json({ status: "payment verification_failed" });
}
break;
default:
console.log("Unhandled webhook event:", event);
res.status(200).json({ status: "ok" });
}
} else {
console.log("Signature verification failed in webhook route");
res.status(400).json({ message: "signature verification_failed" });
}
});
app.get("/", (req, res) => res.send("Hello World"));
app.get("/orders", (req, res) => {
const orders = readData();
res.status(200).json(orders);
});
app.get("/purchase", (req, res) =>
res.sendFile(__dirname + "/public/purchase.html")
);
app.get("/payment-cancel", (req, res) =>
res.sendFile(__dirname + "/public/payment-cancel.html")
);
app.get("/payment-success", (req, res) =>
res.sendFile(__dirname + "/public/payment-success.html")
);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () =>
console.log(`Server is running on http://localhost:${PORT}`)
);