商户H5跳转微信支付分小程序-授权服务

简介

接口名称: openBusinessView

接口兼容: 

  • 此接口引用 JSAPI版本1.5.0,引用地址: https://res.wx.qq.com/open/js/jweixin-1.5.0.js
  • 要求用户微信版本>=7.0.5
  • 接口参数

    Object

    字段名 必填 类型 示例值 说明
    businessType String wxpayScoreEnable 固定配置:wxpayScoreEnable
    queryString String 见queryString示例 使用URL的query string方式传递参数,格式为key=value&key2=value2,其中value,value2需要进行UrlEncode处理
    envVersion String release 要打开的小程序版本。仅在当前小程序为开发版或体验版时此参数有效。如果当前小程序是正式版,则打开的小程序必定是正式版

    query示例

    mch_id=1230000109&service_id=88888888000011&out_request_no=1234323JKHDFE1243252&
    timestamp=1530097563&nonce_str=zyx53Nkey8o4bHpxTQvd8m7e92nG5mG2&
    sign_type=HMAC-SHA256&sign=029B52F67573D7E3BE74904BF9AEA

    query内部参数

    字段名 必填 类型 示例值 说明
    mch_id String 1230000109 微信支付分配的商户号
    service_id String 88888888000011 服务ID
    out_request_no String 1234323JKHDFE1243252 调用授权服务接口提交的商户请求唯一标识
    timestamp String 1530097563 生成签名时间戳,单位秒
    nonce_str String zyx53Nkey8o4bHpxTQvd8m7e92nG5mG2 生成签名随机串。由数字、大小写字母组成,长度不超过32位
    sign_type String HMAC-SHA256 签名类型。仅支持HMAC-SHA256
    sign String 029B52F67573D7E3BE74904BF9AEA 使用字段mch_id、service_id、out_request_no、timestamp、nonce_str、sign_type按照签名生成算法计算得出的签名值

    object.envVersion 的合法值

    示例值 说明
    release 正式版

    示例代码:

    let wechatInfo = navigator.userAgent.match(/MicroMessenger\/([\d\.]+)/i);
    let wechatVersion = wechatInfo[1];
    ​
    if (compareVersion(wechatVersion, '7.0.5') >= 0) {
       goToWXScore();
    } else {
       // 提示用户升级微信客户端版本
       window.href = 'https://support.weixin.qq.com/cgi-bin/readtemplate?t=page/
       common_page__upgrade&text=text005&btn_text=btn_text_0'
    }
    ​
    /**
     * 跳转微信支付分
     */
    function goToWXScore() {
        wx.checkJsApi({
            jsApiList: ['openBusinessView'], // 需要检测的JS接口列表
            success: function (res) {
            // 以键值对的形式返回,可用的api值true,不可用为false
            // 如:{"checkResult":{"openBusinessView":true},"errMsg":"checkJsApi:ok"}
            if (res.checkResult.openBusinessView) {
                wx.invoke(
                    'openBusinessView', {
                        businessType: 'wxpayScoreEnable',
                        queryString: 'mch_id=1230000109&service_id=88888888000011&out_request_no=1234323JKHDFE1243252&
    					timestamp=1530097563&nonce_str=zyx53Nkey8o4bHpxTQvd8m7e92nG5mG2
    					&sign_type=HMAC-SHA256&sign=029B52F67573D7E3BE74904BF9AEA'
                    },
                    function (res) {
                    // 从微信侧小程序返回时会执行这个回调函数
                        if (parseint(res.err_code) === 0) {s
                        // 返回成功 
                        } else {
                        // 返回失败
                        }
                    });
                }
            }
        });
     }
    ​
     /**
      * 版本号比较
      * @param {String} v1 
      * @param {String} v2 
      */
    function compareVersion(v1, v2) {
        v1 = v1.split('.')
        v2 = v2.split('.')
        const len = Math.max(v1.length, v2.length)
      
        while (v1.length < len) {
          v1.push('0')
        }
        while (v2.length < len) {
          v2.push('0')
        }
      
        for (let i = 0; i < len; i++) {
          const num1 = parseint(v1[i])
          const num2 = parseint(v2[i])
      
          if (num1 > num2) {
            return 1
          } else if (num1 < num2) {
            return -1
          }
        }
      
        return 0
     }