In-app purchase in Flutter

Talha SEZER
Codimis
5 min readAug 24, 2022

--

Although we always make our applications with care, we don’t always make them free. Apart from uploading our apps to the Play store for a fee, another way of making money is through in-app purchases. Flutter in_app_purchase (IAP) is a first-party Flutter package that allows developers to implement in-app purchases in their app from the App Store on iOS or Google Play on Android. Two other solutions, flutter_inapp_purchase, and purchases_flutter, also provide similar functionalities.

In this article, our aim is to guide you on purchases_flutter package.

purchases_flutter

This is a plugin that implements in-app purchases using RevenueCat’s solution.

RevenueCat is a third-party agency that simplifies the implementation of in-app purchases in applications. When using the official Flutter plugin (in_app_purchase), you are required to implement your logic on the server to handle processes like purchase validation, subscriptions, and cancellations.

Doing this is a lot of logical work, and as such, alternatives can be attractive as they can do much of the heavy lifting. purchases_flutter can handle this logic for you, and implementing in-app purchases in your app is much more accessible as a result. RevenueCat’s server will handle the purchase validation and all the in-between logic and middleware.

Similar to the other implementations of in-app purchases, you will need to set up active in-app purchases on the Play Store and App Store.

Let’s Start with RevenueCat

Before using RevenueCat you have to sign up to RevenueCat. Don’t be afraid. RevenueCat is free if your monthly income from the app is less than 10000$.

After signing up, navigate to the RevenueCat dashboard and add a new project from the dropdown in the top navigation menu called Projects.

From Project Settings > Apps in the left menu of the project dashboard, select the platform for the app you will be adding.

The field App name is required to add your app to RevenueCat. The rest of the configuration fields can be added later.

Before you can start using RevenueCat to fetch products, you must configure your products in the respective stores. If you want to publish your app through Play Store, you can watch Johannes Milke’s tutorial about “How to publish Flutter app on Play Store” video. This video shows how to create a Google Play Console account.

In Play Console, you need to set up a subscription with the same Product ID and same trial and amount.

subscription in android

Details for both subscriptions should be kept the same.

Installation

To use this plugin, add purchases_flutter it as a dependency in your pubspec.yaml file (and run an implicit dart pub get):

dependencies:   purchases_flutter: ^3.0.0

You should now be able to import purchases_flutter.

import 'package:purchases_flutter/purchases_flutter.dart';

If you’re using other plugins like mobx, you may run into conflicts with types from other plugins having the same name as those defined in purchases_flutter.
If this happens, you can resolve the ambiguity in the types by adding an import alias, for example:

import 'package:purchases_flutter/purchases_flutter.dart' as purchases;

After that, you can reference the types from purchases_flutter as purchases.Foo, like purchases.PurchaserInfo.

Configure the SDK

You should only configure the shared instance of Purchases once, usually on app launch. After that, the same instance is shared throughout your app by accessing the .shared instance in the SDK. If you want to see the guide about Configuring SDK for more information you can click here.

import 'dart:io' show Platform;
//...
Future<void> initPlatformState() async {
await Purchases.setDebugLogsEnabled(true);
if (Platform.isAndroid) {
await Purchases.setup("public_google_sdk_key");
}
else if (Platform.isIOS) {
await Purchases.setup("public_ios_sdk_key");
/* OR: if building for Amazon, be sure to follow the installation instructions then: */
await Purchases.setup("public_amazon_sdk_key", useAmazon: true);
}
}

Displaying Available Products

The SDK will automatically fetch the configured Offerings and retrieve the product information from Apple, Google, or Amazon. Thus, available products will already be loaded when customers launch your purchase screen.

try {   Offerings offerings = await Purchases.getOfferings(); 
if (offerings.current != null)
{ // Display current offering with offerings.current }
} on PlatformException catch (e) {
// optional error handling
}

Make a Purchase

The SDK includes a simple method for facilitating purchases. The purchase:package takes a package from the fetched Offering and processes the transaction with the respective app store.

try {  
PurchaserInfo purchaserInfo = await Purchases.purchasePackage(package);
var isPro = purchaserInfo.entitlements.all["my_entitlement_identifier"].isActive;
if (isPro) {
// Unlock that great "pro" content
}
} on PlatformException catch (e) {
var errorCode = PurchasesErrorHelper.getErrorCode(e);
if (errorCode != PurchasesErrorCode.purchaseCancelledError) { showError(e);
}
}

Get Subscription Status

The SDK makes it easy to check what active subscriptions the current customer has, too. This can be done by checking if a specific Entitlement is active, or by checking if the active Entitlements array contains a specific Entitlement ID.

try {   
PurchaserInfo purchaserInfo = await Purchases.getPurchaserInfo(); if (purchaserInfo.entitlements.all["my_entitlement_identifier"].isActive)
{
// Grant user "pro" access
}
} on PlatformException catch (e) {
// Error fetching purchaser info
}

Restoring Purchases

RevenueCat enables your users to restore their in-app purchases, reactivating any content that they previously purchased from the same store account (Apple, Google, or Amazon account). We recommend that all apps have some way for users to trigger the restore method. Note that Apple does require a restore mechanism in the event, in case a user loses access to their purchases (e.g: uninstalling/reinstalling the app, losing their account information, etc).

try {   
PurchaserInfo restoredInfo = await Purchases.restoreTransactions();
// ... check restored purchaserInfo to see if entitlement is now active
} on PlatformException catch (e) {
// Error restoring purchases
}

Conclusion

In this article, I have tried to explain one of the methods of in-app purchase methods. It is an introduction to in-app purchase methods. You can find some complex tutorials on Youtube that make an app from the start. If you don't understand this article and want to watch a tutorial, I can recommend you to watch Johannes Milke’s RevenueCat tutorial.

I hope I was able to answer your “How to add In-app purchase to a flutter app?” question with this article that I wrote.

--

--