先上代码

export default {

components: {

pHeader

},

data() {

return {

iap: null, // 支付通道

ids: ['qiming100', 'meimingbao','ceming811'], // 应用内购项目产品 ID 数组

product_list: [] ,// 应用内购项目列表

mealId:0,

};

},

methods: {

// 获取支付通道

getChannels() {

// #ifdef APP-PLUS

plus.payment.getChannels(res => {

let channel = res.find(i => i.id === 'appleiap')

this.iap = channel ? channel : null

this.requestOrder()

}, function(e) {

plus.nativeUI.alert("获取支付通道失败,请稍后重试。", function() {}, "提示");

})

// #endif

},

// 获取内购项目列表

requestOrder() {

uni.showLoading({

title: '检测支付环境...'

})

// #ifdef APP-PLUS

this.iap.requestOrder(

this.ids,

res => {

uni.hideLoading()

this.product_list = res;

console.log(res);

this.pay();

},

(errormsg)=> {

uni.hideLoading()

plus.nativeUI.alert("获取应用内购项目失败,请稍后重试。", function(e) {

}, "提示");

console.log(JSON.stringify(errormsg))

}

)

// #endif

},

// 调起支付

pay() {

console.log('开始支付')

uni.requestPayment({

provider: 'appleiap',

orderInfo: {

productid: this.ids[this.mealId]

},

success: (e) => {

console.log('支付成功')

console.log(e)

},

fail: (e) => {

uni.showModal({

content: "支付失败,原因为: " + e.errMsg,

showCancel: false

})

this.restoreComplateRequest();

},

complete: () => {

console.log("payment结束")

this.loading = false;

}

})

},

// 获取已购的非消耗性商品和订阅商品

restoreComplateRequest() {

this.iap.restoreComplateRequest({}, results => {

// results 格式为数组存放恢复的IAP商品交易信息对象 IAPTransaction,通用需将返回的支付凭证传给后端进行二次认证

console.log('restoreComplateRequest-results---', JSON.stringify(results))

}, e => {

// 错误回调

console.log('restoreComplateRequest-results ---fail---', JSON.stringify(e))

});

},

handlePay() {

this.iospay();

},

}

};

如果出现返回订单信息失败,很大原因是你打包的测试包appId 和你后台申请内购的appId不相同导致的