Skip to content

Instantly share code, notes, and snippets.

@anonrig
Last active December 23, 2022 07:44
Show Gist options
  • Save anonrig/cfdf88ad3a1d2f7d3b46 to your computer and use it in GitHub Desktop.
Save anonrig/cfdf88ad3a1d2f7d3b46 to your computer and use it in GitHub Desktop.
Problems with inAppPurchase cordova plugin

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.

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

  2. Calling inAppPurchase.restore() only calls onRestoreCompleted method and doesn't call any other method.

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

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

@d3orn
Copy link

d3orn commented Nov 9, 2015

Having the exact same issues 1, 2 and 4. Did you figure something out?

@anonrig
Copy link
Author

anonrig commented Nov 10, 2015

@d3orn Not really, I've been waiting for an official (who has more information about this plugin) to actually reply 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment