vue中实现微信公众号支付

最近做项目遇到微信支付,根据项目需求使用了微信h5支付。大概的流程介绍:

1、配置微信公众号
2、静默授权,获取路径中code
3、根据code拿到openid
4、根据openid获取prepay_id
5、获取支付签名
6、调起支付功能

一、配置微信公众号
必须是已认证,配置安全域名、开放高级接口权限等等,这里就不过多介绍了。

二、授权
介绍一下授权的俩种方式:

  • 静默授权,用户无感知(scope=snsapi_base)
  • 授权弹窗,用户主动授权(scope=snsapi_userinfo)

微信文档已经写得很清楚了:
在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=url&response_type=code&scope=snsapi_base&state=state&connect_redirect=1#wechat_redirect

参数必填说明
appid公众号唯一标识
redirect_uri授权后的回调链接地址
response_type返回类型,code
scope授权方式
state重定向参数
connect_redirect动态参数,=1 发一次请求
#wechat_redirect重定向必带参数

注意:推荐使用微信开发者工具:公众号调试

redirect_uri:授权后的回调地址,要与配置的js安全域名处在同域下,而且需要使用encodeURIComponent()编码。
如果跳转类似带参数地址 :https://www.baidu.com/news.html?id=1

`https${encodeURIComponent('://') }www.baidu.com${encodeURIComponent('/')}news.html${encodeURIComponent('?')}id=1`

上面已经提到,对于以snsapi_base为scope的网页授权,就静默授权的,用户无感知;
对于已关注公众号的用户,如果用户从公众号的会话或者自定义菜单进入本公众号的网页授权页,即使是scope为snsapi_userinfo,也是静默授权,用户无感知。

三,授权成功
授权成功后,地址中出现了code,接下来取出code,进行下一步操作。这里注意code只能使用一次,并且只会存在5分钟,5分钟后过期失效。

if (window.location.href.indexOf('code') != -1){
	let code = this._getParam('code') // 取出code
	//接下来请求服务器,根据需求获取openid、access_token等等
    let data = {
      code:this._getParam('code')
    }
    this.httpCli({
      url:config.URL_WX_OPENID,
      data:data
    })
      .then(res => {
        if (res.errorCode == 100){
          res.data.openid ? sessionStorage.setItem('openId',res.data.openid) : common.toast(res.data.errorMsg)
          console.log(sessionStorage.getItem('openId')) // 获取openid存到本地
        }
      })
}

//获取地址中的参数
 _getParam(key) {
   let results = new RegExp('[\?&]' + key + '=([^&#]*)').exec(window.location.href);
   if (results) {
     return results[1];
   } else {
     return '';
   }
 },
},

四、根据openid获取prepay_id
在这里插入图片描述
五、获取微信支付签名
签名需app secret,鉴于安全,服务端签名。

本项目获取微信支付签名:
在这里插入图片描述
六、调起微信支付
在这里插入图片描述
签名完毕:
在这里插入图片描述
至此,微信支付流程已经走通,坑相对比较多。

七、链接
微信内调起支付:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
支付签名生成算法:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3

//微信充值 //支付接口测试 function balance(url, data) { uni.request({ url: cfg.originUrl + '/wx/mp/js_sig.do', data: { route: url }, method: 'GET', success: (res) => { jweixin.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { uni.request({ url: cfg.originUrl + '/wx/recharge/pay.do', method: 'POST', header: { 'Content-type': "application/x-www-form-urlencoded", }, data: JSON.stringify(data), success: function(res) { alert("下单成功"); alert(JSON.stringify(res)); alert(res.data.order_id); all.globalData.orderId = res.data.order_id; uni.setStorageSync('orderId', res.data.order_id); jweixin.chooseWXPay({ timestamp: res.data.payParams.timeStamp, // 支付签名时间戳 nonceStr: res.data.payParams.nonceStr, // 支付签名随机串 package: res.data.payParams.package, // 接口返回的prepay_id参数 signType: res.data.payParams.signType, // 签名方式 paySign: res.data.payParams.paySign, // 支付签名 success: function(e) { alert("支付成功"); alert(JSON.stringify(e)); // 支付成功后的回调函数 } }); } }) }); jweixin.error(function(res) { // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数查看,对于SPA可以在这里更新签名。 console.log("验证失败!") }); } }) }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小小白zyw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值