How to Implement Subscriptions In-App Purchase in Flutter

Varun Kamani
Oct 5, 2020 · 8 min read
How to Implement Subscriptions In-App Purchase in Flutter
How to Implement Subscriptions In-App Purchase in Flutter

One way or another we build our apps to make money out of it. Sometimes we sell ads and ask users to upgrade for a better experience and sometimes we have premium content only for paid users.

Let’s add In-App Purchase in your app and get rich in no time😇

To add a premium user feature we need to add a payment gateway in our app. Configuring payment gateways are a big hassle. The easiest way to integrate payment gateway is by adding in-app purchases in your app.

So before we start I want to give you heads up that this article is going to be a little bit lengthy. We should know about each and every detail before handling the payment related things. So grab yourself a cup of coffee before you start deep diving in Subscriptions In-App Purchase in Flutter.

Before we get into the coding part let’s talk more about in-app purchases and how it is different for both the platforms.

In-app purchase products on both platforms have very few differences. Let’s see what are they.

Products in Android

Products in iOS

For both iOS and Android, you can not re-purchase a subscription before it expires.

Based on your requirement you have to create products on Google Play Console and App Store Connect.

Both the platform gives features like a grace period, trial period, upgrade or downgrade a subscription, etc.

In this post, we will see how to add subscriptions in the Flutter app.

In order to test the in-app purchase, your app must have an alpha release in android.

Step 1: Create a Product

Create a product for Android

Don’t forget to activate the newly created subscription.

Create a product for iOS

For simplicity use same product id for same products on both the platform.

User can activate any one of the product in one subscription group and user can upgrade and downgrade their subscription within the group.

Yay! You have successfully created a product for both platforms.

Now let’s go to the second step.

Step 2: Setup Testing Accounts

You must need to test the in-app purchase flow before publishing it to the stable release.

You don’t want to pay, every time you purchase a product while testing the in-app purchase flow.

For that, you have to set up a tester account for Android and iOS both.

Setup Testing Account For Android

Setting up a testing account for Android is very easy. Just follow below two steps and you are ready to test.

Follow this detailed guide on how to add testers: Test your Google Play Billing Library integration

Setup Testing Account For iOS

Setting up a sandbox testing account for iOS is not as straight forward as Android.

Adding a sandbox tester is like creating a new apple id. You can not use an existing apple id as an email address for the sandbox tester. You’ll have to create a new email address for beta testing.

Tip: You can use temporary email address services to create a new sandbox tester account.

Log in with this account while processing in-app purchases in iOS.

Before we actually start coding we need to understand how in-app purchase works for Android and iOS both. Let’s see.

Step 3: Understanding How Purchase of Subscription Works

Purchase Flow reference taken from flutter_inapp_purchase
Purchase Flow reference taken from flutter_inapp_purchase
Purchase flow reference is taken from flutter_inapp_purchase

The flow of purchase for both platforms is more or less similar.

At the start of the app, you initiate a connection with Google / Apple billing server. Connection initialization may fail if billing SDK is not supported for the particular OS version.

After the connection is initialized your app subscribes to PurchaseUpdateStream of the billing server. The billing server notifies you about the status of every purchase made by the current user (inside or outside of the app from Play Store /App Store) via PurchaseUpdateStream.

After your app successfully subscribes to the PurchaseUpdateStream you load a list of all available products for the user.

The user chooses any product from the list and proceeds further to buy the product. Now following events happen.

Now let’s got to our last and the most important step the actual integration of in-app purchases in Flutter.

Step 4: Integrate In-App Purchase With Flutter

This is the most important step of all steps. You’ll have to handle the purchase and payment related details. You have to handle each and every possible exception that can happen during purchasing a product.

If you have understood the flow of purchase of subscription then this step will make more sense to you.

So let’s code.

I used the flutter_inapp_purchase plugin for my in-app purchase project. The reason for that was I find this plugin well documented and easy to understand.

To manage our code we will create one new file called payment_service.dart.

Now let’s create some variables to store the data and stream to listen to the latest status of all purchases.

If view(UI) want to get the latest updates then it can subscribe with the following methods and it can also cancel the subscription.

PaymentService will use the following methods to notify all the listeners.

Create initConnection and dispose method inside PaymentService. Don’t forget to call the initConnection method at the startup of your app.

Handle purchase errors using following method.

Now let’s see how to handle purchase updates.

Create two separate methods for Android and iOS to handle purchase updates. In iOS call finishTransaction in each case. In Android call finishTransaction only when the status is PURCHASED.

Now let’s verify the purchase if the status is a success.

in _verifyPurchase call the API of your back end to verify the purchase and based on the response handle the UI.

To get all the available products call the _getItems() in initConnection().

If you have noticed that in initConnection we are calling the _getPastPurchases() method. In iOS, this method returns all the purchases made in past(only finished). Another use of this method in iOS is when the user changes the device and you want to allow the user to restore his/her purchases then call this method. In Android, this method returns only active subscriptions (finished & unfinished both).

To buy the product call the following method.

If any error occurs during the transaction in iOS then the next time when the user will open the app the same purchased item will arrive at PurchaseUpdateStream. In Android, you will get the unfinished transaction via _pastPurchases.

Now all you have to do is call the initConnection method at the startup of your app. Get all the items from PaymentService to the UI & showcase your products. When users select a product and click on the buy button call the buyProduct() method with the selected product. Listen to the _proStatusChangedListeners and _errorListeners to handle all the scenarios.

So finally we did it.

To read more about the In-App Purchases read these official blogs.

Android: https://developer.android.com/google/play/billing/integrate

iOS: https://developer.apple.com/documentation/storekit/in-app_purchase

Check out other articles written by me.

Thank you for reading this. If you have any doubts about this article comment below.

BOSC Tech Labs Private Limited

Transforming IDEA into reality

BOSC Tech Labs Private Limited

We have experienced team members who bring their 20+ years of experience to the business. Our team members are master in crafting your requirements into an application. We are customer-centric and crazy enough to innovate and create new opportunities

Varun Kamani

Written by

BOSC Tech Labs Private Limited

We have experienced team members who bring their 20+ years of experience to the business. Our team members are master in crafting your requirements into an application. We are customer-centric and crazy enough to innovate and create new opportunities

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store