
Security News
GitHub Actions Checkout Now Blocks Risky pull_request_target Checkouts
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.
tiny-wechatpay
Advanced tools
通知类中间件 - 支付结果通知, 退款结果通知前端支付支持 - 支持JSAPI, WeixinJSBridge, 小程序, APP, H5支付模式支持 - 付款码/公众号/小程序/APP/H5/扫码支付支付工具支持 - 微信红包, 企业付款(支持付款到零钱和银行卡)营销功能支持 - 微信代金券对帐账单支持 - 支持微信对帐单, 微信资金帐单服务商模式支持 - 所有api均可自行传入sub_appid, sub_mch_id微信支付仿真测试系统 - 支持沙盒模式, 用于完成支付验收流程QQ群:157964097,使用疑问,开发,贡献代码请加群。
nodejs >= 8.3.0
因涉及金额等敏感问题, API和中间件并没有对数据字段做类型转换
微信返回值XML做JSON转换之后的字段均为字符串类型, 请自行转换后再进行数据运算
'1' + 0 = '10'微信支付中传入的金额单位为分API和中间件均对所有错误进行了处理, 统一通过error返回, 包括:
网络类错误 - 网络中断, 连接超时等微信返回值检验错误 - 微信返回值非法(伪造请求等, 可能性非常低)业务逻辑错误 - 订单重复, 退款金额大于支付金额等其它错误 - 应传参数未传入等未出错时正常返回为JSON格式数据
downloadBill和downloadFundflow下载的帐单返回值为字符串文本npm i tenpay
# 如已安装旧版, 重新安装最新版
npm i tenpay@latest
const tenpay = require('tenpay');
const config = {
appid: '公众号ID',
mchid: '微信商户号',
partnerKey: '微信支付安全密钥',
pfx: require('fs').readFileSync('证书文件路径'),
notify_url: '支付回调网址',
spbill_create_ip: 'IP地址'
};
// 方式一
const api = new tenpay(config);
// 方式二
const api = tenpay.init(config);
// 调试模式(传入第二个参数为true, 可在控制台输出数据)
const api = new tenpay(config, true);
// 沙盒模式(用于微信支付验收)
const sandboxAPI = await tenpay.sandbox(config);
appid - 公众号ID(必填)mchid - 微信商户号(必填)partnerKey - 微信支付安全密钥(必填, 在微信商户管理界面获取)pfx - 证书文件(选填, 在微信商户管理界面获取)
notify_url - 支付结果通知回调地址(选填)
refund_url - 退款结果通知回调地址(选填)
spbill_create_ip - IP地址(选填)
127.0.0.1pay<支付结果通知, 默认> refund<退款结果通知> nativePay<扫码支付模式一回调>app.use(bodyParser.text({type: '*/xml'}));
// 支付结果通知/退款结果通知
router.post('/xxx', api.middlewareForExpress('pay'), (req, res) => {
let info = req.weixin;
// 业务逻辑...
// 回复消息(参数为空回复成功, 传值则为错误消息)
res.reply('错误消息' || '');
});
// 扫码支付模式一回调
router.post('/xxx', api.middlewareForExpress('nativePay'), (req, res) => {
let info = req.weixin;
// 业务逻辑和统一下单获取prepay_id...
// 响应成功或失败(第二个可选参数为输出错误信息)
res.replyNative(prepay_id, err_msg);
});
app.use(bodyParser({
enableTypes: ['json', 'form', 'text'],
extendTypes: {
text: ['text/xml', 'application/xml']
}
}));
router.post('/xxx', api.middleware('refund'), async ctx => {
let info = ctx.request.weixin;
// 业务逻辑...
// 回复消息(参数为空回复成功, 传值则为错误消息)
ctx.reply('错误消息' || '');
// 扫码支付模式一模式
ctx.replyNative(prepay_id);
});
appid mchidlet result = await api.getPayParams({
out_trade_no: '商户内部订单号',
body: '商品简单描述',
total_fee: '订单金额(分)',
openid: '付款用户的openid'
});
trade_type - JSAPI// 该方法需先调用api.unifiedOrder统一下单, 获取prepay_id;
let result = await api.getPayParamsByPrepay({
prepay_id: '预支付会话标识'
});
let result = await api.getAppParams({
out_trade_no: '商户内部订单号',
body: '商品简单描述',
total_fee: '订单金额(分)'
});
trade_type - APP// 该方法需先调用api.unifiedOrder统一下单<注意传入trade_type: 'APP'>, 获取prepay_id;
let result = await api.getAppParamsByPrepay({
prepay_id: '预支付会话标识'
});
let result = await api.getNativeUrl({
product_id: '商品ID'
});
// 使用统一下单API可直接获取code_url, 需自行生成二维码图片
let {prepay_id, code_url} = await api.unifiedOrder({
out_trade_no: '商户内部订单号',
body: '商品简单描述',
total_fee: '订单金额(分)',
openid: '用户openid',
trade_type: 'NATIVE',
product_id: '商品id'
});
let result = await api.micropay({
out_trade_no: '商户内部订单号',
body: '商品简单描述',
total_fee: '订单金额(分)',
auth_code: '授权码'
});
let result = await api.unifiedOrder({
out_trade_no: '商户内部订单号',
body: '商品简单描述',
total_fee: '订单金额(分)',
openid: '用户openid'
});
trade_type - JSAPInotify_url - 默认为初始化时传入的值或空spbill_create_ip - 默认为初始化时传入的值或127.0.0.1let result = await api.orderQuery({
// transaction_id, out_trade_no 二选一
// transaction_id: '微信的订单号',
out_trade_no: '商户内部订单号'
});
let result = await api.reverse({
// transaction_id, out_trade_no 二选一
// transaction_id: '微信的订单号',
out_trade_no: '商户内部订单号'
});
let result = await api.closeOrder({
out_trade_no: '商户内部订单号'
});
let result = await api.refund({
// transaction_id, out_trade_no 二选一
// transaction_id: '微信的订单号',
out_trade_no: '商户内部订单号',
out_refund_no: '商户内部退款单号',
total_fee: '订单金额(分)',
refund_fee: '退款金额(分)'
});
op_user_id - 默认为商户号(此字段在小程序支付文档中出现)notify_url - 默认为初始化时传入的refund_url, 无此参数则使用商户后台配置的退款通知地址let result = await api.refundQuery({
// 以下参数 四选一
// transaction_id: '微信的订单号',
// out_trade_no: '商户内部订单号',
// out_refund_no: '商户内部退款单号',
refund_id: '微信退款单号'
});
/**
* 新增一个format参数(默认: false), 用于自动转化帐单为json格式
* json.total_title: 统计数据的标题数组 - ["总交易单数","总交易额","总退款金额", ...],
* json.total_data: 统计数据的数组 - ["3", "88.00", "0.00", ...],
* json.list_title: 详细数据的标题数组 - ["交易时间","公众账号ID","商户号", ...],
* json.list_data: 详细数据的二维数据 - [["2017-12-26 19:20:39","wx12345", "12345", ...], ...]
*/
let result = await api.downloadBill({
bill_date: '账单日期'
}, true);
bill_type - ALLformat - false/**
* 新增一个format参数(默认: false), 用于自动转化帐单为json格式
* json.total_title: 统计数据的标题数组 - ["资金流水总笔数","收入笔数","收入金额", ...],
* json.total_data: 统计数据的数组 - ["20.0", "17.0", "0.35", ...],
* json.list_title: 详细数据的标题数组 - ["记账时间","微信支付业务单号","资金流水单号", ...],
* json.list_data: 详细数据的二维数据 - [["2018-02-01 04:21:23","12345", "12345", ...], ...]
*/
let result = await api.downloadFundflow({
bill_date: '账单日期'
}, true);
account_type - Basicformat - falselet result = await api.sendCoupon({
coupon_stock_id: '代金券批次id',
partner_trade_no: '商户单据号',
openid: '用户openid'
});
let result = await api.queryCouponStock({
coupon_stock_id: '代金券批次id'
});
let result = await api.queryCouponInfo({
coupon_id: '代金券id',
openid: '用户openid',
stock_id: '批次号'
});
let result = await api.transfers({
partner_trade_no: '商户内部付款订单号',
openid: '用户openid',
re_user_name: '用户真实姓名',
amount: '付款金额(分)',
desc: '企业付款描述信息'
});
check_name - FORCE_CHECKspbill_create_ip - 默认为初始化时传入的值或127.0.0.1let result = await api.transfersQuery({
partner_trade_no: '商户内部付款订单号'
});
let result = await api.payBank({
partner_trade_no: '商户内部付款订单号',
bank_code: '收款方开户行',
enc_bank_no: '收款方银行卡号',
enc_true_name: '收款方用户名',
amount: '付款金额(分)',
desc: '企业付款到银行卡描述信息'
});
let result = await api.queryBank({
partner_trade_no: '商户内部付款订单号'
});
let result = await api.sendRedpack({
// mch_billno, mch_autono 二选一
// mch_billno: '商户内部付款订单号',
mch_autono: '10位当日唯一数字, 用于自动生成mch_billno',
send_name: '商户名称',
re_openid: '用户openid',
total_amount: '红包金额(分)',
wishing: '红包祝福语',
act_name: '活动名称',
remark: '备注信息'
});
mch_billno - 商户内部订单号(传入则mch_autono失效)mch_autono - 当日10位唯一数字, 用于自动处理商户内部订单号逻辑total_num - 1client_ip - 默认为初始化时的spbill_create_ip参数值或127.0.0.1scene_id - 空, 当红包金额大于2元时必传(微信文档说明为200元, 实测为2元)let result = await api.sendGroupRedpack({
// mch_billno, mch_autono 二选一
// mch_billno: '商户内部付款订单号',
mch_autono: '10位当日唯一数字, 用于自动生成mch_billno',
send_name: '商户名称',
re_openid: '种子用户openid',
total_amount: '红包金额(分)',
wishing: '红包祝福语',
act_name: '活动名称',
remark: '备注信息'
});
mch_billno - 商户内部订单号(传入则mch_autono失效)mch_autono - 当日10位唯一数字, 用于自动处理商户内部订单号逻辑total_num - 3, 分裂红包要求值为3~20之间amt_type - ALL_RANDscene_id - 空, 当红包金额大于2元时必传(文档中未说明)api.redpackQuery({
mch_billno: '商户内部付款订单号'
});
bill_type - MCHTFAQs
微信支付 for nodejs
The npm package tiny-wechatpay receives a total of 6 weekly downloads. As such, tiny-wechatpay popularity was classified as not popular.
We found that tiny-wechatpay demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.