Nowadays, Every business makes their presence on the internet to grow their business. Online payment is now a common thing. It helps businesses to run their business more smoothly.
Depending on the business, they want to integrate a payment gateway for their platform or website. Selecting a payment gateway makes lives easier for the customer and the business owner. On the market, there are several payment gateways to choose from. But this is where we’ll look at the Razorpay payment gateway.
In this post, we’ll look at how to use Spring Boot (Java) and Angular(Client-Side) to integrate the Razorpay payment gateway and accept payments quickly and easily.
The Razorpay payment gateway is a software solution that makes it possible for individuals and businesses to accept payments and route the same to owners’ accounts.
By using Razorpay you can collect payment from customers using all payment modes, including debit and credit cards, UPI, and other popular mobile wallets.
With the help of Razorpay, you can automate bank transfers, collect recurring payments, share invoices with customers, and manage your marketplace.
Integration of the Razorpay payment gateway is different for different technologies. Here we are going to use Angular (Client-Side) and Spring Boot Java (Server-Side).
To integrate Razorpay, all you need to do is create a Razorpay account. Visit the link to create an account.
There are basically two environments provided by Razorpay payment.
Here we will use the test mode for integration.
Before accessing any API of Razorpay you will need Razorpay Id and Secret which you can get from the Razorpay dashboard.
– Go To Razorpay Dashboard
– On The Left Side, The Sidebar Menu Clicks On Settings. After That Click On The API Keys Tab
– Click On Generate A Key To View On Downloading The Id And Secret Key
Make sure the secret key will not be shared with anyone. We need a Razorpay Id at the client side.
Sample keys will look like below :
Key Id : rzp_test_********
Key Secret : i*********
For starting using Razorpay first you need to add the official Razorpay library into your project. This will allow you to access all Razorpay APIs.
– Server Side:
For the spring boot project, you will need to add dependency into the pom.xml file.
<dependency> <groupId>com.Razorpay</groupId> <artifactId>Razorpay-java</artifactId> <version>x.y.z</version> //x.y.z = the version you want to install </dependency>
– Client Side:
For the Angular side, you will need to run the following command to install the npm package of Razorpay.
npm i Razorpay
The Razorpay payment flow is depicted in the diagram below.
From the above steps, we have completed the setup of Razorpay into our project and understood the flow of payment checkout.
Let’s consider one scenario, you are building a web application for an e-commerce store. You want to create a checkout flow for purchasing any item. Then we need to follow the below process to accept customers’ payments through Razorpay checkout.
A customer visits your website or application, then selects the item they want to purchase. After clicking on the buy button the front end will call the Backend API with the price of the item to create an Order in Razorpay.
Use the Razorpay orders API to generate an order from your server for each customer order.
To build order in your (backend) server, use the endpoint below.
POST: /orders
try { JSONObject orderRequest = new JSONObject(); orderRequest.put("amount", 50000); // amount in the smallest currency unit orderRequest.put("currency", "INR"); orderRequest.put("receipt", "order_rcptid_11"); Order order = Razorpay.Orders.create(orderRequest); } catch (RazorpayException e) { // Handle Exception System.out.println(e.getMessage()); }
You can store customer details and order details in your payments table for future reference. When actual payment is completed by the customer at Razorpay that time using the order id you can verify the same purchase.
Pass the order_id returned by order create API of Razorpay to your front end for checkout.
There are two sample codes provided for checkout:
With Handler Function - When you use this: | With Callback URL - When you use this: |
---|---|
On successful payment, the customer will be shown your webpage. | On successful payment, the customer will be redirected to the specified URL, for example, a payment success page. |
On payment failure, the customer will be notified of the reason for failure and asked to retry the payment. | On payment failure, the customer will be asked to retry payment on Checkout. |
We will need to pass Razorpay Id, Amount, and Order Id which are generated from the server-side to Razorpay checkout for opening the payment window.
var options = { "key": "YOUR_KEY_ID", // Enter the Key ID generated from the Dashboard "amount": "50000", // Amount is in currency subunits. Default currency is INR. Hence, 50000 refers to 50000 paise "currency": "INR", "name": "Acme Corp", "description": "Test Transaction", "image": "https://example.com/your_logo", "order_id": "order_9A33XWu170gUtm", //This is a sample Order ID. Pass the `id` obtained in the response of Step 1 "handler": function (response){ alert(response.Razorpay_payment_id); alert(response.Razorpay_order_id); alert(response.Razorpay_signature) }, "prefill": { "name": "Gaurav Kumar", "email": "gaurav.kumar@example.com", "contact": "9999999999" }, "notes": { "address": "Razorpay Corporate Office" }, "theme": { "color": "#3399cc" } }; var rzp1 = new Razorpay(options); document.getElementById('rzp-button1').onclick = function(e){ rzp1.open(); e.preventDefault();
Depending on the Checkout sample code you chose in the previous step, the way you handle payment success and failure circumstances will be different.
The customer will be directed to your page, and Checkout will return the response object of the successful payment (Razorpay payment id, Razorpay order id, and Razorpay signature). These must be gathered and sent to your server.
– Payment Success
"handler": function (response){ alert(response.Razorpay_payment_id); alert(response.Razorpay_order_id); alert(response.Razorpay_signature) }
On payment failure, the customer will be notified of the reason for failure and asked to retry the payment.
– Payment Failure
rzp1.on('payment.failed', function (response){ alert(response.error.code); alert(response.error.description); alert(response.error.source); alert(response.error.step); alert(response.error.reason); alert(response.error.metadata.order_id); alert(response.error.metadata.payment_id); }
After payment is authorized, you need to capture the payment made by the customer for the amount to get settled in your bank account as per the settlement schedule. Payments that are not captured by you manually or automatically then those payments will be auto-refunded after a fixed time period.
Authorized payments can be automatically captured. You can auto captured payments using settings in the Razorpay dashboard.
Each authorized payment can also be captured individually. You can manually capture payments:
Try to call all Razorpay APIs from the server-side only. So store the secret key on the server-side only. Do not share a secret key to the client-side for security reasons.
When a payment made by the customer is authorized, it needs to settle in your bank account. For that, you must capture the payment by using any of the payment capture options. Payments that are not captured are auto-refunded after a fixed period of time.
Orders APIs help in binding multiple payment attempts against a single order. This helps to prevent multiple payments. Integrate with Orders API of the Razorpay on your server-side and pass the order_id to the client-side for the checkout process. When you create an order from the server-side you can store order details for the customer in your payments table and at the time of capturing payment, you can change the payment status against that order id.
Check the payment/order status, that is if the payment’s status is captured and the order’s status is paid before providing the services to the customers. It is a must-do activity to verify the payment is actually done or not and giving appropriate service to the customer.
Implement webhooks or the query API to avoid any cases of callback failure (drop-offs could be connectivity or network failure) and to verify the payment details via S2S(Server to Server) call.
In this article, we discussed the integration of the Razorpay payment gateway for the server-side and client-side.
More features of payments, API, and documentation you can find in the official documentation.
Thank you for reading!
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowThe Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork