Within the Billing API, you can integrate in-app billing
into your Android applications.
Hi.
I'm Neto Marin.
In a previous episode, we looked at what
you can offer to users through in-app billing.
In this episode, let's implement it.
To make it generally more comfortable,
we created a [INAUDIBLE] implementation
for in-app billing version 3 API called Trivial Drive.
This app includes convenience class
to quick setup the in-app billing service.
You can import this sample from Android Studio
or use a GUID to clone repository.
But before we actually start coding,
I will help you to set up your app in the Google Play
Developer Console and prepare development environment
with Android Studio.
To sell products, you need to add them to the Google Play
Developer Console.
To add these products, you must have an app.
But don't necessarily publish it.
If you need help about how to create and set up
a developer account on Google Play, check our documentation.
To create your app, click the Add New Application button.
Enter a name for your new application
and click on Prepare Store Listing.
And now you have an Android app as a draft.
And you imagine, now it's time to add your products, right?
Almost.
Before you can start adding new products,
you need to upload an APK, the Android application package,
with the billing permission in your app.
Now, let's move to Android Studio and check
what you need to do to prepare the development environment,
generate and upload your APK with in-app billing enabled.
First, you have to install the in-app billing library
using the Android SDK Manager.
In Android Studio, we can access it through the icon,
or using the menu, Tools, Android, Android SDK Manager.
Then, in the SDK Manager, go to the SDK Tools tab.
In the list of tools, check the Google Play Billing library.
Click OK to complete the installation.
Now you can create your project to integrate in-app billing
or open existing project for which you
want to use in-app billing and start preparing it.
The communication between Google Play and your app
will be done using a service.
And to define this service, you need
to create an Android Interface Definition Language
file, or AIDL.
In your project, this file should
be placed inside a specific directory in a package.
First, create the director AIDL under the path Search [? Me. ?]
Then create a new package called com.android.vending.BILLING.
A simple AIDL file, inappbillingservice.aidl,
is provided by in-app billing library,
and you can find it in your SDK installation in the folder
extras/google/play_billing.
You can check during the SDK installation
inside Android Studio.
Using the File Explorer and Operation assistant,
copy this file into your project.
The next step is to update your Android manifest,
adding the permissions to com.android.vending.BILLING
under the Application tag.
Now your approach is ready and with the right permissions.
So you can generate APK and upload into the Google Play
Developer Console, and use the web interface
to add the products and subscriptions you
want to offer.
To add your products, go to Google Play Developer Console,
and then to the In-app Products tab.
For example, here, we have a product
called Gas with the ID Gas and the price of $1.
And it's configured as a managed product.
These IDs, also known as SKUs, are
important because they will be used to start the purchase flow
and make other requests.
Now that you created a product, configured the development
environment, let's see how to send requests and purchase
products.
Before I can start sending requests from Google Play,
you need to establish the connection between your app
and the in-app billing service.
The convenience class IAB Helper provided in this sample
under the package [INAUDIBLE] handles
the binding to the in-app billing service.
For example, to bind your app to the Google Play Service,
first create an IAB Helper instance in your activities
on create method, passing your public license key along
to the construct.
And important point here is about your license key.
This license key can be found in Google Play Developer Console
under the section Services and API.
But instead of hard coding your key,
construct it at runtime from substrings or retrieve it from
encrypted resource before using with the constructor.
Next, perform the service binding by calling the start
setup method on the IAB Helper instance you created.
To check if the binding's finished,
implement the listener on IAB setup finished listener.
And, if setup completed successfully,
you can now use the service [INAUDIBLE]
to communicate with the Google Play.
Remember to unbind from in-app billing service
when you are done with your activity
because if you don't unbind, this open service connection
could cause your device performance to degrade.
With your connection to Google Play working,
you can start listing your products
and present some details.
To get the item details from Google Play,
you can call getSkuDetails, passing
a bundle that contains a string [INAUDIBLE] list of product
IDs with key item ID list, where each string is a product
ID for an item.
GetSkuDetails returns a bundle that
contains the response code with the key response code
and a string [INAUDIBLE] list with the key details list.
Each string in the [INAUDIBLE] list
is a JSON containing the product information.
And when the user chooses a product in your list, like Gas
in your sample, and hits the Buy button,
it's time to start the purchase flow.
Since all transactions are done by Google Play,
you must create an intent with purchase information.
This is done by calling the method getBuyIntent,
passing the in-app building API version, the app's package
name, the product ID to be purchased, the purchase type,
and a developer payload string.
The developer payload string is used
to specify any additional argument that you
want Google Play sent back along with the purchase information.
This method will return a bundle,
and you can get the pendingIntent
with the key BuyIntent.
To complete the purchase transaction,
call the startIntentSenderForResult
method.
Then use the pendingIntent that you created.
In this example, we are using an arbitrary value of 1001
for the request code.
Once the user finishes the transaction,
you will receive the response from Google Play
in your pendingIntent through the method onActiveToResult.
The purchase data for the order is a string in the JSON format
that it's mapped to the in-app purchase data
key and the response intent, like this one.
Anything you need you can hit a list of purchases
that were made by your user, calling the method getPurchase.
And Google Play manages the local cache
for purchased items, so you don't
need to keep your own database or history.
Some products can be purchased several times, like Gas
on our Trivial Drive sample.
But the in-app billing API offers managed products,
which means that Google Play manages and tracks
the product's ownership for you.
And before a user can purchase the same product again,
you must send the consumption request to Google Play.
To record a purchase consumption,
send the consumePurchase method to the in-app billing service
and pass in the purchase token string
the value that identifies a purchase to be consumed.
The purchase token is part of the data returned
in the in-app purchase data string
after a successful purchase request.
Other options you can offer in your app
is a subscription service.
Let's suppose I want my car to go on forever without having
to worry about fuel.
Great, huh?
For a small fee, Trivial Drive lets
me purchase a recurring subscription
so that my tank is always full.
The purchase flow for subscriptions
is similar to the purchase flow for a product,
with the exception that the product type
must be set to Subs.
Check out this video for more details about subscriptions.
And now you know how to prepare your project
to use in-app billing, add the products to Google Play
Developer Console, and also how to use
the API to list products, start a purchase, and list purchase.
For handling funds and manging your products programmatically,
check out docs about the Google Play Developer API.
If you have any questions, check out
documentation for in-app billing on developer.android.com.
And don't forget the download this sample application,
Trivial Drive, where you can see all the in-app billing
implementation working.
Looking forward to see you making money
with in-app billing.
Thanks for watching.
Bye bye.
[MUSIC PLAYING]