Categories: Blog

Google in app purchase in android app

Before we start with app purchase, Let’s get a basic idea of what type of digital content we are selling to users.

Google’s Billing system gives us mainly two types of digital content to sell

  • Subscription
  • One Time product

One time product has two types of product

  • Consumable
  • Non Consumable

Consumable product is the one that is consumed in due time and you will purchase it again, Like a game currency in a gaming app, when the user consumes the product, your app dispenses associated content and the user can then purchase that item again.

Non Consumable Products is the one in which users can buy permanent benefits of purchase, without need of buying it again and again.

SubScription : it is a product that provides access on a recurring basis, subscription is not cancelled until the user does not cancel it, for example, music streaming services.

The google play console offers lot of flexibility when creating subscription products,

  • you can set billing period
  • offer free trial
  • offer introductory price
  • provides grace periods when payment fails
  • allow users to pause their subscription as an alternative to cancelling

Subscription has 6 states:

  • Active- When a new subscription is purchased.
  • Cancelled- Users can voluntarily cancel subscription, or subscription automatically cancelled if they don’t recover after being in account on hold.
  • In grace period- if grace period is enabled, then if there are payment issues at the end of billing cycle in subscription, then the subscription enters in grace period.
  • On hold- When a user’s payment fails and if the grace period is there, that also has ended without payment resolution then subscription enters into on hold period, and it lasts for 30 days.
  • Paused- All subscription paused by default, you have to change that setting from google play console, when pause feature is enable, user can pause their subscription for a period of time between one week to three months, depending on the recurring period of subscription, only annual subscription can not be paused.
  • Expired- once subscription enters into expiration state, users should lose access of their subscription.

Now steps for implementing in app purchase are

  • Set up a google merchant account.
  • Add billing library and billing permission to your in-app purchase demo app and upload that app to the internal publishing track.
  • Add your license key in your app in console and in your app and upload that version of app for publishing
  • Add products in that account
  • Handle in app purchase flow in app.

1. Adding a merchant account in Google play console.

Log in to your google developer account : with link https://play.google.com/apps/publish/

Click settings tab from the menu on left

Scroll down to the bottom of this page and you will see a link to activate your account, when your merchant account is activated, it will look like this

2. Now create simple app and add your billing library and billing permission to that app.

We are using this library from git repo :

Open android Manifest.xml and add billing permission in it

Upload this app in your developer account

3. Now add your billing License key in your uploaded app.

From Left menu in your developer account console, select all applications and select your app in which you are applied to in app purchase to.

From the menu on the left select services and APIs found under development tools

Copy the license key found under section titled “Your license key for this application”

Upload your license/Billing key to the commerce section of your app’s control panel

Now your merchant account is activated and your Licensing/Billing key is also uploaded, before adding products to your app, your app version must be uploaded that has a billing /license key included.

4. Add products in your app

Go to google developer console and select your app from all applications

if your app was already live on play store, you are now just including your in app purchases, in app products is found under “Store presence” tab in your app’s listing

Make sure subscription tab is selected for adding subscription, then click on “Create Subscription”

Add your product id : product id must be composed of lower letters(a-z), numbers(0-9), Underlies(_), or dots, without any spaces, and it should start with lower letters or numbers.

Input your title and description, then change status to active.

Set a price and billing period and save product

Now in your app’s control panel, step into the commerce section from the menu on the left. Copy and paste your product id into google play product id field,
And click add button to save it

5. Now handle in app purchase flow in your app.

As you already added billing library in gradle and billing permission in android manifest.xml,

Now create an instance of Billing Processor class and implement callback in your activity class, Constructor will take these three parameter
1, Context 2, Your licensing key from google developer console, 3, IBillingHandler interface implementation to handle purchase results and errors

Here billingProcessor. isIabServiceAvailable() is used to check if a google play store app is there in your device or not.

You will get four override methods for implemented interface

Override activity’s OnActivity method

Call purchase method for a billing processor instance to initiate purchase or subscribe method to initiate subscription

For subscribe or purchase method, you need your product id as a parameter needed to pass nad context

Don’t forget to release your billing processor instance in onDestroy method, once it’s work is done.

That’s it, if you have your purchase successful you will get your transaction details in onProductPurchased methods, from there you will get the State of your product also there, and depending on your product state you will update your app ui.

Let’s Nurture, a top mobile app development company in India has got the expertise in providing many solutions based applications with E-Commerce, Social Networking, Health sector, IoT & Machine learning. Our team at LetsNurture provides best solutions for business (saving time), shopping experience, teaching experience and many more. If you want to know more about this technology in your existing mobile app, get a free consultation from our experts at Let’s Nurture.

Lets Nurture

Share
Published by
Lets Nurture

Recent Posts

7 Powerful Psychological Triggers to Skyrocket Your Website Engagement

In the digital age, understanding the hidden forces driving user behavior is essential. By strategically…

7 months ago

What is haptics? How can we implement in in AR based mobile App? What are Haptics Use cases?

What is haptics?   Haptics refers to the use of touch feedback technology to simulate…

9 months ago

The Benefits of Using Virtual Reality in Business

In today's fast-paced and technologically driven world, businesses are constantly seeking innovative ways to stay…

1 year ago

A Closer Look at New Jersey’s Thriving Incubator Ecosystem

The Garden State, more popularly known as New Jersey, is not only known for its…

1 year ago

Why You Need a Mobile App for Your Business

In today's digital age, mobile apps have become an indispensable tool for businesses across all…

1 year ago

How to Optimize Your Website for Better User Experience

In today's digital era, a seamless and enjoyable user experience is crucial for the success…

1 year ago