diff --git a/App.vue b/App.vue index 6366b76..a6ddbf3 100644 --- a/App.vue +++ b/App.vue @@ -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({ diff --git a/manifest.json b/manifest.json index 7b25306..7c7b252 100644 --- a/manifest.json +++ b/manifest.json @@ -12,8 +12,8 @@ "src" : "图片路径" } ], - "versionName" : "1.2.48", - "versionCode" : 1248, + "versionName" : "1.2.49", + "versionCode" : 1249, "app-plus" : { "compatible" : { "ignoreVersion" : true diff --git a/pages/peanut/home.vue b/pages/peanut/home.vue index 0f78a4e..95374f3 100644 --- a/pages/peanut/home.vue +++ b/pages/peanut/home.vue @@ -685,7 +685,6 @@ // this.getJtData() }, getJtData(cid) { - this.$http // .post('book/label/list',{ .post('book/labelAndMarket/getShopProductListByLabelId', { diff --git a/pages/peanut/reCharge.vue b/pages/peanut/reCharge.vue index 8043ce4..516c0fb 100644 --- a/pages/peanut/reCharge.vue +++ b/pages/peanut/reCharge.vue @@ -4,7 +4,7 @@ - 充值金额 + 充值金额1 @@ -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' @@ -99,12 +101,12 @@ export default { data() { return { - playData:{}, + playData: {}, xieyi: { title: '', content: '' }, - chargeOrderSn:'', // 通过query传过来的orderSN(二次支付情况) + chargeOrderSn: '', // 通过query传过来的orderSN(二次支付情况) xieyiShow: false, stepsCj: {}, cjList: [], @@ -135,7 +137,7 @@ } ], iosPaylist: [{ - title: 'apple pay支付', + title: 'IAP支付', id: 3, img: '../../static/icon/pay_2.png' }], @@ -156,7 +158,7 @@ // 隐藏原生的tabbar // this.iphonepay() uni.hideTabBar(); - this.getDevName() + this.getDevName() // setTimeout(()=>{ // uni.navigateTo({ // url:'/pages/user/persCount' @@ -166,20 +168,52 @@ computed: { ...mapState(['userInfo']), }, - components:{ - musicPlay + components: { + musicPlay }, //方法 methods: { - ...mapMutations(['setUserInfo']), + ...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){ + finishTransaction(trans) { this.iapChannel.finishTransaction(trans, (success) => { - console.log('关闭订单成功'); - this.setUserInfo({restoreFlag:false}); - }, (fail) => { - console.log('关闭订单失败'); - }); + console.log('关闭订单成功'); + this.setUserInfo({ + restoreFlag: false + }); + }, (fail) => { + console.log('关闭订单失败'); + }); }, showXieyi() { this.$http @@ -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,16 +280,20 @@ 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') for (var index in event) { var OrderItem = event[index] // console.log(OrderItem, 'OrderItem') - console.log(OrderItem.productid,'OrderItem.productid') + console.log(OrderItem.productid, 'OrderItem.productid') that.topay(OrderItem.productid) } }, function(erroemsg) { @@ -245,21 +303,21 @@ }) }) }, - topay(id) { + 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, // 用户标识/订单标识 manualFinishTransaction: true // 3.5.1+ 支持,设置此参数后需要开发者主动关闭订单,参见下面的关闭订单方法 finishTransaction() - + }, function(result) { // restoreFlag = false; // 支付成功清除标记 restoreFlag = false that.transaction = result @@ -267,30 +325,35 @@ that.iapCheck(result) }, function(e) { console.log('错误回调', e) - if(e.errCode == 2){ - // 用户未绑定支付方式,app内支付流程结束,系统弹出框引导用户绑定支付方式,此过程将跳转到系统应用 AppStore 进行绑定支付方式,绑定成功同步支付成功,用户成功付款 - plus.runtime.openURL("https://apps.apple.com/account/billing"); - }else{ - // restoreFlag = false; // 支付失败清楚标记 - that.finishTransaction(result); - //console.log('订单关闭后的用户信息', that.userInfo); - console.log(e) - //console.log(e.message) + if (e.code == 2) { + uni.showToast({ + title:'取消支付,内购订单即将关闭', + icon:'none' + }) + }else { + 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, + customerOid: that.userInfo.id, productId: result.payment.productid, // 产品id orderId: result.payment.username, // 系统订单号 - receiptData: result.transactionReceipt ,// 苹果返回收据 - // body: that.stepsCj.priceTypeId // 充值类型id + 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,具体说明查看官方文档 @@ -300,25 +363,28 @@ }, }).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) - that.finishTransaction(result); + if (res.code == 0) { + uni.hideLoading() + uni.showToast({ + title:'充值成功!', + icon:'success' + }) + console.log("充值订单已处理,请留意账户金额变动...."); + that.finishTransaction(result); } - }).catch(e => { + }).catch(e => { + uni.hideLoading() + console.log('后台验证失败=>',e); uni.showModal({ - title: '提示', - showCancel:false, - content: '支付验证失败,请稍后重启app,如不能解决您的问题,可联系官方客服', - success: function (res) { + title: "提示", + showCancel: false, + content: "支付验证失败,请稍后重启app,如不能解决您的问题,可联系官方客服", + success: function(res) { if (res.confirm) { - console.log('用户点击确定'); + console.log("用户点击确定"); } - } - }); + }, + }); }) }, getDevName() { @@ -335,15 +401,15 @@ // 获取充值金额 getData() { // console.log(this.isAndroid) - if(this.isAndroid){ + if (this.isAndroid) { this.getAndorList() - }else{ + } else { this.getAppleList() } - + }, // 安卓充值列表 - getAndorList(){ + getAndorList() { this.$http .post('book/bookbuyconfig/getVipOrPoint', { type: 'point', @@ -355,7 +421,7 @@ }); }, // 苹果充值列表 - getAppleList(){ + getAppleList() { this.$http .post('book/bookbuyconfig/getVipOrPoint', { type: 'point', @@ -382,11 +448,11 @@ this.iphonepay() }, // 充值 - goToPay(){ + goToPay() { this.kaiChar() }, // 正常充值 - kaiChar() { // 常规充值 + kaiChar() { // 常规充值 if (this.radioValue == '1') { uni.showLoading({ title: '支付中,请勿离开', @@ -402,7 +468,9 @@ realMoney: that.stepsCj.money * 1, //实收金额 orderStatus: 0, //订单状态 orderType: "point", //订单类型 - productId: that.stepsCj.priceTypeId // 充值的类型id + // appName: "", + come: 0, + productId: that.stepsCj.priceTypeId // 充值的类型id } $http.request({ url: "book/buyOrder/rechargeSave", @@ -412,8 +480,8 @@ 'Content-Type': 'application/json' }, }).then(res => { - that.orderSn = res.orderSn - uni.hideLoading() + that.orderSn = res.orderSn + // uni.hideLoading() if (res.code == 0) { if (that.payType == 2) { setPay({ @@ -425,15 +493,17 @@ body: that.stepsCj.priceTypeId, }, res => { if (res.success) { + uni.hideLoading(); uni.showToast({ title: "支付成功" }); - setTimeout(()=>{ + setTimeout(() => { uni.navigateTo({ - url:'/pages/user/persCount' + url: '/pages/user/persCount' }) - },2000) - } else { + }, 2000) + } else { + uni.hideLoading(); uni.showToast({ title: "支付失败", icon: "none", @@ -444,51 +514,58 @@ } else if (that.payType == 1) { // 微信支付 that.orderSn = res.orderSn - let data1={ - orderSn:that.orderSn, + let data1 = { + orderSn: that.orderSn, buyOrderId: that.stepsCj.priceTypeId, totalAmount: that.stepsCj.money } - console.log(data1,'data1') + console.log(data1, 'data1') // console.log(this.userInfo.channelList,'channelList') // this.userInfo.channelList.map(item => { // if(item.id == "wxpay"){ - // console.log('支持微信支付') - setWXPay(data1,res => { - if (res.success) { - uni.showToast({ - title: "支付成功" - }); - setTimeout(()=>{ - uni.navigateTo({ - url:'/pages/user/persCount' - }) - },2000) - } else { - console.log(res) - if(res.data.errMsg.indexOf('User canceled') != -1){ - uni.showToast({ - title: "用户取消支付", - icon: "none", - image: '../../static/icon/ic_close.png' - }); - }else{ - uni.showToast({ - title: "支付失败", - icon: "none", - image: '../../static/icon/ic_close.png' - }); - } - } - }) + // console.log('支持微信支付') + setWXPay(data1, res => { + if (res.success) { + uni.hideLoading(); + uni.showToast({ + title: "支付成功" + }); + setTimeout(() => { + uni.navigateTo({ + url: '/pages/user/persCount' + }) + }, 2000) + } else { + console.log(res) + uni.hideLoading(); + if (res.data.errMsg.indexOf('User canceled') != -1) { + uni.showToast({ + title: "用户取消支付", + icon: "none", + image: '../../static/icon/ic_close.png' + }); + } else { + uni.showToast({ + title: "支付失败", + icon: "none", + image: '../../static/icon/ic_close.png' + }); + } + } + }) // } // }) - + } else if (that.payType == 3) { console.log('苹果支付', ) - that.iosPay() + that.iosPay() } } + }).catch(e => { + uni.showToast({ + title: "系统下单失败", + icon: "none", + }); }) } else { @@ -500,7 +577,7 @@ } - }, + }, // 跳转 onPageJump(url) { uni.navigateTo({ @@ -753,4 +830,4 @@ border-radius: 50rpx; } } - + \ No newline at end of file diff --git a/pages/user/login.vue b/pages/user/login.vue index ed4d8d3..61671d4 100644 --- a/pages/user/login.vue +++ b/pages/user/login.vue @@ -247,7 +247,7 @@ yszcText: {}, quShow: false, quCodeList: [], // 国家区域码 - quCode: 86, + quCode: "86", }; }, diff --git a/unpackage/debug/iOS_debug.ipa b/unpackage/debug/iOS_debug.ipa index 9203b97..d24fffc 100644 Binary files a/unpackage/debug/iOS_debug.ipa and b/unpackage/debug/iOS_debug.ipa differ diff --git a/utils/myIapCheck.js b/utils/myIapCheck.js new file mode 100644 index 0000000..4696e75 --- /dev/null +++ b/utils/myIapCheck.js @@ -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} +