微信支付封装成npm 包

node-wxpay3

(支付文档v2)
普通商户接入文档

如果你使用的是微信最新推出的v3版支付请看微信支付3

前言

本模块集成了大部分微信支付、提现等模块的接口,采用async、await的方式调用,使用者不用在考虑参数加密发送,秘钥发送方式、xml怎么解析、json怎么转成xml等一系列麻烦事。

安装

npm i node-wxpay3 --save

版本介绍

本版本是2.*.*相对于旧版本1.*.*做了大的变更,本插件改用typescript重写,合并了旧的接口方法。支持requireimport两种方法导入。

使用

const WxPay = require('node-wxpay3'); 或者 import WxPay from 'node-wxpay3'

// https://api.mch.weixin.qq.com/pay/unifiedorder
const wxpay = new Wxpay({
  appid: '',
  mch_id: '',
  key: '',
  pfx: fs.readFileSync('./apiclient_cert.p12'),
});
const options = {
    body: '测试',
    out_trade_no: '23214234, 
    total_fee: 1,
    spbill_create_ip: 'ip',
    notify_url: 'https://域名/gateway/boboteacher/student/order/_payReturnss', //自己的接口
    trade_type: 'MWEB',
    scene_info: JSON.stringify({
      h5_info: {
        type: 'Wap',
        wap_url: 'https://域名',
        wap_name: 'bobo',
      },
    }),
    redirect_url: 'https://域名/webpage/api/index.html#/', // 支付成功 返回页面 h5支付需要
  };
  const result = await wxpay.unifiedorder(options);

其他接口和微信文档路径同名 同上一样使用

额外增加的接口:

  1. md5 参数object
wxpay.md5({
 body: '测试',
 out_trade_no: '23214234, 
 })
  1. hmac 参数object 使用同上
  2. xmltojson 参数string xml 转json 暴露给外部调用 使用同上
  3. callback_check 支付回调验证 参数object 返回boolean koa

  // 微信返回的数据是text/xml的数据流格式 
  // 接收数据流并且处理
  ctx.req.setEncoding('utf8');
  ctx.req.on('data', function(chunk) {
      data += chunk;
  });
  // getxml 就是xml形式的数据
  const getxml = await new Promise(function(resolve) {
        ctx.req.on('end', function() {
                resolve(data);
        });
  });

  // 调用wxpay.xmltojson(getxml) 获得的参数就是data
  let data = wxpay.xmltojson(getxml)
  let result = wxpay.callback_check(data)
====》 result = true 则校验成功
ctx.type = 'application/xml';
ctx.body =
            `<xml>
                <return_code><![CDATA[SUCCESS]]></return_code>
                <return_msg><![CDATA[OK]]></return_msg>
            </xml>`;
            return; d
  1. publicEncrypt 公钥加密
wxpay.publicEncrypt(publicKey, data)

其他

如果使用的是1.*.* 请看文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值