medusa-razorpay-webhook
A Medusa.js plugin that handles Razorpay webhook events for payment processing and instantly update order payment status just after the order is completed.
Features
- Handles Razorpay webhook events
- Currently supports Copy to clipboard
payment.failed
and Copy to clipboardpayment.captured
events - Secure webhook handling with a secret key
- Uses Razorpay order status endpoint for immediate payment verification
Installation
To install the plugin, run the following command in your Medusa project:
1npm install medusa-razorpay-webhook
Configuration
1. Update medusa-config.js
Add Copy to clipboard
medusa-razorpay-webhook
to the plugins array in your Copy to clipboardmedusa-config.js
file:12345678910const plugins = [// ... other plugins{resolve: `medusa-razorpay-webhook`,options: {key_id: process.env.RAZORPAY_ID,key_secret: process.env.RAZORPAY_SECRET}},]
2. Set up environment variables
Add the following variables to your Copy to clipboard
.env
file:123RAZORPAY_WEBHOOK_SECRET=your_razorpay_webhook_secretRAZORPAY_ID=your_razorpay_idRAZORPAY_SECRET=your_razorpay_secret
Replace the placeholder values with your actual Razorpay credentials:
- Copy to clipboard
your_razorpay_webhook_secret
: The webhook secret provided by you in Razorpay - Copy to clipboard
your_razorpay_id
: Your Razorpay Key ID - Copy to clipboard
your_razorpay_secret
: Your Razorpay Key Secret
Usage
Webhook URL
The plugin sets up a webhook endpoint at:
1/webhook/razorpay
For example, if your Medusa server is running locally, the full URL would be:
1http://localhost:9000/webhook/razorpay
Use this URL when configuring webhooks in your Razorpay dashboard.
Supported Events
Currently, the plugin handles the following Razorpay events:
- Copy to clipboard
payment.failed
- Copy to clipboard
payment.captured
When these events are received, the plugin will process them accordingly within your Medusa instance.
Checking Order Payment Status
This plugin supports checking if an order is paid when the checkout is completed. Here's how it works:
- Immediately after the order is completed, the plugin uses the Razorpay order status endpoint to verify the payment status.
- If the order status endpoint confirms the payment, the order is marked as paid.
- In case of any failure or if the payment is not immediately confirmed, the plugin will wait for the webhook event to update the order status.
This dual approach ensures that the order status is updated as quickly as possible while also providing a fallback mechanism for cases where there might be delays or issues with the immediate status check.
Security
The Copy to clipboard
RAZORPAY_WEBHOOK_SECRET
is used to verify that incoming webhook requests are actually from Razorpay. The Copy to clipboardRAZORPAY_ID
and Copy to clipboardRAZORPAY_SECRET
are used for authenticating API requests to Razorpay, including the order status checks. Always keep these secrets secure and never expose them publicly.Development and Extending
If you need to handle additional Razorpay events or extend the plugin's functionality, you can modify the plugin code. Refer to the Medusa documentation on how to extend plugins.
Troubleshooting
If you're having issues with webhook events not being processed or payment status checks:
- Ensure your environment variables (Copy to clipboard
RAZORPAY_WEBHOOK_SECRET
, Copy to clipboardRAZORPAY_ID
, and Copy to clipboardRAZORPAY_SECRET
) are correctly set in your Copy to clipboard.env
file. - Verify that the webhook URL is correctly configured in your Razorpay dashboard.
- Check your Medusa server logs for any error messages related to webhook processing, Razorpay API calls, or order status checks.
- Make sure the plugin is properly configured in your Copy to clipboard
medusa-config.js
file. - If orders are not being marked as paid immediately, check if there are any network issues or delays in the Razorpay API response.
Support
For any issues or questions, please open an issue in the GitHub repository or contact the plugin maintainer.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.