Node.js Implementation of Razorpay Payment API
The payment gateway, of which there are many on the market, is where every online transaction is processed. You could look here. Here, I’ll explain how to use Node.js to implement Razorpay Payment API on an online store.
Online payment is a technique to make a specific purchase-related payment through the internet. E-payment is another name for electronic payment. Comparatively speaking to other options, electronic payment is quick and secure. People these days prefer to shop and pay for goods online because it saves so much time.
One of the most practical payment gateways is Razorpay. It takes all debit and credit cards, wallets, and UPIs; however, certain additional payment methods are not supported.
You must first create a Razorpay account and generate secret keys since we require both of these steps in order to access the Razorpay checkout page and all APIs. In test mode, we can produce these secret keys for free.
Employing Secret Keys
To access all Razorpay APIs, we must first install the Razorpay package and then utilize those secret keys.
const Razorpay = require('razorpay') const razorpay = new Razorpay({ key_id: 'rzp_test_uGoq5ADrFTgYRAhk', key_secret: 'FySe2f58UYtg6Hjkj1a5s6clk9B' })
Create Order
We must use the UI to place an order and call the API listed below. To keep the fundamental information about the order and return the order, I constructed the request body for Razorpay in this API and then called the create order function. I then needed to send this order Id to the frontend team so they could utilize it and the secret keys for the Razorpay checkout page. The status will change from “Failed” to “Authorized” if the payment is successful. In the Razorpay dashboard, you may check.
app.post('/orders', async (req, res) => { const options = { amount: req.body.amount, currency: 'INR', receipt: shortid.generate(), //any unique id payment_capture = 1 //optional } try { const response = await razorpay.orders.create(options) res.json({ order_id: response.id, currency: response.currency, amount: response.amount }) } catch (error) { console.log(error); res.status(400).send('not able to establish order'); } })
Payment Capture
Since Razorpay first authorizes the bank information and bank transactions take place during capture, we must then capture the payment once it has been completed. I’m going to capture the money via a webhook. Typically, webhooks are used to launch URLs after certain events.
In the Razorpay settings, we must enter the following URL in a webhook with a special secret key and choose the “payment.capture” column so that it will be activated whenever a payment is successful.
The secret key you supplied in the webhook must be used to verify the signature in this URL. The status of the payment will change to “Captured” after verification.
const crypto = require('crypto') const secret_key = '1234567890' app.post('/paymentCapture', (req, res) => { // do a validation const data = crypto.createHmac('sha256', secret_key) data.update(JSON.stringify(req.body)) const digest = data.digest('hex') if (digest === req.headers['x-razorpay-signature']) { console.log('request is legit') //We can send the response and store information in a database. res.json({ status: 'ok' }) } else { res.status(400).send('Invalid signature'); } })
Refund
Once the payment has been received, we can return it. To do that, all we have to do is make the following API call along with the payment Id and amount, and internally, Razorpay’s refund function is called to return the money to the same account.
app.post('/refund', async (req, res) => { try { //Verify the payment Id first, then access the Razorpay API. const options = { payment_id: req.body.paymentId, amount: req.body.amount, }; const razorpayResponse = await razorpay.refund(options); //We can send the response and store information in a database res.send('Successfully refunded') } catch (error) { console.log(error); res.status(400).send('unable to issue a refund'); } })
Advantages
The benefits of integrating with Razorpay Payment Gateway are listed below.
Onboarding
Utilizing Standard Checkout, connect the Razorpay Payment Gateway to your natively developed website. Explore our plugins for a number of different platforms, including WooCommerce, WordPress, Magento, Shopify, and more.
Success Rate
By employing numerous connections to route a transaction, also known as direct net banking pipelines, we increase the success rate.
Refunds
Based on the error code it receives from the bank, Razorpay intelligently retries failed API reimbursements. You have the choice to offer your clients the finest possible refund experience thanks to our Instant refunds tool.
Scalability and Availability
800 transaction requests per second can be processed by our system without it degrading. Aside from the status page and Dashboard, we also send emails with updates on outages.
Settlements Reconciliation
To keep track of all the transactions, including payments, refunds, transfers, and adjustments paid to you for a specific day or month, use settlement reconciliation.
Coverage
Numerous domestic and foreign cards, different net banking alternatives, UPI collect and intent, EMI, Cardless EMI, and wallets like Paytm and PhonePe are all supported by our company.
Conclusion
The end of that. Everything is set up for you to launch your online payments business. Hope you understand the use of Node.js to implement Razorpay Payment API on an online store.