var iapChannel = null //获取是否支持iap支付 export function Init() { return new Promise((resolve, reject) => { //使用uni.getProvider来获取通道 uni.getProvider({ service: 'payment', success: (res) => { console.log(res, 'init') iapChannel = res.providers.find((channel) => { return (channel.id === 'appleiap') }) //成功之后会返回通道 resolve(iapChannel) }, }) }) } //获取订单 export function restore(iapChannel) { console.log("获取苹果服务器已支付且未关闭的交易列表") return new Promise((resolve, reject) => { iapChannel.restoreCompletedTransactions({ manualFinishTransaction: true, username: '' }, (res) => { resolve(res) }, (err) => { reject(err); }) }); } //关闭订单 export function finishTransaction(transaction, iapChannel) { console.log("关闭订单") return new Promise((resolve, reject) => { iapChannel.finishTransaction(transaction, (res) => { console.log("关闭订单成功", res) resolve(res); }, (err) => { reject(err); }); }); } export function getReview(iapChannel, token, dev) { //请求是否有已完成未关闭的订单 restore(iapChannel).then(res => { //如果有并且状态为已支付则请求关闭并回调给后端 console.log(res) if (res.length > 0) { //轮询关闭订单 res.map(item => { finishTransaction(item, iapChannel) //如果状态为已完成的状态 if (item.transactionState == '1') { } }) } }) } export function requestOrder(iapChannel, productIds) { uni.showLoading({ title: '初始化中~', mask: true }) return new Promise((resolve, reject) => { iapChannel.requestOrder(productIds, (orderList) => { //必须调用此方法才能进行 iap 支付 console.log('requestOrder success: ' + JSON.stringify(orderList)); resolve(orderList) uni.hideLoading() }, (e) => { console.log('requestOrder failed: ' + JSON.stringify(e)); uni.hideLoading() uni.showToast({ icon: 'none', title: '当前环境不支持内购支付' }) reject(e) }); }) }