@@ -189,6 +189,23 @@ SDK 使用的是 unchecked exception,会抛出四种自定义异常。每种
189
189
- 解析回调请求体为 JSON 字符串失败:上报监控和日志打印。
190
190
- 解密回调通知内容失败:确认传入的 apiV3 密钥是否正确。
191
191
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
+
192
209
## 自动更新微信支付平台证书
193
210
194
211
为确保 API 请求过程中的安全性,客户端需要使用微信支付平台证书来验证服务器响应的真实性和完整性。
@@ -233,23 +250,6 @@ Config config =
233
250
.build();
234
251
```
235
252
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
-
253
253
## 回调通知
254
254
255
255
首先,你需要在你的服务器上创建一个公开的 HTTP 端点,接受来自微信支付的回调通知。
@@ -264,7 +264,7 @@ Config config =
264
264
- HTTP 头 ` Wechatpay-Nonce ` 。签名中的随机数。
265
265
- HTTP 头 ` Wechatpay-Timestamp ` 。签名中的时间戳。
266
266
- HTTP 头 ` Wechatpay-Signature-Type ` 。签名类型。
267
- 1 . 初始化 ` RSAAutoCertificateConfig ` 。微信支付平台证书由 SDK 的自动更新平台能力提供,也可以使用本地证书。
267
+ 1 . 初始化 ` NotificationConfig ` ,根据回调签名选择使用微信支付公钥或者平台证书选择不同的Config
268
268
1 . 初始化 ` NotificationParser ` 。
269
269
1 . 调用 ` NotificationParser.parse() ` 验签、解密并将 JSON 转换成具体的通知回调对象。如果验签失败,SDK 会抛出 ` ValidationException ` 。
270
270
1 . 接下来可以执行你的业务逻辑了。如果执行成功,你应返回 ` 200 OK ` 的状态码。如果执行失败,你应返回 ` 4xx ` 或者 ` 5xx ` 的状态码,例如数据库操作失败建议返回 ` 500 Internal Server Error ` 。
@@ -279,15 +279,34 @@ RequestParam requestParam = new RequestParam.Builder()
279
279
.body(requestBody)
280
280
.build();
281
281
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
284
293
NotificationConfig config = new RSAAutoCertificateConfig .Builder ()
285
294
.merchantId(merchantId)
286
295
.privateKeyFromPath(privateKeyPath)
287
296
.merchantSerialNumber(merchantSerialNumber)
288
297
.apiV3Key(apiV3Key)
289
298
.build();
290
299
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
+
291
310
// 初始化 NotificationParser
292
311
NotificationParser parser = new NotificationParser (config);
293
312
@@ -371,24 +390,26 @@ inputStream.close();
371
390
372
391
如果 SDK 尚未支持某个接口,你可以使用 [ cipher] ( core/src/main/java/com/wechat/pay/java/core/cipher ) 中的 ` RSAPrivacyEncryptor ` 和 ` RSAPrivacyDecryptor ` ,手动对敏感信息加解密。
373
392
374
- 当你使用自动获取的微信支付平台证书时,可以通过以下方法获取加密器 ` PrivacyEncryptor ` ,以及对应的证书序列号。
393
+ 当你使用本地的微信支付平台证书或者微信支付公钥,可以通过以下方法直接构建加密器 ` PrivacyEncryptor `
375
394
376
395
``` 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);
379
400
String ciphertext = encryptor. encryptToString(plaintext);
380
401
```
381
402
382
- 当你使用本地的公钥或私钥,可以通过以下方法直接构建加密器 ` PrivacyEncryptor ` 和解密器 ` PrivacyDecryptor ` 。
403
+ 当你使用自动获取的微信支付平台证书时,可以通过以下方法获取加密器 ` PrivacyEncryptor ` ,以及对应的证书序列号 。
383
404
384
405
``` 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();
389
408
String ciphertext = encryptor. encryptToString(plaintext);
390
409
```
391
410
411
+ 你可以使用如下方法加载本地私钥来构建解密器 ` PrivacyDecryptor `
412
+
392
413
``` java
393
414
// 商户私钥
394
415
PrivateKey merchantPrivateKey = null ;
0 commit comments