# weixinpay-sdk

> 基于微信官方的原生 API 封装而成的 SDK，将官方要求的 xml 格式均通过 sdk 转换成 JSON 格式方便开发，当前已支持 Native

Latest version **1.0.2** (published 2020-01-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install weixinpay-sdk
pnpm add weixinpay-sdk
yarn add weixinpay-sdk
bun add weixinpay-sdk
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-01-15 |
| First published | 2020-01-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 12.6 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | xiluo |
| Maintainers | xiluotop |
| Keywords | 微信支付, weixinpay, weixinpaysdk, weixinpay-sdk, 支付, 微信, Native |

## Links

- npm: https://www.npmjs.com/package/weixinpay-sdk
- Repository: https://github.com/xiluotop/weixinpay-nodejs
- Homepage: https://github.com/xiluotop/weixinpay-nodejs#readme
- Issues: https://github.com/xiluotop/weixinpay-nodejs/issues
- npm.io page: https://npm.io/package/weixinpay-sdk

## Dependencies (3)

- [md5](https://npm.io/package/md5.md) ^2.2.1
- [axios](https://npm.io/package/axios.md) ^0.19.1
- [random-string](https://npm.io/package/random-string.md) ^0.2.0

## Recent versions

- 1.0.2 (latest) — 2020-01-15
- 1.0.1 — 2020-01-15

## README

# 微信支付 weixinpay-sdk

## 版本说明

* v1.0.1
  * 封装微信官方原生接口，可以使用 json 格式进行数据请求
  * 当前完成 Native 支付方式
  * 完成接口：统一下单、查询订单、关闭订单
  * 包含功能：验签方法，转换工具等。

## 安装

`npm i weixinpay-sdk`

## 使用示例

* 接口方法：
  * 统一下单：`unifiedorder`
  * 查询订单：`orderquery`
  * 关闭订单：`closeorder`

### 统一下单

```JavaScript
const WeiXinPay = require('weixinpay-sdk')
// 基础配置
// 特别说明：只要正确配置了商户 api 私密 key， SDK 会自动创建随机字符串和签名。
let config = {
  appid: '', // 公众账号 ID
  mch_id: '', // 商户号
  key: '' // 商户 api key
}
let wxPay = new WeiXinPay(config);
let order = Date.now();
let orderRes = await wxPay.exec('unifiedorder', {
  body: '支付测试', // 商品简单描述
  out_trade_no: order, // 商户订单号
  total_fee: 1, // 订单总金额，单位分
  spbill_create_ip: req.ip, // 客户端 Ip
  trade_type: 'NATIVE', // 交易类型 JSAPI | NATIVE | APP
  notify_url: 'http://domain.com/notify', // 交易成功后的异步通知地址
})
```

### 查询订单

```javascript
app.get('/queryorder', async (req, res) => {
  let order = req.query;
  let result = await wxPay.exec('orderquery', order);
  if (wxPay.checkSign(result)) {
    res.send(result);
  } else {
    res.send({
      msg: '查询失败！'
    });
  }
})
```

### 关闭订单

```javascript
app.get('/closeorder', async (req, res) => {
  let order = req.query;
  let result = await wxPay.exec('closeorder', order);
  if (wxPay.checkSign(result)) {
    if (result.return_msg === 'OK') {
      res.send({
        msg: '订单已关闭！'
      })
    } else {
      res.send({
        msg: result.return_msg
      })
    }
  } else {
    res.send({
      msg: result.return_msg
    });
  }
})
```

### 异步通知

```javascript
app.post('/notify', XmlBodyParser({
  trim: false,
  explicitArray: false
}), (req, res) => {
  let data = req.body.xml;
  if (wxPay.checkSign(data)) {
    // 验签成功
    // doWork...
    console.log('验签成功')
  } else {
    // 验签失败
    console.log('验签失败')
  }
  res.send(wxPay.notifyOK)
})
```

---
_Source: https://npm.io/package/weixinpay-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
