SPEAKER: Making money with an app
is a goal pursued by many developers.
To help Android developers, we launched the In-App Billing API
v3 in 2013, to make it easier for developers
to offer products and subscription within their apps.
And now, we are taking a new step
to simplify billing further with the new Play Billing Library.
This library includes several convenience classes, as well as
a wrapper for the billing service,
allowing you to focus on implementing the logic
specific to your app.
In-app billing relies on the Google Play Store
app, which handles the communication between your app
and Google's billing service.
To use in-app billing features, your app
must first request the com.android.vending billing
permission in your Android manifest XML file.
To use the library, add the following dependency
in your [INAUDIBLE] file.
But attention-- check on the Play Billing Library
page for the latest version number
to add the correct dependency in your app.
After you're set up, you are good to go and start
using the Play Billing Library.
To start sending billing requests,
you must establish a connection to the service
on Google Play using the following steps.
First, create a BillingClient instance
using the Builder class, passing the [INAUDIBLE] at Context,
and set a reference to an instance
of PurchasesUpdatedListener.
You can implement this interface in your activity.
When the user finishes a purchase process,
Google Play calls the onPurchasesUpdated method
on the listener that implements the PurchasesUpdatedListener
interface, where you should handle the transaction result.
Second, establish a connection to the in-app billing service
on Google Play.
The setup process is asynchronous,
and you must implement a BillingClientStateListener
to receive a callback once the setup is completed.
Overwrite the onBillingServices connected callback method
and implement your own retry policy
to handle lost connections with in-app billing service,
in the case the client loses connection.
For example, the Play Billing Library client
may lose connection if the Play Store Service
is updating in the background.
You can check the full sample code in our documentation.
With your app connected to Google Play,
you can state sending requests for in-app products.
Let's start by listing the products available
for purchase, executing the asynchronous method,
querySkuDetailsAsync.
To make a request using querySkuDetailsAsync,
you have to specify the purchase type--
in this case, SkuType.INAPP--
and a list of product ID strings,
where each record is a product ID for a purchasable item.
These product IDs were defined by you
when you created the product list on the Google Play
Console.
For more information about creating a product list,
see Administering In-App Billing.
You must also specify a listener,
which implements the SkuDetailsResponseListener
interface that overwrites the onSkuDetailsResponse method
to handle the method result. Call the ResponseCode
method on the SkuDetailsResult object
to check for the response code.
The response code should be BillingResponse.OK
if the request finishes successfully.
To view all the possible response
codes from Google Play, see Play Billing Library Reference.
To retrieve the list of SkuDetails objects,
call the getSkuDetailsList method
from the SkuDetailsResult. You can
call a variety of methods on SkuDetails objects
to view relevant information about each item,
such as its price or description.
Of course, being able to just list products
doesn't lead to profit.
To start a purchase request from your app,
call the launchBillingFlow method
on the Play Billing Library client from the UI thread.
Pass a reference to the billingFlowParams
object that contains the relevant data to complete
the purchase, such as the product
ID of an item and the purchase type.
In this case, SkuType.INAPP.
To get an instance of Billing Flow Params,
use the Billing Flow Params Builder class.
Calling the launchBillingFlow method
displays a Google Pay UI purchase screen.
Google Play provides an interface for users
to enter their payment method.
As mentioned earlier, when the user finishes a process,
Google Play calls the onPurchasesUpdated method
on the listener that implements the PurchasesUpdatedListener
interface.
In this method, each purchase will be an item on a list,
as you can see here.
When you receive the purchase response from Google Play,
perform a secure validation in your own account.
Don't trust the client, since an Android app could be decompiled
and your security checks replaced with stubs.
To retrieve the history of user purchase,
call the queryPurchases method on the Play Billing Library
client.
The Google Play service returns the purchase
made by the user account associated
with the app installed.
If the request is successful, the Play Billing Library
stores the query result in a list of purchasable objects.
To retrieve the list, call the getPurchasesList method
on the PurchaseResult object.
The Purchase object has methods to view relevant information
about the item, such as the purchase date or time.
Google Play prevents the user from owning more than one copy
of an item by product ID.
The item must be consumed before it can be purchased again.
Your app controls how items are consumed.
To consume items, you first implement an instance
of ConsumeResponseListener to handle the consumption result.
Then you call the consumeAsync method on the Play Billing
Library client, passing the identifying purchaseToken
string value.
If the response code in onConsumeFinished
from your ConsumeResponseListener
is billing OK, it's time to call a method that implements
the logic in your app to update the player's inventory
or release credits to be used.
Another great feature covered by Play Billing API
is subscriptions.
But before launching the billing flow for subscriptions,
call the isFeatureSupported method
on the Play Billing Library client
to check that the device supports subscriptions
with the type as featureType.SUBSCRIPTIONS.
Purchasing or renewing a subscription
is similar to launching the purchase flow for a product.
You just set the product type to SkuType.SUBS and the product
ID of the subscription before you call launchBillingFlow.
If the user is changing a subscription option,
you must also pass the product IDs
for the subscription items you are replacing
in the BillingFlowParams object using the addOldSku method.
You can create an instance of BillingFlowParams
using the BillingFlowParams Builder class.
Just as in purchase, Google Play returns
the result of the operation using
the onPurchasesUpdated callback.
This library is designed and maintained by our engineers,
and we make plans to keep it up to date with the latest In-App
Billing API and Google Play app.
If you want to see details of how to use the Play Billing
Library, including support for Android Wear and Android TV,
check the refreshed version of the Trivial Drive sample.
We look forward to hearing your feedback
about this new library.
Thanks for watching.
Bye.
[MUSIC PLAYING]