PhonePe Payment Gateway Integration using React, Node, Express
October 23, 2024 (2mo ago)
In this simple step-by-step guide, we will learn how to integrate the PhonePe payment gateway with Node and Express as the backend and React in the frontend. After reading this guide, you will be able to integrate the PhonePe payment gateway effectively. All the steps are very simple and easy to follow.
Project Setup
To follow this blog, you need some project prerequisites:
Node.js + NPM
Visual Studio Code
Any Browser
Now let's set up our project. First, create a folder with any name. For this tutorial, we will use the phonepe-payment-gateway folder. Now open this folder using VS Code. Create 2 folders inside this folder named:
client
server
Before starting the coding part lets try to understand the phone pe payment gateway api flow. For making it clear I have created this API Flow diagram.
User will fill the data like name, mobile, amount. Then it will hit our server api end point. Then our server will will send a request to the phone pe api end point. Which will send back the status of payment. Which we will get in server response then again wi will send the response to the client.
Frontend Setup
For this tutorial, I am using React as the frontend library, and for styling, I have used Tailwind CSS. Now click on the client folder and open the terminal. To install React, run this command:
Use . after vite@latest so that it will install the React in the current directory (client).
Then select React as framework and Javascript as language.
Install Tailwind CSS
Install tailwindcss and its peer dependencies, then generate your tailwind.config.js and postcss.config.js files.
Run these commands
Configure your template paths
Update the tailwind.config.js file with the following code:
Update index.css to use Tailwind CSS
Update App.jsx
Run Frontend
In this way you will be able to see the React application in your browser.
Frontend Designing
Now we are ready to design our frontend. To keep it simple we will create a form with 3 basic fields for taking users name, mobile, amount. Because will more focus on the backend.
Ok before designing the pages we need some dependencies. So let's install dependencies for React.
axios => To make HTTP requests
react-router-dom => To handle routing
To install these dependencies, run the following command:
Create Some Files
/src/components/Header.jsx
/src/components/Hero.jsx
/src/components/Payment.jsx
Now you are done with the design part. Now create the routes for frontend.
Main.jsx
Now frontend is complete. Now we will create our backend with Node and Express. Go open server folder and create a new file named server.js. Then run this command to initialize the npm.
keep everything as default. In package.json file, make some update.
Now for this project we will need some dependencies. So run the following command to install. Make sure you are in server folder.
express => to create server
nodemon => to run server without refreshing page
axios => to make http requests
cors => to allow cross-origin requests
crypto => for hashing
Now open server.js and add the following code. Before you start coding open this page of phonepe developer page for more details. Phone Pe API Guide
Now write the code. Then we will understand our code line by line.
Now run this command to start the server. Then you will be able to see the response in your browser.
To check the status of our server open http://localhost:8000/. It will show you Hello World!. It means our server is up and running. To test our payment gateway, go to http://localhost:5173/ or the client port. and fill the details like name, mobile, amount. If you have implemented the payment gateway correctly as described then it will redirect you to Phone Pay Payment Page. Now to test phone pe payment use dummy bedit card provided by phone pe.
Debit Card
Credit Card
NOTE : The OTP to be used on the Bank Page: 123456
If your payment is successful, it will redirect you to Success Page.
Understand the Backend Logic
/payment route
Get data from frontend using req.body
Store data in data named variable. (which will be send to phone pe server using api)
Generate the payLoad and checkSum using this code
Make a post request to phone pe server using axios
Return response to frontend
/status route
Get data from frontend using req.query
Store data in data named variable. (which will be send to phone pe server using api)
Generate the payLoad and checkSum using this code
Make a get request to phone pe server using axios
Return response to frontend
Congratulations! You have completed the tutorial. I hope it has helped you in understanding the PhonePe payment gateway integration. If you have any questions or suggestions, please don't hesitate to contact me. I'm here to help you!
For more insights on React and other web development topics, follow me for updates. shubhadipbhowmik