Skip to content

Commit

Permalink
feat: wechat pay (#30)
Browse files Browse the repository at this point in the history
* feat: 配置 dockerfile

* feat: wechat pay
  • Loading branch information
thinkasany authored Oct 29, 2023
1 parent 9508824 commit 4570e49
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
7 changes: 6 additions & 1 deletion docs/nest/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@

# 拓展

💄 目前额外拓展了 ali-oss,alipay 的内容
目前额外拓展的内容如下:

- 💄 [ali-oss](https://thinkasany.github.io/docs/#/docs/nest/24/24?id=ali-oss)
- 💄 [ali-pay](https://thinkasany.github.io/docs/#/docs/nest/pay/1)
- 💄 [wechat-pay](https://thinkasany.github.io/docs/#/docs/nest/pay/wechat)
- 💄 [wechat-pay](https://thinkasany.github.io/docs/#/docs/nest/sso/1)
Binary file added docs/nest/pay/images/2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions docs/nest/pay/wechat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
[源码](https://github.com/thinkasany/nestjs-course-code/tree/master/%E6%94%AF%E4%BB%98/node-wechat/server)

# 实现效果

<img src="/docs/nest/pay/images/2.jpeg" style="width: 200px; height: 300px;" />

# core

github: https://github.com/yjiewei/payment-demo 感谢大哥的仓库和尚硅谷提供的账号

主要用的是微信 v3 的支付,总所周知,最头疼的问题就是商家账号的事情了这边为大家提供了 pem 和 商户账号信息,不知道啥时候回过期,反正留下了截图也了解了总体的过程就好了。

接口请求发送成功返回的url,在微信打开就是上面的实现效果

```
weixin://wxpay/bizpayurl?pr=dDeABFRzz
```

# 核心配置

```
const order = {
appid: "wx74862e0dfcf69954", // appid
mchid: "1558950191", // 商户号
description: "测试支付",
out_trade_no: "wzm20210209111",
amount: {
total: 1
},
notify_url: "https://xxx.cn/"
};
v3Pay(order, "34345964330B66427E0D3D28826C4993C77E631F"); // 证书编号,在商户后台申请证书后会有编号
```

# 代码

```
import express from "express";
const app = new express();
import axios from "axios";
import { readFileSync } from "fs";
import { createSign as _createSign } from "crypto";
/**
* 生成随机字符串
* @param {number} len 字符串长度
*/
function createRandomString(len) {
const data = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
let str = "";
for (let i = 0; i < len; i++) {
str += data.charAt(Math.floor(Math.random() * data.length));
}
return str;
}
/**
* 微信支付v3 签名生成
* @param {string} method 请求方法
* @param {string} url
* @param {number} timestamp 时间戳 秒级
* @param {string} nonce_str 随机字符串
* @param {Object} order 主体信息
*/
function createSign(method, url, timestamp, nonce_str, order) {
const signStr = `${method}\n${url}\n${timestamp}\n${nonce_str}\n${JSON.stringify(order)}\n`;
const cert = readFileSync("./apiclient_key.pem", "utf-8");
const sign = _createSign("RSA-SHA256");
sign.update(signStr);
return sign.sign(cert, "base64");
}
/**
* 微信支付v3
* @param {Object} order 订单信息
*/
function v3Pay(order, serial_no) {
const timestamp = Math.floor(new Date().getTime() / 1000);
const nonce_str = createRandomString(32);
const signature = createSign(
"POST",
"/v3/pay/transactions/native",
timestamp,
nonce_str,
order
);
const Authorization = `WECHATPAY2-SHA256-RSA2048 mchid="${order.mchid}",nonce_str="${nonce_str}",timestamp="${timestamp}",signature="${signature}",serial_no="${serial_no}"`;
console.log(Authorization);
axios
.post("https://api.mch.weixin.qq.com/v3/pay/transactions/native", order, {
headers: { Authorization }
})
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});
}
/**
* github: https://github.com/yjiewei/payment-demo 感谢大哥的仓库和尚硅谷提供的账号
*/
const order = {
appid: "wx74862e0dfcf69954", // appid
mchid: "1558950191", // 商户号
description: "测试支付",
out_trade_no: "wzm20210209111",
amount: {
total: 1
},
notify_url: "https://xxx.cn/"
};
v3Pay(order, "34345964330B66427E0D3D28826C4993C77E631F"); // 证书编号,在商户后台申请证书后会有编号
app.listen(3000);
```

<!-- -->
5 changes: 3 additions & 2 deletions docs/summary.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
- Nest 通关秘籍
- [前言](/docs/nest/index.md)
- [pay-alipay-sendbox](/docs/nest/pay/1.md)
- [pay-wechat](/docs/nest/pay/wechat.md)
- [sso-单点登录](/docs/nest/sso/1.md)
- [5.IOC 解决了什么痛点问题?](/docs/nest/5/5.md)
- [7.使用多种 Provider,灵活注入对象](/docs/nest/7/1.md)
- [9.AOP 架构有什么好处?🔥](/docs/nest/9/1.md)
Expand All @@ -23,6 +26,4 @@
- [54.MySQL + TypeORM + JWT 实现登录注册](/docs/nest/54/54.md)
- [55.基于 ACL 实现权限控制](/docs/nest/55/55.md)
- [56.基于 RBAC 实现权限控制](/docs/nest/56/56.md)
- [pay](/docs/nest/pay/1.md)
- [sso](/docs/nest/sso/1.md)
- [todo...](/docs/typeorm/1.md)

0 comments on commit 4570e49

Please sign in to comment.