Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Validation of the webhook not working #434

Open
rohittiwari-dev opened this issue Feb 24, 2025 · 0 comments
Open

[Bug]: Validation of the webhook not working #434

rohittiwari-dev opened this issue Feb 24, 2025 · 0 comments

Comments

@rohittiwari-dev
Copy link

I used both the methods below mention and have encountered that what ever i try i am i getting unverified

i printed the custom signature generated it is resulting in some other signature than in the headers of the webhook call

export default async function handler(req, res) {
  const secret = process.env.RAZORPAY_WEBHOOK_SECRET;
  const signature = req.headers['x-razorpay-signature'];
  const body = JSON.stringify(req.body);

  // Verify webhook signature
  const generated_signature = crypto
    .createHmac('sha256', secret)
    .update(body)
    .digest('hex');

  if (generated_signature === signature) {
    const event = req.body.event;
    if (event === 'payment.captured') {
      const payment = req.body.payload.payment.entity;
      const paymentId = payment.id;
      const orderId = payment.order_id;
      // Update order status in your database
      // e.g., markOrderAsPaid(orderId, paymentId);
    }
    res.status(200).json({ status: 'success' });
  } else {
    res.status(400).json({ status: 'verification_failed' });
  }
}

and also

 const signature =
            (req.headers["x-razorpay-signature"] as string) ||
            (req.headers["X-Razorpay-Signature"] as string);

        const secrets = process.env.RAZORPAY_WEBHOOK_SECRET_KEY as string;
        if (!signature || !secrets) {
            throw new BadRequest(
                "Unverified Payment Details Received. Omitted Adding Subscriptions"
            );
        }
        if (
            !validateWebhookSignature(
                JSON.stringify(req.body),
                signature,
                secrets
            )
        ) {
            throw new BadRequest(
                "Unverified Payment Details Received. Omitted Adding Subscriptions"
            );
        }

in both the ways i am getting different signature in webhook header then i am creating expected signature and the function razopay is giving is also returning false in this particular case

can some one help me out if i am doing wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant