razorpay-sdk
v1.0.6
Published
A lightweight, secure, and reusable Node.js SDK for integrating Razorpay payments efficiently, with built-in helpers for order creation and payment verification.
Maintainers
Readme
Razorpay SDK for Node.js
A lightweight, secure, and reusable Node.js module for integrating Razorpay payments that saves development time and costs.
Overview
The razorpay-sdk is a comprehensive solution for backend payment integration with Razorpay. It simplifies common payment operations while ensuring security and scalability for applications of all sizes.
Key Features
Simplified Order Management
- Easily create payment orders with custom options
- Support for multiple currencies
- Custom notes and metadata for orders
Payment Verification
- Secure verification of payment responses
- Protection against fraudulent transactions
Webhook Handling
- Automatic verification of Razorpay webhooks
- Reliable handling of payment events
Multi-Client / Multi-Tenant Support
- Manage multiple merchant accounts in one application
- Isolated and secure account handling
Security & Compliance
- HMAC signature validation for all operations
- Safe handling of sensitive API keys
- No hardcoded credentials
Extensible & Scalable
- Supports refunds, order listing, and payment details
- Compatible with serverless environments
- Easy to extend with custom functionality
Installation
For local development:
npm installTo use this package in another project locally:
npm install /path/to/this/directoryOr if you publish it to npm:
npm install @local/razorpay-sdkUsage
Single Client Setup
const RazorpaySDK = require('@local/razorpay-sdk');
// Initialize with your API keys
const razorpay = new RazorpaySDK('YOUR_KEY_ID', 'YOUR_KEY_SECRET');Creating an Order
async function createOrder() {
try {
const order = await razorpay.createOrder({
amount: 50000, // amount in smallest currency unit (50000 paise = INR 500)
currency: 'INR',
receipt: 'order_rcptid_11',
notes: {
'description': 'Test order'
}
});
console.log('Order created:', order);
return order;
} catch (error) {
console.error('Error:', error.message);
}
}Verifying a Payment
// After receiving payment response in your backend
const isValid = razorpay.verifyPayment(
razorpay_order_id,
razorpay_payment_id,
razorpay_signature
);
if (isValid) {
// Payment is valid, proceed with business logic
console.log('Payment verified successfully');
} else {
// Invalid payment, do not proceed
console.log('Payment verification failed');
}Creating a Payment Link
async function createPaymentLink() {
try {
const paymentLink = await razorpay.createPaymentLink({
amount: 50000, // amount in smallest currency unit (50000 paise = INR 500)
currency: 'INR',
description: 'Test payment',
customer: {
name: 'John Doe',
email: '[email protected]',
contact: '+919876543210'
},
notes: {
'reference': 'Test payment link'
}
// UPI is enabled by default for Indian merchants
// No additional configuration needed for basic UPI support
});
console.log('Payment link created:', paymentLink);
return paymentLink;
} catch (error) {
console.error('Error:', error.message);
}
}Creating a UPI-Specific Payment Link
async function createUpiPaymentLink() {
try {
const paymentLink = await razorpay.createUpiPaymentLink({
amount: 50000, // amount in smallest currency unit (50000 paise = INR 500)
currency: 'INR',
description: 'Test UPI payment',
customer: {
name: 'John Doe',
email: '[email protected]',
contact: '+919876543210'
},
notes: {
'reference': 'Test UPI payment link'
},
// Optional: Specify a default UPI VPA
upiVpa: 'john@upi' // This is just an example
});
console.log('UPI Payment link created:', paymentLink);
return paymentLink;
} catch (error) {
console.error('Error:', error.message);
}
}createPaymentLink(options)
Create a payment link for customers to make payments.
createUpiPaymentLink(options)
Create a UPI-specific payment link for customers to make payments.
Options:
amount(number) - Amount in smallest currency unitcurrency(string) - Currency code (e.g., 'INR')description(string) - Description of the paymentcustomer(object) - Customer detailsname(string) - Customer nameemail(string) - Customer emailcontact(string) - Customer contact number
notes(object) - Custom notes for the paymentupiVpa(string, optional) - Default UPI Virtual Payment Address
Note: UPI payments are enabled by default for Indian merchants. Customers will see the UPI option when they open the payment link on their mobile devices.
Multi-Tenant Support
const MultiTenantRazorpay = require('@local/razorpay-sdk/lib/multi-tenant-sdk');
const multiTenantRazorpay = new MultiTenantRazorpay();
// Add multiple merchant clients
multiTenantRazorpay.addClient('client1', 'CLIENT1_KEY_ID', 'CLIENT1_KEY_SECRET');
multiTenantRazorpay.addClient('client2', 'CLIENT2_KEY_ID', 'CLIENT2_KEY_SECRET');
// Get a specific client
const client1 = multiTenantRazorpay.getClient('client1');
// Create an order for client1
const order = await client1.createOrder({
amount: 30000,
currency: 'INR'
});API Reference
RazorpaySDK
new RazorpaySDK(key_id, key_secret)
Initialize the SDK with your Razorpay API credentials.
createOrder(options)
Create a new payment order.
verifyPayment(razorpay_order_id, razorpay_payment_id, razorpay_signature)
Verify a payment response.
verifyWebhook(body, signature)
Verify a webhook signature.
fetchPayment(payment_id)
Fetch payment details.
capturePayment(payment_id, amount, currency)
Capture a payment.
refundPayment(payment_id, options)
Refund a payment.
fetchOrders(options)
Fetch all orders.
createPaymentLink(options)
Create a payment link for customers to make payments.
createUpiPaymentLink(options)
Create a UPI-specific payment link for customers to make payments.
Options:
amount(number) - Amount in smallest currency unitcurrency(string) - Currency code (e.g., 'INR')description(string) - Description of the paymentcustomer(object) - Customer detailsname(string) - Customer nameemail(string) - Customer emailcontact(string) - Customer contact number
notes(object) - Custom notes for the paymentupi(object) - UPI payment optionsenable(boolean) - Enable UPI paymentsvpa(string) - Default UPI Virtual Payment Address
method(object) - Payment method optionsnetbanking(boolean) - Enable netbankingcard(boolean) - Enable card paymentswallet(boolean) - Enable wallet paymentsupi(boolean) - Enable UPI payments
fetchPaymentLink(paymentLinkId)
Fetch a payment link by its ID.
MultiTenantRazorpay
addClient(clientId, key_id, key_secret)
Add a new merchant client.
getClient(clientId)
Get a merchant client by ID.
removeClient(clientId)
Remove a merchant client.
listClients()
List all client IDs.
Benefits
- Saves Development Time: Pre-built functions for all common Razorpay operations
- Reduces Errors: Built-in validation and error handling
- Supports Growth: Multi-tenant architecture for scaling
- Secure by Design: Cryptographically secure verification methods
- Cost Effective: Reduces development and maintenance costs
- Easy Integration: Simple API that works with any Node.js application
Testing
Run the test suite:
npm testLicense
MIT
Support
For issues and feature requests, please open an issue on GitHub.
