React JS Payment Gateway Integration: How to Integrate a Payment Gateway in React JS (Stripe Example, 2026)

Published

This guide explains how to integrate a payment gateway in React JS using Stripe as the example. The key rule: a React app runs in the browser, so it can never hold your secret key. Your React component calls your own backend, the backend creates the payment session, and React redirects the customer. Total code is under 30 lines on the React side.

How to Integrate Payment Gateway in React JS: The Architecture

  1. The customer clicks Checkout in your React app.
  2. React sends the cart to your backend, for example POST /create-checkout-session.
  3. The backend (Node.js, PHP, Python) uses the Stripe secret key to create a Checkout Session and returns its url.
  4. React sends the browser to that URL, where Stripe collects the card.
  5. Stripe calls your webhook so the backend can mark the order paid.

Step 1: The React Checkout Button

import { useState } from 'react';

export default function CheckoutButton({ items }) {
  const [loading, setLoading] = useState(false);

  async function handleClick() {
    setLoading(true);
    const res = await fetch('/create-checkout-session', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ items }),
    });
    const { url } = await res.json();
    window.location.href = url; // go to Stripe Checkout
  }

  return (
    <button onClick={handleClick} disabled={loading}>
      {loading ? 'Redirecting…' : 'Checkout'}
    </button>
  );
}

Use this component anywhere you have a cart: <CheckoutButton items={[{ id: 'tshirt', qty: 2 }]} />. Send only product IDs and quantities, never prices.

Step 2: The Backend Endpoint

Any backend works. The Node.js and Express version, including the webhook, is in our Stripe payment gateway integration guide for Node.js e-commerce. The endpoint looks up prices from a trusted catalog and creates the session.

Embedded Checkout Instead of a Redirect

If you want customers to stay on your page, Stripe offers embedded Checkout and the Payment Element through the @stripe/react-stripe-js and @stripe/stripe-js packages. You call loadStripe() with your publishable key and render Stripe's component. Follow Stripe's current "Embed a payment form on your site" quickstart, since component names evolve.

Other Payment Gateways in React

The same backend-session pattern applies to other gateways: PayPal, Razorpay and Square all provide a server API plus a client button or redirect. Never put a secret key in a Vite or Create React App environment variable that is exposed to the browser; only variables meant to be public (publishable keys) belong there.

React Payment Integration Checklist

  • Secret key only on the server.
  • Prices and totals calculated on the server.
  • Webhook with signature verification fulfils the order.
  • Test mode and test cards first, then live keys.
  • Handle a failed fetch and show a friendly error.

Related Guides

Frequently Asked Questions

Can I put my Stripe secret key in React?

No. React code is visible to every visitor. Keep the secret key on your server and call it through an endpoint.

Do I need a backend to integrate a payment gateway in React?

For Stripe Checkout yes, a small server endpoint. The only no-backend option is a Stripe Payment Link.

How much does Stripe charge on a React store?

The same as any website: 2.9% + 30¢ per domestic card payment in the US.

Sources

Educational use only. Not financial advice. FeeCalculatorBuzz is not affiliated with Stripe, Venmo or Toast. Fees change, so confirm current rates on the provider's official pricing page before you set prices.