Fastify : Razorpay Webhook Signature validation

How to handler Razorpay Webhooks with signature validation

Firstly create a webhook, for local testing make use of ngrok port forwaring.

razorpay webhook fastify tutorial

Fastify Backend Code – Razorpay webhook

import { validateWebhookSignature } from 'razorpay/dist/utils/razorpay-utils';

export async function verificationHandler(
  request: FastifyRequest,
  reply: FastifyReply,
) {

  const webhookSignature = request.headers['x-razorpay-signature'] as string;
  const webhookBody = request.body;
  const webhookSecret = 'YOUR_WEBHOOK_SECRET'

  const isValidSignature = validateWebhookSignature(JSON.stringify(webhookBody), webhookSignature, webhookSecret)

  if (!isValidSignature) {
    console.log("invalid signature ....")

    reply.code(400).send('Invalid signature');

   console.log("signature validated....")
   //DO rest of actions

Node js example of using razorpay webhook handing with razorpay header signature. Given example fastify controller code for Razorpay webhook

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *