Home » Razorpay Integration Using NodeJS

Razorpay Integration Using NodeJS

Razorpay Integration Using NodeJS

Introduction

RazorPay is the first and the most convenient payment gateway service provider in India through which organizations are able to accept and collect online payments. Acceptable modes of payment include credit/debit cards, internet banking, Unified Payment Interface (UPI) and mobile wallet. Razorpay’s integration with Node. js application can be very useful for such developers to work in an organized manner and also more importantly, for handling payments.

Features of Razorpay Integration using Node js

  • Multiple payment methods: Accept payment by all modes – credit card, net banking, UPI and wallet facilities.
  • Easy integration: Helps user to get set up quickly with a clear API and SDK to integrate the website builder to existing projects easily.
  • Real-time data: Offers flexibility with and options for tracking payment status and transaction status respectively.
  • Safe Behavior: PCI-DSS makes sure that all the business follow set standards and very tight safety measures are conducted.
  • Custom Checkout: Make confirmation phase a reflection of your company and stick to it.
  • Subscription Billing: Extends support to the recurring or subscription-based billing system.
  • Detailed Analytics: Contains guidelines and tools for assessing transactions and revenues, as well as detailed reports and analytics data.

Installation

First, install the Razorpay Node.js SDK:

npm install razorpay
Bash

Examples with Output

1. Setup and Configuration

Create a basic Express server and configure Razorpay:

const express = require('express');
const Razorpay = require('razorpay');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());

const razorpay = new Razorpay({
  key_id: 'your_key_id',
  key_secret: 'your_key_secret',
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
JavaScript

2. Create an Order

Add a route to create a payment order:

app.post('/create-order', async (req, res) => {
  const { amount, currency, receipt } = req.body;

  try {
    const order = await razorpay.orders.create({
      amount: amount * 100, // amount in the smallest currency unit
      currency,
      receipt,
      payment_capture: '1', // auto capture
    });

    res.json(order);
  } catch (error) {
    res.status(500).send({ error: error.message });
  }
});
JavaScript

3. Client-Side Integration

Integrate Razorpay Checkout on the client side. Create an index.html file:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Razorpay Payment</title>
  <script src="https://checkout.razorpay.com/v1/checkout.js"></script>
</head>
<body>
  <h1>Razorpay Payment</h1>
  <button id="pay-btn">Pay Now</button>

  <script>
    document.getElementById('pay-btn').onclick = async function () {
      const response = await fetch('/create-order', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ amount: 1000, currency: 'INR', receipt: 'receipt#1' })
      });
      const order = await response.json();

      const options = {
        key: 'your_key_id', // Enter the Key ID generated from the Dashboard
        amount: order.amount,
        currency: order.currency,
        name: 'Acme Corp',
        description: 'Test Transaction',
        order_id: order.id,
        handler: function (response) {
          alert('Payment successful: ' + response.razorpay_payment_id);
        },
        prefill: {
          name: 'John Doe',
          email: 'john.doe@example.com',
          contact: '9999999999'
        }
      };

      const rzp1 = new Razorpay(options);
      rzp1.open();
    }
  </script>
</body>
</html>
HTML

why do we need it?

  • Comprehensive Payment Solution: One of the prime features of Razorpay is that it integrates with multiple payment modes so that customers are not left with a restricted option set.
  • Ease of Use: Its APIs and SDKs are easy to use and integrating the service into the software can be done easily and quickly.
  • Security: For example, Razorpay is safe as it follows a high level of security, meeting stringent requirements to help merchants minimize fraud while maximizing compliance with regulatory requirements.
  • Customization: This is because the checkout customization does not only maintain brand consistency for smaller sites but also improves user experience.
  • Analytics and Reporting: Reports and analysis showing the exact financial data of a business unit enable organizations to make profitable decisions.

Conclusion

The first step of integrating Razorpay with a Node is to first create a development account. Though there are many differences in the implementation of js application has a strong and secure solution for carrying out online payment. In view of the various features that it has to offer and how simple it is to use, Razorpay is a great tool to use by businesses mostly when it comes to checking on the way that its customers pay them.

Frequently Asked Questions

1. What is Razorpay?

Razorpay is an online payment gateway tool that lets the businesses to conduct the transactions through credit cards, net banking, UPI, wallet.

2. How can we Installation of Razorpay in Node. js project?

To integrate Razorpay, it can be installed through npm command like npm install razorpay.

3. Is Razorpay safe to use to facilitate transactions?

Yes, Razorpay is a PCI-DSS complaint platform, and the platform is also secured to prevent all transactions.