In-app purchases allow users to purchase digital content, services, or features from within your app. Google Play’s billing system is supported by tools, APIs, and management tools.
One of the better ways to monetize your app is through in-app purchases that grant added value from app usage.
Types of in-app items:
- Consumables: Things purchased by a user and then used up, so the user could purchase them again, such as virtual money or lives in a game.
- Non-consumables: This involves one-time purchasing by users for things they can always have, such as a special feature or a version of the app that does not have advertisements.
- Subscriptions: These are periodic payments a user will make for continuous service, such as a monthly fee for additional in-game functionality.
The post will talk about how to set this up, best practices, and some tips for making your users’ experience smooth.
Requirements
Acquire the following basics before being able to implement in-app purchases:
- Google Developer Account: You will need a Google Developer account to host your app in the Play Store and to allow in-app purchases.
- Adding Google Play Billing Library: You are supposed to add the Google Play Billing library to your project. The library highly simplifies the process of realization for in-app purchases.
- Android Development Basics: You should know the basics of Android development, Activities, Fragments, and configuration files of Gradle.
Once these are completed, you can begin the work of the project.
Step 1: Setting Up Your Google Play Console
Put up and create your app.
- Log in to Google Play Console: Open Google Play Console and sign in using your developer account.
- Create New App: If your app is new, click on the
Create App
button on the top right. You will fill out some information about the app. - Add In-App Billing: In the “Monetize” tab, turn it on. Set it up with your payment profile to allow for disbursements made on in-app purchases.
Add In-App Products
- Add a Product: Tap either on “Managed Products” (consumables and non-consumables) or “Subscriptions” under the “Monetize” tab.
- Product Details: Specify a special product ID, its price, and a description for each of these products. It should be clearly defined what all the products are: the type of product—whether it can be used up or cannot be used up, or it’s a subscription.
Checking In-App Buying
- Add Test Accounts: Add test accounts—usually your personal Gmail accounts—to test the in-app purchases feature without spending any real money.
- Test Products: Formulate a testing plan and test if all your in-app products are working fine before publication. Your app can be tested from various angles, be it through Google Play’s internal testing process or by downloading the app directly from an APK.
Step 2: Integrate Google Play Billing Library with your Android application
Add Dependencies
You will need to include the Google Play Billing Library as shown in the following build. gradle:
implementation 'com.android.billingclient:billing:4.0.0'
Synchronize your project with Gradle so that dependency can be added.
Start the Billing Client
You will need to create an BillingClient
in your application. This client will allow your application to communicate with the Google Play Store.
BillingClient billingClient = BillingClient.newBuilder
.enable
.setListener((PurchasesUpdatedResponseListener) billingResult, List<Purchase> purchaseList) -> {
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List purchases) {
Donec commodo urna finibus eros congue
}
})
.build
### Build Relationship with Google Play
The BillingClient must be connected to Google Play when created.
java
billingClient.startConnection(new BillingClientStateListener() {
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// Billing client is already to be queried. You can query purchases here.
HUMAN
}
@Override
public void onBillingServiceDisconnected() {
// Reconnect with your connection when a button is clicked to kick off a purchase request to Google Play
}
});
### Ask for products in stock to be shown to user(Listing)
This data needs to be sourced from Google Play service to display a list of products available for purchase.
java
List skuList = new ArrayList<>();
skuList
SkuDetailsParams params = Sku
.set
.setType(BillingClient.SkuType.INAPP) // or SkuType.SUBS for subscriptions
.build
billing client.querySkuDetailsAsync(params, new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List skuDetailsList) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
for (SkuDetails skuDetails : skuDetails
// Handle SKU details here
}
}
}
});
function
`Human
Purchase Flow Implemented
Once you have the details of the product, using them, the buying process can be started if the user wants to purchase something.
BillingResult billingResult = billingClient.launchBillingFlow(activity, billingFlowParams);
boolean purchased product = handle(purchase);
You might have noticed in the above-shown API flow that some response not only contains the information about the purchase of the product(s) but also contains the SKU details. For the responses where the response code is `BillingClient.BillingResponseCode.OK`, you're free to consume the acquired consumable against the product(s) that are just purchased. See how fetching of the item that is to be consumed is done:
java
You
public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List
// Process the purchase
process purchase
}
} else if (billingResult.getResponseCode() == BillingClient
// User canceling the purchase
} else {
// Handle other errors
}
}
### Consume Consumable Purchases
For items that you can buy and use more than once, make sure to use the item after it has been successfully bought.
java
ConsumeParams consumeParams =
Have
.set
.build
billing client.consumeAsync(consumeParams, new ConsumeResponseListener() {
Examples:
public void onConsumeResponse(BillingResult billingResult, String purchase
If (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// Handle successful consumption
}
Behave
}
### Acknowledge Non-Consumable Purch
For single-purchase products that you normally do not run out of or repurchase, a confirmation is needed whenever you receive the message that your purchase has been successful.
java
AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder(); .set
.build
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED
&& .purchase.isAcknowledged()) {
billing client.acknowledge purchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
if (billingResult.getResponseCode() == Billing)
// Handle successful acknowledgment } } }); `` Step 3: Use Good Security Practices ### Checks Purchased You must verify the purchase on your server in order to avoid fraud. Make sure to pass the purchase token to your server and validate it with Google's system. Security of your App Turn on ProGuard or R8 to obfuscate the code of your app. This makes the teardown of the app a bit harder for potential attackers and subsequently messes up your in-app purchases. ## Step 4: Manage Subscriptions In the event that your app offers subscriptions, you need to ensure that your app allows the customer to access any premium content during their subscription period. The subscription lifecycle is managed by Google Play, but you should: - Subscription Upgrades/Downgrades: Allow a user to change their subscription level. - Grace Period: Grant grace time so that, if their payment fails, users can renew their subscription for a few more hours. - Account Hold: Deal with the situation when someone's account is on hold for payment issues. For querying information and managing subscriptions, you will be using those same
BillingClientmethods, this time passing
SkuType.SUBS` for the variable. ## Step 5: Create Your App and Test It Out That’s very important: testing. You don’t want people purchasing your in-app purchases, only to find out that the features don’t work, do you? Here’s how you can test your setup: – Google Play’s Testing Tracks: Google Play has different testing tracks: internal, closed, and open. These tracks let you share your app with testers. – Test Purchases: This allows you to check your Google Play purchases by simulating charges on the test accounts.