Stripe Payment Gateway Integration Node.js Express E-commerce (2026 Guide)

Published

This is a practical Stripe payment gateway integration for Node.js Express e-commerce. You will build a server that creates Stripe Checkout Sessions from a trusted product catalog and confirms payments with a signed webhook. It works with any front end, including the React button in our payment gateway in React JS guide.

Stripe Payment Gateway Integration for Node.js Express E-commerce: Setup

npm init -y
npm install express stripe

Add your test secret key and webhook secret as environment variables: STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET. Never commit them.

Complete Express E-commerce Checkout Server

The webhook route is registered before express.json() because Stripe's signature check needs the raw request body. Prices live on the server so a customer cannot change the amount.

const express = require('express');
const stripe  = require('stripe')(process.env.STRIPE_SECRET_KEY);
const app     = express();

// Trusted catalog: prices live on the server (amounts in cents)
const PRODUCTS = {
  tshirt: { name: 'T-Shirt', amount: 2500 },
  mug:    { name: 'Mug',     amount: 1200 },
};

// 1) Webhook FIRST, with the raw body
app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => {
  let event;
  try {
    event = stripe.webhooks.constructEvent(
      req.body, req.headers['stripe-signature'], process.env.STRIPE_WEBHOOK_SECRET
    );
  } catch (err) {
    return res.status(400).send('Invalid signature');
  }
  if (event.type === 'checkout.session.completed') {
    const session = event.data.object; // mark the order as paid
  }
  res.json({ received: true });
});

// 2) Everything else can use JSON
app.use(express.json());

app.post('/create-checkout-session', async (req, res) => {
  const items = req.body.items || []; // [{ id: 'tshirt', qty: 2 }]
  const line_items = items.filter(i => PRODUCTS[i.id]).map(i => ({
    price_data: {
      currency: 'usd',
      product_data: { name: PRODUCTS[i.id].name },
      unit_amount: PRODUCTS[i.id].amount,
    },
    quantity: Math.max(1, parseInt(i.qty, 10) || 1),
  }));
  if (!line_items.length) return res.status(400).json({ error: 'Empty cart' });
  const session = await stripe.checkout.sessions.create({
    mode: 'payment',
    line_items,
    success_url: 'https://yoursite.com/success?session_id={CHECKOUT_SESSION_ID}',
    cancel_url:  'https://yoursite.com/cart',
  });
  res.json({ url: session.url });
});

app.listen(3000);

Test Webhooks Locally With the Stripe CLI

stripe listen --forward-to localhost:3000/webhook

The CLI prints a whsec_ secret for local testing. Use it as STRIPE_WEBHOOK_SECRET while developing, then create a real webhook endpoint in the Dashboard for production.

Checkout Sessions vs Payment Intents for Node.js Stores

Stripe recommends the Checkout Sessions API with the Payment Element over the lower-level Payment Intents API for most integrations, because Payment Intents makes you build and maintain more checkout logic yourself. Use Payment Intents only when you need a fully custom flow. See Does Stripe have an API for payments? for the full picture.

E-commerce Details You Should Not Skip

  • Fulfil from the webhook, not the success redirect.
  • Store the Checkout Session ID with your order so you can match them.
  • Handle refunds: Stripe does not return the original processing fee on a refund.
  • Price your products with fees in mind: 2.9% + 30¢ takes $3.20 from a $100 sale. See how much Stripe charges per transaction.

Related Guides

Frequently Asked Questions

Why must the webhook use express.raw?

Stripe signs the exact bytes it sends. If express.json() parses the body first, the signature check fails.

Should I use Payment Intents or Checkout Sessions in Node.js?

Stripe recommends Checkout Sessions with the Payment Element for most new integrations.

How do I test a Stripe integration in Node.js?

Use test keys, the 4242 test card and the Stripe CLI to forward webhooks to localhost.

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.