How to Create Payment Integration in Express: Simple Guide
To create a payment integration in
Express, set up an Express server and use a payment provider's SDK like Stripe. Create routes to handle payment requests and securely process payments using their API.Syntax
Here is the basic syntax to set up payment integration in Express using Stripe:
importorrequirethe Stripe library and Express.- Create an Express app with
express(). - Use middleware like
express.json()to parse JSON bodies. - Define a POST route to create a payment intent with Stripe's API.
- Send the client secret back to the frontend to complete payment.
javascript
import express from 'express'; import Stripe from 'stripe'; const app = express(); const stripe = new Stripe('your_stripe_secret_key'); app.use(express.json()); app.post('/create-payment-intent', async (req, res) => { const { amount } = req.body; try { const paymentIntent = await stripe.paymentIntents.create({ amount, currency: 'usd' }); res.send({ clientSecret: paymentIntent.client_secret }); } catch (error) { res.status(400).send({ error: error.message }); } }); app.listen(3000, () => console.log('Server running on port 3000'));
Example
This example shows a complete Express server that creates a Stripe payment intent for a given amount. It listens on port 3000 and returns the client secret needed for frontend payment confirmation.
javascript
import express from 'express'; import Stripe from 'stripe'; const app = express(); const stripe = new Stripe('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); // Replace with your Stripe secret key app.use(express.json()); app.post('/create-payment-intent', async (req, res) => { const { amount } = req.body; if (!amount || amount <= 0) { return res.status(400).send({ error: 'Invalid amount' }); } try { const paymentIntent = await stripe.paymentIntents.create({ amount: amount * 100, // amount in cents currency: 'usd' }); res.send({ clientSecret: paymentIntent.client_secret }); } catch (error) { res.status(500).send({ error: error.message }); } }); app.listen(3000, () => { console.log('Payment server running on http://localhost:3000'); });
Output
Payment server running on http://localhost:3000
Common Pitfalls
Common mistakes when creating payment integration in Express include:
- Not parsing JSON body with
express.json(), causingreq.bodyto be undefined. - Using incorrect currency or amount format (Stripe expects amount in smallest currency unit, e.g., cents).
- Exposing secret keys in frontend code instead of backend.
- Not handling errors properly, which can crash the server or leak sensitive info.
Always keep your secret keys safe and validate input data.
javascript
/* Wrong: Missing express.json() middleware */ import express from 'express'; import Stripe from 'stripe'; const app = express(); const stripe = new Stripe('your_secret_key'); app.post('/create-payment-intent', async (req, res) => { // req.body will be undefined without express.json() const { amount } = req.body; // This will cause an error }); /* Right: Add express.json() middleware */ app.use(express.json());
Quick Reference
Tips for smooth payment integration in Express:
- Use environment variables to store secret keys securely.
- Always validate and sanitize incoming data.
- Test payment flows in Stripe's test mode before going live.
- Use HTTPS in production to secure data transmission.
- Handle errors gracefully and return meaningful messages.
Key Takeaways
Use Stripe's SDK with Express to create secure payment intents on the backend.
Always parse JSON requests with express.json() to access payment data.
Keep secret keys on the server and never expose them to the frontend.
Validate payment amounts and handle errors to avoid crashes.
Test your integration thoroughly in test mode before production.