app 内购+充值bug

This commit is contained in:
@fawn-nine
2024-07-12 16:02:51 +08:00
parent 9843424e1c
commit 8da6c50ded
7 changed files with 317 additions and 117 deletions

View File

@@ -3,6 +3,9 @@
music,
bgm
} from '@/utils/music.js'
import {
iap
} from "@/utils/myIapCheck.js";
import store from '@/store/index.js';
import socket from '@/config/socket';
// #ifdef H5
@@ -27,7 +30,10 @@
onLaunch: function(e) {
// 检测自动更新
// #ifdef APP-PLUS
updata()
updata();
// 检测是否有未关闭苹果内购订单
iap.getChannels();
console.log('引入了?');
// #endif
uni.getSystemInfo({

View File

@@ -12,8 +12,8 @@
"src" : "图片路径"
}
],
"versionName" : "1.2.48",
"versionCode" : 1248,
"versionName" : "1.2.49",
"versionCode" : 1249,
"app-plus" : {
"compatible" : {
"ignoreVersion" : true

View File

@@ -685,7 +685,6 @@
// this.getJtData()
},
getJtData(cid) {
this.$http
// .post('book/label/list',{
.post('book/labelAndMarket/getShopProductListByLabelId', {

View File

@@ -4,7 +4,7 @@
<public-module></public-module>
<z-nav-bar title="充值"></z-nav-bar>
<view class="cha_jine">
<view class="cj_title">充值金额</view>
<view class="cj_title">充值金额1</view>
<view class="cj_xiang">
<view v-for="(item, index) in cjList" @click="chosPric(item)"
:class="stepsCj.priceTypeId == item.priceTypeId?'Tab_cj cj_price':'cj_price'">
@@ -87,11 +87,13 @@
// IapTransactionState
// } from "@/utils/iap.js"
import {
mapState,mapMutations
mapState,
mapMutations
} from 'vuex';
import {
setPay,
setPayAssign,setWXPay
setPayAssign,
setWXPay
} from '@/config/utils';
// const IAPOrders = [ // 根据这些ids获取到苹果app内商品信息这些ids就是你上面设置的产品id
// 'add69'
@@ -135,7 +137,7 @@
}
],
iosPaylist: [{
title: 'apple pay支付',
title: 'IAP支付',
id: 3,
img: '../../static/icon/pay_2.png'
}],
@@ -172,11 +174,43 @@
//方法
methods: {
...mapMutations(['setUserInfo']),
// 查询未关闭iap订单
async restoreComplateRequest() {
let that = this
console.log('检测未完成订单')
await this.iapChannel.restoreComplateRequest({
manualFinishTransaction: true
}, function(results) {
// console.log(that.checking)
// results 格式为数组存放恢复的IAP商品交易信息对象 IAPTransaction通用需将返回的支付凭证传给后端进行二次认证
that.ComplateRequestArr = results
console.log('未完成订单数组共有:=》',that.ComplateRequestArr.length )
if (results && results.length > 0) {
results.map((item, index) => {
// "0"为正在支付;"1"为支付成功;"2"为支付失败;"3"为支付已恢复。
if (item.transactionState == '1') {
// 已经支付,但是没有走逻辑的内购订单 就发给后台做验证
that.iapCheck('未完成订单的验证',item, index)
// that.finishTransaction(item)
} else if(item.transactionState != '1' || item.transactionState != '0') {
// 不是正在支付订单,也不是已经支付订单就关闭掉
// 其他状态的内购订单
that.finishTransaction(item)
}
})
}
});
// }
},
// 关闭交易订单
finishTransaction(trans) {
this.iapChannel.finishTransaction(trans, (success) => {
console.log('关闭订单成功');
this.setUserInfo({restoreFlag:false});
this.setUserInfo({
restoreFlag: false
});
}, (fail) => {
console.log('关闭订单失败');
});
@@ -201,17 +235,37 @@
}
// console.log(this.radioValue)
},
iphonepay() {
async iphonepay() {
const that = this
// 如果ios已经绑定支付信息就直接支付如果没有绑定就需要先绑定
console.log('检测支付环境...')
plus.payment.getChannels((channels) => {
console.log(channels, 'channels')
uni.showLoading({
title: '检测支付环境'
})
plus.payment.getChannels(async (channels) => {
// console.log(channels, 'channels')
for (var i in channels) {
// 判断是否苹果支付1
if (channels[i].id === 'appleiap') {
that.iapChannel = channels[i]
that.requestOrder()
uni.showLoading({
title: '检测历史订单'
})
await that.restoreComplateRequest()
var timer = setInterval(() => {
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)
}
}
})
@@ -226,9 +280,13 @@
console.log(that.stepsCj.priceTypeId, 88888888)
// ['xxxxx'] 是平台申请拿到的内购商品的id
let IAPOrders = []
IAPOrders.push(that.stepsCj.priceTypeId+'')
IAPOrders.push(that.stepsCj.priceTypeId+'');
console.log(IAPOrders, 'IAPOrders')
// 新建订单
uni.showLoading({
title: "正在创建订单",
mask: true,
});
that.iapChannel.requestOrder(IAPOrders, function(event) {
// uni.hideLoading()
console.log(event, 'event')
@@ -247,14 +305,14 @@
},
topay(id) {
const that = this
uni.hideLoading()
// if(this.chargeOrderSn != '' || this.chargeOrderSn != undefined){ // 判断是不是二次支付的订单
// that.orderSn = this.chargeOrderSn
// console.log(that.orderSn,'二次支付的订单号')
// }
// var restoreFlag = true; // 调用支付接口时标记 restoreFlag = true , 实际应用请将标记存储在 storage 中
this.setUserInfo({restoreFlag:true});
console.log(this.userInfo,'更新后的用户信息')
uni.showLoading({
title: "正在支付",
mask: true,
});
// this.setUserInfo({
// restoreFlag: true
// });
// console.log(this.userInfo, '更新后的用户信息')
plus.payment.request(that.iapChannel, {
productid: id,
username: that.orderSn, // 用户标识/订单标识
@@ -267,21 +325,26 @@
that.iapCheck(result)
}, function(e) {
console.log('错误回调', e)
if(e.errCode == 2){
// 用户未绑定支付方式app内支付流程结束系统弹出框引导用户绑定支付方式此过程将跳转到系统应用 AppStore 进行绑定支付方式,绑定成功同步支付成功,用户成功付款
plus.runtime.openURL("https://apps.apple.com/account/billing");
if (e.code == 2) {
uni.showToast({
title:'取消支付,内购订单即将关闭',
icon:'none'
})
}else {
// restoreFlag = false; // 支付失败清楚标记
that.finishTransaction(result);
//console.log('订单关闭后的用户信息', that.userInfo);
console.log(e)
//console.log(e.message)
uni.showToast({
title:'支付失败,内购订单即将关闭',
icon:'none'
})
console.log('其他支付错误',e);
}
});
},
iapCheck(result) {
let that = this
console.log('进入后台验证')
uni.showLoading({
title:'正在验证订单结果'
})
let data = {
transactionId: result.transactionIdentifier, // 支付交易id
customerOid: that.userInfo.id,
@@ -290,7 +353,7 @@
receiptData: result.transactionReceipt, // 苹果返回收据
// body: that.stepsCj.priceTypeId // 充值类型id
}
console.log(data,'data')
console.log("提交给后台的数据",data);
$http.request({
url: "/Ipa/veri",
method: "POST", // POST、GET、PUT、DELETE具体说明查看官方文档
@@ -301,23 +364,26 @@
}).then(res => {
console.log(JSON.stringify(res))
if (res.code == 0) {
//uni.hideLoading()
that.userInfo.restoreFlag == true ? that.setUserInfo({restoreFlag:false}) : ''
console.log(that.userInfo.restoreFlag)
console.log('充值订单已处理,请留意账户金额变动....')
// 服务器验证票据有效后在客户端关闭订单 (iapChannel.finishTransaction)
uni.hideLoading()
uni.showToast({
title:'充值成功!',
icon:'success'
})
console.log("充值订单已处理,请留意账户金额变动....");
that.finishTransaction(result);
}
}).catch(e => {
uni.hideLoading()
console.log('后台验证失败=>',e);
uni.showModal({
title: '提示',
title: "提示",
showCancel: false,
content: '支付验证失败请稍后重启app如不能解决您的问题可联系官方客服',
content: "支付验证失败请稍后重启app如不能解决您的问题可联系官方客服",
success: function(res) {
if (res.confirm) {
console.log('用户点击确定');
}
console.log("用户点击确定");
}
},
});
})
},
@@ -402,6 +468,8 @@
realMoney: that.stepsCj.money * 1, //实收金额
orderStatus: 0, //订单状态
orderType: "point", //订单类型
// appName: "",
come: 0,
productId: that.stepsCj.priceTypeId // 充值的类型id
}
$http.request({
@@ -413,7 +481,7 @@
},
}).then(res => {
that.orderSn = res.orderSn
uni.hideLoading()
// uni.hideLoading()
if (res.code == 0) {
if (that.payType == 2) {
setPay({
@@ -425,6 +493,7 @@
body: that.stepsCj.priceTypeId,
}, res => {
if (res.success) {
uni.hideLoading();
uni.showToast({
title: "支付成功"
});
@@ -434,6 +503,7 @@
})
}, 2000)
} else {
uni.hideLoading();
uni.showToast({
title: "支付失败",
icon: "none",
@@ -456,6 +526,7 @@
// console.log('支持微信支付')
setWXPay(data1, res => {
if (res.success) {
uni.hideLoading();
uni.showToast({
title: "支付成功"
});
@@ -466,6 +537,7 @@
}, 2000)
} else {
console.log(res)
uni.hideLoading();
if (res.data.errMsg.indexOf('User canceled') != -1) {
uni.showToast({
title: "用户取消支付",
@@ -489,6 +561,11 @@
that.iosPay()
}
}
}).catch(e => {
uni.showToast({
title: "系统下单失败",
icon: "none",
});
})
} else {

View File

@@ -247,7 +247,7 @@
yszcText: {},
quShow: false,
quCodeList: [], // 国家区域码
quCode: 86,
quCode: "86",
};
},

Binary file not shown.

118
utils/myIapCheck.js Normal file
View File

@@ -0,0 +1,118 @@
import store from '@/store/index.js'
import $http from '@/config/requestConfig.js';
// #ifdef APP-PLUS
const iapChannel = null;
const checking = false
const ComplateRequestArr = []
// #endif
// #ifdef H5
// const bgm = {};
// #endif
var iap = {
// 检测支付通道
getChannels(){
const that = this;
console.log('检测支付通道')
plus.payment.getChannels(async (channels) => {
for (var i in channels) {
// 判断是否苹果支付1
if (channels[i].id === "appleiap") {
console.log("存在苹果内购channels",channels, );
that.iapChannel = channels[i];
// 先检测有没有未完成的订单
that.restoreComplateRequest()
}else{
console.log('安卓客户端');
}
}
});
},
// 检测是否有未关闭订单
restoreComplateRequest() {
let that = this
console.log('检测未完成订单')
this.iapChannel.restoreComplateRequest({
manualFinishTransaction: true
}, function(results) {
if (!that.checking) {
that.checking = true
// results 格式为数组存放恢复的IAP商品交易信息对象 IAPTransaction通用需将返回的支付凭证传给后端进行二次认证
that.ComplateRequestArr = results
console.log('未完成订单数组共有:=》',that.ComplateRequestArr.length ,that.ComplateRequestArr)
if (that.ComplateRequestArr.length > 0) {
that.ComplateRequestArr.map((item, index) => {
// "0"为正在支付;"1"为支付成功;"2"为支付失败;"3"为支付已恢复。
if (item.transactionState == '1') {
console.log('待验证订单,即将进入后台验证:=>', item)
// return false
// 已经支付,但是没有走逻辑的内购订单 就发给后台做验证
that.iapCheck(item, index)
// that.finishTransaction(item)
} else {
// 其他状态的内购订单
that.finishTransaction(item)
}
})
}
}
});
},
// 关闭订单
finishTransaction(trans) {
this.iapChannel.finishTransaction(
trans,
(success) => {
console.log("关闭订单成功");
that.checking = false
},
(fail) => {
console.log("关闭订单失败");
that.checking = false
}
);
},
iapCheck(result) {
let that = this;
console.log("进入后台验证");
let data = {
transactionId: result.transactionIdentifier, // 支付交易id
customerOid: store.state.userInfo.id,
productId: result.payment.productid, // 产品id
orderId: result.payment.username, // 系统订单号
receiptData: result.transactionReceipt, // 苹果返回收据
// isSandBox:true
// body: that.stepsCj.priceTypeId // 充值类型id
};
console.log("提交给后台的数据=>", data);
// return false
$http
.request({
url: "/Ipa/veri",
method: "POST", // POST、GET、PUT、DELETE具体说明查看官方文档
data,
header: {
//默认 无 说明:请求头
"Content-Type": "application/json",
},
})
// $http.post('Ipa/veri', data)
.then((res) => {
// console.log(JSON.stringify(res));
if (res.code == 0) {
// uni.hideLoading()
console.log("充值订单已处理....");
// 服务器验证票据有效后在客户端关闭订单 (iapChannel.finishTransaction)
that.finishTransaction(result);
}
})
.catch((e) => {
console.log('后台验证失败=>',e);
// that.finishTransaction(result);
});
},
}
module.exports = {iap}