Implementing Google Play Billing Library v4 for in-app and Subscriptions

Ali Mehdi
3 min readAug 13, 2021
Photo by Mika Baumeister on Unsplash

Recently Google has updated their billing library to version 4. I had implemented v2 payment library 2 years ago using a 3rd party library in my app. But the problem is now i am facing is that they are not maintaining this library so i have to update it to Google’s Play Billing library v4. this app contains in-app purchase as well as subscriptions. So we will cover most of the code which will have:

  1. Initialization
  2. Sku Details
  3. in-app purchases
  4. Subscriptions
  5. Restore purchases.
  6. Testing

before we begin here is the url of the google billing library documentation. (https://developer.android.com/google/play/billing)

Prior to reading this tutorial, you will want to access your application from your developer console for “In-App Product” (Under Application Monetize > In-app products > Create Product).

And for “Subscriptions” (Under Application Monetize > Subscriptions > Create subscription )

  1. Initialization

Add the billing dependency to your Gradle File

implementation 'com.android.billingclient:billing:4.0.0'

In you Activity initialize your BillingClient and implement PurchasesUpdatedListener and implement required methods with it e.g. OnPurchaseUpdated. This will notify any updates in the purchases.

Define your global variables for your class. You should have a string that exactly matches your product ID from the “In-app products” or “Subscriptions” section of the developer console. Your product must also be Active in your console.

You should have a global BillingClient variable that you will use for your in app purchases or subscriptions.

Inside onCreate you can intialize your client and can also implement PurchasesUpdatedListener which contains a method named onPurchasesUpdated which returns a list of purchased item. there you can process you purchases.

after that you can startConnection from your client.

onBillingSetupFinished you can here start using the for purchases.

2. Sku Details

To start a purchase you need SkuDetails object which will be used to get the product info from the developer portal e.g. name, price etc.
for that you need to provide productID in this case we have inAppItem1 store variable and SkuType which can be INAPP or SUBS.

3. in-app purchases
4. Subscriptions
To start buying inapp or subs you just need an sku object (step 2) of either type and can start purchase with lauchBillingFlow.

5. Restore purchases.
to request your previous purchases you can implement queryPurchasesAsync with SkuType.

6. Testing
After implementation the most important part is testing because users money is on the stake. for testing you have to upload a test build to developer portal and invite testers. Only those testers will be able to test the inapp purchases or subscriptions free of charge in sandbox environment.

Precautions
you can save your purchases in SharedPreferences or in localDB, but the most important thing here is that you always check the response code first, in onPurchasesUpdated. there are different type of responses e.g.

int SERVICE_TIMEOUT = -3;
int FEATURE_NOT_SUPPORTED = -2;
int SERVICE_DISCONNECTED = -1;
int OK = 0;
int USER_CANCELED = 1;
int SERVICE_UNAVAILABLE = 2;
int BILLING_UNAVAILABLE = 3;
int ITEM_UNAVAILABLE = 4;
int DEVELOPER_ERROR = 5;
int ERROR = 6;
int ITEM_ALREADY_OWNED = 7;
int ITEM_NOT_OWNED = 8;

if you are only updating you previous version to latest just follow the release notes and change accordingly.

https://developer.android.com/google/play/billing/release-notes

Follow Me on GitHub:
https://github.com/alimehdipk

--

--

Ali Mehdi

CEO of Cortex Technology, medium writer, playing console games, Digital Marketing