Table of Contents
Stripe backend Integration Intro
Stripe backend integration, Most developers make a huge mistake while integrating Stripe — they try to do everything from the frontend.
Stripe requires a backend to handle payments securely.
In this tutorial, we build a Stripe backend using Node.js and Express, and understand how real-world payment systems work.
Stripe Flow
Here’s a over all stripe flow we implement
Frontend → Backend → Stripe → Backend → Frontend
Creating Payment Intent API
In Stripe, all payments must be created from the backend.
This is because Stripe uses a secret key, which should never be exposed in frontend applications.
To handle this securely, we create a backend API using Node.js and Express, which communicates with Stripe.
API Endpoint: /create-payment-intent
This API is responsible for creating a PaymentIntent in Stripe.
app.post("/create-payment-intent", async (req, res) => {
This endpoint is called from the frontend (Flutter / Android / Web).
The frontend never talks directly to Stripe.
Reading Request Data
const { amount } = req.body;
The frontend sends the payment amount to the backend.
The amount must be in the smallest currency unit
(for example: cents for USD, paise for INR).
Input Validation :
if (!amount) {
return res.status(400).json({ error: "Amount is required" });
}
If the amount is missing, we stop execution.
This prevents invalid payment requests.
Creating Stripe PaymentIntent
const paymentIntent = await stripe.paymentIntents.create({
amount: amount,
currency: "usd",
automatic_payment_methods: {
enabled: true
}
});
paymentIntents.create() creates a new payment request in Stripe.
automatic_payment_methods allows Stripe to automatically choose the best payment method.
This is the recommended approach by Stripe.
Sending Response to Frontend
res.send({
clientSecret: paymentIntent.client_secret
});
Stripe returns a client secret.
This clientSecret is safe to send to the frontend.
The frontend uses it with Stripe SDK to complete the payment.
Error Handling
catch (error) {
res.status(500).json({ error: error.message });
}
Any Stripe or server error is handled safely.
This prevents backend crashes and helps debugging.
Starting the Backend Server
app.listen(4244, () => {
console.log("Server running on port 4244");
});
The backend server starts on port 4244.
The frontend calls this API using:
http://localhost:4244/create-payment-intent
Stripe backend Integration Complete Tutorial
Stripe Implementation on Mobile Apps
Explore the complete Stripe Mobile Integration series below, covering step-by-step implementation from setup to production-ready payments.