Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPa
public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData, String signType) throws WxPayException {
try {
log.debug("微信支付异步通知请求参数:{}", xmlData);

// 检测数据格式并给出适当的处理建议
if (xmlData != null && xmlData.trim().startsWith("{")) {
throw new WxPayException("检测到V3版本的JSON格式通知数据,请使用parseOrderNotifyV3Result方法解析。" +
" V3 API需要传入SignatureHeader参数进行签名验证。");
}

WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlData);
if (signType == null) {
this.switchover(result.getMchId(), result.getAppid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,42 @@ public void testParseOrderNotifyResult() throws Exception {

}

/**
* Test parse order notify result with JSON format should give helpful error.
* 测试当传入V3版本的JSON格式通知数据时,应该抛出清晰的错误提示
*
* @throws Exception the exception
*/
@Test
public void testParseOrderNotifyResultWithJsonShouldGiveHelpfulError() throws Exception {
String jsonString = "{\n" +
" \"id\": \"EV-2018022511223320873\",\n" +
" \"create_time\": \"2015-05-20T13:29:35+08:00\",\n" +
" \"resource_type\": \"encrypt-resource\",\n" +
" \"event_type\": \"TRANSACTION.SUCCESS\",\n" +
" \"summary\": \"支付成功\",\n" +
" \"resource\": {\n" +
" \"algorithm\": \"AEAD_AES_256_GCM\",\n" +
" \"ciphertext\": \"test\",\n" +
" \"associated_data\": \"transaction\",\n" +
" \"nonce\": \"test\"\n" +
" }\n" +
"}";

try {
this.payService.parseOrderNotifyResult(jsonString);
fail("Expected WxPayException for JSON input");
} catch (WxPayException e) {
// 验证错误消息包含V3版本和parseOrderNotifyV3Result方法的指导信息
String message = e.getMessage();
assertTrue(message.contains("V3版本"), "错误消息应包含'V3版本'");
assertTrue(message.contains("JSON格式"), "错误消息应包含'JSON格式'");
assertTrue(message.contains("parseOrderNotifyV3Result"), "错误消息应包含'parseOrderNotifyV3Result'方法名");
assertTrue(message.contains("SignatureHeader"), "错误消息应包含'SignatureHeader'");
log.info("JSON格式检测正常,错误提示: {}", message);
}
}

/**
* Test get wx api data.
*
Expand Down