@sarmay/wechatpay-v3
v1.0.1
Published
Nodejs微信支付v3,适配Nestjs
Downloads
24
Readme
微信支付v3 支持在ts和js中使用
欢迎大家加入一起完善这个api
此项目基于 klover2 和 bobby169 两位大神的代码
前言
微信官方在2020-12-25正式开放了v3版本的接口,相比较旧版本v2有了不少改变,例如:
- 遵循统一的Restful的设计风格
- 使用JSON作为数据交互的格式,不再使用XML
- 使用基于非对称密钥的SHA256-RSA的数字签名算法,不再使用MD5或HMAC-SHA256
- 不再要求HTTPS客户端证书
- 使用AES-256-GCM,对回调中的关键信息进行加密保护
由于官方文档只支持java和php,所以我在这里使用ts简单的封装了一个版本,支持在js或者ts中使用,后续会更加完善这个npm包,谢谢。
Nestjs中使用
// module中注册
import { WeChatPayModule } from '@sarmay/wechatpay-v3';
import * as fs from 'fs';
WeChatPayModule.registerAsync({
useFactory: () => ({
appid: 'xxxxx',
mchid: 'xxxxx',
publicKey: fs.readFileSync('./apiclient_cert.pem'),
privateKey: fs.readFileSync('./apiclient_key.pem'),
}),
}),
// 服务中使用
import { WeChatPayService } from '@sarmay/wechatpay-v3';
constructor(
private readonly weChatPayService: WeChatPayService,
) {}
async pay() {
const res = await this.weChatPayService.xxxxx()
}使用
yarn add @sarmay/wechatpay-v3(也可以用npm,请加上版本号,使用正式版本)
import WxPay from '@sarmay/wechatpay-v3'; // 支持使用require
import fs from 'fs';
import request from 'superagent';
const pay = new WxPay({
appid: '直连商户申请的公众号或移动应用appid',
mchid: '商户号',
publicKey: fs.readFileSync('./apiclient_cert.pem'), // 公钥
privateKey: fs.readFileSync('./apiclient_key.pem'), // 秘钥
});
# 这里以h5支付为例
try {
# 参数介绍请看h5支付文档 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_1.shtml
const params = {
appid: '直连商户申请的公众号或移动应用appid',
mchid: '商户号',
description: '测试',
out_trade_no: '订单号',
notify_url: '回调url',
amount: {
total: 1,
},
scene_info: {
payer_client_ip: 'ip',
h5_info: {
type: 'Wap',
app_name: '网页名称 例如 百度',
app_url: '网页域名 例如 https://www.baidu.com',
},
},
};
const nonce_str = Math.random().toString(36).substr(2, 15), // 随机字符串
timestamp = parseInt(+new Date() / 1000 + '').toString(), // 时间戳 秒
url = '/v3/pay/transactions/h5';
# 获取签名
const signature = pay.getSignature('POST', nonce_str, timestamp, url, params); # 如果是get 请求 则不需要params 参数拼接在url上 例如 /v3/pay/transactions/id/12177525012014?mchid=1230000109
# 获取头部authorization 参数
const authorization = pay.getAuthorization(nonce_str, timestamp, signature);
const result = await request
.post('https://api.mch.weixin.qq.com/v3/pay/transactions/h5')
.send(params)
.set({
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
Authorization: authorization,
});
console.log('result==========>', result.body);
} catch (error) {
console.log(error);
}使用自定义 http 请求
import { IPayRequest, Output } from '@sarmay/wechatpay-v3/dist/define';
自己实现 IPayRequest 接口,使用如下方法注入
pay.createHttp(...);WxPay 介绍
import WxPay from '@sarmay/wechatpay-v3'; 或者 const WxPay = require('@sarmay/wechatpay-v3')
参数介绍 |参数名称 |参数介绍 |是否必须| |--|--|--| | appid| 直连商户申请的公众号或移动应用appid|是| |mchid|商户号|是 |serial_no|证书序列号|否| |publicKey|公钥|是| |privateKey|密钥|是| |signPublicKey|验签公钥|否| |authType|认证类型,目前为WECHATPAY2-SHA256-RSA2048|否| |userAgent|自定义User-Agent|否| |key|APIv3密钥|否 有验证回调必须|
注意
- serial_no是证书序列号 请在命令窗口使用
openssl x509 -in apiclient_cert.pem -noout -serial获取 - 头部参数需要添加 User-Agent 参数
- 需要在商户平台设置APIv3密钥才会有回调,详情参看文档指引http://kf.qq.com/faq/180830E36vyQ180830AZFZvu.html
使用介绍
以下函数是我针对微信相关接口进行进一步封装,可以直接使用。 | api名称 | 函数名 | |--|--| | h5支付 |transactions_h5 | | native支付 |transactions_native | | app支付 |transactions_app | | JSAPI支付 或者 小程序支付 |transactions_jsapi | | 查询订单 |query | | 关闭订单 |close | | 申请交易账单 |tradebill | | 申请资金账单 |fundflowbill | | 下载账单 |downloadBill | | 回调解密 |decipher_gcm | |合单h5支付|combine_transactions_h5| |合单native支付|combine_transactions_native| |合单app支付|combine_transactions_app| |合单JSAPI支付 或者 小程序支付|combine_transactions_jsapi| |查询合单|combine_query| |关闭合单|combine_close| |获取序列号|getSN| |申请退款|refunds| |查询退款|find_refunds| |签名验证|verifySign| |公钥签名验证|verifySignByPublicKey| |微信提现到零钱|batches_transfer| |分账|create_profitsharing_orders| |用户确认模式,发起商家转账|transfer_bills| |用户确认模式,撤销转账|transfer_bills| |用户确认模式,商户单号查询转账单|transfer_bills| |用户确认模式,微信单号查询转账单|transfer_bills|
版本介绍
| 版本号 | 版本介绍 | |--|--| | v0.0.1 | 添加公钥验签方法 |
文档
贡献
欢迎提存在的Bug或者意见。
