Back

Quick Guide to Implementing Webhooks in Razorpay

Aug 16, 20246 minute read

Hey there, fellow JavaScript dev! Ready to level up your Razorpay integration with webhooks? Let's dive right in and get those real-time updates flowing!

Introduction

Webhooks are like your app's personal news feed from Razorpay. They'll ping you whenever something interesting happens, like a successful payment or a refund. Today, we're focusing on setting this up for your user-facing integration. Trust me, your users (and your sanity) will thank you!

Prerequisites

Before we start, make sure you've got:

  • A Razorpay account (duh!)
  • Node.js and npm installed
  • Some Express.js know-how

Got all that? Great! Let's code!

Setting Up the Webhook Endpoint

First things first, let's create a simple Express server to receive those juicy webhook events:

const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.json()); app.post('/webhook', (req, res) => { // We'll fill this in soon! console.log('Webhook received:', req.body); res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));

Easy peasy, right? We've got our server up and running, ready to receive those webhook events.

Configuring Webhooks in Razorpay Dashboard

Now, hop over to your Razorpay dashboard and let's tell it where to send those events:

  1. Navigate to Settings > Webhooks
  2. Click on "Add New Webhook"
  3. Enter your webhook URL (e.g., https://your-domain.com/webhook)
  4. Select the events you want to track (like payment.captured, payment.failed)
  5. Save it!

Handling Webhook Events

Alright, time for the fun part! Let's verify that incoming webhook and process those events:

const crypto = require('crypto'); app.post('/webhook', (req, res) => { const shasum = crypto.createHmac('sha256', 'YOUR_WEBHOOK_SECRET'); shasum.update(JSON.stringify(req.body)); const digest = shasum.digest('hex'); if (digest === req.headers['x-razorpay-signature']) { console.log('Request is legit'); // Process the webhook event switch (req.body.event) { case 'payment.captured': console.log('Payment successful'); // Update your database, send a confirmation email, etc. break; case 'payment.failed': console.log('Payment failed'); // Notify the user, log the failure, etc. break; // Add more cases as needed } res.sendStatus(200); } else { console.log('Invalid signature'); res.sendStatus(400); } });

Testing Webhooks

Time to put on your QA hat! Head to the Razorpay dashboard and use their webhook testing tool. Simulate some events and watch your server light up like a Christmas tree!

Best Practices

A few pro tips to keep your webhook game strong:

  • Log everything. Future you will thank present you.
  • Implement retry logic for those "just in case" moments.
  • Keep your endpoint secure. HTTPS is your friend!

Troubleshooting Common Issues

Running into problems? Here are some usual suspects:

  • Invalid signatures: Double-check that webhook secret!
  • Timeouts: Razorpay expects a quick response. Keep your processing snappy!

Conclusion

And there you have it! You're now a Razorpay webhook wizard. Your app is ready to handle real-time payment updates like a champ. Remember, practice makes perfect, so keep experimenting and refining your implementation.

Code Repository

Want to see it all put together? Check out this GitHub repo for a complete example.

Happy coding, and may your payments always be successful! 🚀💰