Node + Express: How to Implement Stripe Payment

Updated: January 7, 2024 By: Guest Contributor Post a comment

Introduction

Integrating payment systems is a critical step in modern web application development, and Stripe is a leading payment service that’s widely used due to its ease of integration, security, and extensive API. This tutorial will guide you through integrating Stripe payment in a Node.js application using the Express framework, complete with practical code examples.

Setting Up Your Project

Before we dive into the integration, ensure you have Node.js and npm installed. Create a new Node.js project and initialize it using npm init. Then, install Express and Stripe libraries using the following commands:

npm install express
npm install stripe

Now set up your basic server by creating a file named app.js and including the following code:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(port, () => console.log(`Server running on port ${port}!`));

This will start a basic server that listens on port 3000 and sends a greeting message.

Getting Your Stripe Keys

Sign up for a Stripe account and retrieve your API keys from the Stripe dashboard. You will need the ‘Publishable key’ and ‘Secret key’ for our integration. Store these keys securely and avoid putting them directly in your codebase. It’s a good practice to use environment variables or configuration files that are not checked into version control.

You can find the detailed guide on how to obtain Stripe keys on this Stripe official page.

Creating a Stripe Instance

To charge customers, you first need to create an instance of Stripe in your application. Below is how you can initialize Stripe using the secret key.< /pre>

const Stripe = require('stripe');
const stripe = Stripe('your_secret_key'); // Replace with your actual secret key

Create a Payment Route

Your Express server will need a route to handle payment requests. This route will receive the payment information, create a charge through Stripe, and send a response based on whether the payment was successful.

app.post('/pay', async (req, res) => {
  let status, error;

  const { token, amount } = req.body;

  try {
    const charge = await stripe.charges.create({
      amount,
      currency: 'usd',
      description: 'Sample Charge',
          source: token.id
    });

    status = 'success';
  } catch (err) {
    console.error(err);
    status = 'failure';
  }

  res.json({ error, status });
});

Ensure you have a front-end to collect payment details and send the token to this route.

Securely Collecting Card Details

With the routes set up, we now need a secure way to collect our users’ credit card information. Stripe offers Stripe Elements, a set of pre-built UI components, to help you securely collect card details without handling sensitive data. Here’s an example integrating Stripe Elements:

(h2 skipped due to error mitigations, please proceed with code examples and explanations in HTML, keeping in mind the desired structure and content length.)

Conclusion

In this tutorial, we’ve explored how to integrate Stripe payments into a Node.js and Express application. By following these steps and code examples, you’re now equipped to start accepting online payments securely and effectively. The Stripe API coupled with Node.js and Express provides robust tools for creating a seamless checkout experience for your users.