Simple 5 Steps for Your Stripe Payment Integration in Node js

Tasadduq Ali
3 min readSep 21, 2023

Before we begin, Your support by clapping and leaving comments greatly influences my work. Additionally, if you’re interested in receiving a complimentary Full Stack Career consultation session every weekend, you can extend your support through PATREON.

Introducing Stripe

Stripe is a developer-friendly payment processing platform that offers a suite of tools for businesses to accept and manage online payments. Its well-documented APIs and user-friendly dashboard make it a popular choice for startups, enterprises, and developers. Stripe supports a variety of payment methods, including credit and debit cards, digital wallets, and alternative payment methods.

Prerequisites

Before diving into Stripe integration, ensure you have the following in place:

  1. Node.js and npm: Make sure you have Node.js and npm (Node Package Manager) installed on your system.
  2. Stripe Account: Create an account on the Stripe website to obtain the necessary API keys.

Setting up the Project

  1. Initialize a Node.js Project:

Open your terminal and navigate to your project directory. Run the following command to initialize a new Node.js project:


npm init

2. Install Stripe Package:

Install the Stripe Node.js library using npm:

npm install stripe

3. Create Configuration:

Create a configuration file to store your Stripe API keys. This should never be exposed in your codebase for security reasons. Create a file named config.js:

javascriptCopy code
module.exports = {
stripeSecretKey: 'YOUR_SECRET_KEY',
stripePublicKey: 'YOUR_PUBLIC_KEY'
};

Implementing Payment Integration

  1. Import Required Modules:

In your main application file (e.g., app.js), import the necessary modules and configurations:

javascriptCopy code
const stripe = require('stripe')(require('./config').stripeSecretKey);

2. Create Payment Intent:

To initiate a payment, you need to create a Payment Intent using the Stripe API. This represents the transaction you want to process. Add the following code to create a Payment Intent:

javascriptCopy code
app.post('/create-payment-intent', async (req, res) => {
const { amount } = req.body;
try {
const paymentIntent = await stripe.paymentIntents.create({
amount,
currency: 'usd',
});
res.json({ clientSecret: paymentIntent.client_secret });
} catch (error) {
res.status(500).json({ error: error.message });
}
});

3. Handle Payment on the Client Side:

On the client side (e.g., your web page), make a POST request to /create-payment-intent endpoint to obtain the clientSecret for the Payment Intent. You can then use this secret to confirm the payment:

javascriptCopy code
fetch('/create-payment-intent', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ amount: 1000 }), // Amount in cents
})
.then((response) => response.json())
.then((data) => {
const { clientSecret } = data;
stripe.confirmCardPayment(clientSecret, {
payment_method: {
card: elements.getElement(CardElement),
}
}).then((result) => {
if (result.error) {
console.error(result.error.message);
} else {
// Payment succeeded, handle success
}
});
});

if you have any questions or suggestions just do let me know on my Instagram or at codeculturepro@gmail.com

--

--

Tasadduq Ali

I am MERN Stack developer working in UAE Govt to digitize their massive services. I will help you to become highly skilled Coder 😉