/*! * @overview es6-promise - a tiny implementation of Promises/A+. * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald) * @license Licensed under MIT license * See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE * @version 4.0.5 */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.ES6Promise=e()}(this,function(){"use strict";function t(t){return"function"==typeof t||"object"==typeof t&&null!==t}function e(t){return"function"==typeof t}function n(t){I=t}function r(t){J=t}function o(){return function(){return process.nextTick(a)}}function i(){return"undefined"!=typeof H?function(){H(a)}:c()}function s(){var t=0,e=new V(a),n=document.createTextNode("");return e.observe(n,{characterData:!0}),function(){n.data=t=++t%2}}function u(){var t=new MessageChannel;return t.port1.onmessage=a,function(){return t.port2.postMessage(0)}}function c(){var t=setTimeout;return function(){return t(a,1)}}function a(){for(var t=0;t>> 0; var thisArg = arguments[1]; var value; for (var i = 0; i < length; i++) { value = list[i]; if (predicate.call(thisArg, value, i, list)) { return value; } } return undefined; }; } 'use strict'; /*! * * Author: Alex Disler (alexdisler.com) * github.com/alexdisler/cordova-plugin-inapppurchase * * Licensed under the MIT license. Please see README for more information. * */ var utils = {}; utils.errors = { 101: 'invalid argument - productIds must be an array of strings', 102: 'invalid argument - productId must be a string', 103: 'invalid argument - product type must be a string', 104: 'invalid argument - receipt must be a string of a json', 105: 'invalid argument - signature must be a string' }; utils.validArrayOfStrings = function (val) { return val && Array.isArray(val) && val.length > 0 && !val.find(function (i) { return !i.length || typeof i !== 'string'; }); }; utils.validString = function (val) { return val && val.length && typeof val === 'string'; }; utils.chunk = function (array, size) { if (!Array.isArray(array)) { throw new Error('Invalid array'); } if (typeof size !== 'number' || size < 1) { throw new Error('Invalid size'); } var times = Math.ceil(array.length / size); return Array.apply(null, Array(times)).reduce(function (result, val, i) { return result.concat([array.slice(i * size, (i + 1) * size)]); }, []); }; 'use strict'; /*! * * Author: Alex Disler (alexdisler.com) * github.com/alexdisler/cordova-plugin-inapppurchase * * Licensed under the MIT license. Please see README for more information. * */ var inAppPurchase = { utils: utils }; var createIapError = function createIapError(reject) { return function () { var err = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; err.errorCode = err.code; return reject(err); }; }; var nativeCall = function nativeCall(name) { var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; return new Promise(function (resolve, reject) { window.cordova.exec(function (res) { resolve(res); }, createIapError(reject), 'InAppBillingV3', name, args); }); }; var chunkedGetSkuDetails = function chunkedGetSkuDetails(productIds) { // We need to chunk the getSkuDetails call cause it is only allowed to provide a maximum of 20 items per call return utils.chunk(productIds, 19).reduce(function (promise, productIds) { return promise.then(function (result) { return nativeCall('getSkuDetails', productIds).then(function (items) { return result.concat(items); }); }); }, Promise.resolve([])); }; inAppPurchase.getProducts = function (productIds) { return new Promise(function (resolve, reject) { if (!inAppPurchase.utils.validArrayOfStrings(productIds)) { reject(new Error(inAppPurchase.utils.errors[101])); } else { nativeCall('init', []).then(function () { return chunkedGetSkuDetails(productIds); }).then(function (items) { var arr = items.map(function (val) { return { productId: val.productId, title: val.title, description: val.description, price: val.price, currency: val.currency, priceAsDecimal: val.priceAsDecimal, }; }); resolve(arr); }).catch(reject); } }); }; var executePaymentOfType = function executePaymentOfType(type, productId) { return new Promise(function (resolve, reject) { if (!inAppPurchase.utils.validString(productId)) { reject(new Error(inAppPurchase.utils.errors[102])); } else { nativeCall(type, [productId]).then(function (res) { resolve({ signature: res.signature, productId: res.productId, transactionId: res.purchaseToken, type: res.type, productType: res.type, receipt: res.receipt }); }).catch(reject); } }); }; inAppPurchase.buy = function (productId) { return executePaymentOfType('buy', productId); }; inAppPurchase.subscribe = function (productId) { return executePaymentOfType('subscribe', productId); }; inAppPurchase.consume = function (type, receipt, signature) { return new Promise(function (resolve, reject) { if (!inAppPurchase.utils.validString(type)) { reject(new Error(inAppPurchase.utils.errors[103])); } else if (!inAppPurchase.utils.validString(receipt)) { reject(new Error(inAppPurchase.utils.errors[104])); } else if (!inAppPurchase.utils.validString(signature)) { reject(new Error(inAppPurchase.utils.errors[105])); } else { nativeCall('consumePurchase', [type, receipt, signature]).then(resolve).catch(reject); } }); }; inAppPurchase.restorePurchases = function () { return nativeCall('init', []).then(function () { return nativeCall('restorePurchases', []); }).then(function (purchases) { var arr = []; if (purchases) { arr = purchases.map(function (val) { return { productId: val.productId, state: val.state, transactionId: val.orderId, date: val.date, type: val.type, productType: val.type, signature: val.signature, receipt: val.receipt }; }); } return Promise.resolve(arr); }); }; inAppPurchase.getReceipt = function () { return Promise.resolve(''); }; module.exports = inAppPurchase;