@akshay4362/razorpay-plugin
v3.0.0
Published
Plugin to enable payments through [Razorpay](https://razorpay.com/docs/) via the Orders API and Checkout.js.
Readme
Razorpay Payment Plugin
Plugin to enable payments through Razorpay via the Orders API and Checkout.js.
Requirements
You will need a Razorpay account and your Key ID / Key Secret from the dashboard (Settings -> API Keys).
Create a webhook in the Razorpay dashboard (Settings -> Webhooks, "Add New Webhook") which listens to the
payment.captured,payment.failed,refund.processed, andrefund.failedevents. The URL should behttps://my-server.com/payments/razorpay, wheremy-server.comis the host of your Vendure server.Get the webhook secret for the newly created webhook.
Install the plugin and the Razorpay Node library:
npm install @vendure-community/razorpay-plugin razorpay
Setup
- Add the plugin to your VendureConfig
pluginsarray:import { RazorpayPlugin } from '@vendure-community/razorpay-plugin'; // ... plugins: [ RazorpayPlugin.init({ // optional: see the RazorpayPluginOptions type for storeCustomersInRazorpay / refundSpeed }), ] - Create a new PaymentMethod in the Admin UI, and select "Razorpay payments" as the handler.
- Set the "Key ID", "Key Secret", and "Webhook Secret" arguments on the PaymentMethod form. Each PaymentMethod using the Razorpay handler can be configured with its own Razorpay account, so different channels/PaymentMethods can point at different accounts. Only one enabled PaymentMethod using the Razorpay handler is supported per channel at a time.
Storefront Usage
- Call the
createRazorpayOrdermutation to create a Razorpay Order for the active order, returning{ orderId, amount, currency, keyId }. - Pass these values into Razorpay Checkout.js:
const options = { key: keyId, amount, currency, order_id: orderId, handler: function (response) { // response.razorpay_order_id, response.razorpay_payment_id, response.razorpay_signature }, }; new Razorpay(options).open(); - On success, call Vendure's standard
addPaymentToOrdermutation with:
The plugin verifies the signature server-side before settling the payment.{ "method": "<your payment method code>", "metadata": { "razorpayOrderId": "<razorpay_order_id>", "razorpayPaymentId": "<razorpay_payment_id>", "razorpaySignature": "<razorpay_signature>" } }
The /payments/razorpay webhook acts as a reconciliation backstop only — it settles the order if the
storefront's addPaymentToOrder call never completes (e.g. the browser tab closed after payment).
Refunds
Creating a refund via the Admin UI (or the refundOrder mutation) calls the
Razorpay Refunds API. By default, refunds are processed at
Razorpay's 'normal' speed and settle asynchronously (typically 5-7 days later); set the refundSpeed plugin
option to 'optimum' to let Razorpay attempt an instant refund where supported, falling back to normal
processing otherwise.
Because a 'normal'-speed refund is created in a Pending state, the plugin registers a custom refund
process that permits a Pending -> Pending self-transition (Vendure's default process only allows
Pending -> Settled | Failed). The /payments/razorpay webhook then reconciles the refund to Settled or
Failed once Razorpay sends the corresponding refund.processed/refund.failed event.
Local Development
Set RAZORPAY_KEY_ID, RAZORPAY_KEY_SECRET, and RAZORPAY_WEBHOOK_SECRET in a .env file in this package
(these seed the dev-server's Razorpay PaymentMethod handler arguments, not RazorpayPlugin.init()), then run:
npm run dev-server