As you know, Flutter web is becoming more mature now. Developers are using Flutter web not only for creating portfolios but also for creating more complex apps as well. Integrating a payment gateway into the web app is the most important feature of any E-commerce App. In this article, we are going to see how easily one could integrate Stripe payments with Flutter web using Firebase Extensions.
As you know, Flutter web is becoming more mature now. Developers are using Flutter web not only for creating portfolios but also for creating more complex apps. Integrating a payment gateway into the web app is the most important feature of any E-commerce App. This article will show how easily one could integrate Stripe payments with Flutter web using Firebase Extensions.
Stripe is a third-party payment service that manages one-time payments, prebuilt checkouts, subscriptions, invoices, billings, and many other features. it’s the one-stop solution for payment gateway.
Firebase Extensions are pre-packaged solutions that help you deploy functionality to your Firebase application. It is the fastest way of integrating Stripe payment into the app with Firebase. It works as the backend for your stripe payment, so don’t need to think about the backend now!! You are good to go by integrating the Stripe extension and adding client-side code to your Flutter app. You can read more about this here
Integrating Stripe extension, you will need to authenticate the user first. So once the user is authenticated, then it triggers a Cloud function to create a Stripe customer.
To install this extension, your Firebase project must be on the Blaze (pay-as-you-go) plan. You will only be charged for the resources you use. Most Firebase services offer a free tier for low-volume use. Learn more about Firebase billing.
While using Run Payments with Stripe extension, you don’t need to research, code or debug the code on your own. Firebase extension works for you behind the scenes, and it uses Storage, Eventarc API, and Cloud Functions
To configure an extension, you will need to specify the following:
Firestore collection to store customer data.
First, select the location for the deployment. Tip: You usually want a location closer to your database. For help selecting a location, refer to the location selection guide
Firestore collection to store customer data.
Do you want to delete customer objects in Stripe automatically? When a user is deleted in Firebase, then select accordingly.
Setup Stripe API restricted key to giving access only to the “Customers”, “Checkout Sessions”, and “Customer portal” resources.
This is your signing secret for a Stripe-registered webhook. This webhook can only be registered after installation. Leave this value untouched during installation, then follow the post-install instructions for registering your webhook and configuring this value.
Enable events. To make things easy, we can choose to select all.
Finally, click the Install extension button and wait for the setup to finish. It will take 3 to 5 minutes.
You need to set up a webhook that synchronises relevant details from Stripe with your Cloud Firestore. This includes product and pricing data from the Stripe Dashboard and customer’s subscription details. Create a webhook with the endpoint that the extension docs give you. Please add the event as per the documentation.
After adding a webhook key, it’s time to create a list of products. On the Stripe dashboard, it’s very easy to create product listings.
You can specify the title, description, and all the price (including currency) information.
You need to select a recurring payment option. The example further down in the article is all about how to use Stripe payment to subscribe to a membership. In that case, we need to select Payment mode:'subscription'for this product.
Once you have created products in the Stripe dashboard, the products will also be mirrored in Cloud Firestore.
Now, Let’s start building the Flutter project. We have done all of the setup, and now it’s time to learn how to implement the extension in Flutter web. Here is the project we want to build:
Start with main.dart adding the code for Firebase integration and Routing.
main.dart
login_page.dart
An application that requires user authentication typically has a login page. We can authenticate users with email and password, but you can use any option like Google or Facebook etc. Once you finish the user authentication, it will trigger the cloud function and add a custom collection into Firestore. For the demo purpose below, we have created the Signing Page and manually created a user in Firebase, but if you want, you can code a separate one.
Dashboard.dart
On the Dashboard page, we are retrieving the data from the product collection. Here, you will get the product ID. Using that product ID, we can fetch the product price from the prices collection. See Below:
To subscribe to Products, you need to have a Checkout Session ID. In order to get this ID, you can create a Checkout Session document in the Customer collection with the help of the recurring price ID.
To create a Checkout Session ID for the recurring payment, pass mode:'subscription'to the Checkout Session doc creation. For example:
In the below code, we have created a Subscription stateful widget, and here we are passing the Checkout Session ID, which we have retrieved from the Checkout session. Please wait for the Checkout Session to get attached by the extension. The extension will update the doc with a Stripe Checkout session ID and generate the URL, and this can then be used to redirect the user to the checkout page.
You can check the final output. We are first authenticating a user with a username and password. Once logged in, the application will navigate to the Dashboard page, where you can select the products you want to subscribe to. Once you click the subscribe button, it will redirect you to the Stripe Checkout page. You can then fill in all the required details and hit the subscribe button. Following a successful transaction, it will take you back to your app.
Stay tuned for more updates and exciting news that we will share in the future. Follow us on Invertase Twitter, Linkedin, and Youtube, and subscribe to our monthly newsletter to stay up-to-date. You may also join our Discord to have an instant conversation with us.
We are excited to announce many new features and improvements to the Firestore Stripe Payments Extension. This blog post aims to walk you through these updates, including support for the flow_data parameter in the createPortalLink function, creating Stripe customers on-the-fly with portal links and customisable payment methods options when making payments.
This article provides a complete breakdown of an exciting and widely anticipated new feature called Lifecycle events. Here we explore what, when, and how to use them.