Step 1: First of all follow these two steps to add in app functionality :
1. Add the library using :
2. Add permission in manifest file.
Step 2: Initialise your billing processor:
and implement Billing Handler : BillingProcessor.IBillingHandler
which contains 4 methods :
b. onProductPurchased(String productId, TransactionDetails details) : This is where you need to handle actions to be performed after successful purchase
c. onBillingError(int errorCode, Throwable error) : Handle any error occurred during purchase process
d. onPurchaseHistoryRestored() : For restoring in app purchases
Step 3: How to purchase a product.
To purchase a managed product :
And to Purchase a subscription :
Step 4 : Consuming a product.
To consume a product simply call consumePurchase method.
bp.consumePurchase("YOUR PRODUCT ID FROM GOOGLE PLAY CONSOLE HERE");
Consumable Managed Products are products that can be bought multiple times such as in-game currency, game lives, power-ups, etc.
In this example, we are going to implement 4 different consumable managed products"item1", "item2", "item3", "item4".
Steps in summary:
Add the In-app Billing library to your project (AIDL File).
Add the required permission in AndroidManifest.xml file.
Deploy a signed apk to Google Developers Console.
Define your products.
Implement the code.
Test In-app Billing (optional).
First of all, we will need to add the AIDL file to your project as clearly explained in Google Documentation here.
IInAppBillingService.aidl is an Android Interface Definition Language (AIDL) file that defines the interface to the In-app Billing Version 3 service. You will use this interface to make billing requests by invoking IPC method calls.
After adding the AIDL file, add BILLING permission in AndroidManifest.xml:
Generate a signed apk, and upload it to Google Developers Console. This is required so that we can start defining our in-app products there.
Define all your products with different productID, and set a price to each one of them. There are 2 types of products (Managed Products and Subscriptions). As we already said, we are going to implement 4 different consumable managed products"item1", "item2", "item3", "item4".
After doing all the steps above, you are now ready to start implementing the code itself in your own activity.
After implementing the code, you can test it by deploying your apk to beta/alpha channel, and let other users test the code for you. However, real in-app purchases can't be made while in testing mode. You have to publish your app/game first to Play Store so that all the products are fully activated.
More info on testing In-app Billing can be found here.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0