先在頭部定義商品id 全部商品id (在蘋果商店申請的真實商品id)
let iapChannel = null; var productId = 'lvbi_a'; var productIds = ['lvbi_a'];
進入頁面就判斷
//1.獲取支付渠道
plus.payment.getChannels((channels) => {
for (var i in channels) {
var channel = channels[i];
//蘋果支付
if (channel.id === 'appleiap') {
iapChannel = channel;
this.requestOrder(); //關鍵
}
}
if (!iapChannel) {
that.errorMsg()
}
}, (error) => {
that.errorMsg()
});
requestOrder() {
iapChannel.requestOrder(productIds,
(orderList) => { //必須調用此方法才能進行 iap 支付
uni.hideLoading();
}, (e) => {
uni.hideLoading();
this.errorMsg()
});
},
調接口獲取真實的商品id
// 獲取頁面內容
getAppleProducts() {
AppleProducts({
openid:this.openid,
token:this.tpken
}).then(res =>{
if(res.code == 1){
this.data = res.data;
this.price = this.data.products[0];
productId =
this.data.products[0].product_id;
productIds =
this.data.products_ids;
}
})
},
確定支付
topupprice() {
let that = this;
uni.showLoading({});
uni.requestPayment({
provider: 'appleiap',
orderInfo: {
productid: productId
},
success: (e) => {
uni.hideLoading();
var statement = {
productid: productId
}
// 支付成功查詢訂單
plus.payment.request(iapChannel, statement, function(res){
paysuccers({
data:{
openid:that.openid,
token:that.token,
receipt_data:JSON.stringify(res)
}
}).then(tow =>{
uni.hideLoading();
if(tow.code == 1){
uni.showToast({
title:tow.msg,
icon:'none'
})
}else{
uni.showToast({
title:tow.msg,
icon:'none'
})
}
})
}, function(){
});
},
fail: (e) => {
uni.hideLoading();
uni.showModal({
content: "支付失敗,原因為: " + e.errMsg,
showCancel: false
})
}
})
},

