Seamless Integration: Mastering Stripe Payments in Node.js

Sandeep Singh
3 min readMay 8, 2023

--

Introduction:

In today’s digital world, accepting online payments has become a crucial aspect of many applications. Stripe is a popular payment processing platform that provides developers with a simple and secure way to handle transactions. In this blog post, we will explore how to integrate Stripe payments into a Node.js application. By the end of this tutorial, you’ll have a solid understanding of how to set up a basic payment system using Stripe and Node.js.

Prerequisites:

To follow along with this tutorial, you should have a basic understanding of Node.js and Express.js. Additionally, you’ll need to create a Stripe account (https://stripe.com/) and obtain your API keys.

Setting Up a Node.js Project:

  1. Create a new directory for your project and navigate to it using your terminal.
  2. Initialize a new Node.js project by running the following command:
npm init -y

This will create a package.json file that tracks your project's dependencies.

Installing Required Packages:

3. Install the stripe package by running the following command:

npm install stripe

This package allows us to interact with the Stripe API from our Node.js application.

4. Install the express package for creating a basic web server:

npm install express

Creating a Server:

5. Create a new file called server.js and open it in your preferred code editor.

6. Import the necessary packages at the top of the file:

const express = require('express'); 
const app = express();
const stripe = require('stripe')('YOUR_STRIPE_SECRET_KEY');

Replace 'YOUR_STRIPE_SECRET_KEY' with your own secret API key from the Stripe dashboard.

7. Add a basic route to handle the payment process:

app.use(bodyparser.urlencoded({extended:false}))
app.use(bodyparser.json())
app.post('/payment', function(req, res){

stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken,
name: 'John Doe',
address: {
line1: '#32',
postal_code: 'pin',
city: 'City',
state: 'State',
country: 'Country',
}
})
.then((customer) => {

return stripe.charges.create({
amount: 5500, // for charging Rs 55
description: 'Apple iPhone 14',
currency: 'INR',
customer: customer.id
});
})
.then((charge) => {
res.send("Payment Success")
})
.catch((err) => {
res.send(err)
});
})

8. Start the server by adding the following code at the end of the file:

app.listen(3000, function(error){
if(error) throw error
console.log("Server is running")
})

Creating a Front-end:

9. Create a new file called index.html and include the following content:

<!DOCTYPE html>
<html>
<title>Stripe Payment Demo</title>
<body>
<h3>Welcome to Payment Gateway</h3>
<form action="payment" method="POST">
<script
src="//checkout.stripe.com/v2/checkout.js"
class="stripe-button"
data-key="<%= key %>"
data-amount="5500"
data-currency="inr"
data-name="John Doe"
data-description="Apple iPhone 14"
data-locale="auto" >
</script>
</form>
</body>
</html>

Replace '<%= key %>' in the front-end code with your own publishable API key from the Stripe dashboard.

Testing the Integration:

10. Start the server by running the following command in your terminal: node server.js

Conclusion:

Congratulations! You have successfully integrated Stripe payments into your Node.js application. We covered the basics of setting up a server, creating a payment intent, and handling the payment on the client side. Stripe provides many more features for handling subscriptions, managing customers, and more, so feel free to explore their extensive documentation to enhance your payment system further.

Remember to handle errors and security considerations when implementing payments in a production environment and if you like my work buy me a coffee.

--

--