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.

It's strongly recommended that you instantiate only one BillingClient instance at one time to avoid multiple PurchasesUpdatedListener.onPurchasesUpdated(BillingResult, List) callbacks for a single event.

All methods annotated with AnyThread can be called from any thread and all the asynchronous callbacks will be returned on the same thread. Methods annotated with UiThread should 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 queryPurchasesAsync(QueryPurchasesParams, PurchasesResponseListener) and queryProductDetailsAsync(QueryProductDetailsParams, ProductDetailsResponseListener).

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 Activity.onCreate(Bundle) method, then the recommended place to dispose is the Activity.onDestroy() method. After cleanup, it cannot be reused again for connection.

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.BillingResponseCode

Possible response codes. 

class BillingClient.Builder

Builder to configure and create a BillingClient instance. 

@interface BillingClient.ConnectionState

Connection state of billing client. 

@interface BillingClient.FeatureType

Features/capabilities supported by BillingClient.isFeatureSupported(String)

@interface BillingClient.ProductType

Supported Product types. 

@interface BillingClient.SkuType

This @interface is deprecated. Use BillingClient.ProductType instead.  

Public constructors

BillingClient()

Public methods

abstract void acknowledgePurchase(AcknowledgePurchaseParams params, AcknowledgePurchaseResponseListener listener)

Acknowledges in-app purchases.

abstract void consumeAsync(ConsumeParams consumeParams, ConsumeResponseListener listener)

Consumes a given in-app product.

abstract void endConnection()

Closes the connection and releases all held resources such as service connections.

abstract int getConnectionState()

Get the current billing client connection state.

abstract BillingResult isFeatureSupported(String feature)

Checks if the 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 BillingResult launchBillingFlow(Activity activity, BillingFlowParams params)

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

static BillingClient.Builder newBuilder(Context context)

Constructs a new BillingClient.Builder instance.

abstract void queryProductDetailsAsync(QueryProductDetailsParams params, ProductDetailsResponseListener listener)

Performs a network query the details of products available for sale in your app.

abstract void queryPurchaseHistoryAsync(QueryPurchaseHistoryParams queryPurchaseHistoryParams, PurchaseHistoryResponseListener listener)

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

abstract void queryPurchaseHistoryAsync(String skuType, PurchaseHistoryResponseListener listener)

This method is deprecated. Use queryPurchaseHistoryAsync(QueryPurchaseHistoryParams, PurchaseHistoryResponseListener) instead;

abstract void queryPurchasesAsync(String skuType, PurchasesResponseListener listener)

This method is deprecated. Use queryPurchasesAsync(QueryPurchasesParams, PurchasesResponseListener) instead.

abstract void queryPurchasesAsync(QueryPurchasesParams queryPurchasesParams, PurchasesResponseListener listener)

Returns purchases details for currently owned items bought within your app.

abstract void querySkuDetailsAsync(SkuDetailsParams params, SkuDetailsResponseListener listener)

This method is deprecated. Use queryProductDetailsAsync instead.

abstract BillingResult showInAppMessages(Activity activity, InAppMessageParams params, InAppMessageResponseListener listener)

Overlay billing related messages on top of the calling app.

abstract void startConnection(BillingClientStateListener listener)

Starts up BillingClient setup process asynchronously.

Inherited methods

Public constructors

BillingClient

public BillingClient ()

Public methods

acknowledgePurchase

public abstract void acknowledgePurchase (AcknowledgePurchaseParams params, 
                AcknowledgePurchaseResponseListener listener)

Acknowledges in-app purchases.

Developers are required to acknowledge that they have granted entitlement for all in-app purchases for their application.

Warning! All purchases require acknowledgement. Failure to acknowledge a purchase will result in that purchase being refunded. For one-time products ensure you are using consumeAsync(ConsumeParams, ConsumeResponseListener) which acts as an implicit acknowledgement or you can explicitly acknowledge the purchase via this method. For subscriptions use acknowledgePurchase(AcknowledgePurchaseParams, AcknowledgePurchaseResponseListener). Please refer to the integration guide for more details.

Parameters
params AcknowledgePurchaseParams: Params specific to this acknowledge purchase request.

listener AcknowledgePurchaseResponseListener: The listener for the result of the acknowledge operation returned asynchronously through the callback with the BillingClient.BillingResponseCode.

consumeAsync

public abstract void consumeAsync (ConsumeParams consumeParams, 
                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.

Warning! All purchases require acknowledgement. Failure to acknowledge a purchase will result in that purchase being refunded. For one-time products ensure you are using this method which acts as an implicit acknowledgement or you can explicitly acknowledge the purchase via acknowledgePurchase(AcknowledgePurchaseParams, AcknowledgePurchaseResponseListener). For subscriptions use acknowledgePurchase(AcknowledgePurchaseParams, AcknowledgePurchaseResponseListener). Please refer to https://developer.android.com/google/play/billing/billing_library_overview#acknowledge for more details.

Parameters
consumeParams ConsumeParams: Params specific to consumimg a purchase.

listener ConsumeResponseListener: The listener for the result of the consume operation returned asynchronously through the callback with the purchase token and BillingClient.BillingResponseCode.

endConnection

public abstract void endConnection ()

Closes the connection and releases all held resources such as service connections.

Call this method once you are done with this BillingClient reference, and when Activity or Fragment is destroyed to avoid memory leak.

getConnectionState

public abstract int getConnectionState ()

Get the current billing client connection state.

Returns
int The BillingClient.ConnectionState corresponding to the current client connection state.

isFeatureSupported

public abstract BillingResult isFeatureSupported (String feature)

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

Parameters
feature String: One of the BillingClient.FeatureType constants.

Returns
BillingResult BillingClient.BillingResponseCode.OK if the feature is supported or BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED otherwise.

isReady

public abstract 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

public abstract BillingResult launchBillingFlow (Activity activity, 
                BillingFlowParams params)

Initiates 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 set by BillingClient.Builder.setListener(PurchasesUpdatedListener).

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

params BillingFlowParams: Params specific to the launch billing flow request.

Returns
BillingResult The result of the launch billing flow operation. BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED if the user already owns the item being purchased, BillingClient.BillingResponseCode.ITEM_UNAVAILABLE if the item is not available to be purchased, and BillingClient.BillingResponseCode.USER_CANCELED if the user dismissed the purchase flow.

newBuilder

public static 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

queryProductDetailsAsync

public abstract void queryProductDetailsAsync (QueryProductDetailsParams params, 
                ProductDetailsResponseListener listener)

Performs a network query the details of products available for sale in your app.

Parameters
params QueryProductDetailsParams: Params containing list of QueryProductDetailsParams.Product where each product contains product id and BillingClient.ProductType.

listener ProductDetailsResponseListener: The listener for the result of the query operation returned asynchronously through the callback with the BillingClient.BillingResponseCode.

queryPurchaseHistoryAsync

public abstract void queryPurchaseHistoryAsync (QueryPurchaseHistoryParams queryPurchaseHistoryParams, 
                PurchaseHistoryResponseListener listener)

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

Parameters
queryPurchaseHistoryParams QueryPurchaseHistoryParams: Params specific to this query request.

listener PurchaseHistoryResponseListener: The listener for the result of the query returned asynchronously through the callback with the BillingResult and the list of PurchaseHistoryRecord.

queryPurchaseHistoryAsync

public abstract void queryPurchaseHistoryAsync (String skuType, 
                PurchaseHistoryResponseListener listener)

This method is deprecated.
Use queryPurchaseHistoryAsync(QueryPurchaseHistoryParams, PurchaseHistoryResponseListener) instead;

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: The listener for the result of the query returned asynchronously through the callback with the BillingResult and the list of PurchaseHistoryRecord.

queryPurchasesAsync

public abstract void queryPurchasesAsync (String skuType, 
                PurchasesResponseListener listener)

This method is deprecated.
Use queryPurchasesAsync(QueryPurchasesParams, PurchasesResponseListener) instead.

Returns purchases details for currently owned items bought within your app.

Only active subscriptions and non-consumed one-time purchases are returned. 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 one of the following APIs: https://developers.google.com/android-publisher/api-ref/purchases/products/get https://developers.google.com/android-publisher/api-ref/purchases/subscriptions/get

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

listener PurchasesResponseListener: The listener for the result of the query returned asynchronously through the callback with the BillingResult and the list of Purchase.

queryPurchasesAsync

public abstract void queryPurchasesAsync (QueryPurchasesParams queryPurchasesParams, 
                PurchasesResponseListener listener)

Returns purchases details for currently owned items bought within your app.

Only active subscriptions and non-consumed one-time purchases are returned. 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 one of the following APIs: https://developers.google.com/android-publisher/api-ref/purchases/products/get https://developers.google.com/android-publisher/api-ref/purchases/subscriptions/get

Parameters
queryPurchasesParams QueryPurchasesParams: Params specific to this query request.

listener PurchasesResponseListener: The listener for the result of the query returned asynchronously through the callback with the BillingResult and the list of Purchase.

querySkuDetailsAsync

public abstract void querySkuDetailsAsync (SkuDetailsParams params, 
                SkuDetailsResponseListener listener)

This method is deprecated.
Use queryProductDetailsAsync instead.

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

Parameters
params SkuDetailsParams: Params specific to this query request.

listener SkuDetailsResponseListener: The listener for the result of the query operation returned asynchronously through the callback with the BillingClient.BillingResponseCode and the list of SkuDetails.

showInAppMessages

public abstract BillingResult showInAppMessages (Activity activity, 
                InAppMessageParams params, 
                InAppMessageResponseListener listener)

Overlay billing related messages on top of the calling app.

For example, show a message to inform users that their subscription payment has been declined and provide options to take them to fix their payment method.

Callback will be returned on UI thread.

Parameters
activity Activity: An activity reference to host the rendering of in-app messages.

params InAppMessageParams: Params specific to this show in-app messages request.

listener InAppMessageResponseListener: The listener for the result response of the in-app messaging flow.

Returns
BillingResult BillingClient.BillingResponseCode.OK if the request is successfully submitted to Play Store, or an error code otherwise.

startConnection

public abstract 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.