Note: I've inspired from inAppPurchase Cordova Plugin's test example code. This is my service file for Ionic Framework with In App Purchase plugin for Cordova:
(function() {
angular.module('trackFollowersApp').service('inAppPurchase', function($resource) {
var IAP = {
initialize: function() {
if (!window.storekit) {
console.log('In-App Purchases not available');
return;
}
// Initialize
storekit.init({
debug: true,
ready: IAP.onReady,
purchase: IAP.onPurchase,
purchasing: IAP.onPurchasing,
finish: IAP.onFinish,
restore: IAP.onRestore,
error: IAP.onError,
finish: IAP.onFinish,
restoreCompleted: IAP.onRestoreCompleted,
restoreFailed: IAP.onRestoreFailed
});
},
onReady: function() {
storekit.load([
'onemonthsubscription',
'threemonthsubscription',
'sixmonthsubscription'
], function (products, invalidIds) {
console.log('IAPs loading done:');
for (var j = 0; j < products.length; ++j) {
var p = products[j];
console.log('Loaded IAP(' + j + '). title:' + p.title +
' description:' + p.description +
' price:' + p.price +
' id:' + p.id);
}
});
},
onPurchase: function(transactionId, productId) {
var localStorage = window.localStorage;
var expirationDate = moment();
switch (productId) {
case 'onemonthsubscription':
expirationDate.add('1', 'month');
break;
case 'threemonthsubscription':
expirationDate.add('3', 'month');
break;
case 'sixmonthsubscription':
expirationDate.add('6', 'month');
break;
default: break;
};
localStorage.setItem('expirationDate', expirationDate.valueOf());
if (IAP.purchaseCallback) {
IAP.purchaseCallback(productId);
delete IAP.purchaseCallback;
}
storekit.finish(transactionId);
},
onPurchasing: function(a, b, c) {
console.log('purchasing', a, b, c)
},
onFinish: function(transactionId, productId) {
console.log('Finished transaction for ' + productId + ' : ' + transactionId);
},
onError: function (errorCode, errorMessage) {
navigator.notification.alert('Error: ' + errorMessage, null, 'Track Followers', 'Dismiss');
},
onRestore: function (transactionId, productId, transactionReceipt) {
console.log("Restored: " + productId);
var n = (localStorage['storekit.' + productId]|0) + 1;
localStorage['storekit.' + productId] = n;
},
onRestoreCompleted: function () {
navigator.notification.alert('Restore completed.', null, 'Track Followers', 'Dismiss');
console.log("Restore Completed");
},
buy: function (productId, callback) {
IAP.purchaseCallback = callback;
storekit.purchase(productId);
console.log('productId', productId)
},
restore: function () {
storekit.restore();
console.log('restore');
},
onFinish: function(transactionId, productId) {
console.log('onfinish', transactionId, productId)
},
onRestoreFailed: function(transactionId, productId) {
console.log('onRestoreFailed', transactionId, productId)
},
fullVersion: function () {
return localStorage['storekit.babygooinapp1'];
}
};
return IAP;
});
})();
In my onReady function I am calling inAppPurchase.initialize()
method, and do the purchasing method using inAppPurchase.buy(purchaseName);
There are some major absurd/weird behaviors the application in doing.
-
When the application is loaded, it prompts for my iTunes Store account email&password. (It shouldn't have done it unless I buy the product)
-
Calling
inAppPurchase.restore()
only callsonRestoreCompleted
method and doesn't call any other method. -
Even though it says that it takes 5 minutes for a monthly subscription in sandbox to expire, it doesn't expire at all. Therefore I tried validator method and nothing have changed. Any suggestions?
-
If a subscription is still active and you try to buy it again it says
It will be restored free
but there isn't any recognition in plugin side regarding which function will be called upon that
Having the exact same issues 1, 2 and 4. Did you figure something out?