实现微信公众号开发中常用的微信支付功能。
一、代码
paymentAdded:function(order){//调用微信支付
let that=this
let load = that.$createToast({txt:'', type:'loading',time: 100000,mask:true}).show()//加载信息
that.$ajax.post('wechatPay/toConfig',{url1 :location.href.split('#')[0]})
.then(response=> {
console.log(response);
if (response.success) {
//alert("获取签名成功");
wx.config({
beta:true,
debug : false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId : response.data.weixinAppId, // 必填,公众号的唯一标识
timestamp : response.data.timestamp, // 必填,生成签名的时间戳
nonceStr : response.data.noncestr, // 必填,生成签名的随机串
signature : response.data.signature,// 必填,签名,见附录1
jsApiList : [ 'requestWxFacePictureVerify' ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
that.$ajax.post('orders/getOverTimePayInfoByOrderId',{orderId:order.orderId,orderType:order.orderType})
.then(response=> {
console.log(response);
if (response.success) {
that.$ajax.post('wechatPay/toPaySign',{amount:100,body:"公司名",order_id:response.data.orderNo,orderType:'5'})
.then(response=> {
console.log(response);
load.hide()
if (response.success) {
wx.chooseWXPay({
timestamp : response.data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr : response.data.nonceStr, // 支付签名随机串,不长于 32 位
package : response.data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
signType : response.data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign : response.data.paySign, // 支付签名
success : function(res) {
if (res.errMsg == "chooseWXPay:ok") {
//支付成功
that.$store.commit('toast',{message:'微信支付成功',type:'correct',time:2000})
order.state=14
order.stateName='体检已完成'
} else {
that.$store.commit('toast',{message:'放弃支付',type:'error',time:2000})
//that.$router.replace({path:'/paymentSuccess',query:this.userData})
}
}
});
}
})
}
})
}
})
},