Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 22, 2025

修复当开发者使用微信支付V3 API的JSON格式通知数据调用parseOrderNotifyResult方法时出现的解析错误问题。

问题描述:
在使用Spring Boot 3.4.2配合WxJava 4.7.7.B时,调用parseOrderNotifyResult处理V3版本的JSON通知数据会报错:

XmlPullParserException: only whitespace content allowed before start tag and not { (position: START_DOCUMENT seen {... @1:2)

错误原因:

  • V3 API的通知数据为JSON格式,结构如:{"id": "...", "resource_type": "encrypt-resource", ...}
  • V2 API期望的是XML格式,结构如:<xml><appid>...</appid>...</xml>
  • 该方法尝试将JSON解析为XML,导致XStream解析器报错

解决方案:
parseOrderNotifyResult方法中添加数据格式检测逻辑:

  1. 检测输入数据是否以{开头(JSON格式)
  2. 若检测到JSON格式,抛出带有明确指导信息的WxPayException

    "检测到V3版本的JSON格式通知数据,请使用parseOrderNotifyV3Result方法解析。V3 API需要传入SignatureHeader参数进行签名验证。"

  3. 保持对XML数据的完全向后兼容

使用示例:

// 修改前:抛出晦涩的XmlPullParserException异常
// 修改后:抛出清晰的指导信息
public Result notify(@RequestBody String notifyData) {
    try {
        // 现在会对JSON数据给出明确的错误提示
        final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(notifyData);
    } catch (WxPayException e) {
        // 错误信息会指引使用parseOrderNotifyV3Result方法处理V3 API
    }
}

// V3版本JSON通知的正确用法:
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);
}

此改动为从V2迁移到V3微信支付API的开发者提供了即时帮助,将晦涩的解析错误替换为可操作的指导信息。

修复 #3699


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copilot AI changed the title [WIP] 支付回调结果解析报错 Fix payment callback parsing error with helpful format detection Sep 22, 2025
Copilot AI requested a review from binarywang September 22, 2025 16:42
Copilot finished work on behalf of binarywang September 22, 2025 16:42
@binarywang binarywang added this to the 4.7.9 milestone Sep 24, 2025
@binarywang binarywang modified the milestones: 4.7.9, 4.8.0 Nov 27, 2025
@binarywang
Copy link
Owner

@copilot 请用中文改下描述文字

Copy link
Contributor Author

Copilot AI commented Nov 28, 2025

@copilot 请用中文改下描述文字

已将PR描述改为中文。

Copilot AI changed the title Fix payment callback parsing error with helpful format detection 修复支付回调结果解析报错问题 Nov 28, 2025
Copilot finished work on behalf of binarywang November 28, 2025 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants