It was my first time I was adding in-app purchases to any app. I had a lot of questions.

  • “How do I internationalise the price?”
  • “How do I not hardcode in product details in my app?”
  • “How much commission will Google charge?”

The answer is, Google Play Store handles everything for you. And keeps 30%. You receive 70% of the payment.

Before you read further, let me tell you that this tutorial covers only the Android part. Also, you need a developer account to integrate IAP, even for testing. So before starting I’ll recommend you to spend $25 and buy a play store developer account.

The first step is to add the dependency to your flutter app. Add the following line to your pubspec.yaml

dependencies:
 in_app_purchase: ^0.3.4

For the latest version of this package check here.

$ flutter pub get

After this, we need to add some required permissions so that the play store can identify that this app has in-app purchases. Add the billing permissionto your /android/app/src/main/**AndroidManifest.xml**

<uses-permission android:name="com.android.vending.BILLING" />

Now let’s create a draft app on Google play store. Generate a signed apk for your Flutter app. Read about creating a release apk from the official flutter dev docs. Remember play store uses your app package name (application ID) to identify your in-app products.

Upload your apk in alpha channel for testing purpose and save it as a draft.

Now, let us create some in-app products. Go to Store Presence > In-app Products > Managed Products. Click on CREATE MANAGED PRODUCTS.

The Product ID is unique and once created it cannot be changed. Even if you delete it, you can not create a product with the same id for the same app. So be careful with that.

The best thing is that Google Play Store automatically handles the price internationalisation for you. Just set the default price and customise it for any specific country.

After this, we will write the flutter code to trigger the purchase and deliver the product on successful payment.

Create a set of product IDs. Note, its a set, be careful of the order of product ids. They will be alphabetically sorted.

const _productIds = {'product_1', 'product_2', 'product_3'};

Define the following variables.

InAppPurchaseConnection _connection = InAppPurchaseConnection.instance;

StreamSubscription<List<PurchaseDetails>> _subscription;
List<ProductDetails> _products = [];

#merchant #in-app-purchase #iap #android #flutter

Flutter In-App-Purchase, step-by-step!
4.45 GEEK