iap.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // uni iap
  2. const ProviderType = {
  3. IAP: 'iap'
  4. }
  5. const IapTransactionState = {
  6. purchasing: "0", // A transaction that is being processed by the App Store.
  7. purchased: "1", // A successfully processed transaction.
  8. failed: "2", // A failed transaction.
  9. restored: "3", // A transaction that restores content previously purchased by the user.
  10. deferred: "4" // A transaction that is in the queue, but its final status is pending external action such as Ask to Buy.
  11. };
  12. class Iap {
  13. _channel = null;
  14. _channelError = null;
  15. _productIds = [];
  16. _ready = false;
  17. constructor({
  18. products
  19. }) {
  20. this._productIds = products;
  21. }
  22. init() {
  23. return new Promise((resolve, reject) => {
  24. this.getChannels((channel) => {
  25. this._ready = true;
  26. resolve(channel);
  27. }, (err) => {
  28. reject(err);
  29. })
  30. })
  31. }
  32. getProduct(productIds) {
  33. return new Promise((resolve, reject) => {
  34. this._channel.requestProduct(productIds || this._productIds, (res) => {
  35. resolve(res);
  36. }, (err) => {
  37. reject(err);
  38. })
  39. });
  40. }
  41. requestPayment(orderInfo) {
  42. return new Promise((resolve, reject) => {
  43. uni.requestPayment({
  44. provider: 'appleiap',
  45. orderInfo: orderInfo,
  46. success: (res) => {
  47. resolve(res);
  48. },
  49. fail: (err) => {
  50. reject(err);
  51. }
  52. });
  53. });
  54. }
  55. restoreCompletedTransactions(username) {
  56. return new Promise((resolve, reject) => {
  57. this._channel.restoreCompletedTransactions({
  58. manualFinishTransaction: true,
  59. username
  60. }, (res) => {
  61. resolve(res);
  62. }, (err) => {
  63. reject(err);
  64. })
  65. });
  66. }
  67. finishTransaction(transaction) {
  68. return new Promise((resolve, reject) => {
  69. this._channel.finishTransaction(transaction, (res) => {
  70. resolve(res);
  71. }, (err) => {
  72. reject(err);
  73. });
  74. });
  75. }
  76. getChannels(success, fail) {
  77. if (this._channel !== null) {
  78. success(this._channel)
  79. return
  80. }
  81. if (this._channelError !== null) {
  82. fail(this._channelError)
  83. return
  84. }
  85. uni.getProvider({
  86. service: 'payment',
  87. success: (res) => {
  88. this._channel = res.providers.find((channel) => {
  89. return (channel.id === 'appleiap')
  90. })
  91. if (this._channel) {
  92. success(this._channel)
  93. } else {
  94. this._channelError = {
  95. errMsg: 'paymentContext:fail iap service not found'
  96. }
  97. fail(this._channelError)
  98. }
  99. }
  100. });
  101. }
  102. get channel() {
  103. return this._channel;
  104. }
  105. }
  106. export {
  107. Iap,
  108. IapTransactionState
  109. }