In-app purchase is the easiest way to make a purchase of some products or make a subscriptions
Here’s how you can do it.
Google Play :
In order to test or implement InApp Purchase / Subscription, you need a developer account and your app need to be published either in alpha/beta/production level
After creating application go to Development Tools > Services & APIs
Inside Services & APIs, you will find Licensing & in-app billing Section where you find a Base64 encoded Licence Key
Upload your signed APK under release Management > App releases > Manage Alpha > Upload your first APK
Google play will take some time to approve your APK ( 2–3 hours )
Create Subscription / Products
Once the APK is published you can now able to create Subscription / Product
In Developer console go to Store Presence > In-app products
Now create a product or a subscription
fill the details and click on save.
now your in app product is ready to purchase
If In-app products shows an error message like “enable google merchant account” refer here
Now Lets get out hands dirty
Open your android app code in android studio
include google play billing library in your build.gradle file
Initialise billing client
Once the Billing client is initialised you can get the list of products or can start purchasing product / subscription
In below code product_id, subscription_id the id of the product, subscription you mentioned while creating the product in play console
List all the Items available for purchase
In below code product_1 and product_2 are the product ids which we mentioned during the creation of In-app products
If you want to get the available subscriptions inthe place of product_id’s add subscription_id and change BillingClient.SkuType.INAPP to BillingClient.SkuType.SUBS
Purchased Items List
Inorder to get the user purchased items list use the below code
Consuming a purchase
For consuming a purchase
That is Now you can perform In-app purchase very easily.
Verify your purchase
Now we verify the purchase we got in the above process.
Create a file named
Security.java and copy paste the below code
Now to implement the verification process we do the process in
onPurchaseUpdated method which is triggered when a new purchase is made
Hurry!!! Now are able to validate your purchase.
Thanks for your time.
Need any help or have a freelance job contact me at email@example.com
If you find it useful Please like and share, Don’t forget to clap