Beejartha

Categories
Node & React Js

Integrating India’s UPI API with Node.js: A Comprehensive Guide

In the ever-evolving landscape of digital payments, India’s Unified Payments Interface (UPI) stands out as a revolutionary system. Launched by the National Payments Corporation of India (NPCI), UPI has transformed the way transactions are conducted in India, providing a seamless and secure platform for both users and businesses.

For developers and businesses looking to leverage UPI for payment processing, integrating UPI with your application can open doors to a vast market. In this blog post, we’ll explore what UPI is, and guide you through the process of integrating UPI with a Node.js application using the UPI API.

What is UPI?

UPI is a real-time payment system that facilitates instant money transfers between banks through mobile devices. It simplifies transactions by linking multiple bank accounts to a single mobile application and allows for easy and quick payments with just a few taps. UPI supports a range of transactions including bill payments, peer-to-peer transfers, and merchant payments.

Benefits of UPI Integration

  1. Seamless Transactions: UPI transactions are instantaneous and can be completed 24/7, including on holidays.
  2. Cost-Effective: UPI transactions typically incur lower costs compared to traditional banking methods.
  3. User-Friendly: UPI provides a straightforward and easy-to-use interface for users, enhancing their overall experience.
  4. Security: UPI transactions are encrypted and include multiple layers of security to protect users.

Integrating UPI with Node.js

To integrate UPI with a Node.js application, you’ll need to follow these steps:

Step 1: Set Up Your Development Environment

Before starting the integration process, ensure you have Node.js and npm (Node Package Manager) installed. If not, download and install them from the official Node.js website.

Step 2: Choose a UPI Service Provider

UPI integration is typically done through third-party service providers or payment gateways that offer UPI services. Some popular providers include Razorpay, Paytm, and PhonePe. For this guide, we’ll use Razorpay as an example. Sign up for a Razorpay account and obtain your API key and secret.

Step 3: Install Necessary Packages

First, create a new Node.js project or navigate to your existing project directory. Install the razorpay package using npm:

npm install razorpay

Step 4: Initialize Razorpay in Your Node.js Application

Create a new file, upi Integration.js, and initialize Razorpay with your API credentials:

const Razorpay = require('razorpay');
const razorpay = new Razorpay({
key_id: 'YOUR_RAZORPAY_KEY_ID',
key_secret: 'YOUR_RAZORPAY_KEY_SECRET',
});

Step 5: Create a Payment Order

To initiate a UPI payment, you need to create a payment order. This involves specifying the amount, currency, and payment method. Here’s an example of how to create an order:

const createOrder = async () => {
try {
const order = await razorpay.orders.create({
amount: 50000, // Amount in paise (50000 paise = ₹500)
currency: 'INR',
payment_capture: 1, // 1 to capture payment automatically, 0 otherwise
});
return order;
} catch (error) {
console.error('Error creating order:', error);
throw error;
}
};
createOrder().then(order => {
console.log('Order created:', order);
});

Step 6: Implement UPI Payment Gateway

Once the order is created, you need to redirect the user to the UPI payment gateway. This step involves using Razorpay’s payment link or integrating their frontend SDK. Here’s a basic example of how you might create a payment link:

const generatePaymentLink = async (orderId) => {
const paymentLink = `https://api.razorpay.com/v1/checkout/1?order_id=${orderId}`;
return paymentLink;
};
createOrder().then(order => {
generatePaymentLink(order.id).then(link => {
console.log('Payment link:', link);
});
});

Step 7: Handle Payment Confirmation

After the user completes the payment, Razorpay will send a callback to your server with payment details. Implement a webhook to handle payment confirmation:

const express = require('express');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const app = express();
app.use(bodyParser.json());
const webhookSecret = 'YOUR_WEBHOOK_SECRET';
app.post('/webhook', (req, res) => {
const hmac = crypto.createHmac('sha256', webhookSecret);
const sig = hmac.update(JSON.stringify(req.body)).digest('hex');
if (sig === req.headers['x-razorpay-signature']) {
// Payment verified
console.log('Payment details:', req.body);
res.status(200).send('Success');
} else {
// Payment verification failed
res.status(400).send('Invalid signature');
}
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});

Step 8: Testing and Deployment

Thoroughly test your UPI integration using Razorpay’s test mode or sandbox environment. Ensure that you handle different payment scenarios, such as successful payments, failed payments, and refunds.
Once testing is complete, deploy your application to a production environment and switch to Razorpay’s live credentials.

Conclusion

Integrating UPI with your Node.js application can significantly enhance the payment experience for your users. By leveraging UPI’s seamless and secure transaction capabilities, you can streamline payment processes and tap into India’s vast market. With the steps outlined in this guide, you should be well on your way to implementing UPI in your Node.js application.

Happy coding and may your payment integration be smooth and successful! If you have any questions or run into issues, feel free to ask in the comments below.

By Eric K

Experienced Software Developer with a demonstrated history of working in the computer software industry. Skilled in React.js, Vue.js, JavaScript and Node.js.

Any Query?

Ask Beejartha