How to Integrate Stripe Payment in a Web App: A Practical Step-by-Step Guide

Integrating Stripe into your web app allows you to accept payments securely with minimal friction. Stripe provides robust APIs and client-side libraries that handle sensitive card data, reducing PCI compliance burden. This tutorial walks you through the essential steps for a smooth integration using Node.js on the backend and plain JavaScript on the frontend.

Article illustration

1. Set Up Your Stripe Account and API Keys

Register at dashboard.stripe.com and navigate to the Developers section. Copy your Publishable Key (frontend) and Secret Key (backend). Store the secret key in an environment variable (e.g., STRIPE_SECRET_KEY) – never expose it on the client side.

2. Create a Backend Endpoint to Generate Payment Intents

On your server (Express.js example), install Stripe: npm install stripe. Then create a POST endpoint that initializes a PaymentIntent:

  • Import Stripe with your secret key.
  • Call stripe.paymentIntents.create() with the amount (in cents) and currency.
  • Return the client_secret to the frontend.

Example route:

app.post("/create-payment-intent", async (req, res) => {
  const paymentIntent = await stripe.paymentIntents.create({
    amount: 2000, // $20.00
    currency: "usd",
  });
  res.send({ clientSecret: paymentIntent.client_secret });
});

3. Build the Frontend Payment Form

Include Stripe.js and Elements on your HTML page:

  • Load the script: https://js.stripe.com/v3/
  • Create a Stripe instance with your publishable key.
  • Mount a card element inside a div.
  • On form submission, call stripe.confirmCardPayment(clientSecret, cardElement).

Use fetch to get the clientSecret from your backend endpoint before confirming payment. Handle success and error states accordingly.

4. Handle the Payment Result

After confirmation, check the paymentIntent.status. If succeeded, display a success message and optionally update your database. Always verify the payment on your server using the PaymentIntent ID received from the webhook (recommended for production).

Conclusion

Stripe’s Payment Intents API gives you a secure, scalable way to accept one-time payments. With a backend endpoint for creating intents and a frontend using Stripe Elements, you can integrate payments in under an hour. Remember to test with test card numbers before going live.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *