BillingClient
public abstract class 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 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 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 and queryProductDetailsAsync.
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 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
Public constructors |
|---|
Public methods |
|
|---|---|
abstract void |
acknowledgePurchase(Acknowledges in-app purchases. |
abstract void |
consumeAsync(Consumes a given in-app product. |
abstract void |
createAlternativeBillingOnlyReportingDetailsAsync(Creates alternative billing only purchase details that can be used to report a transaction made via alternative billing without user choice to use Google Play billing. |
abstract void |
createBillingProgramReportingDetailsAsync(Creates details required to report transactions made through billing programs outside of Google Play Billing using the External Transactions API. |
abstract void |
This method is deprecated. Use |
abstract void |
Closes the connection and releases all held resources such as service connections. |
abstract void |
getBillingChoiceInfoAsync(Returns information about billing choice. |
abstract void |
getBillingConfigAsync(Gets the billing config, which stores configuration used to perform billing operations. |
abstract int |
Get the current billing client connection state. |
abstract void |
Checks the availability of offering alternative billing without user choice to use Google Play billing. |
abstract void |
isBillingProgramAvailableAsync(Checks the availability of a billing program. |
abstract void |
This method is deprecated. Use |
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. |
abstract void |
launchExternalLink(Initiates linking out of the app for an external offer or app install. |
static BillingClient.Builder |
newBuilder(Context context)Constructs a new |
abstract void |
queryProductDetailsAsync(Performs a network query the details of products available for sale in your app. |
abstract void |
queryPurchasesAsync(Returns purchases details for currently owned items bought within your app. |
abstract BillingResult |
showAlternativeBillingOnlyInformationDialog(Shows the alternative billing only information dialog on top of the calling app. |
abstract void |
showBillingProgramInformationDialog(Shows the billing program information dialog on top of the calling app. |
abstract BillingResult |
This method is deprecated. Use |
abstract BillingResult |
showInAppMessages(Overlays billing related messages on top of the calling app. |
abstract void |
startConnection(BillingClientStateListener listener)Starts up BillingClient setup process asynchronously. |
Public constructors
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 which acts as an implicit acknowledgement or you can explicitly acknowledge the purchase via this method. For subscriptions use acknowledgePurchase. Please refer to the integration guide for more details.
| Parameters | |
|---|---|
AcknowledgePurchaseParams params |
Params specific to this acknowledge purchase request. |
AcknowledgePurchaseResponseListener listener |
The listener for the result of the acknowledge operation returned asynchronously through the callback with the |
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. For subscriptions use acknowledgePurchase. Please refer to https://developer.android.com/google/play/billing/billing_library_overview#acknowledge for more details.
| Parameters | |
|---|---|
ConsumeParams consumeParams |
Params specific to consuming a purchase. |
ConsumeResponseListener listener |
The listener for the result of the consume operation returned asynchronously through the callback with the purchase token and |
createAlternativeBillingOnlyReportingDetailsAsync
public abstract void createAlternativeBillingOnlyReportingDetailsAsync(
AlternativeBillingOnlyReportingDetailsListener listener
)
Creates alternative billing only purchase details that can be used to report a transaction made via alternative billing without user choice to use Google Play billing.
| Parameters | |
|---|---|
AlternativeBillingOnlyReportingDetailsListener listener |
The listener for the response of the create alternative billing only reporting details flow. |
createBillingProgramReportingDetailsAsync
public abstract void createBillingProgramReportingDetailsAsync(
BillingProgramReportingDetailsParams params,
BillingProgramReportingDetailsListener listener
)
Creates details required to report transactions made through billing programs outside of Google Play Billing using the External Transactions API.
| Parameters | |
|---|---|
BillingProgramReportingDetailsParams params |
Params necessary to create the reporting details for transaction reporting. |
BillingProgramReportingDetailsListener listener |
The listener for the response of the create billing program reporting details flow. |
createExternalOfferReportingDetailsAsync
public abstract voidcreateExternalOfferReportingDetailsAsync(
ExternalOfferReportingDetailsListener listener
)
Creates purchase details that can be used to report a transaction made via external offer.
| Parameters | |
|---|---|
ExternalOfferReportingDetailsListener listener |
The listener for the response of the create external offer reporting details flow. |
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.
getBillingChoiceInfoAsync
public abstract void getBillingChoiceInfoAsync(
GetBillingChoiceInfoParams params,
BillingChoiceInfoResponseListener listener
)
Returns information about billing choice.
| Parameters | |
|---|---|
GetBillingChoiceInfoParams params |
Params necessary to get the billing choice info. |
BillingChoiceInfoResponseListener listener |
The listener for the response containing the billing choice info for the user locale specified in |
getBillingConfigAsync
public abstract void getBillingConfigAsync(
GetBillingConfigParams params,
BillingConfigResponseListener listener
)
Gets the billing config, which stores configuration used to perform billing operations.
Note: This data can change and should not be cached.
| Parameters | |
|---|---|
GetBillingConfigParams params |
Params specific to this get billing config request. |
BillingConfigResponseListener listener |
The listener for the response of the get billing config flow. |
getConnectionState
public abstract int getConnectionState()
Get the current billing client connection state.
| Returns | |
|---|---|
int |
The |
isAlternativeBillingOnlyAvailableAsync
public abstract void isAlternativeBillingOnlyAvailableAsync(
AlternativeBillingOnlyAvailabilityListener listener
)
Checks the availability of offering alternative billing without user choice to use Google Play billing.
Note: This data can change and should not be cached.
| Parameters | |
|---|---|
AlternativeBillingOnlyAvailabilityListener listener |
The listener for the response of the alternative billing only availability. |
isBillingProgramAvailableAsync
public abstract void isBillingProgramAvailableAsync(
int billingProgram,
BillingProgramAvailabilityListener listener
)
Checks the availability of a billing program.
Note: This data can change and should not be cached.
| Parameters | |
|---|---|
int billingProgram |
The billing program to check the availability for. |
BillingProgramAvailabilityListener listener |
The listener for the response of the billing program availability. |
isExternalOfferAvailableAsync
public abstract voidisExternalOfferAvailableAsync(
ExternalOfferAvailabilityListener listener
)
Checks the availability of providing external offer.
Note: This data can change and should not be cached.
| Parameters | |
|---|---|
ExternalOfferAvailabilityListener listener |
The listener for the response of the external offer availability. |
isFeatureSupported
public abstract BillingResult isFeatureSupported(String feature)
Checks if the specified feature or capability is supported by the Play Store.
| Parameters | |
|---|---|
String feature |
One of the |
| Returns | |
|---|---|
BillingResult |
|
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.
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 Builder.setListener.
| Parameters | |
|---|---|
Activity activity |
An activity reference from which the billing flow will be launched. |
BillingFlowParams params |
Params specific to the launch billing flow request. |
| Returns | |
|---|---|
BillingResult |
The result of the launch billing flow operation. |
launchExternalLink
public abstract void launchExternalLink(
Activity activity,
LaunchExternalLinkParams params,
LaunchExternalLinkResponseListener listener
)
Initiates linking out of the app for an external offer or app install.
| Parameters | |
|---|---|
Activity activity |
An activity reference from which the external link will be launched. |
LaunchExternalLinkParams params |
Params specific to the external link request. |
LaunchExternalLinkResponseListener listener |
The listener for the response of the external link flow. |
newBuilder
public static BillingClient.Builder newBuilder(Context context)
Constructs a new Builder instance.
| Parameters | |
|---|---|
Context context |
It will be used to get an application context to bind to the in-app billing service. |
queryProductDetailsAsync
public abstract void queryProductDetailsAsync(
QueryProductDetailsParams params,
ProductDetailsResponseListener listener
)
Performs a network query the details of products available for sale in your app.
| Parameters | |
|---|---|
QueryProductDetailsParams params |
Params containing list of Product where each product contains product id and |
ProductDetailsResponseListener listener |
The listener for the result of the query operation returned asynchronously through the callback with the |
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.
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. |
PurchasesResponseListener listener |
The listener for the result of the query returned asynchronously through the callback with the |
showAlternativeBillingOnlyInformationDialog
public abstract BillingResult showAlternativeBillingOnlyInformationDialog(
Activity activity,
AlternativeBillingOnlyInformationDialogListener listener
)
Shows the alternative billing only information dialog on top of the calling app.
If this dialog has previously been shown and acknowledged by the user on the app and device, it won't be shown again.
The listener will be invoked on the UI thread on completion.
| Parameters | |
|---|---|
Activity activity |
An activity reference to host the rendering of the alternative billing only dialog. |
AlternativeBillingOnlyInformationDialogListener listener |
The listener for the response of the alternative billing only dialog. |
| Returns | |
|---|---|
BillingResult |
|
showBillingProgramInformationDialog
public abstract void showBillingProgramInformationDialog(
Activity activity,
BillingProgramInformationDialogParams params,
BillingProgramInformationDialogListener listener
)
Shows the billing program information dialog on top of the calling app.
If this dialog has previously been shown and acknowledged by the user on the app and device, it won't be shown again.
The listener will be invoked on the UI thread on completion.
| Parameters | |
|---|---|
Activity activity |
An activity reference to host the rendering of the information dialog. |
BillingProgramInformationDialogParams params |
Params specific to the billing program information dialog request. |
BillingProgramInformationDialogListener listener |
The listener for the response of the information dialog. |
showExternalOfferInformationDialog
public abstract BillingResultshowExternalOfferInformationDialog(
Activity activity,
ExternalOfferInformationDialogListener listener
)
Shows the external offer information dialog on top of the calling app.
The listener will be invoked on the UI thread on completion.
| Parameters | |
|---|---|
Activity activity |
An activity reference to host the rendering of the external offer information dialog. |
ExternalOfferInformationDialogListener listener |
The listener for the response of the external offer information dialog. |
| Returns | |
|---|---|
BillingResult |
|
showInAppMessages
public abstract BillingResult showInAppMessages(
Activity activity,
InAppMessageParams params,
InAppMessageResponseListener listener
)
Overlays billing related messages on top of the calling app.
For example, show a message to inform users that their subscription payment has been declined or that there is an outstanding opt-in price increase requiring user confirmation. This provides options to take them to fix their payment method or confirm the price increase.
Callback will be returned on UI thread.
| Parameters | |
|---|---|
Activity activity |
An activity reference to host the rendering of in-app messages. |
InAppMessageParams params |
Params specific to this show in-app messages request. |
InAppMessageResponseListener listener |
The listener for the result response of the in-app messaging flow. |
| Returns | |
|---|---|
BillingResult |
|
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 | |
|---|---|
BillingClientStateListener listener |
The listener to notify when the setup process is complete. |