Inapp billing implementation in phonegap for android

Posted By : Avilash Choudhary | 21-Dec-2015

Implementing in app billing for android in phonegap, follow the below code to install the plugin

first clone plugin from https://github.com/poiuytrez/AndroidInAppBilling  and then install locally from below command
cordova plugin add ../AndroidInAppBilling-master --variable BILLING_KEY=“MIIBIjANBgkqhkiG9w0BA……………"

First you need to initialize your products then you will be able to purchase it or subscribe it. below is the code to initialise inapp billing products. This initialize method won't work if there is no account added in your device. For testing of sandbox you need to add that account in settings and also active that account in play store then it will work.

inappbilling.init(function(product) {
}, function(error) {
}, {
showLog : true
},[“product1”,”product2"]);

To fetch product details use the below method this will give the list of all products you have subscribed or purchased  with signature and purchase token also the productid and application package.

inappbilling.getPurchases(function(products) {
		// this will give the list of products with details.
	}, function(error) {
});

To  subscribing a product you need to use the below code   

inappbilling.subscribe(function(product) {
// This will give the receipt of product subscribed then you can verify that receipt on server side using client_auth 2.0 api’s. After verification you will get the expiry date of that subscription.
}, function(error){
},”productid”);

For purchasing a consumable product the mechanism is little different. First you need to consume that product if it is already purchased then you will be able to purchase again. without consuming a product google won’t allowed you to purchase that product again if it is already  purchased. So the best way is, fetch the product details and check if that product is available or not on that list. if available then first consume that product then purchase it again. if not then you can simply buy it.

For consuming a product use below code

inappbilling.consumePurchase(function(product) {	
	}, function(error) {		
},”productid");

after consuming you can buy a product again 

inappbilling.buy(function(product) {
	// this will give the receipt of purchased product.
}, function(error) {
},”productId");

 

THANKS

About Author

Author Image
Avilash Choudhary

Avilash has excellent experience in developing mobile and web applications using jQuery , Javascript and PhoneGap. His hobbies are watching and playing cricket.

Request for Proposal

Name is required

Comment is required

Sending message..