Skip to content

Most visited

Recently visited

navigation

BillingClient

public abstract class BillingClient
extends Object

java.lang.Object
   ↳ com.android.billingclient.api.BillingClient


Main interface for communication between the library and user application code.

It provides convenience methods for in-app billing. You 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.

After instantiating, you must perform setup in order to start using the object. To perform setup, call the startConnection(BillingClientStateListener) method and provide a listener; that listener will be notified when setup is complete, after which (and not before) you may start calling other methods. After setup is complete, you will typically want to request an inventory of owned items and subscriptions. See queryPurchases(String) and querySkuDetailsAsync(SkuDetailsParams, SkuDetailsResponseListener).

When you are done with this object, don't forget to call endConnection() to ensure proper cleanup. This object holds a binding to the in-app billing service and the manager to handle broadcast events, which will leak unless you dispose it correctly. If you created the object inside the onCreate(Bundle) method, then the recommended place to dispose is the the onDestroy() method.

To get library logs inside Android logcat, set corresponding logging level. E.g.: adb shell setprop log.tag.BillingClient VERBOSE

Summary

Nested classes

@interface BillingClient.BillingResponse

Possible response codes. 

class BillingClient.Builder

Builder to configure and create a BillingClient instance. 

@interface BillingClient.FeatureType

Features/capabilities supported by isFeatureSupported(String)

@interface BillingClient.SkuType

Supported SKU types. 

Public constructors

BillingClient()

Public methods

abstract void consumeAsync(String purchaseToken, ConsumeResponseListener listener)

Consumes a given in-app product.

abstract void endConnection()

Close the connection and release all held resources such as service connections.

abstract int isFeatureSupported(String feature)

Check if specified feature or capability is supported by the Play Store.

abstract boolean isReady()

Checks if the client is currently connected to the service, so that requests to other methods will succeed.

abstract int launchBillingFlow(Activity activity, BillingFlowParams params)

Initiate the billing flow for an in-app purchase or subscription.

static BillingClient.Builder newBuilder(Context context)

Constructs a new BillingClient.Builder instance.

abstract void queryPurchaseHistoryAsync(String skuType, PurchaseHistoryResponseListener listener)

Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed.

abstract Purchase.PurchasesResult queryPurchases(String skuType)

Get purchases details for all the items bought within your app.

abstract void querySkuDetailsAsync(SkuDetailsParams params, SkuDetailsResponseListener listener)

Perform a network query to get SKU details and return the result asynchronously.

abstract void startConnection(BillingClientStateListener listener)

Starts up BillingClient setup process asynchronously.

Inherited methods

From class java.lang.Object

Public constructors

BillingClient

BillingClient ()

Public methods

consumeAsync

void consumeAsync (String purchaseToken, 
                ConsumeResponseListener listener)

Consumes a given in-app product. Consuming can only be done on an item that's owned, and as a result of consumption, the user will no longer own it.

Consumption is done asynchronously and the listener receives the callback specified upon completion.

Parameters
purchaseToken String: The purchase token of the item to consume.

listener ConsumeResponseListener: Implement it to get the result of your consume operation returned asynchronously through the callback with token and BillingClient.BillingResponse parameters.

endConnection

void endConnection ()

Close the connection and release all held resources such as service connections.

Call this method once you are done with this BillingClient reference.

isFeatureSupported

int isFeatureSupported (String feature)

Check if specified feature or capability is supported by the Play Store.

Parameters
feature String: One of BillingClient.FeatureType constants.

Returns
int BILLING_RESULT_OK if feature is supported and corresponding error code otherwise.

isReady

boolean isReady ()

Checks if the client is currently connected to the service, so that requests to other methods will succeed.

Returns true if the client is currently connected to the service, false otherwise.

Note: It also means that INAPP items are supported for purchasing, queries and all other actions. If you need to check support for SUBSCRIPTIONS or something different, use isFeatureSupported(String) method.

Returns
boolean

launchBillingFlow

int launchBillingFlow (Activity activity, 
                BillingFlowParams params)

Initiate the billing flow for an in-app purchase or subscription.

It will show the Google Play purchase screen. The result will be delivered via the PurchasesUpdatedListener interface implementation reported to the ERROR(/BillingClientImpl) constructor.

Parameters
activity Activity: An activity reference from which the billing flow will be launched.

params BillingFlowParams: Params specific to the request BillingFlowParams).

Returns
int int The response code (BillingClient.BillingResponse) of launch flow operation.

newBuilder

BillingClient.Builder newBuilder (Context context)

Constructs a new BillingClient.Builder instance.

Parameters
context Context: It will be used to get an application context to bind to the in-app billing service.

Returns
BillingClient.Builder

queryPurchaseHistoryAsync

void queryPurchaseHistoryAsync (String skuType, 
                PurchaseHistoryResponseListener listener)

Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed.

Parameters
skuType String: The type of SKU, either "inapp" or "subs" as in BillingClient.SkuType.

listener PurchaseHistoryResponseListener: Implement it to get the result of your query returned asynchronously through the callback with a Purchase.PurchasesResult parameter.

queryPurchases

Purchase.PurchasesResult queryPurchases (String skuType)

Get purchases details for all the items bought within your app. This method uses a cache of Google Play Store app without initiating a network request.

Note: It's recommended for security purposes to go through purchases verification on your backend (if you have one) by calling the following API: https://developers.google.com/android-publisher/api-ref/purchases/products/get

Parameters
skuType String: The type of SKU, either "inapp" or "subs" as in BillingClient.SkuType.

Returns
Purchase.PurchasesResult PurchasesResult The Purchase.PurchasesResult containing the list of purchases and the response code (BillingClient.BillingResponse

querySkuDetailsAsync

void querySkuDetailsAsync (SkuDetailsParams params, 
                SkuDetailsResponseListener listener)

Perform a network query to get SKU details and return the result asynchronously.

Parameters
params SkuDetailsParams: Params specific to this query request SkuDetailsParams.

listener SkuDetailsResponseListener: Implement it to get the result of your query operation returned asynchronously through the callback with the BillingClient.BillingResponse and the list of SkuDetails.

startConnection

void startConnection (BillingClientStateListener listener)

Starts up BillingClient setup process asynchronously. You will be notified through the BillingClientStateListener listener when the setup process is complete.

Parameters
listener BillingClientStateListener: The listener to notify when the setup process is complete.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)