Google trends Public Holidays Coupon Code Code Compiler

Paytm Payment gateway integration using Node js


Nov 21, 2023

Paytm Payment gateway integration using Node js

Learn how to seamlessly integrate the Paytm Payment Gateway into your Node.js application.

Creating a Paytm payment gateway for your Node.js application is achievable by leveraging the Paytm Developer API. This API allows you to seamlessly integrate Paytm's payment functionality into your Node.js project. Let's take a look at a simple example to understand how you can establish a fundamental payment gateway using Paytm.

Explanation:

If you want to make your Node.js application capable of handling payments through Paytm, you'll need to use the Paytm Developer API. This API serves as a bridge, connecting Paytm's secure payment features with the capabilities of your Node.js application. The integration process involves incorporating Paytm's payment functionalities seamlessly into your Node.js project.

Example:

Here's a straightforward example to illustrate the process of setting up a basic payment gateway using Paytm in your Node.js application. This example will help you understand the essential steps involved in making your application ready to process payments.

Setup your Node.js app and Install the required dependencies:


npm install express axios

Create a new file called app.js and add the following code:


const express = require('express');
const axios = require('axios');

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

// Configure Paytm credentials
const PAYTM_MERCHANT_KEY = 'YourMerchantKey';
const PAYTM_MID = 'YourMerchantID';
const PAYTM_WEBSITE = 'YourWebsite';

// Define the payment route
app.post('/payment', async (req, res) => {
  const { email, mobileNumber, orderId, amount } = req.body;

  try {
    // Generate a transaction token
    const token = await generateToken(orderId, amount);

    // Create a payment request body
    const paymentRequestBody = {
      token,
      amount,
      orderId,
      customerId: email,
      email,
      mobileNumber,
      website: PAYTM_WEBSITE,
      callbackUrl: 'YourCallbackURL' // Replace with your callback URL
    };

    // Make a request to initiate the paytm payment     
 const response = axios.post('https://securegw-stage.paytm.in/theia/api/v1/initiateTransaction?mid=' + PAYTM_MID + '&orderId;=' + orderId, paymentRequestBody, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token
      }
    });

    // Return the payment URL to the client
    res.json({ paymentUrl: response.data.body.txnToken });
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: 'Failed payment' });
  }
});

// Generate the transaction token using Paytm secure gateway API
async function generateToken(orderId, amount) {
  try {    
 const response = axios.post('https://securegw-stage.paytm.in/theia/api/v1/token?mid=' + PAYTM_MID + '&orderId;=' + orderId, {
      amount,
      customerId: 'YourCustomerId' // Replace with your customer ID
    });

    return response.data.body.txnToken;
  } catch (error) {
    throw new Error('Failed to generate token');
  }
}

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

Conclusion:

Summarize the key points covered in the article and encourage readers to apply their newfound knowledge to enhance the payment functionality of their Node.js applications using the Paytm Payment Gateway.

Copyright 2024. All rights are reserved