1.获取支付环境
// 获取支付通道
getChannels() {
let that = this
plus.payment.getChannels(res => {
let channel = res.find(i => i.id === 'appleiap')
that.iap = channel ? channel : null
// 这一步是获取支付参数列表,也就是你苹果后台申请的商品列表 requestOrder
that.requestOrder()
}, function(e) {
plus.nativeUI.alert("获取支付通道失败,请稍后重试。", function() {}, "提示");
})
},
2.获取内购项目列表
// 获取内购项目列表
requestOrder() {
uni.showLoading({
title: '支付环境检测中......'
})
this.vipRuleList = []
this.iap.requestOrder(this.ids,res => {
uni.hideLoading()
this.vipRuleList = res
this.btnFlag = true
uni.hideLoading()
},
(errormsg) => {
plus.nativeUI.alert("获取应用内购项目失败,请稍后重试。", function(e) {}, "提示");
uni.hideLoading()
}
)
},
3.查询订单并且关闭订单 然后拉起支付
/* ios内购 */
requestPayment() {
console.log(this.ids[this.index],"订单ID 商品ID");
uni.showLoading({
title: '支付中...'
})
let that = this
//检测是否有未关闭订单 或者 未支付的订单 若有 关闭订单
that.iap.restoreCompletedTransactions({
manualFinishTransaction: true,
username: ''
}, (res) => {
console.log(res,"未关闭的订单");
if (res.length > 0) {
that.iap.finishTransaction(res[0], (reslut) => {
console.log(reslut, '关闭成功');
})
}
}, (err) => {
console.log(err);
})
uni.requestPayment({
provider: 'appleiap',
orderInfo: {
productid: this.ids[this.index],
manualFinishTransaction: false,
// username:uni.getStorageSync('userinfo').user_id
},
success: (e) => {
console.log(e, "支付结果返回");
console.log('goods',this.vipRuleList[this.index]);
// 请求后台接口 并且完成支付后的逻辑
// 后面的逻辑根据自己的需求来 我这就省略了
},
fail: (e) => {
uni.hideLoading()
console.log(e, "支付失败回调");
uni.showModal({
content: "支付失败",
showCancel: false,
success() {
uni.navigateBack()
}
})
},
complete: () => {
uni.hideLoading()
this.loading = false;
}
})
},