Akhir Dari Sebuah Dekade

Sebuah dekade akan berakhir, dekade ke-2 dalam milenium ini. Walaupun tidak sebesar perpindahan milenium yang terjadi antara 1999–2000, tapi perpindahan dekade ini juga perlu diperhatikan. Mengapa…

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:

The Paradigm of Infant Sleep is Creating Unrealistic Expectations for New Parents.

An article detailing the truth about the biologically normal baby and infant sleep behaviours. It debunks the myths about baby sleep training as necessary

Try This API To Obtain Indonesian Realistic Voices

If you are looking for realistic Indonesian voices to use in your next project, then you should try using the API from Woord. This API provides high-quality Indonesian voices that can be useful for a…

A Review of Your Precious Sight

The primary message contained in Your Precious Sight, Dr. Eichin Chang-Lim’s informative but accessible compilation of professional ophthalmic case studies, is self-evident in the title, and the…