razorpay / razorpay/razorpay-node
cant verify signature
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 243
- Forks
- 128
- PR merge metrics
- No merged PRs in 30d
Description
Steps to reproduce the behavior
import crypto from "crypto";
import prisma from "@/utils/connect";
import { NextResponse } from "next/server";
export const POST = async (req) => {
try {
const body = await req.json();
const { event, payload } = body;
// Your Razorpay key secret
const razorpayKeySecret = "**************";
// Retrieve the Razorpay signature from the x-razorpay-signature header
const razorpaySignature = req.headers.get("x-razorpay-signature");
// Verify the Razorpay signature
const generatedSignature = crypto
.createHmac("sha256", razorpayKeySecret)
.update(JSON.stringify(payload))
.digest("hex");
if (generatedSignature !== razorpaySignature) {
console.error("Invalid Razorpay signature");
return new NextResponse("Invalid Razorpay signature", { status: 401 });
}
switch (event) {
case "payment.authorized":
break;
case "payment.captured":
const donationId = payload.payment.entity.notes.donationId;
if (donationId) {
await prisma.donation.update({
where: { id: donationId },
data: { status: "Success" },
});
console.log("Payment Captured:", donationId);
} else {
console.error(
"Invalid or missing donationId from Razorpay webhook payload"
);
return new NextResponse("Invalid or missing donationId", {
status: 400,
});
}
break;
default:
// Handle other events if needed
console.log("Unhandled Event:", event);
}
return new NextResponse("Webhook Received", { status: 200 });
} catch (error) {
console.error("Error processing webhook:", error);
return new NextResponse(
JSON.stringify({ message: "Something went wrong!" }, { status: 500 })
);
}
};
Expected behavior
it should verify signature but no , its not, please help me if anyone can
Actual behavior
its not matching both the values
Code snippets
import crypto from "crypto";
import prisma from "@/utils/connect";
import { NextResponse } from "next/server";
export const POST = async (req) => {
try {
const body = await req.json();
const { event, payload } = body;
// Your Razorpay key secret
const razorpayKeySecret = "**************";
// Retrieve the Razorpay signature from the x-razorpay-signature header
const razorpaySignature = req.headers.get("x-razorpay-signature");
// Verify the Razorpay signature
const generatedSignature = crypto
.createHmac("sha256", razorpayKeySecret)
.update(JSON.stringify(payload))
.digest("hex");
if (generatedSignature !== razorpaySignature) {
console.error("Invalid Razorpay signature");
return new NextResponse("Invalid Razorpay signature", { status: 401 });
}
switch (event) {
case "payment.authorized":
break;
case "payment.captured":
const donationId = payload.payment.entity.notes.donationId;
if (donationId) {
await prisma.donation.update({
where: { id: donationId },
data: { status: "Success" },
});
console.log("Payment Captured:", donationId);
} else {
console.error(
"Invalid or missing donationId from Razorpay webhook payload"
);
return new NextResponse("Invalid or missing donationId", {
status: 400,
});
}
break;
default:
// Handle other events if needed
console.log("Unhandled Event:", event);
}
return new NextResponse("Webhook Received", { status: 200 });
} catch (error) {
console.error("Error processing webhook:", error);
return new NextResponse(
JSON.stringify({ message: "Something went wrong!" }, { status: 500 })
);
}
};
Node version
latest
Library version
latest
Additional Information
No response
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no repository file; start with the Next.js POST handler shown, checking how req.json(), the x-razorpay-signature header, and the crypto HMAC input are obtained. Reproduce the webhook and confirm the computed signature matches the header and the handler reaches its successful response.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, next.js, node.js
- Domain
- api, payments, security
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100