A step-by-step guide to integrating Stripe or Razorpay into your MERN stack for secure checkout experiences
A seamless and secure payment experience is the cornerstone of any e-commerce application. Whether you're building a simple online store or a full-featured multi-vendor platform using MERN stack development services, integrating a payment gateway is a must. This post will walk you through integrating Stripe (international) or Razorpay (popular in India) with React, Express, and MongoDB.
Install the SDK and create a secure payment route.
// server/routes/payment.js (Stripe Example) import express from 'express'; import Stripe from 'stripe'; const router = express.Router(); const stripe = new Stripe(process.env.STRIPE_SECRET_KEY); router.post('/create-checkout-session', async (req, res) => { const session = await stripe.checkout.sessions.create({ payment_method_types: ['card'], line_items: req.body.items.map(item => ({ price_data: { currency: 'usd', product_data: { name: item.name }, unit_amount: item.price * 100, }, quantity: item.quantity, })), mode: 'payment', success_url: `${process.env.CLIENT_URL}/success`, cancel_url: `${process.env.CLIENT_URL}/cancel`, }); res.json({ id: session.id }); }); export default router;
Use the Stripe JS SDK or Razorpay checkout widget on the React side.
// client/pages/Checkout.jsx import { loadStripe } from '@stripe/stripe-js'; import axios from 'axios'; const stripePromise = loadStripe('pk_test_...'); const handleCheckout = async () => { const stripe = await stripePromise; const res = await axios.post('/api/create-checkout-session', { items: [{ name: 'Product A', price: 1000, quantity: 1 }], }); await stripe.redirectToCheckout({ sessionId: res.data.id }); };
Razorpay supports both server-initiated orders and client-only payments.
Once payment is successful, store order details securely in your database.
Adding a payment gateway to your MERN stack e-commerce site is easier than ever with modern APIs. Whether you choose Stripe for global reach or Razorpay for localized transactions, integrating secure checkout flows enhances trust and boosts conversions. With clean separation between backend payment logic and frontend UI, your online store stays secure, scalable, and user-friendly.
Whether you need video editing, web development, or more, we're here to help you achieve your goals. Reach out to us today!
Discover Custom Solutions
At OrganicOpz, We Specialize In Crafting Tailored Strategies To Elevate Your Online Presence. Let's Collaborate To Achieve Your Digital Goals!
Share Your Idea Or Requirement — We’ll Respond With A Custom Plan.
Give Us A Call On Our Phone Number For Immediate Assistance Or To Discuss Your Requirements.
Feel Free To Reach Out To Us Via Email For Any Inquiries Or Assistance You May Need.
Our Standard Operating Hours Are From 4:00 To 16:00 Coordinated Universal Time (UTC).