How To Add In App Purchase Android
An Overview of Google Play's Billing System
Before directly jumping into the code, let's get a basic idea of the billing system. One of the important things to know is what kind of products we can sell to users. Google Play's billing system helps us sell the following types of digital content:
So now let's understand each of them. The in-app purchases mainly consist of two types of items: one-time products and subscriptions.
One-time products
By the name itself, we understand that it's a one-time purchase without a recurring charge. A one-time product is an item that users can purchase with a single, nonrecurring charge. And again, this one-time product has two different categories, as shown above:
- A consumable product is one in which a user can purchase repeatedly to get access to in-app content. For example, this could be purchasing currency in a game that'll run out in due time, and users can again purchase the consumable product to get currency, food, etc.
- A non-consumable product is one in which a user can purchase a permanent benefit without the need of buying it again and again. Mainly, we use this kind of product to offer premium membership to users for removing ads, gaining access to a certain level in a game, etc.
Subscription
By the name itself, we can understand that we're talking about a recurring purchase to gain access to the app content.
Subscriptions get renewed automatically until they're canceled by users, meaning users get billed until they cancel the subscription on a recurring period of time — like one month, six months, one year, etc. For example, these subscriptions are mainly used in the case of music streaming services, online magazines, etc.
Android Implementation of In-App Purchases
Let's take a step-by-step approach to better understand this. If you don't want to go through these steps and would rather directly jump into to the code, here's the GitHub repo.
Let's start with the coding!
1. Let's first create a sample app
2. Once the project is created, let's add the billing-client dependency into the build.gradle file at the app level or module level — wherever you want
dependencies {
def billing_version = "3.0.0" //Kotlin
implementation "com.android.billingclient:billing-ktx:$billing_version"
}
If you're using Kotlinm use the above dependency because the KTX module contains Kotlin extensions and coroutines support, which will ease things for us. Otherwise, you can add the following and sync the Gradle files to download your required dependency
implementation 'com.android.billingclient:billing:$billing_version' 3. Before proceeding further, we need to upload an app to the Play Store
Once we're done adding the library, we need to generate a build and publish the app. It can be published to the internal testing track, which restricts access for your testing purposes.
Note: Remember the app version we're uploading to the Play Store. The version tested should be the same — otherwise, we'll run into unrelated issues.
4. After publishing the app, we need to create the products we want to sell
For the creation of in-app products navigate to:
Store presence → In-app products → Managed products
Then, we'll see the following page, where we can create our products.
Initially, click on "Set up a merchant account," and fill in the form that asks basic info like name, address, email, etc. After that, click on the "CREATE MANAGED PRODUCT" option.
Then it navigates to the product-creation page that looks like the following:
We need to be a bit careful when creating a product ID because it can't be modified later, and remember it should be a unique ID. After filling in that, fill in the title and description with the appropriate info. This will be visible to the user when we launch the purchase flow.
Note: After you're done with the creation of products. Copy all the product IDs, and keep them on your server. We need these product IDs because those are the ones that need to be provided to the billing client to get info, initiate a purchase, etc. Otherwise, if you're serverless, keep them in a constants file.
5. Initialize the billing client
Now that everything has been completed on the Google Play, console let's come back to our code base and start handling the in-app purchases flow. Inside our activity, firstly let's create a method, setUpBillingClient, where we can initialize the billing client, as follows:
We're using the builder pattern provided to create the billing client instance. For this, we need to provide an PurchasesUpdatedListener, where we get our callbacks related to any purchases initiated. We go to that part later once the basic setup is done.
"BillingClient is the main interface for communication between the library and application code. It provides convenience methods for in-app billing. We can create one instance of this class for your 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" —Android Developers docs
6. Start the connection on the billing-client instance
Once the client instance is created, we need to start a connection on the instance created:
Once we receive the callback in onBillingSetupFinished, we can use billingResult.responseCode to check whether the connection is established successfully. Then, we can go ahead with launching the purchase flow or a query about our products, etc.
7. Let's query the products available
We need to create a SkuDetailsParams instance to initiate a query for the SKU details. We need to set the list of the SKUs and their types to SkuDetailsParams.
Possible SKU types are in-app products and subscriptions, as discussed above.
Once SkuDetailsParams is ready, we need to call the querySkuDetailsAsync method on the billing-client instance created.
Once the items are fetched, we can update and show them on the screen. As we have only one unit, let's create a simple XML file to show the details.
8. UI creation to show SKU details
Let's create the XML file, with a title and description fields to show the info to the user. And add one button so when the user clicks on that we can start the purchase flow.
9. Update the UI from the result of 'QueryPurchases'
Try to write better code yourself by using extensions.
10. Launch the purchase flow upon click of the buy button
As we have the details of the SKUs fetched, we can launch the billing flow.
11. Let's handle the purchase result in 'purchaseUpdateListener'
If the response code of billingResult is OK — e.g., 0 — then the purchase is successful, and we need to handle the flows for both consumable and non-consumable purchases (which were discussed above in the Managed Products section).
12. Handle consumable and non-consumable purchases — then grant access for users to use the particular feature
For a non-consumable purchase, we need to acknowledge the purchase through the billing client. However, for consumable purchases, we need to consume them by calling consumeAsync on the billing client.
The following is the output:
How To Add In App Purchase Android
Source: https://betterprogramming.pub/how-to-implement-in-app-purchases-in-your-android-app-7cc1f80148a4
Posted by: reynoldspook1977.blogspot.com

0 Response to "How To Add In App Purchase Android"
Post a Comment