{"id":23246267,"url":"https://github.com/tvrcgo/weixin-pay","last_synced_at":"2025-05-15T20:02:49.093Z","repository":{"id":29385316,"uuid":"32920326","full_name":"tvrcgo/weixin-pay","owner":"tvrcgo","description":"微信支付 Nodejs SDK","archived":false,"fork":false,"pushed_at":"2023-09-01T02:37:03.000Z","size":100,"stargazers_count":922,"open_issues_count":17,"forks_count":241,"subscribers_count":54,"default_branch":"master","last_synced_at":"2025-04-11T23:15:02.715Z","etag":null,"topics":["nodejs","payment","wechat","weixin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tvrcgo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-03-26T10:12:41.000Z","updated_at":"2025-04-09T08:28:01.000Z","dependencies_parsed_at":"2024-06-18T15:31:43.990Z","dependency_job_id":"4823b1f0-0ff1-4124-825d-18141636d5d4","html_url":"https://github.com/tvrcgo/weixin-pay","commit_stats":{"total_commits":17,"total_committers":2,"mean_commits":8.5,"dds":"0.11764705882352944","last_synced_commit":"134277b6ef17d70141e5e15f87cf9cb5c04214dd"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvrcgo%2Fweixin-pay","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvrcgo%2Fweixin-pay/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvrcgo%2Fweixin-pay/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvrcgo%2Fweixin-pay/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tvrcgo","download_url":"https://codeload.github.com/tvrcgo/weixin-pay/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248492884,"owners_count":21113163,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs","payment","wechat","weixin"],"created_at":"2024-12-19T07:13:52.266Z","updated_at":"2025-04-11T23:15:07.888Z","avatar_url":"https://github.com/tvrcgo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# weixin-pay\r\n微信支付 for node.js\r\n\r\n[![npm version](https://badge.fury.io/js/weixin-pay.svg)](http://badge.fury.io/js/weixin-pay)\r\n\r\n## Installation\r\n```\r\nnpm install weixin-pay\r\n```\r\n\r\n## Usage\r\n\r\n创建统一支付订单\r\n```js\r\nvar WXPay = require('weixin-pay');\r\n\r\nvar wxpay = WXPay({\r\n\tappid: 'xxxxxxxx',\r\n\tmch_id: '1234567890',\r\n\tpartner_key: 'xxxxxxxxxxxxxxxxx', //微信商户平台API密钥\r\n\tpfx: fs.readFileSync('./wxpay_cert.p12'), //微信商户平台证书\r\n});\r\n\r\nwxpay.createUnifiedOrder({\r\n\tbody: '扫码支付测试',\r\n\tout_trade_no: '20140703'+Math.random().toString().substr(2, 10),\r\n\ttotal_fee: 1,\r\n\tspbill_create_ip: '192.168.2.210',\r\n\tnotify_url: 'http://wxpay_notify_url',\r\n\ttrade_type: 'NATIVE',\r\n\tproduct_id: '1234567890'\r\n}, function(err, result){\r\n\tconsole.log(result);\r\n});\r\n```\r\n\r\n查询订单\r\n```js\r\n// 通过微信订单号查\r\nwxpay.queryOrder({ transaction_id:\"xxxxxx\" }, function(err, order){\r\n\tconsole.log(order);\r\n});\r\n\r\n// 通过商户订单号查\r\nwxpay.queryOrder({ out_trade_no:\"xxxxxx\" }, function(err, order){\r\n\tconsole.log(order);\r\n});\r\n```\r\n\r\n关闭订单\r\n```js\r\nwxpay.closeOrder({ out_trade_no:\"xxxxxx\"}, function(err, result){\r\n\tconsole.log(result);\r\n});\r\n```\r\n退款接口\r\n```js\r\nvar params = {\r\n\tappid: 'xxxxxxxx',\r\n\tmch_id: '1234567890',\r\n    op_user_id: '商户号即可',\r\n    out_refund_no: '20140703'+Math.random().toString().substr(2, 10),\r\n    total_fee: '1', //原支付金额\r\n    refund_fee: '1', //退款金额\r\n    transaction_id: '微信订单号'\r\n};\r\n\r\nwxpay.refund(params, function(err, result){\r\n    console.log('refund', arguments);\r\n});\r\n```\r\n\r\n### 原生支付 (NATIVE)\r\n\r\n#### 模式一\r\n\r\n提供一个生成支付二维码链接的函数，把url生成二维码给用户扫。\r\n\r\n```js\r\nvar url = wxpay.createMerchantPrepayUrl({ product_id: '123456' });\r\n```\r\n\r\n商户后台收到微信的回调之后，调用 createUnifiedOrder() 生成预支付交易单，将结果的XML数据返回给微信。\r\n\r\n[什么是模式一？](http://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_4)\r\n\r\n#### 模式二\r\n\r\n直接调用 createUnifiedOrder() 函数生成预支付交易单，将结果中的 code_url 生成二维码给用户扫。\r\n\r\n[什么是模式二？](http://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5)\r\n\r\n### 公众号支付 (JS API)\r\n\r\n生成JS API支付参数，发给页面\r\n```js\r\nwxpay.getBrandWCPayRequestParams({\r\n\topenid: '微信用户 openid',\r\n\tbody: '公众号支付测试',\r\n    detail: '公众号支付测试',\r\n\tout_trade_no: '20150331'+Math.random().toString().substr(2, 10),\r\n\ttotal_fee: 1,\r\n\tspbill_create_ip: '192.168.2.210',\r\n\tnotify_url: 'http://wxpay_notify_url'\r\n}, function(err, result){\r\n\t// in express\r\n    res.render('wxpay/jsapi', { payargs:result })\r\n});\r\n```\r\n\r\n网页调用参数（以ejs为例）\r\n```js\r\nWeixinJSBridge.invoke(\r\n\t\"getBrandWCPayRequest\", \u003c%-JSON.stringify(payargs)%\u003e, function(res){\r\n\t\tif(res.err_msg == \"get_brand_wcpay_request:ok\" ) {\r\n    \t\t// success\r\n    \t}\r\n});\r\n```\r\n\r\n### 中间件\r\n\r\n商户服务端处理微信的回调（express为例）\r\n```js\r\n// 原生支付回调\r\nrouter.use('/wxpay/native/callback', wxpay.useWXCallback(function(msg, req, res, next){\r\n\t// msg: 微信回调发送的数据\r\n}));\r\n\r\n// 支付结果异步通知\r\nrouter.use('/wxpay/notify', wxpay.useWXCallback(function(msg, req, res, next){\r\n\t// 处理商户业务逻辑\r\n\r\n    // res.success() 向微信返回处理成功信息，res.fail()返回失败信息。\r\n    res.success();\r\n}));\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftvrcgo%2Fweixin-pay","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftvrcgo%2Fweixin-pay","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftvrcgo%2Fweixin-pay/lists"}