index-android.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*!
  2. * @overview es6-promise - a tiny implementation of Promises/A+.
  3. * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
  4. * @license Licensed under MIT license
  5. * See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE
  6. * @version 4.0.5
  7. */
  8. !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<G;t+=2){var e=$[t],n=$[t+1];e(n),$[t]=void 0,$[t+1]=void 0}G=0}function f(){try{var t=require,e=t("vertx");return H=e.runOnLoop||e.runOnContext,i()}catch(n){return c()}}function l(t,e){var n=arguments,r=this,o=new this.constructor(p);void 0===o[et]&&k(o);var i=r._state;return i?!function(){var t=n[i-1];J(function(){return x(i,o,t,r._result)})}():E(r,o,t,e),o}function h(t){var e=this;if(t&&"object"==typeof t&&t.constructor===e)return t;var n=new e(p);return g(n,t),n}function p(){}function v(){return new TypeError("You cannot resolve a promise with itself")}function d(){return new TypeError("A promises callback cannot return that same promise.")}function _(t){try{return t.then}catch(e){return it.error=e,it}}function y(t,e,n,r){try{t.call(e,n,r)}catch(o){return o}}function m(t,e,n){J(function(t){var r=!1,o=y(n,e,function(n){r||(r=!0,e!==n?g(t,n):S(t,n))},function(e){r||(r=!0,j(t,e))},"Settle: "+(t._label||" unknown promise"));!r&&o&&(r=!0,j(t,o))},t)}function b(t,e){e._state===rt?S(t,e._result):e._state===ot?j(t,e._result):E(e,void 0,function(e){return g(t,e)},function(e){return j(t,e)})}function w(t,n,r){n.constructor===t.constructor&&r===l&&n.constructor.resolve===h?b(t,n):r===it?j(t,it.error):void 0===r?S(t,n):e(r)?m(t,n,r):S(t,n)}function g(e,n){e===n?j(e,v()):t(n)?w(e,n,_(n)):S(e,n)}function A(t){t._onerror&&t._onerror(t._result),T(t)}function S(t,e){t._state===nt&&(t._result=e,t._state=rt,0!==t._subscribers.length&&J(T,t))}function j(t,e){t._state===nt&&(t._state=ot,t._result=e,J(A,t))}function E(t,e,n,r){var o=t._subscribers,i=o.length;t._onerror=null,o[i]=e,o[i+rt]=n,o[i+ot]=r,0===i&&t._state&&J(T,t)}function T(t){var e=t._subscribers,n=t._state;if(0!==e.length){for(var r=void 0,o=void 0,i=t._result,s=0;s<e.length;s+=3)r=e[s],o=e[s+n],r?x(n,r,o,i):o(i);t._subscribers.length=0}}function M(){this.error=null}function P(t,e){try{return t(e)}catch(n){return st.error=n,st}}function x(t,n,r,o){var i=e(r),s=void 0,u=void 0,c=void 0,a=void 0;if(i){if(s=P(r,o),s===st?(a=!0,u=s.error,s=null):c=!0,n===s)return void j(n,d())}else s=o,c=!0;n._state!==nt||(i&&c?g(n,s):a?j(n,u):t===rt?S(n,s):t===ot&&j(n,s))}function C(t,e){try{e(function(e){g(t,e)},function(e){j(t,e)})}catch(n){j(t,n)}}function O(){return ut++}function k(t){t[et]=ut++,t._state=void 0,t._result=void 0,t._subscribers=[]}function Y(t,e){this._instanceConstructor=t,this.promise=new t(p),this.promise[et]||k(this.promise),B(e)?(this._input=e,this.length=e.length,this._remaining=e.length,this._result=new Array(this.length),0===this.length?S(this.promise,this._result):(this.length=this.length||0,this._enumerate(),0===this._remaining&&S(this.promise,this._result))):j(this.promise,q())}function q(){return new Error("Array Methods must be provided an Array")}function F(t){return new Y(this,t).promise}function D(t){var e=this;return new e(B(t)?function(n,r){for(var o=t.length,i=0;i<o;i++)e.resolve(t[i]).then(n,r)}:function(t,e){return e(new TypeError("You must pass an array to race."))})}function K(t){var e=this,n=new e(p);return j(n,t),n}function L(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}function N(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}function U(t){this[et]=O(),this._result=this._state=void 0,this._subscribers=[],p!==t&&("function"!=typeof t&&L(),this instanceof U?C(this,t):N())}function W(){var t=void 0;if("undefined"!=typeof global)t=global;else if("undefined"!=typeof self)t=self;else try{t=Function("return this")()}catch(e){throw new Error("polyfill failed because global object is unavailable in this environment")}var n=t.Promise;if(n){var r=null;try{r=Object.prototype.toString.call(n.resolve())}catch(e){}if("[object Promise]"===r&&!n.cast)return}t.Promise=U}var z=void 0;z=Array.isArray?Array.isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)};var B=z,G=0,H=void 0,I=void 0,J=function(t,e){$[G]=t,$[G+1]=e,G+=2,2===G&&(I?I(a):tt())},Q="undefined"!=typeof window?window:void 0,R=Q||{},V=R.MutationObserver||R.WebKitMutationObserver,X="undefined"==typeof self&&"undefined"!=typeof process&&"[object process]"==={}.toString.call(process),Z="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel,$=new Array(1e3),tt=void 0;tt=X?o():V?s():Z?u():void 0===Q&&"function"==typeof require?f():c();var et=Math.random().toString(36).substring(16),nt=void 0,rt=1,ot=2,it=new M,st=new M,ut=0;return Y.prototype._enumerate=function(){for(var t=this.length,e=this._input,n=0;this._state===nt&&n<t;n++)this._eachEntry(e[n],n)},Y.prototype._eachEntry=function(t,e){var n=this._instanceConstructor,r=n.resolve;if(r===h){var o=_(t);if(o===l&&t._state!==nt)this._settledAt(t._state,e,t._result);else if("function"!=typeof o)this._remaining--,this._result[e]=t;else if(n===U){var i=new n(p);w(i,t,o),this._willSettleAt(i,e)}else this._willSettleAt(new n(function(e){return e(t)}),e)}else this._willSettleAt(r(t),e)},Y.prototype._settledAt=function(t,e,n){var r=this.promise;r._state===nt&&(this._remaining--,t===ot?j(r,n):this._result[e]=n),0===this._remaining&&S(r,this._result)},Y.prototype._willSettleAt=function(t,e){var n=this;E(t,void 0,function(t){return n._settledAt(rt,e,t)},function(t){return n._settledAt(ot,e,t)})},U.all=F,U.race=D,U.resolve=h,U.reject=K,U._setScheduler=n,U._setAsap=r,U._asap=J,U.prototype={constructor:U,then:l,"catch":function(t){return this.then(null,t)}},U.polyfill=W,U.Promise=U,U});
  9. /**
  10. *
  11. * Array.find() Polyfill
  12. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
  13. *
  14. */
  15. if (!Array.prototype.find) {
  16. Array.prototype.find = function(predicate) {
  17. if (this === null) {
  18. throw new TypeError('Array.prototype.find called on null or undefined');
  19. }
  20. if (typeof predicate !== 'function') {
  21. throw new TypeError('predicate must be a function');
  22. }
  23. var list = Object(this);
  24. var length = list.length >>> 0;
  25. var thisArg = arguments[1];
  26. var value;
  27. for (var i = 0; i < length; i++) {
  28. value = list[i];
  29. if (predicate.call(thisArg, value, i, list)) {
  30. return value;
  31. }
  32. }
  33. return undefined;
  34. };
  35. }
  36. 'use strict';
  37. /*!
  38. *
  39. * Author: Alex Disler (alexdisler.com)
  40. * github.com/alexdisler/cordova-plugin-inapppurchase
  41. *
  42. * Licensed under the MIT license. Please see README for more information.
  43. *
  44. */
  45. var utils = {};
  46. utils.errors = {
  47. 101: 'invalid argument - productIds must be an array of strings',
  48. 102: 'invalid argument - productId must be a string',
  49. 103: 'invalid argument - product type must be a string',
  50. 104: 'invalid argument - receipt must be a string of a json',
  51. 105: 'invalid argument - signature must be a string'
  52. };
  53. utils.validArrayOfStrings = function (val) {
  54. return val && Array.isArray(val) && val.length > 0 && !val.find(function (i) {
  55. return !i.length || typeof i !== 'string';
  56. });
  57. };
  58. utils.validString = function (val) {
  59. return val && val.length && typeof val === 'string';
  60. };
  61. utils.chunk = function (array, size) {
  62. if (!Array.isArray(array)) {
  63. throw new Error('Invalid array');
  64. }
  65. if (typeof size !== 'number' || size < 1) {
  66. throw new Error('Invalid size');
  67. }
  68. var times = Math.ceil(array.length / size);
  69. return Array.apply(null, Array(times)).reduce(function (result, val, i) {
  70. return result.concat([array.slice(i * size, (i + 1) * size)]);
  71. }, []);
  72. };
  73. 'use strict';
  74. /*!
  75. *
  76. * Author: Alex Disler (alexdisler.com)
  77. * github.com/alexdisler/cordova-plugin-inapppurchase
  78. *
  79. * Licensed under the MIT license. Please see README for more information.
  80. *
  81. */
  82. var inAppPurchase = { utils: utils };
  83. var createIapError = function createIapError(reject) {
  84. return function () {
  85. var err = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  86. err.errorCode = err.code;
  87. return reject(err);
  88. };
  89. };
  90. var nativeCall = function nativeCall(name) {
  91. var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  92. return new Promise(function (resolve, reject) {
  93. window.cordova.exec(function (res) {
  94. resolve(res);
  95. }, createIapError(reject), 'InAppBillingV3', name, args);
  96. });
  97. };
  98. var chunkedGetSkuDetails = function chunkedGetSkuDetails(productIds) {
  99. // We need to chunk the getSkuDetails call cause it is only allowed to provide a maximum of 20 items per call
  100. return utils.chunk(productIds, 19).reduce(function (promise, productIds) {
  101. return promise.then(function (result) {
  102. return nativeCall('getSkuDetails', productIds).then(function (items) {
  103. return result.concat(items);
  104. });
  105. });
  106. }, Promise.resolve([]));
  107. };
  108. inAppPurchase.getProducts = function (productIds) {
  109. return new Promise(function (resolve, reject) {
  110. if (!inAppPurchase.utils.validArrayOfStrings(productIds)) {
  111. reject(new Error(inAppPurchase.utils.errors[101]));
  112. } else {
  113. nativeCall('init', []).then(function () {
  114. return chunkedGetSkuDetails(productIds);
  115. }).then(function (items) {
  116. var arr = items.map(function (val) {
  117. return {
  118. productId: val.productId,
  119. title: val.title,
  120. description: val.description,
  121. price: val.price,
  122. currency: val.currency,
  123. priceAsDecimal: val.priceAsDecimal,
  124. };
  125. });
  126. resolve(arr);
  127. }).catch(reject);
  128. }
  129. });
  130. };
  131. var executePaymentOfType = function executePaymentOfType(type, productId) {
  132. return new Promise(function (resolve, reject) {
  133. if (!inAppPurchase.utils.validString(productId)) {
  134. reject(new Error(inAppPurchase.utils.errors[102]));
  135. } else {
  136. nativeCall(type, [productId]).then(function (res) {
  137. resolve({
  138. signature: res.signature,
  139. productId: res.productId,
  140. transactionId: res.purchaseToken,
  141. type: res.type,
  142. productType: res.type,
  143. receipt: res.receipt
  144. });
  145. }).catch(reject);
  146. }
  147. });
  148. };
  149. inAppPurchase.buy = function (productId) {
  150. return executePaymentOfType('buy', productId);
  151. };
  152. inAppPurchase.subscribe = function (productId) {
  153. return executePaymentOfType('subscribe', productId);
  154. };
  155. inAppPurchase.consume = function (type, receipt, signature) {
  156. return new Promise(function (resolve, reject) {
  157. if (!inAppPurchase.utils.validString(type)) {
  158. reject(new Error(inAppPurchase.utils.errors[103]));
  159. } else if (!inAppPurchase.utils.validString(receipt)) {
  160. reject(new Error(inAppPurchase.utils.errors[104]));
  161. } else if (!inAppPurchase.utils.validString(signature)) {
  162. reject(new Error(inAppPurchase.utils.errors[105]));
  163. } else {
  164. nativeCall('consumePurchase', [type, receipt, signature]).then(resolve).catch(reject);
  165. }
  166. });
  167. };
  168. inAppPurchase.restorePurchases = function () {
  169. return nativeCall('init', []).then(function () {
  170. return nativeCall('restorePurchases', []);
  171. }).then(function (purchases) {
  172. var arr = [];
  173. if (purchases) {
  174. arr = purchases.map(function (val) {
  175. return {
  176. productId: val.productId,
  177. state: val.state,
  178. transactionId: val.orderId,
  179. date: val.date,
  180. type: val.type,
  181. productType: val.type,
  182. signature: val.signature,
  183. receipt: val.receipt
  184. };
  185. });
  186. }
  187. return Promise.resolve(arr);
  188. });
  189. };
  190. inAppPurchase.getReceipt = function () {
  191. return Promise.resolve('');
  192. };
  193. module.exports = inAppPurchase;