Skip to content

Latest commit

 

History

History
82 lines (69 loc) · 2.18 KB

WXPay.md

File metadata and controls

82 lines (69 loc) · 2.18 KB

微信支付

官方文档

支持的功能

方法 说明 是否需要证书
microPay 刷卡支付
unifiedOrder 统一下单
orderQuery 查询订单
reverse 撤销订单
closeOrder 关闭订单
refund 申请退款
refundQuery 查询退款
downloadBill 下载对账单
report 交易保障
shortUrl 转换短链接
authCodeToOpenid 授权码查询openid

使用

// 参数与官方文档对齐
 
const fs = require('fs');
const {
    WXPay,
    WXPayUtil,
    WXPayConstants
 } = require('wx-js-utils');
 
const appId = 'wx8888888888';
const mchId = '8888888',
const key = '8888888888888888888888888888888',
const certFileContent = fs.readFileSync('/path/to/apiclient_cert.p12'),
const caFileContent = fs.readFileSync('/path/to/rootca.pem'),
const timeout = 10000; // 毫秒
    
let wxpay = new WXPay({
    appId,
    mchId,
    key,
    certFileContent,
    caFileContent,
    timeout,
    signType: WXPayConstants.SIGN_TYPE_MD5,  // 使用 HMAC-SHA256 签名,也可以选择  WXPayConstants.SIGN_TYPE_MD5,小程序默认是 WXPayConstants.SIGN_TYPE_MD
    useSandbox: false   // 不使用沙箱环境
});
 
 
var reqObj = {
  body: '商城-商品1',
  out_trade_no: '1478582754970',
  total_fee: 1,
  spbill_create_ip: '123.12.12.123',
  notify_url: 'http://www.example.com/wxpay/notify',
  trade_type: 'NATIVE'
};

// 统一下单
wxpay.unifiedOrder(reqObj).then(function(respObj) {
    console.log(respObj);
}).catch(function(err) {
    console.log(err);
});

// 签名,不同端的签名可能有所不同,以下举小程序作为例子,
// 使用 WXPayUtil.generateSignature 签名,
// 详细文档: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=3
// 小程序签名

const sign = WXPayUtil.generateSignature({
    appId: appId,
    nonceStr: nonce_str,
    package: `prepay_id=${prepay_id}`
    signType: 'MD5',
    timeStamp: time_stamp // 时间戳
}, KEY);