Installations
npm install wechat-pay
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
8.4.0
NPM Version
5.5.1
Score
69.2
Supply Chain
91.4
Quality
71.7
Maintenance
50
Vulnerability
97.3
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
supersheep
Download Statistics
Total Downloads
58,521
Last Day
15
Last Week
68
Last Month
262
Last Year
5,174
GitHub Statistics
585 Stars
66 Commits
142 Forks
37 Watching
1 Branches
9 Contributors
Bundle Size
843.60 kB
Minified
225.72 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.3.2
Package Id
wechat-pay@0.3.2
Size
7.57 kB
NPM Version
5.5.1
Node Version
8.4.0
Total Downloads
Cumulative downloads
Total Downloads
58,521
Last day
114.3%
15
Compared to previous day
Last week
-1.4%
68
Compared to previous week
Last month
-5.8%
262
Compared to previous month
Last year
-6.6%
5,174
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
5
微信支付 for Nodejs
初始化
1var Payment = require('wechat-pay').Payment; 2var initConfig = { 3 partnerKey: "<partnerkey>", 4 appId: "<appid>", 5 mchId: "<mchid>", 6 notifyUrl: "<notifyurl>", 7 pfx: fs.readFileSync("<location-of-your-apiclient-cert.p12>") 8}; 9var payment = new Payment(initConfig);
所有参数都不是必须的,不过这样配置最省事。实际调用时候的参数若有同名会覆盖。
付个钱
1var order = { 2 body: '吮指原味鸡 * 1', 3 attach: '{"部位":"三角"}', 4 out_trade_no: 'kfc' + (+new Date), 5 total_fee: 10 * 100, 6 spbill_create_ip: req.ip, 7 openid: req.user.openid, 8 trade_type: 'JSAPI' 9}; 10 11payment.getBrandWCPayRequestParams(order, function(err, payargs){ 12 res.json(payargs); 13}); 14 15// 也可以使用`async/await`形式 16// let payargs = await payment.getBrandWCPayRequestParams(order)
注:
- 页面的路径需要位于
支付授权目录
下 - 由于每次呼出支付界面,无论用户是否支付成功,out_trade_no 都会失效(OUT_TRADE_NO_USED),所以这里使用timestamp保证每次的id不同。业务逻辑中应该自行维护之
前端通过
1WeixinJSBridge.invoke('getBrandWCPayRequest', payargs, function(res){ 2 if(res.err_msg == "get_brand_wcpay_request:ok"){ 3 alert("支付成功"); 4 // 这里可以跳转到订单完成页面向用户展示 5 }else{ 6 alert("支付失败,请重试"); 7 } 8});
来呼出微信的支付界面
接收微信付款确认请求
1var middleware = require('wechat-pay').middleware; 2app.use('<notifyUrl>', middleware(initConfig).getNotify().done(function(message, req, res, next) { 3 var openid = message.openid; 4 var order_id = message.out_trade_no; 5 var attach = {}; 6 try{ 7 attach = JSON.parse(message.attach); 8 }catch(e){} 9 10 /** 11 * 查询订单,在自己系统里把订单标为已处理 12 * 如果订单之前已经处理过了直接返回成功 13 */ 14 res.reply('success'); 15 16 /** 17 * 有错误返回错误,不然微信会在一段时间里以一定频次请求你 18 * res.reply(new Error('...')) 19 */ 20}));
退个款
1payment.refund({ 2 out_trade_no: "kfc001", 3 out_refund_no: 'kfc001_refund', 4 total_fee: 10 * 100, 5 refund_fee: 10 * 100 6}, function(err, result){ 7 /** 8 * 微信收到正确的请求后会给用户退款提醒 9 * 这里一般不用处理,有需要的话有err的时候记录一下以便排查 10 */ 11});
接收退款确认请求
1var middleware = require('wechat-pay').middleware; 2app.use('<notifyUrl>', middleware(initConfig).getRefundNotify().done(function(message, req, res, next) { 3 var openid = message.openid; 4 var refund_order_id = message.out_refund_no; 5 var order_id = message.out_trade_no; 6 var attach = {}; 7 try{ 8 attach = JSON.parse(message.attach); 9 }catch(e){} 10 11 /** 12 * 查询订单,在自己系统里把订单标为已处理 13 * 如果订单之前已经处理过了直接返回成功 14 */ 15 res.reply('success'); 16 17 /** 18 * 有错误返回错误,不然微信会在一段时间里以一定频次请求你 19 * res.reply(new Error('...')) 20 */ 21}));
发红包
1payment.sendRedPacket({ 2 mch_billno: 'kfc002', 3 send_name: '肯德基', 4 re_openid: '', 5 total_amount: 10 * 100, 6 total_num: 1, 7 wishing: '祝多多吃鸡', 8 client_ip: '', 9 act_name: '吃鸡大奖赛', 10 remark: '记得吐骨头', 11 scene_id: 'PRODUCT_1' 12}, (err, result) => { 13 /** 14 * 微信收到正确的请求后会给用户发红包,用户不必关注公众号也能收到。 15 * 红包没有通知回调,有需要的话标记订单状态,和有err的时候记录一下以便排查 16 */ 17 }); 18});
查询红包状态
1payment.redPacketQuery({ 2 mch_billno: 'kfc002' 3}, (err, result) => { 4 /** 5 * 根据状态相应处理订单 6 */ 7});
企业付款
1payment.transfers({ 2 partner_trade_no: 'kfc003', 3 openid: '', 4 check_name: 'NO_CHECK', 5 amount: 10 * 100, 6 desc: '', 7 spbill_create_ip: '' 8}, (err, result) => { 9 // 根据微信文档,当返回错误码为“SYSTEMERROR”时,一定要使用原单号重试,否则可能造成重复支付等资金风险。 10});
查询历史订单
1payment.downloadBill({ 2 bill_date: "20140913", 3 bill_type: "ALL" 4}, function(err, data){ 5 // 账单列表 6 var list = data.list; 7 // 账单统计信息 8 var stat = data.stat; 9});
错误处理
在回调的Error上的以name做了区分,有需要可以拿来做判断
- ProtocolError 协议错误,看看有没有必须要传的参数没传
- BusinessError 业务错误,可以从返回的data里面看看错误细节
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 7/24 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 13 are checked with a SAST tool
Score
2.9
/10
Last Scanned on 2025-01-06
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More