[MUSIC PLAYING]
THOMAS EZAN: Hello.
I'm Tom Ezan, and I'm a Developer Relations Engineer
in the Android team at Google.
Today, we will talk about how to monetize your Android TV
app using Google Play billing.
TV has always been an excellent way to consume content.
And with the advent of smart TVs,
it has become a medium to find new content.
With recommendations sections and immersive trailers,
Android TV is also a great way to discover paid content.
In integrating Google Play billing,
you will create a seamless experience
by enabling purchases right in your Android TV application.
Besides, Android TV benefits from the Android ecosystem
by providing a familiar environment
for existing Android users.
A lot of your users will be already
familiar with the purchase flow, and some of them
will even already have their credit card associated
with their accounts.
And if there is no credit card tied to their account,
users can be redirected to a screen
to enter their information.
Let's get an overview of Google Play billing.
Google Play billing lets you sell two types of products,
one-time purchase, like a movie or a TV show episode,
and subscriptions, which can be a monthly subscription
to a premium channel, for instance.
But Google Play billing goes beyond processing payments.
It gives you access to a comprehensive commerce stack.
With global coverage, you can target more than 150 countries
and support more than 290 forms of payments, all of it
with a simplified tax management and price optimization
functionalities.
And Google Play billing manages your user subscription payment
cycle or your in-app purchase transaction
at scale on a highly reliable platform.
The payment screen flow provides a simple and secure user
experience for your users.
It also includes advanced fraud detection mechanism and refund
management features.
Let's look at the Google Play Console
and how to create in-app products.
If you already sell digital content on mobile, great news.
Selling on Android TV is the same as selling on mobile.
If you are not, let's review the steps to get you started.
First, start by setting up your payment profile.
Open the Payment Profile Page in the setup section
of the left navigation menu.
Keep in mind that only the account
owner of your Google Play account will have access to it.
Then, to create an in-app purchase,
select your application and open the in-app products
page on the Products section of the left navigation menu.
Click Create a Product and define the product's
description and price.
Similarly, to create a subscription offer,
select Subscriptions in the Products section
of the left navigation menu.
Click Create Subscription and define the subscription
details, the billing period from weekly to yearly, the default
price, and potentially an introductory price
or a free trial period.
You can also create both subscriptions and in-app
purchases via the Android publisher API.
Let's review the main components of your integration
with Google Play billing.
You can consider the four following components,
your Android application concerning your business logic,
Google Play billing, Android library embedded
in your application to communicate
between your app and Google Play,
your application backend, holding your content catalog
and keeping track of your users' entitlements,
and the Google Play backend, listing your items for sales
and managing the purchase.
After this high level review of Google Play billing,
let's talk about how to add it to your Android TV application.
Again, if you already have integrated
Google Play billing to your mobile app
before, well, great news again.
The process is identical for Android TV.
The only difference will be the full screen purchase
flow more adapted to a user experience on TV,
but it is fully handled by the Play Billing Library,
so you don't have to worry about it.
Let's jump in.
First, let's add the Play Billing Library
to your application module dependencies.
Add this snippet of code to your app's build.gradle file.
Make sure that you're using the latest version.
It is 4.0.0 at the time of the recording of this video.
After syncing your dependencies, you
will be able to import the billing client
class in your code.
Instantiate your billing client when you create your activity
using the builder pattern.
Set a purchase's updated listener and its callback
method, onPurchasesUpdated.
The callback method will be called once a user completes
a purchase.
Verify that this purchase was successful before handling it
in your app and give the entitlement to the user.
Now that we have instantiated the built in client and setup
the listener, let's connect it to Google Play.
Call the startConnection method on your billing client's
instance and set up the BillingClientStateListener
to be notified when the connection to state changes.
Wait for onBillingSetupFinished to be called, with the billing
response code OK, before interacting with the billing
client to receive pending purchases or query your SKUs.
And if the onBuildingServiceDisconnected
method is called instead, try to restore the connection.
When your billing client instance
is connected to Google Play, you can
retrieve your in-app products, one time in-app purchases,
or subscriptions, and make them available to your users.
To do so, create an instance of SKU details
params with the list of your SKU IDs and the type of products,
SKU type, in-app for in-app purchases,
and SKU type subs for subscriptions.
Pass the skuDetailsParams as a parameter
of the querySkuDetailsAsync to retrieve
the list of SKU details.
The SkuDetailsObject is a representation
of your application product, as you define it
in the Google Play Console.
It contains values such as the product title, description,
price, or the subscription period for subscription
products.
If your user decides to select one of the SKUs
that you have for sale, you can launch the Google Play purchase
flow.
The Play billing library will then
take over and display the screen flow handling the transaction.
You will be notified of the completion of this interaction
in the purchase updated listener that we saw earlier.
To launch the purchase flow, create a billing flow params
instance containing the SKU details of the product
your user is purchasing.
Then call launchBillingFlow, passing your billing flow
params as a parameter along with the activity context.
When you handle the purchase, in addition to giving access
to the newly purchased feature, make
sure to also acknowledge the purchase if the purchase isn't
acknowledged Google Play will assume that the user never
received the in-app products and will eventually
refund the user.
To acknowledge the purchase, create an acknowledged purchase
params with the purchase token and call
acknowledge purchase passing your acknowledged purchase
params as a parameter.
This code is all you need to start processing purchases
in your Android TV app.
But a subscription can go through state changes
during its lifecycle, and you might
want Google Play to notify you when
the state of a subscription changes
for a user that's where real time developer
notifications come in.
When the state of a subscription changes
for a user, for example, canceled, expired, or both,
Google Play will send a pop up message to your back end.
You will then be able to reflect it
in your Android TV application.
The message you will receive will
contain data encoded in Base64.
Once decoded, the data will give a developer
notification containing some metadata like a package name
or the date of the event and a subscription notification.
The subscription notification will contain the purchase
token, which is a unique identifier of the transaction
and the ID of the subscription event it is tied to.
You will also get the notification type.
Here, 2, for example, which indicates a user
renewed their subscription.
You will get notifications any time
a user purchases, renews, or cancels a subscription.
You will also be notified when a user recovers
their subscription after fixing an expired payment method,
for instance.
Generally speaking, you will receive the notification
for any lifecycle event indicating a state
change of a subscription here are some additional resources
to continue learning about Google Play building
the Android developer documentation is always
a great place to start, especially the Google Play
building section you can also take a look at Google reference
applications.
And if you want to learn more about Eden,
there is a section of the documentation dedicated to it.
Thank you for joining, and don't forget to and subscribe
to get more content from the Android developer YouTube
channel.