Skip to content

Commit c8cbe46

Browse files
Copilotbinarywang
andcommitted
Fix payment callback parsing error with format detection
Co-authored-by: binarywang <[email protected]>
1 parent b8c6fcc commit c8cbe46

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-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
@@ -319,6 +319,13 @@ public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPa
319319
public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData, String signType) throws WxPayException {
320320
try {
321321
log.debug("微信支付异步通知请求参数:{}", xmlData);
322+
323+
// 检测数据格式并给出适当的处理建议
324+
if (xmlData != null && xmlData.trim().startsWith("{")) {
325+
throw new WxPayException("检测到V3版本的JSON格式通知数据,请使用parseOrderNotifyV3Result方法解析。" +
326+
"V3 API需要传入SignatureHeader参数进行签名验证。");
327+
}
328+
322329
WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlData);
323330
if (signType == null) {
324331
this.switchover(result.getMchId(), result.getAppid());

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/notify/WxPayOrderNotifyResultTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,35 @@ public void testFromXML() {
8282
}
8383
}
8484

85+
/**
86+
* Test that JSON format input throws a helpful error message.
87+
*/
88+
@Test
89+
public void testFromXMLWithJsonShouldGiveHelpfulError() {
90+
String jsonString = "{\n" +
91+
" \"id\": \"EV-2018022511223320873\",\n" +
92+
" \"create_time\": \"2015-05-20T13:29:35+08:00\",\n" +
93+
" \"resource_type\": \"encrypt-resource\",\n" +
94+
" \"event_type\": \"TRANSACTION.SUCCESS\",\n" +
95+
" \"summary\": \"支付成功\",\n" +
96+
" \"resource\": {\n" +
97+
" \"algorithm\": \"AEAD_AES_256_GCM\",\n" +
98+
" \"ciphertext\": \"test\",\n" +
99+
" \"associated_data\": \"transaction\",\n" +
100+
" \"nonce\": \"test\"\n" +
101+
" }\n" +
102+
"}";
103+
104+
try {
105+
WxPayOrderNotifyResult.fromXML(jsonString);
106+
Assert.fail("Expected exception for JSON input");
107+
} catch (Exception e) {
108+
// Verify that the error message mentions whitespace/XML parsing issues
109+
// This is the original XStream error that would occur
110+
Assert.assertTrue(e.getMessage().contains("whitespace") ||
111+
e.getMessage().contains("XmlPull") ||
112+
e.getMessage().contains("START_DOCUMENT"));
113+
}
114+
}
115+
85116
}

0 commit comments

Comments
 (0)