Skip to content

Commit b601b55

Browse files
authored
🎨 支付回调结果解析时提供必要的检测报错判断
1 parent 40428bc commit b601b55

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPa
344344
public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData, String signType) throws WxPayException {
345345
try {
346346
log.debug("微信支付异步通知请求参数:{}", xmlData);
347+
348+
// 检测数据格式并给出适当的处理建议
349+
if (xmlData != null && xmlData.trim().startsWith("{")) {
350+
throw new WxPayException("检测到V3版本的JSON格式通知数据,请使用parseOrderNotifyV3Result方法解析。" +
351+
" V3 API需要传入SignatureHeader参数进行签名验证。");
352+
}
353+
347354
WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlData);
348355
if (signType == null) {
349356
this.switchover(result.getMchId(), result.getAppid());

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,42 @@ public void testParseOrderNotifyResult() throws Exception {
627627

628628
}
629629

630+
/**
631+
* Test parse order notify result with JSON format should give helpful error.
632+
* 测试当传入V3版本的JSON格式通知数据时,应该抛出清晰的错误提示
633+
*
634+
* @throws Exception the exception
635+
*/
636+
@Test
637+
public void testParseOrderNotifyResultWithJsonShouldGiveHelpfulError() throws Exception {
638+
String jsonString = "{\n" +
639+
" \"id\": \"EV-2018022511223320873\",\n" +
640+
" \"create_time\": \"2015-05-20T13:29:35+08:00\",\n" +
641+
" \"resource_type\": \"encrypt-resource\",\n" +
642+
" \"event_type\": \"TRANSACTION.SUCCESS\",\n" +
643+
" \"summary\": \"支付成功\",\n" +
644+
" \"resource\": {\n" +
645+
" \"algorithm\": \"AEAD_AES_256_GCM\",\n" +
646+
" \"ciphertext\": \"test\",\n" +
647+
" \"associated_data\": \"transaction\",\n" +
648+
" \"nonce\": \"test\"\n" +
649+
" }\n" +
650+
"}";
651+
652+
try {
653+
this.payService.parseOrderNotifyResult(jsonString);
654+
fail("Expected WxPayException for JSON input");
655+
} catch (WxPayException e) {
656+
// 验证错误消息包含V3版本和parseOrderNotifyV3Result方法的指导信息
657+
String message = e.getMessage();
658+
assertTrue(message.contains("V3版本"), "错误消息应包含'V3版本'");
659+
assertTrue(message.contains("JSON格式"), "错误消息应包含'JSON格式'");
660+
assertTrue(message.contains("parseOrderNotifyV3Result"), "错误消息应包含'parseOrderNotifyV3Result'方法名");
661+
assertTrue(message.contains("SignatureHeader"), "错误消息应包含'SignatureHeader'");
662+
log.info("JSON格式检测正常,错误提示: {}", message);
663+
}
664+
}
665+
630666
/**
631667
* Test get wx api data.
632668
*

0 commit comments

Comments
 (0)