Step-by-Step Guide to Implementing In-App Purchases in a Flutter App for Android and iOS: Code and Tips.

Nachiketa Pandey
3 min readFeb 6, 2023

--

NACHIKETA PANDEYFriday, January 27, 2023

Implementing in-app purchases (IAP) in a Flutter app for Android involves using the in_app_purchase package, which provides a simple and flexible API for handling IAPs on both iOS and Android. The package uses the native in-app purchase APIs on each platform, so it can handle both subscriptions and one-time purchases.

here’s a step-by-step guide for implementing in-app purchases (IAP) in a Flutter app for Android, including code snippets to help you understand each step:

Step 1:

Add the in_app_purchase package to your pubspec.yaml file

dependencies:
flutter:
sdk: flutter
in_app_purchase: ^0.8.2+1

Step 2:

Configure your app for IAP by creating a new product list on the Google Play Developer Console. You will need to upload your app’s APK to the console, as well as create a list of the products (or in-app items) that you want to sell. Make sure you have the product ID’s ready for the next steps.

Step 3:

Initialize the IAP plugin by calling InAppPurchaseConnection.enablePendingPurchases()and

InAppPurchaseConnection.instance.isAvailable() in your main function.

void main() {
InAppPurchaseConnection.enablePendingPurchases();
runApp(MyApp());
}

Step 4:

Retrieve the list of available products by calling InAppPurchaseConnection.instance.queryPastPurchases()

and the

InAppPurchaseConnection.instance.queryProductDetails().

Future<void> _retrieveAvailableItems() async {
final QueryPurchaseDetailsResponse purchaseResponse =
await InAppPurchaseConnection.instance
.queryPastPurchases();
  final QueryProductDetailsResponse productResponse =
await InAppPurchaseConnection.instance.
queryProductDetails(_productIds);
availableItems = productResponse.productDetails;
pastPurchases = purchaseResponse.pastPurchases;
}

Step 5:

Initiate a purchase by calling InAppPurchaseConnection.instance.buyNonConsumable() or InAppPurchaseConnection.instance.buySubscription() and passing in the product ID of the item to be purchased.

void _buyProduct(ProductDetails item) {
InAppPurchaseConnection.instance.
buyNonConsumable(productDetails: item);
}

Step 6: Listen to the purchaseUpdatedStream to receive updates on the purchase status.

InAppPurchaseConnection.instance.purchaseUpdatedStream
.listen((purchaseDetails) {
// Handle purchase here
});

Step 7:

Once purchase is completed, check if purchase is valid or not by calling verifyPurchase(purchasedItem).

void _handlePurchase(PurchaseDetails purchase) {
if (purchase.status ==
PurchaseStatus.purchased) {
if (Platform.isIOS) {
InAppPurchaseConnection.instance
.completePurchase(purchase);
}
_verifyPurchase(purchase);
} else {
// Handle failed purchase
}
}

Step 8:

Consume the purchase if it is non-consumable.

void _verifyPurchase(PurchaseDetails purchase)
{
InAppPurchaseConnection.instance
.consumePurchase(purchase);
}

It’s important to note that on Android, you will also need to set up a server to handle the purchase verification process, as the in-app purchase package only provides client

Difference between implementing in-app purchases in Flutter for Android and iOS :

The main difference between implementing in-app purchases in Flutter for Android and iOS is the way the purchases are handled by the platform.

In Android, the in-app purchases are handled by the Google Play Store, and the developer needs to set up their own server for purchase verification. The developer can use the Google Play Billing Library’s Security.verifyPurchase() method to verify the purchase on the server.

On the other hand, in iOS, the in-app purchases are handled by the App Store, and the purchase verification is done by the App Store itself. The developer doesn’t need to set up their own server for purchase verification.

Another difference is that on iOS, the app must use the same App Store account as the developer account that created the app in order to test in-app purchases.

Both platforms have similar APIs for implementing in-app purchases, but you have to keep in mind the above differences and configure accordingly.

--

--

Nachiketa Pandey
0 Followers

Hi! I'm Nachiketa, Flutter developer B.Tech. in Computer Science passionate about Mobile Programming with 1.5 + year experience as a Flutter developer,.