Monetising Magic: Unveiling the Power of In-App Purchases(IAP) in Flutter

Understanding In-App Purchases

An in-app purchase (IAP) refers to any purchase made within a mobile application. These purchases typically unlock additional content or features that enhance the user experience. They are common in free-to-download apps (often called freemium apps) where the initial download is free, but certain aspects of the app require payment to access.

These purchases are facilitated through app stores like the Apple App Store or Google Play Store, which manage transactions securely and provide a revenue share to the app developer.

Life of a purchase

Here’s a typical purchase flow for a one-time purchase or a subscription.

  1. Show the user what they can buy.
  2. Launch the purchase flow for the user to accept the purchase.
  3. Verify the purchase on your server.
  4. Give content to the user.
  5. Acknowledge delivery of the content. For consumable products, consume the purchase so that the user can buy the item again.

Subscriptions automatically renew until they are canceled. A subscription can go through the following states:

  • Active: User is in good standing and has access to the subscription.
  • Cancelled: User has cancelled but still has access until expiration.
  • In grace period: User experienced a payment issue but still has access while Google is retrying the payment method.
  • On hold: User experienced a payment issue and no longer has access while Google is retrying the payment method.
  • Paused: User paused their access and does not have access until they resume.
  • Expired: User has cancelled and lost access to the subscription. The user is considered churned at expiration.

Making a purchase

In-app purchases can take various forms:

  1. Consumable
    Once bought, consumable purchases become depleted as the user uses them. After using all of a consumable purchase, the user must purchase another consumable IAP again to enjoy the same benefits.
    Example: Extra lives in a gaming app
  2. Non-consumable
    Once a user buys a non-consumable purchase, it’s permanently available to them. It does not expire with time or use. Non-consumable purchases are often premium features.
    Example: Additional filters in a photo app.
  3. Subscriptions: Services that renew automatically after a certain period, offering access to content or features over time
    • Auto-renewable subscriptions – ones that are prolonged via regular payments.
    • Non-renewing subscriptions – ones that offer premium access for a limited period of time.

Step 1: Setting Up Developer Accounts

Before integrating in-app purchases into your Flutter app, you must have accounts on Google Play Developer Console & App Store Connect. These platforms will be used for managing your in-app products and purchases.
Google Play Store: Sign up for a Google Play Developer account. You’ll need to pay a one-time registration fee

Google Play documentation

Apple App Store: Enroll in the Apple Developer Program. This involves an annual fee.

App Store documentation

Once your developer accounts are set up and activated, you’ll gain access to the developer consoles where you can manage your apps and in-app purchases.

Step 2: Creating Products

1. Create In-App Products in the Google Play Developer Console:

  • Navigate to your app in the Developer Console.
  • Go to “Monetize” > “Products” and click on “Create managed product”.
  • Fill in details like Product ID, Title, Description, Price, etc.
  • Select the appropriate product type (in-app product, subscription, etc.).
  • Set prices and manage availability for different regions.
  • Google Play supports various pricing formats and currencies.

2. Create In-App Purchase Products in App Store Connect:

  • In App Store Connect, go to “My Apps” > select your app > “Features” > “In-App Purchases”.
  • Click on the “+” button to create a new in-app purchase.
  • Choose the type of product (consumable, non-consumable, subscription).
  • Enter details like Reference Name, Product ID, Price, Description, etc.
  • Set prices and manage availability for different regions.
  • Apple App Store supports various pricing tiers and currencies.

To integrate in-app purchases into your Flutter app, it’s essential to assign unique product IDs to each available product or subscription plan. These IDs act as identifiers, distinguishing between different purchase types and allowing your app to fetch specific product information from the app store.

For instance, consider these examples of product IDs for various subscription plans:

  • Yearly Plan Product ID: com.example.yearly
  • Monthly Plan Product ID: com.example.monthly

By creating distinct product IDs for each subscription or product offered within your app, you ensure clarity and consistency throughout the purchasing process. Embed these IDs into your app’s codebase and utilize them when initiating purchases or retrieving product details.

Step 3: Integrating In-App Purchases into Flutter

Adding dependencies

The initial step is to add the dependencies to your Flutter project.

dependencies:
  flutter:
    sdk: flutter
in_app_purchase: ^3.2.0(Latest version)
   

After adding dependencies your first step is to check that the in-app purchases are supported and available on the current platform or not.

Connecting to the underlying store 

Check store is available or not

Once Store is available, Start Listening to purchase updates stream in initState() method.

Listening to purchase updates 

Fetch all the product which we created at the respective store.

Here we will create a list of InAppPurchase product which we have created on to respective store.
Then we will make use this list of ProductId as input while making a call to store for fetching the products details.

Once all products have been fetched, we can utilize their details to create a UI. Subsequently, the next phase involves the purchasing or buying process.

Lets check out how one can purchase Consumable & Non-consumable Items.

buyConsumableOrNonConsumableProduct

When calling buyConsumableOrNonConsumableProduct Method with argument isConsumable=true it will trigger the _iap.buyConsumable Plugin method which in turns initiate the purchase flow of the respective platforms.
Here one thing to keep in mind that in _iap.buyConsumable Plugin method we are passing autoConsume: true, this will enable automatic consumption of the product after purchase, ensuring a seamless user experience.
Here the productId Parameter represents the unique identifier of the product which we had defined in both the Google Play Store and Apple App Store. We are already Fetch all the product which we created at the respective store in the previous step. So, here we just need to pass it. As it serves a reference to the specific product users intend to purchase.

IOS
Android

Completing a purchase 

The InAppPurchase.purchaseStream will send purchase updates after initiating the purchase flow using InAppPurchase.buyConsumable or InAppPurchase.buyNonConsumable. After verifying the purchase receipt and the delivering the content to the user it is important to call InAppPurchase.completePurchase to tell the underlying store that the purchase has been completed. Calling InAppPurchase.completePurchase will inform the underlying store that the app verified and processed the purchase and the store can proceed to finalize the transaction and bill the end user’s payment account.

Warning: Failure to call InAppPurchase.completePurchase and get a successful response within 3 days of the purchase will result a refund.

Restore Purchases

When you reinstall an app or switch devices, restoring purchases lets you get back the stuff you already paid for, so you don’t have to buy it twice.

Restoring purchases typically refers to the process of retrieving a user’s previous purchases, especially in scenarios where the app is reinstalled or the user switches to a new device. This ensures that users don’t have to pay again to access things they’ve already bought.

restorePurchases

Conclusion

Congratulations! You’ve successfully integrated in-app purchases (IAP) into your Flutter app, unlocking a powerful monetization tool. By offering consumable and non-consumable IAPs, you can cater to diverse user preferences, enhance their experience, and generate sustainable revenue for your app.

I hope this blog post has provided you with a clear roadmap for integrating in-app purchase into your Flutter apps. Happy coding!!!

You may also like

Leave a Reply