Google launched the Android In-app Billing API to make it easy for users to buy digital products and subscriptions. Since then Google have continuously improved the API by introducing subscription upgrades/downgrades facilities, promotion codes, and much more. Google Play Billing supports credit cards, gift cards, PayPal and carrier billing in over 130 countries around the world. It’s a great way for you to legitimize your app without having to worry about managing your users’ payment information and setting up relationships with payment providers.
The new Play Billing Library provides a simple API for integration with Google Play In-app Billing. The new Java library makes it easy to create and manage transactions without dealing with a complex service interface.
Play Billing Library Features
1 – Get details of your in-app items and subscriptions in your app
2 – Charge users around the globe for in-app items and subscriptions
3 – Manage your in-app items and subscriptions dynamically
4 – Get the list of non-consumed purchases as well as purchases history
Google Play In-app Billing
1 – In-app Billing is a Google Play service that lets you sell digital content from your applications
2 – You can use In-app Billing to sell products as:
3 – Standard in-app products (one-time billing),
4 – Subscriptions, (recurring, automated billing)
5 – Any application that you publish through Google Play can implement In-app Billing functionality.
Product Types
1 – Non-consumable Items – One Time purchased, these items will be permanently associated of the user’s Google Play account. An example of a non-consumable in- app product is a premium upgrade or a level pack.
2 – Consumable items – Implement consumption for items that can be made available for purchase multiple times. Typically, these items provide some temporary effects. For example, the user’s in-game character might gain life points or gain extra coins in their inventory.
3 – Subscriptions – A subscription is offered in In-app Billing that lets you sell your Application data, services, or features to users from your app with recurring monthly or annual subscription.
Create In-App Product
1 – Login with your Google Play Publisher Account
https://play.google.com/apps/publish/
2 – Create a new Application
Create In-App Product (Cont.)
1 – Click on Store Presence in left drawer
2 – Then click on in-app products
Create In-App Product (Cont.) –
In-app Product configuration option.
1 – Title
2 – Description
3 – Product ID
4 – Price
5 – Billing Period
6 – Free Trial Period
7 – Grace Period
Implementation of In-App Billing
1 – Add Billing library.
2 – Create a BillingClient
3 – Implement Purchase Update methond.
Get SKUs with details
Once onBillingSetupFinished is called with BillingResponse.OK, the BillingClient is ready to be used.
First, try to query for SKU(Products) details. For example we have two in-app products (“coach”, “student”) and two subscriptions SKUs (“coach_monthly”, “student_yearly”).
Let’s query to get their details.
querySkuDetailsAsync
Start a purchase flow
Display Google Play Billing dialog
Implement the code snippet below inside BillingManager.startPurchaseFlow(). The method startPurchaseFlow is execute when any of the buttons inside ProductFragment (i.e. SkusListAdapter) is clicked. It also knows the SKU of the product and billing type of the product that had the button triggered.
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder() .setType(billingType).setSku(skuId).build(); mBillingClient.launchBillingFlow(mActivity, billingFlowParams);
Real-time developer notifications
Google Play Billing provides server push notification allowing you to check state changes for play managed subscriptions. To enable this capability:
1 – Setup Clout Pub/Sub using your Google Cloud Platform project.
2 – Enable real-time developer notification for your Android application.
Create Topic for Pub/Sub
- Open you Google Cloud consol.
- Select your project and click Pub/Sub in left hand navigation.
- Click on Create Topic button for create your Topic
Enable real-time developer notifications for your app
- Open the Google Play console.
- Select your Android app.
- Navigate to Development tools > Services & APIs page.
- Scroll to the Real-time Developer Notification section at the bottom of the page.
- In the Topic name field, enter the full Cloud Pub/Sub topic name.
Implement Cloud Pub/Sub triggers
- Trigger a Pub/Sub function
exports.helloPubSub = functions.pubsub.topic(‘topic-name’).onPublish((message) => {
// …
});
- Access the Pub/Sub message payload
The payload for the Pub/Sub message in accessible from the Message object returned to your function. For messages with json in the Pub/Sub message body, the firebase SDK for Cloud Functions has a helper property to decode message. For example here is a message published with a simple JSON payload.
gcloud pubsub topics publish topic-name –message ‘{“subscription”:”cancel”}’
You can parse data from the JSON.
Sample Real-time developer notifications
This is the JSON user will get in notification
{
“version”:”1.0″,
“packageName”:”com.inwizards.inappbilling”,
“eventTimeMillis”:”1503349566168″,
“subscriptionNotification”:
{
“version”:”1.0″,
“notificationType”:4,
“purchaseToken”:”PURCHASE_TOKEN”,
“subscriptionId”:”my.sku”
}
}
Real-time developer notifications type
(1) SUBSCRIPTION_RECOVERED – A subscription was recovered from account hold.
(2) SUBSCRIPTION_RENEWED – An active subscription was renewed.
(3) SUBSCRIPTION_CANCELED – A subscription was either voluntarily or involuntarily cancelled. For voluntary cancellation, sent when the user cancels.
(4) SUBSCRIPTION_PURCHASED – A new subscription was purchased.
(5) SUBSCRIPTION_ON_HOLD – A subscription has entered account hold (if enabled).
(6) SUBSCRIPTION_IN_GRACE_PERIOD – A subscription has entered grace period (if enabled).
(7) SUBSCRIPTION_RESTARTED – User has reactivated their subscription from Play > Account > Subscriptions (requires opt-in for subscription restoration)
By – Deepak Tiwari
Reference – https://developer.android.com
Looking for Top software development Company in USA, Consult Inwizards software Technologies now!
Hi Deepak,
First of all thanks for sharing your knowledge, a very good start!!!
Your content and example is very descriptive and easy to understand, but it would be great if you can provide a Youtube video link of this entire subscription process and implementation setting of real time pub/sub notifications as this will be more helpful.
Waiting for your next post.
Thanks!