Integrating Razorpay sdk in NodeJS App

razorpay

Razorpay SDK is a popular payment gateway that provides secure and reliable payment processing services to businesses of all sizes. In this article, we will guide you through the process of integrating Razorpay into your NodeJS app.

What is Razorpay?

Razorpay is a payment gateway that allows businesses to accept online payments via credit card, debit card, net banking, UPI, and wallets. It was founded in 2014 and has quickly become one of the most popular payment gateways in India.

Razorpay is known for its fast onboarding process, simple integration, and excellent customer support.

Why Use razorpay sdk in NodeJS?

NodeJS is a popular platform for building scalable and high-performance web applications. Integrating Razorpay in a NodeJS app can be a straightforward process using the Razorpay NodeJS SDK. Razorpay’s SDK provides an easy-to-use interface to integrate Razorpay’s payment gateway in a NodeJS app.

Creating a Razorpay Account

Before integrating Razorpay in your NodeJS app, you need to create a Razorpay account. Go to the Razorpay website and click on the “Sign Up” button. Fill in the required details and create an account.

Getting API Keys from Razorpay

After creating an account, you need to generate the API keys required to integrate Razorpay in your NodeJS app. Go to the Razorpay Dashboard and click on the “API Keys” tab. You will find your “Key ID” and “Key Secret” here. Note down these API keys as you will need them later.

Installing Razorpay NodeJS SDK

To integrate Razorpay in your NodeJS app, you need to install the Razorpay NodeJS SDK. Open your terminal and run the following command:

npm install razorpay

Implementing Razorpay Payment Gateway

Creating a Razorpay Payment Form

First, create a Razorpay payment form where the user can enter their payment details. The following code shows an example Razorpay payment form:

<form>
  <script
    src="https://checkout.razorpay.com/v1/checkout.js"
    data-key="YOUR_API_KEY"
    data-amount="5000"
    data-currency="INR"
    data-name="Acme Corp"
    data-description="Test Transaction"
    data-image="https://example.com/logo.png"
    data-theme.color="#F37254"
  ></script>
  <input type="hidden" value="Hidden Element" name="hidden">
</form>

Handling Payment Response

After the user submits the payment form, Razorpay will send a response to your server with the payment details. You need to handle this response on the server-side using the Razorpay NodeJS SDK. Here’s an example code snippet to handle the payment response:

const Razorpay = require('razorpay');

const razorpay = new Razorpay({
  key_id: 'YOUR_API_KEY',
  key_secret: 'YOUR_API_SECRET'
});

app.post('/payment/success', async (req, res) => {
  const paymentId = req.body.razorpay_payment_id;
  const orderId = req.body.razorpay_order_id;
  const signature = req.body.razorpay_signature;

  try {
    const resp = await razorpay.payments.capture(paymentId, orderId, signature);
    res.send('Payment successful');
  } catch (error) {
    res.send('Payment failed');
  }
});

Verifying Payment Signature

It’s crucial to verify the payment signature to ensure the authenticity of the payment response. Razorpay uses the HMAC-SHA256 algorithm to generate the signature. You need to generate the signature on the server side and compare it with the signature sent by Razorpay. Here’s an example code snippet to generate the signature:

const crypto = require('crypto');

const generateSignature = (orderId, paymentId, secret) => {
  const text = orderId + '|' + paymentId;
  const signature = crypto
    .createHmac('sha256', secret)
    .update(text)
    .digest('hex');
  return signature;
};

Adding Razorpay Webhooks

Razorpay provides webhooks to notify your server about various events related to payments. You can use webhooks to update your database, send notifications, or perform other actions.
To use webhooks, you need to add a webhook URL in your Razorpay dashboard. Here’s an example code snippet to handle the webhook:

app.post('/razorpay-webhook', async (req, res) => {
  const { event, payload } = req.body;

  if (event === 'payment.captured') {
    // Handle payment captured event
  } else if (event === 'payment.failed') {
    // Handle payment failed event
  }

  res.send('Webhook received');
});

In the above code, /razorpay-webhook is the endpoint that receives the webhook from Razorpay. The event and payload are the parameters sent by Razorpay. You can handle different events based on the event parameter.

Integrating Razorpay in a NodeJS app is a straightforward process using the Razorpay NodeJS SDK. By following the steps mentioned in this article, you can easily add a secure and reliable payment gateway to your app. Remember to verify the payment signature and handle webhooks to ensure a smooth payment processing experience.

Follow Us on
https://www.linkedin.com/company/scribblers-den/

Read More
https://scribblersden.com/integrating-google-tag-manager-gtm-commerce-cloud/

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *