Subscribe to learn about new product features, the latest in technology, solutions, and updates.
Payment Gateway
DevOps
Stripe Integration
Tutorial
In this tutorial, we’ll explore how to seamlessly integrate Stripe payment gateway into our full-stack applications, and effortlessly host them on FL0. 🚀
Whether it be an e-commerce or a SaaS application, payment gateways are a central component of our projects. 💳
In this guide, we will explore how to simplify these integrations, specifically focusing on Stripe Checkout for online payment processing.
Stripe’s developer-friendly API ensures secure and efficient transactions while cutting down on our development time.
Just for example, we have taken the case of a SaaS applications payment page.
We would be using !!NodeJs!! for the backend and !!Postgres!! as our database. On the frontend we are using !!ReactJs!! with !!vite!!.
Later we would go ahead and effortlessly host our project on FL0. ⬆️
So, let’s start with a pinch of humor:
🧑💻 In this tutorial, we will create a simple demo application where a user could sign up, select their plan, and checkout with their credit card.
For this, we would need to create 2 separate repositories, one for our backend and another one for !!frontend!!.
🗂️ Here’s how both of our folder structures would look like, just for reference:
Now, let’s get started.
For the sake of efficiency, in this tutorial, we’ll leverage the “fl0zone/blog-express-pg-sequelize” template.
Then we would remove any files or folders not important for our project. 🧑💻
For a more comprehensive understanding of the tutorial, you may want to refer to this blog post:
https://fl0.com/blog/building-and-deploying-a-nodejs-postgres-api-in-less-than-30-minutes
Our template encapsulates a basic !!Node.js!! application and a dockerized !!PostgreSQL!! database.
Here is the corresponding !!docker-compose.yaml!! file for our setup 🐳:
Now we would go ahead and install some essential packages 📦
Now, we would need to get our Stripe API keys 🔑. For this we would need to create a new account on Stripe.
Here we would be using !!Test Mode!! for demo.
Here are the list of environment variables we would need for this project.
!!.env.example!!
Let’s begin by setting up our database now. 🐘
Since we’re utilizing the Sequelize ORM, we’ll need to create a model for our user data.
Here’s the code for our model 👇
!!models/userModel.js!!
Now, let’s go ahead and create our routes
!!POST /login!! - Helps to log in user and store the session
!!POST /signup!! - Helps create a new account
!!POST /create-checkout-session!! - Generates and Returns the stripe checkout page link
These 3 routes are separated into 2 files as follows:
!!routes/userRoutes.js!!
!!routes/stripeRoute.js!!
🧑💻 For setting up the user profile, first we will define a middleware to check if the email address of a new user already exists in the database during signup.
!!middleware/userAuth.js!!
Then we would go ahead and define our login and signup functions 👇
!!controllers/userController.js!!
This is where we will integrate !!Stripe Checkout!! into our application.
We will use the !!Stripe API!! to manage payments and handle user subscriptions.
The following code creates a new Stripe checkout session. 💳
We will provide it with the payment method type, the product data, and the quantity.
We also need to specify the URLs where the user will be redirected upon a successful payment or if they cancel the transaction.
And, the server will respond back with the URL for the Stripe Session if everything is fine. ✅
!!controllers/stripeController.js!!
At last, we would need to add all our routes to our entry point, which is !!server.js!!
!!server.js!!
And we are done with the backend ✅
Now let’s go ahead and try to deploy it on !!FL0!!. 🔼
🚀 For deploying our project to FL0 we will start with pushing our repo to a new GitHub repository first.
This is the link to our repository for reference: https://github.com/dalefl0/stripe-fl0-backend
Now we would head on to app.fl0.dev to start deploying.
🙌 Now we have our backend up and running.
Time for the UI ✨
For setting up the frontend we would get started with the template-react-vite. ⚡️
This includes everything we need to get our !!React-Vite!! project up and running.
Now we would go ahead and install a few packages.
To build our UI components quickly we would take help of the Pricing Section Component and Sign-in and Registration Component from tailwind UI.
For the sake of brevity, we will only look at the important functions of the frontend.
The complete project could be found at: https://github.com/dalefl0/stripe-fl0-frontend
Now, we would need to add a function to handle stripe checkouts
!!src/components/PricingPlans.jsx!!
This function calls our backend’s !!/create-checkout-session route!!, receives a link, and redirects the user to the checkout page. 📄
Apart from this, we need to also connect our !!signup!! and !!login!! pages to respective routes and store the user data in !!localstorage!!.
For frontend we would need to again create a new repository and deploy it in the same project in a similar manner.
We would then need to add the !!VITE_APP_API_BASE_URL!! environment variable to the frontend deployment which should be set to the URL of our backend.
We would also need to set the !!CLIENT_URL!! environment variable in the backend to the hosted URL of the frontend.
Once done, our FL0 project would look like this 👇
Now, let’s go ahead and try our application using this live demo link: https://stripe-fl0-frontend-q8oo-dev.fl0.io/
Thanks for sticking by till the end!
In this tutorial, we learnt how to build payment pages by integrating !!Stripe Checkout!! easily into our full-stack applications. 🎉
We also did blazingly-fast deployments of our project using FL0.
To get started with building your own applications with payment capabilities, head on to fl0.com 🚀
Tool and strategies modern teams need to help their companies grow.