本文章只说明uniapp购买的流程,并没有在苹果后台添加内购产品的流程
ios内购,购买前需要对支付通道进行验证并且需要检查,苹果的内购商品id和你们商品的id是否相同
//检查支付通道
checkPayment() {
const requestOvertime = this.requestOvertime;
uni.showLoading({
title: "检查支付通道",
mask: true,
});
plus.payment.getChannels((channels) => {
channelIap = channels.find((channel) => channel.id === "appleiap");
if (channelIap) {
//检查productIds是否存在苹果内购服务器
channelIap.requestOrder(
this.productIds,//这里是你们所有内购产品的id数组,最好是从后端请求
(orderList) => {
console.log("检查productIds是否存在苹果内购服务器", orderList);
uni.hideLoading();
},
requestOvertime//超时函数
);
} else {
console.log("没有支付通道");
this.requestOvertime();//超时函数
}
}, requestOvertime);
},
// 请求超时
requestOvertime() {
uni.showModal({
content: "请求超时,请稍后重试",
showCancel: false,
});
},
点击支付时,后端接口生成订单信息,并拉起内购弹窗,测试基座拉起的是沙盒,支付成功后,需要后端验证支付是否成功
// 点击支付
clickPayment() {
if (!this.isLogin) {
this.showModal = true;
this.content = '登录后购买,多设备内容同步无忧。轻松畅享,随时随地,尽在掌握。'
return
}
uni.showLoading({
title: "支付中...",
mask: true,
});
// 后端接口生成订单信息
this.requestWithLoading(`********`, "POST", {
id: this.vipList[this.chooseIndex].id,
source: "phone",
system: 2,
})
.then((res) => {
console.log("点击支付", res);
if (res.code == 1) {
// uni.hideLoading();
this.startPayment(res.data.order_sn);//拉起支付
// #ifdef APP-PLUS
// #endif
} else {
uni.hideLoading();
this.$tools.msg(res.msg);
}
})
.catch((err) => {
console.log("err", err);
uni.hideLoading();
});
},
startPayment(order) {
setTimeout(() => {
uni.hideLoading();
}, 1500);
uni.requestPayment({
provider: "appleiap",
orderInfo: {
productid: this.productIds[this.chooseIndex],//支付的id
optimize: true,
username: "*****",
},
success: (e) => {
console.log("支付成功:", e);
this.handlePaymentSuccess(e, order);
},
fail: (err) => {
console.log("取消支付:", err);
},
});
},
// 支付成功后回调
handlePaymentSuccess(e, order) {
uni.showLoading({
title: "支付中...",
mask: true,
});
const data = {
receipt: e.transactionReceipt,
order_no: order,
};
return this.requestWithLoading(
"****************",//接口
"POST",
{
...data,
}
).then((res) => {
console.log("支付成功后回调", res);
uni.hideLoading();
if (res.code === 1) {
this.$tools.msg("会员购买成功");
this.getUserInfo();
setTimeout(() => {
uni.switchTab({
url: "/pages_ai/index/index",
});
}, 1000);
} else {
this.$tools.msg("会员支付失败");
}
});
},