更换苹果支付模式

This commit is contained in:
@fawn-nine
2024-07-19 13:28:31 +08:00
parent 6f61f656d7
commit 3a0b5d5f9d
3 changed files with 123 additions and 111 deletions

View File

@@ -12,8 +12,8 @@
"src" : "图片路径" "src" : "图片路径"
} }
], ],
"versionName" : "1.2.50", "versionName" : "1.2.51",
"versionCode" : 1250, "versionCode" : 1251,
"app-plus" : { "app-plus" : {
"compatible" : { "compatible" : {
"ignoreVersion" : true "ignoreVersion" : true

View File

@@ -234,117 +234,120 @@
} }
// console.log(this.radioValue) // console.log(this.radioValue)
}, },
async iphonepay() { getProvider() {
const that = this return new Promise((resolve, reject) => {
// 如果ios已经绑定支付信息就直接支付如果没有绑定就需要先绑定 uni.getProvider({
uni.showLoading({ service: 'payment',
title: '检测支付环境' success: (res) => {
}) const iapChannel = res.providers.find((channel) => {
plus.payment.getChannels(async (channels) => { return (channel.id === 'appleiap')
// console.log(channels, 'channels')
for (var i in channels) {
// 判断是否苹果支付1
if (channels[i].id === 'appleiap') {
that.iapChannel = channels[i]
uni.showLoading({
title: '检测历史订单'
}) })
await that.restoreComplateRequest() resolve(iapChannel);
var timer = setInterval(() => { // 如果 iapChannel 为 null说明当前包没有包含iap支付模块。注意HBuilder基座不包含 iap 通道
console.log('检测历史订单数量');
that.iapChannel.restoreComplateRequest({
manualFinishTransaction: true
}, function(results) {
if (results.length > 0) {
console.log('未处理订单还存在,继续轮询...');
} else {
console.log('没有历史订单,开始请求新订单..');
that.requestOrder();
clearInterval(timer)
console.log('timer清除了吗', timer);
}
})
}, 1000)
} }
// else{ });
// uni.showToast({
// title:'支付环境不支持IAP',
// icon:'none'
// })
// }
}
}) })
},
requestPayment(orderInfo) {
let that = this
return new Promise((resolve, reject) => {
uni.requestPayment({
provider: 'appleiap',
orderInfo: orderInfo,
success: (res) => {
that.iapCheck(res);
resolve(res);
},
fail: (err) => {
uni.hideLoading()
// console.log('其他支付错误', err);
that.restoreComplateRequest()
if (err.code == 2) {
uni.showToast({
title: '取消支付,内购订单即将关闭',
icon: 'none'
})
} else {
uni.showToast({
title: '支付失败,内购订单即将关闭',
icon: 'none'
})
}
reject(err);
}
});
})
},
async iphonepay() {
const that = this;
uni.showLoading({
title:"检测支付环境"
})
console.log("检测支付环境...");
this.iapChannel = await this.getProvider()
console.log('that.iapChannel',this.iapChannel);
if(this.iapChannel){
this.requestOrder();
}else{
uni.hideLoading()
uni.showToast({
title:'不支持内购支付',
icon:'none'
})
console.log("获取iap支付通道失败" + e.message, that.iapChannel);
}
}, },
requestOrder() { requestOrder() {
uni.showLoading({ uni.showLoading({
title: '获取商品信息', title: "获取商品信息",
mask: true mask: true,
}) });
const that = this const that = this;
console.log(that.stepsCj.priceTypeId, 88888888) console.log(that.stepsCj.priceTypeId, 88888888);
// ['xxxxx'] 是平台申请拿到的内购商品的id // ['xxxxx'] 是平台申请拿到的内购商品的id
let IAPOrders = [] let IAPOrders = [];
IAPOrders.push(that.stepsCj.priceTypeId+''); IAPOrders.push(that.stepsCj.priceTypeId+'');
console.log(IAPOrders, 'IAPOrders') // console.log(IAPOrders, "IAPOrders");
// 新建订单 // 新建订单
uni.showLoading({ uni.showLoading({
title: "正在创建订单", title: "正在创建订单",
mask: true, mask: true,
}); });
that.iapChannel.requestOrder(IAPOrders, function(event) { that.iapChannel.requestProduct(
// uni.hideLoading() IAPOrders,
console.log(event, 'event') function(event) {
for (var index in event) { // uni.hideLoading()
var OrderItem = event[index] console.log(event, "event");
// console.log(OrderItem, 'OrderItem') for (var index in event) {
console.log(OrderItem.productid, 'OrderItem.productid') var OrderItem = event[index];
that.topay(OrderItem.productid) // console.log(OrderItem, 'OrderItem')
console.log(OrderItem.productid, "OrderItem.productid");
that.topay(OrderItem.productid);
}
},
function(erroemsg) {
uni.showToast({
title: "商品获取失败",
icon: "none",
});
} }
}, function(erroemsg) { );
uni.showToast({
title: "商品获取失败",
icon: 'none'
})
})
}, },
topay(id) { async topay(id) {
const that = this const that = this;
uni.showLoading({ uni.showLoading({
title: "正在支付", title: "正在支付",
mask: true, mask: true,
}); });
// this.setUserInfo({ let orderInfo = {
// restoreFlag: true productid: id,
// }); username: that.orderSn, // 用户标识/订单标识
// console.log(this.userInfo, '更新后的用户信息') quantity:1,
plus.payment.request(that.iapChannel, { manualFinishTransaction: true, // 3.5.1+ 支持,设置此参数后需要开发者主动关闭订单,参见下面的关闭订单方法 finishTransaction()
productid: id,
username: that.orderSn, // 用户标识/订单标识
manualFinishTransaction: true // 3.5.1+ 支持,设置此参数后需要开发者主动关闭订单,参见下面的关闭订单方法 finishTransaction()
}, function(result) {
// restoreFlag = false; // 支付成功清除标记 restoreFlag = false
that.transaction = result
// 支付成功result 为 IAP商品交易信息对象 IAPTransaction 需将返回的支付凭证传给后端进行二次认证
that.iapCheck(result)
}, function(e) {
uni.hideLoading()
console.log('错误回调', e)
if (e.code == 2) {
uni.showToast({
title:'取消支付,内购订单即将关闭',
icon:'none'
})
}else {
uni.showToast({
title:'支付失败,内购订单即将关闭',
icon:'none'
})
console.log('其他支付错误',e);
} }
that.restoreComplateRequest() this.transaction = await this.requestPayment(orderInfo)
}); console.log('支付后的that.transaction',this.transaction);
}, },
iapCheck(result) { iapCheck(result) {
let that = this let that = this

View File

@@ -11,28 +11,37 @@ const ComplateRequestArr = []
var iap = { var iap = {
getProvider() {
return new Promise((resolve, reject) => {
uni.getProvider({
service: 'payment',
success: (res) => {
const iapChannel = res.providers.find((channel) => {
return (channel.id === 'appleiap')
})
resolve(iapChannel);
// 如果 iapChannel 为 null说明当前包没有包含iap支付模块。注意HBuilder基座不包含 iap 通道
}
});
})
},
// 检测支付通道 // 检测支付通道
getChannels(){ async getChannels(){
const that = this; const that = this;
console.log('检测支付通道') console.log('检测支付通道')
plus.payment.getChannels(async (channels) => { this.iapChannel = await this.getProvider()
for (var i in channels) { if(this.iapChannel){
console.log("channels",channels, ); // this.requestOrder();
// 判断是否苹果支付1 this.restoreComplateRequest()
if (channels[i].id === "appleiap") { }else{
console.log("存在苹果内购channels",channels, ); console.log("不支持iap支付");
that.iapChannel = channels[i]; }
// 先检测有没有未完成的订单
that.restoreComplateRequest()
}
}
});
}, },
// 检测是否有未关闭订单 // 检测是否有未关闭订单
restoreComplateRequest() { restoreComplateRequest() {
let that = this let that = this
console.log('检测未完成订单') console.log('检测未完成订单')
this.iapChannel.restoreComplateRequest({ this.iapChannel.restoreCompletedTransactions({
manualFinishTransaction: true manualFinishTransaction: true
}, function(results) { }, function(results) {
if (!that.checking) { if (!that.checking) {
@@ -49,7 +58,7 @@ var iap = {
// return false // return false
// 已经支付,但是没有走逻辑的内购订单 就发给后台做验证 // 已经支付,但是没有走逻辑的内购订单 就发给后台做验证
that.iapCheck(item, index) that.iapCheck(item, index)
// that.finishTransaction(item) // that.finishTransaction(item)
} else { } else {
// 其他状态的内购订单 // 其他状态的内购订单
that.finishTransaction(item) that.finishTransaction(item)
@@ -110,7 +119,7 @@ var iap = {
.catch((e) => { .catch((e) => {
console.log('后台验证失败=>',e); console.log('后台验证失败=>',e);
uni.showModal({ uni.showModal({
title: "提示", title: "提示",
cancelColor:'#0081ff', cancelColor:'#0081ff',
content: `您的账户下存在验证异常的订单(订单编号为:${e.data.orderId})可尝试稍后重启app如不能解决您的问题可联系官方客服`, content: `您的账户下存在验证异常的订单(订单编号为:${e.data.orderId})可尝试稍后重启app如不能解决您的问题可联系官方客服`,
cancelText:'已验证关闭订单', cancelText:'已验证关闭订单',