基于vue项目的移动端页面,实现微信支付功能

324 阅读2分钟

1.封装一个微信分享的js文件(utils文件夹下)

1.引入weixin-js-sdk

image.png

2.封装函数方法

image.png

2.上代码,在utils文件夹下新建wx_pay.js

image.png

//微信支付方法(点击按键调用)
import axios from './axios';
import wx from 'weixin-js-sdk';

/*
微信支付方法
*/
export const wexinPay = (data) => {
  let appId = data.appId, //公众号的唯一标识
    timestamp = data.timeStamp, //生成签名的时间戳
    nonceStr = data.nonceStr, //支付签名随机串
    signature = data.sign, //签名
    packages = data.package, //统一支付接口返回的prepay_id参数值
    signType = data.signType; //签名方式,
  wx.config({
    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    appId: appId, // 必填,公众号的唯一标识
    timestamp: timestamp, // 必填,生成签名的时间戳
    nonceStr: nonceStr, // 必填,生成签名的随机串
    signature: signature, // 必填,签名,见附录1
    jsApiList: ['chooseWXPay'], // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  });
  wx.ready(function () {
    wx.chooseWXPay({
      timestamp: timestamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
      nonceStr: nonceStr, // 支付签名随机串,不长于 32 位
      package: packages, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
      signType: signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
      paySign: signature, // 支付签名
      success: function (res) {
        // 支付成功后的回调函数
        console.log(res);
        axios
          .post('/SDSmart/weixin/wxMallPayCallBack')
          .then((res) => {
            console.log(res);
          })
          .catch(() => {
          // 支付成功后的失败回调
          });
      },
      fail: function (res) {
      // 支付失败后的回调函数
        console.log(res);
      },
    });
  });
  wx.error(function () {
    // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
    /*alert("config信息验证失败");*/
  });
};