appleiap.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // uni iap
  2. const IapTransactionState = {
  3. purchasing: "0", // A transaction that is being processed by the App Store.
  4. purchased: "1", // A successfully processed transaction.
  5. failed: "2", // A failed transaction.
  6. restored: "3", // A transaction that restores content previously purchased by the user.
  7. deferred: "4" // A transaction that is in the queue, but its final status is pending external action such as Ask to Buy.
  8. };
  9. class Iap {
  10. constructor(data={}) {
  11. this._productIds = data.products || [];
  12. this._channel = null;
  13. this._channelError = null;
  14. this.ready = false;
  15. }
  16. init() {
  17. return new Promise((resolve, reject) => {
  18. this.getChannels((channel) => {
  19. this.ready = true;
  20. resolve(channel);
  21. }, (err) => {
  22. reject(err);
  23. })
  24. })
  25. }
  26. getProduct(productIds) {
  27. return new Promise((resolve, reject) => {
  28. this._channel.requestProduct(productIds || this._productIds, (res) => {
  29. resolve(res);
  30. }, (err) => {
  31. reject(err);
  32. })
  33. });
  34. }
  35. requestPayment(orderInfo) {
  36. return new Promise((resolve, reject) => {
  37. uni.requestPayment({
  38. provider: "appleiap",
  39. orderInfo: {
  40. quantity: 1,
  41. manualFinishTransaction: true,
  42. ...orderInfo
  43. },
  44. success: (res) => {
  45. resolve(res);
  46. },
  47. fail: (err) => {
  48. //console.log('requestPayment-err: ', err)
  49. reject(err);
  50. }
  51. });
  52. });
  53. }
  54. restoreCompletedTransactions(username) {
  55. return new Promise((resolve, reject) => {
  56. this._channel.restoreCompletedTransactions({
  57. manualFinishTransaction: true,
  58. username
  59. }, (res) => {
  60. resolve(res);
  61. }, (err) => {
  62. console.log('restoreCompletedTransactions-err: ', 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 default {
  107. Iap,
  108. IapTransactionState
  109. };