In-app purchases are a common way for developers to create a free application, and then provide users with options to upgrade through in-app purchases. Google Play in-app purchases are the simplest solution to selling digital products or content on Android apps. Therefore, many app developers who are looking to sell digital goods, or offer premium membership to users, use the Google Play in-app billing process for smooth and easy checkouts.
In-app purchases can be anywhere from a sword that gives you more power in a game, a key that unlocks more features of a free app, or conversion to virtual currency that can be used for purchases.
In-app purchases can vary from app to app and user to user. The following article aims to breakdown the different possible types and provide basic installation instructions.
In this step by step guide we will describe the ways in which in-app purchases can be implemented in Android applications.
In-app purchases will incur transactional fees toward Google. The transaction fee for all purchases in Google Play is 30% of the price that the user pays per transaction*. The end amount paid by buyers therefore, will be distributed. Developers receive 70% of the payment, and the remaining 30% goes to the distribution partner and operating fees.
*Accurate as of the date of publication
As Illustrated here below simply select a project template:
For Kotlin use the above dependency because the KTX module contains Kotlin extensions and coroutines support, which will make the process a lot easier as we proceed.
Select: Store presence → In-app products → Managed products. This is where we will be able to create the desired in-app purchases.
Select: Set up a merchant account, and fill in the form requesting basic information such as name, address, email, etc.
Select: CREATE MANAGED PRODUCT button. Then use the product-creation page, that should be similar to the example here below:
The Product ID will be unique you will not be able to change later. The next step will be to fill in the title and description with the relevant information. Remember to make sure to copy all the product IDs.
The above steps conclude the work that is needed for Google play, and now we can proceed with handling the flow of the in-app purchases.
First and foremost creating the method: In setUpBillingClient we can initialize the billing client, as follows:
We made use of the builder pattern provided in order to create the billing client instance. The PurchasesUpdatedListener should be provided, which is where all callbacks related to any initiated purchases will be received.
BillingClient is the main interface for communication between the library and the application code. It provides many convenient methods for in-app billing. We can create a class billing client instance for an application and use it to process in-app billing operations. It provides synchronous (blocking) and asynchronous (non-blocking) methods for many common in-app billing operations. All methods are supposed to be called from the UI thread and all the asynchronous callbacks will be returned on the UI thread as well.
When we create the client instance, we need to start a connection on the instance created as follows: Once we receive the callback in onBillingSetupFinished, we can use billingResult.responseCode to check whether the connection was established successfully. Then, we can go ahead and launch the purchase flow or a query regarding one of the products.
We need to create a SkuDetailsParams instance to initiate a query for the SKU details. To do this, the list of the SKUs must be set up and their types set to SkuDetailsParams. Once the parameters are ready, call the querySkuDetailsAsync method on the BillingClient instance created.
Now a simple Android view file can be created to display the details of Step 8 as follows:
Create the XML file, identifying the title and description fields so they display the info of the user specific layout.
Next, add one button so when a user selects it, the purchase flow will commence.
The billing flow should be launched as follows:
If the response code of billingResult is ok, then the purchase will have been successful.
The next item that needs to be configured is the flow for both consumable and non-consumable purchases:
For non-consumable purchases, first identify the purchase through the billing client.
With consumable purchases, first you must call consumeAsync for the billing client:
You will notice we incorporated a number of important logs. One of the most important places to use logging is with purchases. In the event that an end user has problems making a purchase, the user’s logs will give you the critical information necessary to discern exactly what caused the issue. Failure to successfully implement purchases in your mobile app will lead to, of course, loss in profits. Once you implement logs into your app, you will need to retrieve them from your end users’ devices with a third-party logging platform, like Shipbook.
In this article we initially reviewed in-app purchases in general. We went on to cover the different types of in-app purchasing that can be developed and provided a step-by-step tutorial for integration of in-app purchasing in an Android app. In-app purchases are growing in popularity, they truly add value to your mobile app. Hopefully this article provided insight as to how to seamlessly integrate in-app purchases onto your app, enhancing app quality and user experience.