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);
}

主要改动

  • 修改文件weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java
  • 修改方法parseOrderNotifyResult(String xmlData, String signType)
  • 改动内容:在XML解析前添加JSON格式检测逻辑
  • 测试文件:在BaseWxPayServiceImplTest.java中添加JSON格式检测测试

验证

  • ✅ 代码编译通过
  • ✅ 测试代码编译通过
  • ✅ 代码风格检查通过(mvn checkstyle:check
  • ✅ 保持向后兼容,XML解析功能不受影响
  • ✅ 测试验证service方法正确检测JSON格式并抛出有帮助的错误消息

修复 #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
@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
@binarywang binarywang marked this pull request as ready for review December 21, 2025 12:28
Copilot AI review requested due to automatic review settings December 21, 2025 12:28
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

此PR旨在改善开发者误将微信支付V3版本的JSON格式通知数据传递给V2版本XML解析方法时的错误提示。通过在解析前检测数据格式,将原本晦涩的XStream解析异常替换为清晰的指导信息,告知开发者应使用parseOrderNotifyV3Result方法处理V3 API的JSON通知数据。

主要变更:

  • BaseWxPayServiceImpl.parseOrderNotifyResult()方法中添加JSON格式检测逻辑
  • 当检测到JSON格式数据时,抛出带有明确指导的WxPayException异常
  • 添加测试用例验证JSON输入会触发异常

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java parseOrderNotifyResult方法中添加JSON格式检测,当检测到以{开头的数据时抛出带有V3 API使用指导的异常
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/notify/WxPayOrderNotifyResultTest.java 添加测试用例验证传入JSON格式数据时会抛出异常(但测试路径与实现不匹配)

@binarywang binarywang linked an issue Dec 21, 2025 that may be closed by this pull request
…ice/impl/BaseWxPayServiceImpl.java

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Remove incorrect test from WxPayOrderNotifyResultTest that tested fromXML() directly
- Add proper test in BaseWxPayServiceImplTest that tests parseOrderNotifyResult() service method
- New test verifies that JSON format triggers helpful error message with V3 API guidance

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
@binarywang binarywang merged commit b601b55 into develop Dec 21, 2025
1 check passed
@binarywang binarywang deleted the copilot/fix-3699 branch December 21, 2025 13:57
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