InApp purchase in phoneGap for iOS

Posted By : Avilash Choudhary | 26-Jun-2015

If you are building a mobile application for iOS platform through phonegap framework and you want to implement InApp purchase functionality then this blog will tell you all the things you need to do to successfully implementing it.

you need to install this plugin: cordova plugin add https://github.com/j3k0/PhoneGap-InAppPurchase-iOS.git

First you need to register your product on itunes connect. click here for more information.

Before purchasing the product you need to initialize your products below code is to initialize your products

window.storekit.init({
debug : true, /* To see logs on console */
noAutoFinish : false,
purchase : function(transactionId, productId) {
console.log(‘purchased : ' + productId);
},
restore : function(transactionId, productId) {
console.log('restored: ' + productId);
},
restoreCompleted : function() {
console.log('all restore complete');
payment.checkForExpireSubscription();
},
restoreFailed : function(errCode) {
console.log('restore failed: ' + errCode);
},
receiptsRefreshed : function(appReceiptBase64) {
console.log('new app receipt: ' + appReceiptBase64);
},
finish : function(transactionId, productId) {
console.log("finished transaction......");
// Called when a transaction has been finished.
},
error : function(errno, errtext) {

console.log('Failed: ' + errtext);
},
ready : function() {

window.storekit.load(productIds, function(validProducts, invalidProductIds) {
		
		$.each(validProducts, function(i, val) {
			//  console.log("id: " + val.id + " title: " + val.title + " val: " + val.description + " price: " + val.price);
		});
		if (invalidProductIds.length) {
			//  console.log("Invalid Product IDs: " + JSON.stringify(invalidProductIds));
		}

});
}
});

you can call this function on your app.js after device ready event or before purchasing any product.

 window.storekit.purchase(productId, 1);
 

first parameter is product id and second is quantity of that product.

When product is purchased the purchase function callback is fired in window.storekit.init function and then finish function call back fired.

if you set noAutoFinish to false then transaction is automatically finised otherwise you need to manually finish it. By calling window.storekit.finish() method.

To get the receipt you can call

 window.storekit.loadReceipts(function(receipts) {
//receipts.appStoreReceipt;  this will give you the receipt in base64 then you need to validate this base64
});

For testing the purchase functionality you need to create the sandbox tester account on iTunes connect.

To validate the receipt:

we have two Urls

For Sandbox: https://sandbox.itunes.apple.com/verifyReceipt

For Production : https://buy.itunes.apple.com/verifyReceipt

To validate you need to provide two inputs

{"receipt-data" :"","password" : ""}  // receipt-data is your receipt base64 and password is your shared secret which you can get from iTunes connect.

This Api will give you the JSON response which will contain the status and latest receipts with expiry date and so on.

if status is 0 the receipt is valid

for status 21006 receipt is expired

for status 21007 that means you need to validate on sandbox url (Note: When apple review the in-app purchase functionality they use sandbox tester account but app is in production environment and you validate the receipt in production url which will return the status code 21007 so that you need to validate on sandbox url).

If your subscription is auto-renewable then when your subscription is going to expire you need to load the Receipt, this receipt will contain the new expire date because your subscription is renewed.

 

 

 

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..