Why Not Get The Covid Vaccine?

The majority of the population of the United States seems willing and anxious to get the COVID-19 vaccine into their arms. They want the added protection that the vaccines offer. They want to see the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Stripe Integration with Flutter and Firebase Cloud Functions

For one of my recent projects in Flutter, I was required to integrate Stripe SDK for payments where I used Firebase for the backend side. It was a pretty challenging task for me as there are not many resources available online for a clean set-up of cloud functions for such frameworks. Thus, I would like to write and share about my experience on this.

Before we continue further into this topic, you should already possess some knowledge on Flutter and Firebase.

So, let’s dive into it, shall we? 😊

Let’s start by creating a sample Flutter project. You can name it as StripeFlutterProject and set up your main.dart file.

You will need to add these two packages into your pubspec.yml file:

Prior to this, you will have to create a Stripe SDK and Firebase account. I shall not go into details on that, but here are some links you can follow to create your accounts.

Firstly, you need to set up a Firebase project and database at console. Then, you can add more packages (the versions provided below are the ones I used in my project. Alternatively, you can add the latest stable packages) into your Flutter project for the Firebase configuration as listed below;

The next step would be to download the GoogleService-info.plist file from your Firebase console and add in the Android and iOS folders.

Now, for the backend side (cloud functions), you will have to create a node project where we write all our stripe functions. Having your stripe keys and functions stored on the backend is more secured and it allows you to perform all the heavy tasks that are related to payment.

I am adding basic cloud functions for payment. I’ll explain them one by one. To set up cloud function create a node.is project. Alternatively, you can also add the cloud function in google cloud console. I will be creating a separate cloud function project, let’s name it stripe-cloud-functions.

Prior to that do install Firebase tools and login to your Firebase account using command line.

You can set up your cloud function project by following below link:

You can add below functions in your index.ts:

You need to deploy these cloud functions on your firebase console. Next step would be to call this cloud function from your flutter app, let’s add code in your flutter app to trigger above cloud functions from your app over http.

Note: Add cloud function package in your pubspec.yaml file:

Don’t forget to run flutter pub get once you add new package in the project, Now create a firebaseService.dart file in your flutter project, this dart file should basically handle your firebase calls and references. You can add your cloud function reference with below intents, one thing to note here is that you need to pass the cloud function name in your callable instance.

Call these Intents from your flutter app, It will basically trigger the respective cloud function, You should always handle the payment business logic on backend (cloud functions in this case) as it’s more secure and recommended by stripe.

To trigger these intents you need to call them from your relevant dart file (in my case it’s the page where I show my card details and ask user to pay) something like this:

Once you call this firebase service Intent, your logic on backend will run and on confirming payment intent, user’s payment method will be charged. In my case I used card payment method and charged user card on any specific service provided in my app.

That’s all for now, you guys can try this and let me know in comments if I missed something or do let me know if you guys have any better approach.

Hope you enjoyed reading this article 😊. Happy coding, Cheers! 🍻

Add a comment

Related posts:

VR and dancing math into place

Last weekend I did a very San Francisco thing. I went to the SF Film Festival turned Creativity Summit turned lecture about VR. With invited guest Jaron Lanier. Link…

A JOURNEY BY SHIP

Early in the morning, I got up from my bed and wake up the other. It was a beautiful sunday. I was very excited as we are going on journey by ship. We all fresh up ourself and had our breakfast and…

Full Ledger Nano S Hardware Wallet Guide for ARK

In this guide we will cover every aspect of interacting with the Ledger Nano S in the following three sections: #2. Installing Ledger manager / ARK wallet app on Ledger —if you already have Ledger…