Skip to content

Commit c12a7d8

Browse files
authored
doc: 调整微信支付公钥相关文档并增加ChangeLog (#320)
1 parent 98dc591 commit c12a7d8

File tree

2 files changed

+69
-28
lines changed

2 files changed

+69
-28
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# ChangeLog
2+
3+
## [0.2.15] - 2024-11-20
4+
5+
### Added
6+
7+
+ 自动填充平台证书序列号或公钥ID到请求的 `Wechatpay-Serial`
8+
9+
## [0.2.14] - 2024-08-25
10+
11+
### Added
12+
13+
+ 支持使用微信支付公钥验证微信支付应答和回调签名
14+
15+
## 【0.2.13】 - 2024-06-17
16+
17+
### Added
18+
19+
+ 支持商家转账到零钱的 `notify_url`
20+
+ 兼容 JDK17LTS

README.md

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ SDK 使用的是 unchecked exception,会抛出四种自定义异常。每种
189189
- 解析回调请求体为 JSON 字符串失败:上报监控和日志打印。
190190
- 解密回调通知内容失败:确认传入的 apiV3 密钥是否正确。
191191

192+
## 使用本地平台公钥
193+
194+
如果你的商户可使用微信支付的公钥验证应答和回调的签名,可使用微信支付公钥和公钥ID初始化。
195+
196+
```java
197+
// 可以根据实际情况使用publicKeyFromPath或publicKey加载公钥
198+
Config config =
199+
new RSAPublicKeyConfig.Builder()
200+
.merchantId(merchantId)
201+
.privateKeyFromPath(privateKeyPath)
202+
.publicKeyFromPath(publicKeyPath)
203+
.publicKeyId(publicKeyId)
204+
.merchantSerialNumber(merchantSerialNumber)
205+
.apiV3Key(apiV3Key)
206+
.build();
207+
```
208+
192209
## 自动更新微信支付平台证书
193210

194211
为确保 API 请求过程中的安全性,客户端需要使用微信支付平台证书来验证服务器响应的真实性和完整性。
@@ -233,23 +250,6 @@ Config config =
233250
.build();
234251
```
235252

236-
## 使用本地平台公钥
237-
238-
如果你的商户可使用微信支付的公钥验证应答和回调的签名,可使用微信支付公钥和公钥ID初始化。
239-
240-
```java
241-
// 可以根据实际情况使用publicKeyFromPath或publicKey加载公钥
242-
Config config =
243-
new RSAPublicKeyConfig.Builder()
244-
.merchantId(merchantId)
245-
.privateKeyFromPath(privateKeyPath)
246-
.publicKeyFromPath(publicKeyPath)
247-
.publicKeyId(publicKeyId)
248-
.merchantSerialNumber(merchantSerialNumber)
249-
.apiV3Key(apiV3Key)
250-
.build();
251-
```
252-
253253
## 回调通知
254254

255255
首先,你需要在你的服务器上创建一个公开的 HTTP 端点,接受来自微信支付的回调通知。
@@ -264,7 +264,7 @@ Config config =
264264
- HTTP 头 `Wechatpay-Nonce`。签名中的随机数。
265265
- HTTP 头 `Wechatpay-Timestamp`。签名中的时间戳。
266266
- HTTP 头 `Wechatpay-Signature-Type`。签名类型。
267-
1. 初始化 `RSAAutoCertificateConfig`。微信支付平台证书由 SDK 的自动更新平台能力提供,也可以使用本地证书。
267+
1. 初始化 `NotificationConfig`,根据回调签名选择使用微信支付公钥或者平台证书选择不同的Config
268268
1. 初始化 `NotificationParser`
269269
1. 调用 `NotificationParser.parse()` 验签、解密并将 JSON 转换成具体的通知回调对象。如果验签失败,SDK 会抛出 `ValidationException`
270270
1. 接下来可以执行你的业务逻辑了。如果执行成功,你应返回 `200 OK` 的状态码。如果执行失败,你应返回 `4xx` 或者 `5xx`的状态码,例如数据库操作失败建议返回 `500 Internal Server Error`
@@ -279,15 +279,34 @@ RequestParam requestParam = new RequestParam.Builder()
279279
.body(requestBody)
280280
.build();
281281

282-
// 如果已经初始化了 RSAAutoCertificateConfig,可直接使用
283-
// 没有的话,则构造一个
282+
// 如果已经初始化了 NotificationConfig,可直接使用
283+
// 没有的话,则构造一个。以下多种 Config 根据情况选择一种即可:
284+
285+
// 1. 如果你使用的是微信支付公私钥,则使用 RSAPublicKeyNotificationConfig
286+
NotificationConfig config = new RSAPublicKeyNotificationConfig.Builder()
287+
.publicKeyFromPath(publicKeyPath)
288+
.publicKeyId(publicKeyId)
289+
.apiV3Key(apiV3Key)
290+
.build();
291+
292+
// 2. 如果你仍在使用微信支付平台证书,则使用 RSAAutoCertificateConfig
284293
NotificationConfig config = new RSAAutoCertificateConfig.Builder()
285294
.merchantId(merchantId)
286295
.privateKeyFromPath(privateKeyPath)
287296
.merchantSerialNumber(merchantSerialNumber)
288297
.apiV3Key(apiV3Key)
289298
.build();
290299

300+
// 3. 如果你正在进行微信支付平台证书到微信支付公私钥的灰度切换,希望保持切换兼容,则使用 RSACombinedNotificationConfig
301+
NotificationConfig config = new RSACombinedNotificationConfig.Builder()
302+
.merchantId(merchantId)
303+
.privateKeyFromPath(privateKeyPath)
304+
.merchantSerialNumber(merchantSerialNumber)
305+
.publicKeyFromPath(wechatpayPublicKeyPath)
306+
.publicKeyId(wechatpayPublicKeyId)
307+
.apiV3Key(apiV3Key)
308+
.build();
309+
291310
// 初始化 NotificationParser
292311
NotificationParser parser = new NotificationParser(config);
293312

@@ -371,24 +390,26 @@ inputStream.close();
371390

372391
如果 SDK 尚未支持某个接口,你可以使用 [cipher](core/src/main/java/com/wechat/pay/java/core/cipher) 中的 `RSAPrivacyEncryptor``RSAPrivacyDecryptor` ,手动对敏感信息加解密。
373392

374-
当你使用自动获取的微信支付平台证书时,可以通过以下方法获取加密器 `PrivacyEncryptor`,以及对应的证书序列号。
393+
当你使用本地的微信支付平台证书或者微信支付公钥,可以通过以下方法直接构建加密器 `PrivacyEncryptor`
375394

376395
```java
377-
PrivacyEncryptor encryptor = config.createEncryptor();
378-
String wechatPayCertificateSerialNumber = encryptor.getWechatpaySerial();
396+
// 平台证书中的公钥 或者 微信支付公钥
397+
PublicKey wechatPayPublicKey = null;
398+
String plaintext = "";
399+
PrivacyEncryptor encryptor = new RSAPrivacyEncryptor(wechatPayPublicKey);
379400
String ciphertext = encryptor.encryptToString(plaintext);
380401
```
381402

382-
当你使用本地的公钥或私钥,可以通过以下方法直接构建加密器 `PrivacyEncryptor` 和解密器 `PrivacyDecryptor`
403+
当你使用自动获取的微信支付平台证书时,可以通过以下方法获取加密器 `PrivacyEncryptor`,以及对应的证书序列号
383404

384405
```java
385-
// 微信支付平台证书中的公钥
386-
PublicKey wechatPayPublicKey = null;
387-
String plaintext = "";
388-
PrivacyEncryptor encryptor = new RSAPrivacyEncryptor(wechatPayPublicKey);
406+
PrivacyEncryptor encryptor = config.createEncryptor();
407+
String wechatPayCertificateSerialNumber = encryptor.getWechatpaySerial();
389408
String ciphertext = encryptor.encryptToString(plaintext);
390409
```
391410

411+
你可以使用如下方法加载本地私钥来构建解密器 `PrivacyDecryptor`
412+
392413
```java
393414
// 商户私钥
394415
PrivateKey merchantPrivateKey = null;

0 commit comments

Comments
 (0)