一.微信小程序支付
首先通过微信的登陆接口获取临时code wx.login({ success: res => { console.log(res.code) } })2.通过微信的auth.code2Session接口传入login获取的code交换用户的openId
wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId wx.request({ url: 'https://30paotui.com/user/wechat', data: { code: res.code }, success: function (response) { that.globalData.openid = response.data.openid; } }) } })3.通过用户的openId和单据的billNo传给后台,获取支付相应的参数吊起微信支付
wx.requestPayment({ 'timeStamp': params.timeStamp, 'nonceStr': params.nonceStr, 'package': params.packages, 'signType': params.signType, 'paySign': params.paySign, 'success': function (res) { wx.showToast({ title: '支付成功', icon: 'success', duration: 2000 }); }, 'fail': function (res) { wx.showToast({ title: '支付失败', icon: 'success', duration: 2000 }); }, 'complete': function (res) { wx.showToast({ title: '支付结束', icon: 'success', duration: 2000 }); } })二、微信公众号支付
1.调用公众号的授权接口获取用户的openId
2.通过单据billNo和openId传到后台,获取支付信息,然后调用微信的JDK吊起支付
pubOrderPayByBillNo({billNo}).then(response => { let that = this; WeixinJSBridge.invoke( 'getBrandWCPayRequest', { "appId": response.data.appId, //公众号名称,由商户传入 "timeStamp": response.data.timeStamp, //时间戳,自1970年以来的秒数 "nonceStr": response.data.nonceStr, //随机串 "package": response.data.packages, "signType": response.data.signType, //微信签名方式: "paySign": response.data.paySign //微信签名 }, function (res) { that.initUpdate(); if (res.err_msg == "get_brand_wcpay_request:ok") { bus.$emit('onUpdate'); that.$router.push('/main'); // 使用以上方式判断前端返回,微信团队郑重提示: //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。 } else { window.location.reload(); } }); })