From 6235ee51ebbe5f31ddfbba63c066a21170bf2403 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 28 May 2015 08:49:46 -0400 Subject: [PATCH] update cordova scripts --- .../thirdparty/cordova/android/iap.js | 50 +++++++ dashboard-ui/thirdparty/cordova/iap.js | 123 ++++++++++++++++ .../cordova/registrationservices.js | 133 ++---------------- 3 files changed, 183 insertions(+), 123 deletions(-) create mode 100644 dashboard-ui/thirdparty/cordova/android/iap.js create mode 100644 dashboard-ui/thirdparty/cordova/iap.js diff --git a/dashboard-ui/thirdparty/cordova/android/iap.js b/dashboard-ui/thirdparty/cordova/android/iap.js new file mode 100644 index 0000000000..e3d5909025 --- /dev/null +++ b/dashboard-ui/thirdparty/cordova/android/iap.js @@ -0,0 +1,50 @@ +(function () { + + var unlockId = "premiumunlock"; + var updatedProducts = []; + + function updateProductInfo(id, owned) { + + updatedProducts = updatedProducts.filter(function (r) { + return r.id != id; + }); + + updatedProducts.push({ + id: id, + owned: owned + }); + } + + function hasPurchased(id) { + var product = getProduct(id); + + return product != null && product.owned; + } + + function getProduct(id) { + var products = updatedProducts.filter(function (r) { + return r.id == id; + }); + + return products.length ? products[0] : null; + } + + function isPurchaseAvailable(id) { + + return NativeIapManager.isStoreAvailable(); + } + + function beginPurchase(id) { + return NativeIapManager.beginPurchase(id); + } + + window.IapManager = { + isPurchaseAvailable: isPurchaseAvailable, + hasPurchased: hasPurchased, + updateProduct: updateProductInfo, + beginPurchase: beginPurchase + }; + + NativeIapManager.isPurchased(unlockId, "window.IapManager.updateProduct"); + +})(); \ No newline at end of file diff --git a/dashboard-ui/thirdparty/cordova/iap.js b/dashboard-ui/thirdparty/cordova/iap.js new file mode 100644 index 0000000000..428d1e28a0 --- /dev/null +++ b/dashboard-ui/thirdparty/cordova/iap.js @@ -0,0 +1,123 @@ +(function () { + + var unlockAlias = "premium features"; + var updatedProducts = []; + + function updateProductInfo(p) { + + updatedProducts = updatedProducts.filter(function (r) { + return r.id != p.id; + }); + + updatedProducts.push(p); + } + + function normalizeId(id) { + + // This is what i named it in itunes + id = id.replace('premiumunlock', 'appunlock'); + + return id; + } + + function getProduct(id) { + + id = normalizeId(id); + + var products = updatedProducts.filter(function (r) { + return r.id == id; + }); + + return products.length ? products[0] : null; + } + + function hasPurchased(id) { + var product = getProduct(id); + + return product != null && product.owned; + } + + function isPurchaseAvailable(id) { + var product = getProduct(id); + + return product != null && product.canPurchase; + } + + function beginPurchase(id) { + + } + + function validateProduct(product, callback) { + + // product attributes: + // https://github.com/j3k0/cordova-plugin-purchase/blob/master/doc/api.md#validation-error-codes + + callback(true, { + + }); + + //callback(true, { ... transaction details ... }); // success! + + //// OR + //callback(false, { + // error: { + // code: store.PURCHASE_EXPIRED, + // message: "XYZ" + // } + //}); + + //// OR + //callback(false, "Impossible to proceed with validation"); + } + + function initializeStore() { + + // Let's set a pretty high verbosity level, so that we see a lot of stuff + // in the console (reassuring us that something is happening). + store.verbosity = store.INFO; + + store.validator = validateProduct; + + // iOS + store.register({ + id: "appunlock", + alias: unlockAlias, + type: store.NON_CONSUMABLE + }); + + // When purchase of the full version is approved, + // show some logs and finish the transaction. + store.when(unlockAlias).approved(function (order) { + log('You just unlocked the FULL VERSION!'); + order.finish(); + }); + + // The play button can only be accessed when the user + // owns the full version. + store.when(unlockAlias).updated(function (product) { + + updateProductInfo(product); + }); + + // When every goes as expected, it's time to celebrate! + // The "ready" event should be welcomed with music and fireworks, + // go ask your boss about it! (just in case) + store.ready(function () { + + console.log("Store ready"); + }); + + // After we've done our setup, we tell the store to do + // it's first refresh. Nothing will happen if we do not call store.refresh() + store.refresh(); + } + + window.IapManager = { + isPurchaseAvailable: isPurchaseAvailable, + hasPurchased: hasPurchased, + beginPurchase: beginPurchase + }; + + initializeStore(); + +})(); \ No newline at end of file diff --git a/dashboard-ui/thirdparty/cordova/registrationservices.js b/dashboard-ui/thirdparty/cordova/registrationservices.js index 20d3001fa4..232d873092 100644 --- a/dashboard-ui/thirdparty/cordova/registrationservices.js +++ b/dashboard-ui/thirdparty/cordova/registrationservices.js @@ -1,38 +1,5 @@ (function () { - var updatedProducts = []; - - var unlockAlias = "premium features"; - - function updateProductInfo(p) { - - updatedProducts = updatedProducts.filter(function (r) { - return r.alias != p.alias; - }); - - updatedProducts.push(p); - } - - function getProduct(alias) { - var products = updatedProducts.filter(function (r) { - return r.alias == alias; - }); - - return products.length ? products[0] : null; - } - - function hasPurchased(alias) { - var product = getProduct(alias); - - return product != null && product.owned; - } - - function isPurchaseAvailable(alias) { - var product = getProduct(alias); - - return product != null && product.canPurchase; - } - function isAndroid() { var platform = (device.platform || '').toLowerCase(); @@ -50,8 +17,7 @@ validateFeature({ - id: 'appunlock', - alias: unlockAlias + id: 'premiumunlock' }, deferred); } @@ -66,8 +32,7 @@ validateFeature({ - id: 'premiumunlock', - alias: unlockAlias + id: 'premiumunlock' }, deferred); } @@ -82,8 +47,7 @@ validateFeature({ - id: 'premiumunlock', - alias: unlockAlias + id: 'premiumunlock' }, deferred); } @@ -100,14 +64,15 @@ function validateFeature(info, deferred) { - if (hasPurchased(info.alias)) { + if (IapManager.hasPurchased(info.id)) { deferred.resolve(); return; } var productInfo = { enableSupporterUnlock: isAndroid(), - enableAppUnlock: isPurchaseAvailable(info.alias) + enableAppUnlock: IapManager.isPurchaseAvailable(info.id), + id: info.id }; var prefix = isAndroid() ? 'android' : 'ios'; @@ -163,13 +128,7 @@ } if (info.enableAppUnlock) { - html += '

'; - html += Globalize.translate('MessageToValidateSupporter'); - html += '

'; - } - - if (info.enableAppUnlock) { - html += ''; + html += ''; } if (info.enableSupporterUnlock) { @@ -199,6 +158,7 @@ $('.inAppPurchaseForm', elem).on('submit', function () { + IapManager.beginPurchase(info.id); return false; }); @@ -222,11 +182,6 @@ } }); }); - - $('.btnAppUnlock', elem).on('click', function () { - - alert('coming soon'); - }); } window.RegistrationServices = { @@ -260,76 +215,8 @@ } }; - function validateProduct(product, callback) { + var depends = isAndroid() ? 'thirdparty/cordova/android/iap' : 'thirdparty/cordova/iap'; - // product attributes: - // https://github.com/j3k0/cordova-plugin-purchase/blob/master/doc/api.md#validation-error-codes - - callback(true, { - - }); - - //callback(true, { ... transaction details ... }); // success! - - //// OR - //callback(false, { - // error: { - // code: store.PURCHASE_EXPIRED, - // message: "XYZ" - // } - //}); - - //// OR - //callback(false, "Impossible to proceed with validation"); - } - - function initializeStore() { - - if (isAndroid()) { - return; - } - // Let's set a pretty high verbosity level, so that we see a lot of stuff - // in the console (reassuring us that something is happening). - store.verbosity = store.INFO; - - store.validator = validateProduct; - - // iOS - store.register({ - id: "appunlock", - alias: unlockAlias, - type: store.NON_CONSUMABLE - }); - - // When purchase of the full version is approved, - // show some logs and finish the transaction. - store.when(unlockAlias).approved(function (order) { - log('You just unlocked the FULL VERSION!'); - order.finish(); - }); - - // The play button can only be accessed when the user - // owns the full version. - store.when(unlockAlias).updated(function (product) { - - updateProductInfo(product); - }); - - // When every goes as expected, it's time to celebrate! - // The "ready" event should be welcomed with music and fireworks, - // go ask your boss about it! (just in case) - store.ready(function () { - - console.log("Store ready"); - }); - - // After we've done our setup, we tell the store to do - // it's first refresh. Nothing will happen if we do not call store.refresh() - store.refresh(); - } - - // We must wait for the "deviceready" event to fire - // before we can use the store object. - initializeStore(); + requirejs([depends]); })(); \ No newline at end of file