{"id":21635067,"url":"https://github.com/yenche123/tcb-wxpay","last_synced_at":"2025-03-18T22:42:04.506Z","repository":{"id":160497579,"uuid":"339258476","full_name":"yenche123/tcb-wxpay","owner":"yenche123","description":"Serverless Cloud Function in TCB with WeChat-Pay","archived":false,"fork":false,"pushed_at":"2021-02-16T03:12:12.000Z","size":279,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-25T00:24:10.984Z","etag":null,"topics":["cloudbase","scf","serverless","tcb","tecent-tcf","tencent-serverless","tenpay","wechat","wechat-miniprogram","wechat-pay","wexinpay"],"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/yenche123.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":"2021-02-16T01:52:21.000Z","updated_at":"2021-02-16T03:12:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"a1b62b60-eb6c-4e96-8e36-9a6c4af79394","html_url":"https://github.com/yenche123/tcb-wxpay","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yenche123%2Ftcb-wxpay","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yenche123%2Ftcb-wxpay/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yenche123%2Ftcb-wxpay/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yenche123%2Ftcb-wxpay/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yenche123","download_url":"https://codeload.github.com/yenche123/tcb-wxpay/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244320322,"owners_count":20434090,"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":["cloudbase","scf","serverless","tcb","tecent-tcf","tencent-serverless","tenpay","wechat","wechat-miniprogram","wechat-pay","wexinpay"],"created_at":"2024-11-25T03:19:24.917Z","updated_at":"2025-03-18T22:42:04.477Z","avatar_url":"https://github.com/yenche123.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tcb-wxpay\n\ncloudbase function + tenpay\n\n使用腾讯云开发云函数实现的[微信支付apiV3](https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_0.shtml)，小程序支付通道（非腾讯云开发云调用的微信支付）\n\n在微信小程序里使用微信支付，多数人首选腾讯云开发原生的[云调用能力 - 微信支付](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/wechatpay/wechatpay.html)，以腾讯云开发作为服务商提供的微信支付方案。\n\n然而，实测后发现，支付后的支付凭证上少了一个常见的入口`商家小程序`，看得很难受，如下图所示。\n\n\u003cimg src=\"./doc/img1.jpg\" width=\"256\" /\u003e\n\n另外，star数排名靠前的[第三方开源的微信支付方案](https://github.com/befinal/node-tenpay)，仍然使用的是apiV2，基于安全和适应新特性的考量，也没有被考虑。\n\n为了在支付凭证上看到`商家小程序`入口，于是自己搭了本仓库所示的支付链路，其中包括通信参数和两道签名的关卡。最后实现了基于腾讯云函数实现的微信支付apiV3统一下单，得到如愿以偿的支付凭证如下图所示。\n\n\u003cimg src=\"./doc/img2.jpg\" width=\"256\" /\u003e\n\n\n\u003cbr /\u003e\n\n## 前置工作\n\n1. 有一个微信小程序账号 https://mp.weixin.qq.com/\n\n2. 有一个微信商户号 https://pay.weixin.qq.com/\n\n3. 依照微信支付`接入前准备`操作 https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_1.shtml\n\n\n\u003cbr /\u003e\n\n## 文件解释\n\n支付时，需要一个后端服务器和微信支付服务器发起[统一下单](https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml)，取得`prepay_id`，接着对其签名获得`paySign`，再将包裹传回前端，以获得发起`wx.requestPayment()`所需的参数。\n\n本仓库下 `cloudFuncs/` 目录下就是核心代码。其中\n\n-  `requestOrder/` 表示统一下单的逻辑\n-  `receiveOrderResult/` 用于接收支付回调的云函数，需要一个 `return {errcode: 0}` 回传给微信支付\n\n\n### 开启Http访问\n\n用于接收支付回调的云函数需要至[云开发控制台](https://www.cloudbase.net/)开启Http访问服务，得到一个可http访问`receiveOrderResult`云函数的url，再将此url填入 `requestOrder/index.js` 里的 `NOTIFY_URL` 中\n\n\u003cimg src=\"./doc/img3.png\" /\u003e\n\n\n### 替换证书和密钥\n\n在 `requestOrder/certKey` 文件夹下存放了证书和密钥，用于签名方案`SHA256 with RSA`\n\n请按照`接入前准备 - 下载并配置商户证书` 操作 https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_1.shtml#part-5\n\n生成 `apiclient_cert.pem`和`apiclient_key.pem`，并替换掉 `requestOrder/certKey` 文件夹下的文件。\n\n\n### 安装依赖\n\n在 `requestOrder`节点下，安装依赖\n```shell\nnpm install\n```\n\n\n\u003cbr /\u003e\n\n## 在小程序里使用\n\n```javascript\nwx.cloud.callFunction({\n    name: \"requestOrder\",\n    data: {fee: 200},  //单位为“分”\n    success(res) {\n        console.log(\"回调结果...\")\n        console.log(res)\n        let result = res.result || {}\n        let payment = result.payment\n        if(payment) {\n            console.log(\"发起支付...\")\n            wx.requestPayment({\n                ...payment,\n                success(res2) {},\n                fail(err2) {},\n            })\n        }\n    },\n    fail(err) {\n        console.log(err)\n    }\n})\n\n```\n\n\n\u003cbr /\u003e\n\n## License\nThe project is released under the [MIT license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyenche123%2Ftcb-wxpay","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyenche123%2Ftcb-wxpay","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyenche123%2Ftcb-wxpay/lists"}