Getting Started With In-App Purchases

Learn how to get started with in-app purchases and implement this library inside your next project. By Mattia Ferigutti.

3.5 (2) · 2 Reviews

Download materials
Save for later
Share

When Android was born, the only way to pay was directly on the Google Play Store. This method wasn’t ideal since no one wants to buy something without seeing it.

To solve this issue, developers started using another solution, in-app purchases. With in-app purchases, users can download your app for free from the play store and decide whether to buy something to unlock new features, such as a new sword from League of Legends. This business model, freemium, is the most used revenue technique among developers.

It’s difficult to handle money transactions on your own while keeping the system as secure as possible. So, Android introduced the Google Play Billing Library. It’s powerful enough to convince users to commit to a service, use subscriptions and make in-app purchases with consumable and non-consumable products.

In this tutorial, you’ll learn how to implement this library and about:

  • Setting up the Google Play Store.
  • Non-consumable purchases.
  • Purchase flow.
  • Best practices.
  • Setting up testing accounts.
Note: To follow along with this tutorial, you need experience with the Android SDK, Kotlin Flow and StateFlow, Coroutines and Google Play Developer Console.

Getting Started

Download the materials by clicking Download Materials at the top or bottom of this tutorial. Then, open the starter project in Android Studio.

The app you’ll build is a monster catalog with two screens.

Monsters home screen

The first screen shows the most powerful monster you have.

Monsters catalogue screen

Users can choose which monster they want to buy from a list on the second screen. Every monster has different power points. In this case, the more expensive, the better.

Project structure

Take a look at the project’s structure:

  • MonsterAdapter.kt contains the logic to display a list of monsters.
  • BaseApplication.kt contains an instance of the BillingHelper class which can be shared among all the activities.
  • BillingHelper.kt contains the logic to handle in-app purchases.
  • Security.kt is a utility class that checks the validity of the public key from the Google Play Developer Console.
  • MonsterData.kt contains the list of monsters.

This article has three different sections:

  1. Learning the service fees, terminology and concepts of the library.
  2. Implementing the logic inside a wrapper class.
  3. Implementing the just created class inside your app.

You’ll start by learning a little more about the service fees.

Service Fees

Unfortunately, Google doesn’t give you access to this library for free. But the good news is that as of July 1, 2021, the fees have decreased from 30% to 15%. However, there are a few conditions you need to respect:

  • If enrolled in the 15% service fee tier:
    • The fee is 15% for the first $1M (USD) of earnings each year.
    • The fee increases to 30% for earnings in excess of $1M (USD) each year.
  • For developers not enrolled in the 15% service fee tier, the service fee is 30%.
  • For subscription products purchased by subscribers you retain after 12 paid months, the service fee is 15%.
  • The fee is 15% for the first $1M (USD) of earnings each year.
  • The fee increases to 30% for earnings in excess of $1M (USD) each year.

Terminology

Before implementing the library, you need to get familiar with Google’s terminology.

Content Types

You can use the Google Play Billing Library to sell digital products and content in your Android app. This library can help you sell the following types of digital content: one-time products and subscriptions. One-time products fit into two subcategories:

  • A consumable product is one that a user consumes to receive in-app content. For example, many games let you buy coins you can spend to enhance your character. When a user consumes the product, your app dispenses the associated content, and the item is available for purchase again.
  • A non-consumable product is a product users purchase only once to provide a permanent benefit. Examples include apps that let you remove ads after making the payment.

This article will walk you through only the one-time products. However, the steps are the same for a subscription.

Concepts

You also need to get familiar with some key concepts:

  • A flow describes the steps required to buy a digital product.
  • A product SKU is the ID of a specific product type. Every item at your favorite grocery store has a barcode, usually on the back of the product. This barcode serves as the product’s identity, so every time the cashier passes the product over the scanner, they know the exact price of the item.
  • A purchase token is a string that indicates that a Google user paid for a specific product.
  • An order ID is a string that represents a financial transaction on Google Play. Every time a financial transaction occurs, Google Play creates an order ID which it includes on the receipt emailed to the buyer. You can also use the order ID to manage refunds in the order management section of the Google Play Console. Google Play also uses order IDs in sales and payout reports.

With that information in hand, it’s time to get started.

Setting-Up the Google Play Console

You’ll use the Google Play Console throughout this tutorial. If you haven’t created an account yet, it’s time to do so. Make sure to follow all the steps in order.

Open the Google Play Console and open All apps. Click Create app in the top-right corner. Give a name to your app and fill out all the other fields.

Then, on the Free or paid field, select Free. Paid means you need to pay directly on the play store before downloading the app. Finally, click Create app.

Now, you’ll see your app’s dashboard. You need the app’s public key, a base-64 string. To find it, scroll the menu on the left until you find Monetization setup. Click it and copy the base64-encoded RSA public key under Licensing.

Next, you’ll set up the Android project.

Setting-Up the Android Project

Now that you have the app key, open Constants.kt inside your starter project. You’ll see this:

// PUBLIC API KEY
const val PUBLIC_KEY = "YOUR_KEY"

Replace YOUR_KEY with the actual key you copied for the Play Console.

Then open build.gradle. Add this dependency and sync the project:

dependencies {

  // Google Play Billing Library
  def billing_version = "4.0.0"
  implementation "com.android.billingclient:billing-ktx:$billing_version"
}

With your Android project ready, it’s time to take a look at the APK.