Stripe Integration with Node.js
Stripe Integration with Node.js

Stripe Integration with Node.js

Spread the love
Stripe Integration with Node.js
Stripe+node.js

The Digital era has increased payments via online methods. It’s simple to buy any product online with just one click. And to serve this purpose many payment gateways are available in the market.n Stripe is one of the most popular international payment gateways. You can charge the customer either as a one-time payment or as a continuous recurring subscription. With stripe, you can easily choose one of these types and maintain a record in your own DB also. Stripe integration with Node.js can be done in few simple steps.

So without wasting any more time, let’s dive into stripe integration with a Node.js project.

stripr Account creation to checkout flow with Node.js
Account creation to checkout flow

Step 1. Stripe Account creation

First, create the stripe account on https://stripe.com/en-in and fill in your data. It will look something like this.

Stripe Dashboard Image

By default, it creates a test environment for you to play around with all the options. you can activate your account by filling in business-related information in the “Activate your account” section. Stripe provides logs & Events under the “Developer” section for developers to check the logs & events related to stripe activity.

Stripe Developer Console

Now go to the “API keys” section to get the API key for your application. There are two types of keys “Publishable key” & “Secret key”. Publishable key is used for the client environment & the Secret key is used for the server-side environment.

Stripe API Keys Console
API keys for stripe account

Step 2. Node.js setup

Install the package with npm or yarn:

npm install stripe --saveyarn 
//add stripe

Now let’s create the index.js file and create the customer & generate the session for the customer to checkout. Provide the server secret key while importing stripe.

const stripe = require('stripe')('sk_test_...');
const customer = await stripe.customers.create({
 email: req.body.email,
 name: `${req.body.firstName} ${req.body.lastName}`,
 phone: `${req.body.countryCode} ${req.body.phoneNo}`,
 description: 'User Account Created'
})

can create the checkout object and add the other data based on the condition. You can specify the coupon code or free trial in subscription payment mode.

const checkoutObject = {
/* Specify different payment method */
payment_method_types: ['card'],
line_items: [{
 price: "price_1IXkvZKjhdisu8333",
 quantity: 1,
}],
metadata: {
 firstName: req.body.firstName,
 /* any meta data for your ref */
},
customer: customer.customerId,
mode: "payment", // payment, subscription etc
success_url: `${getConfig().PROJECT_URL}/thankyou`,
cancel_url: `${getConfig().PROJECT_URL}/subscribe`,
}
if (req.body.couponId) {
 checkoutObject.discounts = [{
  coupon: req.body.couponId,
 }]
}
/* If payment mode is subscription */
if (req.body.subscriptionType === 'subscription') {
 checkoutObject.subscription_data = {
  trial_period_days: 30
 }
}
const session = await stripe.checkout.sessions.create(checkoutObject)

Below is the entire code in a single file.

Stripe Account creation API
Stripe create customer API
Stripe Checkout session API
Stripe checkout session API

Now create the form and ask user data to submit the form. When the user clicks on subscribe form collect the data and call the create account API. On successful signup navigate the user to the account page & show products to the user. On the click of the cart, the checkout button calls the checkout order API.

Invoking Stripe Account creation API
Call create account API

If the session is created successfully it will be redirected to the Stripe checkout page else an error will be shown to the customer.

Invoking Stripe Checkout Session API
Call checkout Order API

Conclusion

And that’s it. You are all ready to start your online payment business. For more info please visit the official documentation of stripe integration. Learn how to mock Stripe APIs for automated testing here. Learn to integrate Razorpay payment gateway to your Node.js project with few simple steps here. Thanks for reading.

2 Comments

  1. Pingback: Payment Gateway - RazorPay integration with Node.js | Noob2Geek

  2. Pingback: How to Mock the Stripe API in Node.js with Jest | Noob2Geek

Leave a Reply

Your email address will not be published.