index-ios.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict';
  2. /*!
  3. *
  4. * Author: Alex Disler (alexdisler.com)
  5. * github.com/alexdisler/cordova-plugin-inapppurchase
  6. *
  7. * Licensed under the MIT license. Please see README for more information.
  8. *
  9. */
  10. var utils = {};
  11. utils.errors = {
  12. 101: 'invalid argument - productIds must be an array of strings',
  13. 102: 'invalid argument - productId must be a string',
  14. 103: 'invalid argument - product type must be a string',
  15. 104: 'invalid argument - receipt must be a string of a json',
  16. 105: 'invalid argument - signature must be a string'
  17. };
  18. utils.validArrayOfStrings = function (val) {
  19. return val && Array.isArray(val) && val.length > 0 && !val.find(function (i) {
  20. return !i.length || typeof i !== 'string';
  21. });
  22. };
  23. utils.validString = function (val) {
  24. return val && val.length && typeof val === 'string';
  25. };
  26. utils.chunk = function (array, size) {
  27. if (!Array.isArray(array)) {
  28. throw new Error('Invalid array');
  29. }
  30. if (typeof size !== 'number' || size < 1) {
  31. throw new Error('Invalid size');
  32. }
  33. var times = Math.ceil(array.length / size);
  34. return Array.apply(null, Array(times)).reduce(function (result, val, i) {
  35. return result.concat([array.slice(i * size, (i + 1) * size)]);
  36. }, []);
  37. };
  38. 'use strict';
  39. /*!
  40. *
  41. * Author: Alex Disler (alexdisler.com)
  42. * github.com/alexdisler/cordova-plugin-inapppurchase
  43. *
  44. * Licensed under the MIT license. Please see README for more information.
  45. *
  46. */
  47. var inAppPurchase = { utils: utils };
  48. var nativeCall = function nativeCall(name) {
  49. var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  50. return new Promise(function (resolve, reject) {
  51. window.cordova.exec(function (res) {
  52. resolve(res);
  53. }, function (err) {
  54. reject(err);
  55. }, 'PaymentsPlugin', name, args);
  56. });
  57. };
  58. inAppPurchase.getProducts = function (productIds) {
  59. return new Promise(function (resolve, reject) {
  60. if (!inAppPurchase.utils.validArrayOfStrings(productIds)) {
  61. reject(new Error(inAppPurchase.utils.errors[101]));
  62. } else {
  63. return nativeCall('getProducts', [productIds]).then(function (res) {
  64. if (!res || !res.products) {
  65. resolve([]);
  66. } else {
  67. var arr = res.products.map(function (val) {
  68. return {
  69. productId: val.productId,
  70. title: val.title,
  71. description: val.description,
  72. price: val.price,
  73. currency: val.currency,
  74. priceAsDecimal: val.priceAsDecimal,
  75. };
  76. });
  77. resolve(arr);
  78. }
  79. }).catch(reject);
  80. }
  81. });
  82. };
  83. inAppPurchase.buy = function (productId) {
  84. return new Promise(function (resolve, reject) {
  85. if (!inAppPurchase.utils.validString(productId)) {
  86. reject(new Error(inAppPurchase.utils.errors[102]));
  87. } else {
  88. nativeCall('buy', [productId]).then(function (res) {
  89. resolve({
  90. transactionId: res.transactionId,
  91. receipt: res.receipt
  92. });
  93. }).catch(reject);
  94. }
  95. });
  96. };
  97. /**
  98. * This function exists so that the iOS plugin API will be compatible with that of Android -
  99. * where this function is required.
  100. * See README for more details.
  101. */
  102. inAppPurchase.subscribe = function (productId) {
  103. return inAppPurchase.buy(productId);
  104. };
  105. /**
  106. * This function exists so that the iOS plugin API will be compatible with that of Android -
  107. * where this function is required.
  108. * See README for more details.
  109. */
  110. inAppPurchase.consume = function () {
  111. return Promise.resolve();
  112. };
  113. inAppPurchase.getReceipt = function () {
  114. return nativeCall('getReceipt').then(function (res) {
  115. var receipt = '';
  116. if (res && res.receipt) {
  117. receipt = res.receipt;
  118. }
  119. return receipt;
  120. });
  121. };
  122. inAppPurchase.restorePurchases = function () {
  123. return nativeCall('restorePurchases').then(function (res) {
  124. var arr = [];
  125. if (res && res.transactions) {
  126. arr = res.transactions.map(function (val) {
  127. return {
  128. productId: val.productId,
  129. date: val.date,
  130. transactionId: val.transactionId,
  131. state: val.transactionState
  132. };
  133. });
  134. }
  135. return arr;
  136. });
  137. };
  138. module.exports = inAppPurchase;