WxJava
WxJava copied to clipboard
Fix payment callback parsing error with helpful format detection
The parseOrderNotifyResult method was failing with a cryptic XmlPullParserException when developers passed JSON data from WeChat Pay V3 API instead of XML data expected by the V2 API method.
Problem:
When using Spring Boot 3.4.2 with WxJava 4.7.7.B, calling parseOrderNotifyResult with V3 JSON notification data resulted in:
XmlPullParserException: only whitespace content allowed before start tag and not { (position: START_DOCUMENT seen {... @1:2)
This error occurs because:
- V3 API sends notifications in JSON format with structure like
{"id": "...", "resource_type": "encrypt-resource", ...} - V2 API expects XML format like
<xml><appid>...</appid>...</xml> - The method attempts to parse JSON as XML, causing the XStream parser to fail
Solution:
Added format detection logic to parseOrderNotifyResult method that:
- Detects when input data starts with
{(JSON format) - Throws a helpful
WxPayExceptionwith clear guidance:"检测到V3版本的JSON格式通知数据,请使用parseOrderNotifyV3Result方法解析。V3 API需要传入SignatureHeader参数进行签名验证。"
- Maintains full backward compatibility for XML data
Usage Example:
// Before: Cryptic XmlPullParserException
// After: Clear guidance message
public Result notify(@RequestBody String notifyData) {
try {
// This now provides helpful error for JSON data
final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(notifyData);
} catch (WxPayException e) {
// Clear message directing to use parseOrderNotifyV3Result for V3 API
}
}
// Correct usage for V3 JSON notifications:
public Result notifyV3(@RequestBody String notifyData, HttpServletRequest request) {
SignatureHeader header = new SignatureHeader(
request.getHeader("Wechatpay-Timestamp"),
request.getHeader("Wechatpay-Nonce"),
request.getHeader("Wechatpay-Signature"),
request.getHeader("Wechatpay-Serial")
);
final WxPayNotifyV3Result notifyResult = wxPayService.parseOrderNotifyV3Result(notifyData, header);
}
This change provides immediate value to developers migrating from V2 to V3 WeChat Pay APIs by replacing confusing parser errors with actionable guidance.
Fixes #3699.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.