Category

Other

Version

1.0.0

Last updated

Oct 30, 2024, 13:03:36 PMa month ago

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 clipboardpayment.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:
npm install medusa-razorpay-webhook

Configuration

1. Update medusa-config.js

Add Copy to clipboardmedusa-razorpay-webhook to the plugins array in your Copy to clipboardmedusa-config.js file:
const 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:
RAZORPAY_WEBHOOK_SECRET=your_razorpay_webhook_secret
RAZORPAY_ID=your_razorpay_id
RAZORPAY_SECRET=your_razorpay_secret
Replace the placeholder values with your actual Razorpay credentials:
  • Copy to clipboardyour_razorpay_webhook_secret: The webhook secret provided by you in Razorpay
  • Copy to clipboardyour_razorpay_id: Your Razorpay Key ID
  • Copy to clipboardyour_razorpay_secret: Your Razorpay Key Secret

Usage

Webhook URL

The plugin sets up a webhook endpoint at:
/webhook/razorpay
For example, if your Medusa server is running locally, the full URL would be:
http://localhost:9000/webhook/razorpay
Use this URL when configuring webhooks in your Razorpay dashboard.

Supported Events

Currently, the plugin handles the following Razorpay events:
  1. Copy to clipboardpayment.failed
  2. Copy to clipboardpayment.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:
  1. Immediately after the order is completed, the plugin uses the Razorpay order status endpoint to verify the payment status.
  2. If the order status endpoint confirms the payment, the order is marked as paid.
  3. 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 clipboardRAZORPAY_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:
  1. Ensure your environment variables (Copy to clipboardRAZORPAY_WEBHOOK_SECRET, Copy to clipboardRAZORPAY_ID, and Copy to clipboardRAZORPAY_SECRET) are correctly set in your Copy to clipboard.env file.
  2. Verify that the webhook URL is correctly configured in your Razorpay dashboard.
  3. Check your Medusa server logs for any error messages related to webhook processing, Razorpay API calls, or order status checks.
  4. Make sure the plugin is properly configured in your Copy to clipboardmedusa-config.js file.
  5. 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.

Build your own plugins

Develop your own plugins with our API to speed up your processes.

Make your plugin available via npm for it to be shared in our Plugin Library with the broader Medusa community.