h5页面发起微信支付

发布于 12 天前 前端 最后更新于 12 天前


引入微信sdk

在html页面head中引入http://res.wx.qq.com/open/js/jweixin-1.6.0.js

注册微信支付方法

// 注册微信支付
const initSdk = () => {
wx.config({
  debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  appId: '', // 必填,服务号的唯一标识
  timestamp: , // 必填,生成签名的时间戳
  nonceStr: '', // 必填,生成签名的随机串
  signature: '',// 必填,签名
  jsApiList: ["chooseWXPay"] // 必填,需要使用的JS接口列表
});
};

发起微信支付

const handlePay = (data) => {
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
            appId: data.appid,
            timeStamp: String(data.timeStamp),
            nonceStr: data.nonceStr,
            package: data.package,
            signType: 'MD5',
            paySign: data.paySign
        },
        function (res) {
            if (res.err_msg === 'get_brand_wcpay_request:ok') {
                uni.reLaunch({
                    url: '/pages/index/index'
                });
            } else {
                uni.showModal({
                    title: '支付失败',
                    content: res.err_msg,
                    showCancel: false
                });
            }
        }
    );
};