Skip to content

Fix V3 payment field serialization mismatch with WeChat-expected JSON keys (package, partnerid, prepayid)#3954

Open
Copilot wants to merge 4 commits intodevelopfrom
copilot/fix-package-value-issue
Open

Fix V3 payment field serialization mismatch with WeChat-expected JSON keys (package, partnerid, prepayid)#3954
Copilot wants to merge 4 commits intodevelopfrom
copilot/fix-package-value-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 7, 2026

V3 payment result objects used Java field names that, when serialized by Gson, produced JSON keys incompatible with WeChat's payment APIs. Specifically:

  • packageValue was serialized as packageValue instead of the required package
  • partnerId / prepayId in WxPayUnifiedOrderV3Result.AppResult were serialized as camelCase instead of the required all-lowercase partnerid / prepayid

Changes

  • WxPayUnifiedOrderV3Result (JsapiResult) — add @SerializedName("package") to packageValue
  • WxPayUnifiedOrderV3Result (AppResult) — add @SerializedName("package") to packageValue; add @SerializedName("partnerid") to partnerId; add @SerializedName("prepayid") to prepayId
  • CombineTransactionsResult (JsapiResult, AppResult) — add @SerializedName("package") to packageValue
  • TransactionsResult (JsapiResult, AppResult) — add @SerializedName("package") to packageValue

Result

// Before (WxPayUnifiedOrderV3Result.AppResult)
{ "packageValue": "Sign=WXPay", "partnerId": "1900000109", "prepayId": "wx..." }

// After
{ "package": "Sign=WXPay", "partnerid": "1900000109", "prepayid": "wx..." }

Java getter/setter names (getPackageValue(), getPartnerId(), getPrepayId(), etc.) are unchanged — only the serialized JSON keys are affected.

New tests in WxPayUnifiedOrderV3ResultTest verify that Gson output for both JsapiResult and AppResult uses the correct WeChat-expected field names (package, partnerid, prepayid) and does not contain the old camelCase keys.

Copilot AI changed the title [WIP] Fix package value mismatch in V3 payment parameters Fix V3 payment package field serialized as packageValue instead of WeChat-expected package Apr 7, 2026
Copilot AI requested a review from binarywang April 7, 2026 09:21
@binarywang binarywang marked this pull request as ready for review April 7, 2026 13:20
Copilot AI review requested due to automatic review settings April 7, 2026 13:20
@augmentcode
Copy link
Copy Markdown

augmentcode bot commented Apr 7, 2026

🤖 Augment PR Summary

Summary: Fixes WeChat Pay V3 payment parameter JSON output so the key is package (as required by WeChat) rather than the Java field name packageValue.

Changes:

  • Adds @SerializedName("package") to packageValue in multiple V3 payment result DTOs (JSAPI/App variants).
  • Updates unified-order V3 result bean to ensure Gson serialization matches WeChat client API expectations.
  • Adds unit tests to verify Gson output contains package and not packageValue for both JSAPI and App results.

Technical Notes: This change is Gson-specific (via com.google.gson.annotations.SerializedName) and keeps Java getters/setters unchanged.

🤖 Was this summary useful? React with 👍 or 👎

Copy link
Copy Markdown

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

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

Review completed. 2 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

private String timeStamp;
private String nonceStr;
/**
* 由于package为java保留关键字,因此改为packageValue,序列化时会自动转换为package字段名
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

packageValue 这里的注释写“序列化时会自动转换为package字段名”,但实际是依赖 Gson 的 @SerializedName("package") 才会生效;如果调用方用其他 JSON 库(如 Jackson)仍可能输出 packageValue。考虑在注释里明确这是 Gson 序列化行为以避免误导。

Severity: low

Other Locations
  • weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayUnifiedOrderV3Result.java:116
  • weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/CombineTransactionsResult.java:77
  • weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/CombineTransactionsResult.java:97
  • weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/TransactionsResult.java:69
  • weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/TransactionsResult.java:88

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

Assert.assertEquals(jsapiResult.getPackageValue(), "prepay_id=" + testPrepayId);

// 验证JSON序列化后字段名为package(微信官方要求)
Gson gson = new Gson();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

这里用 new Gson() 来验证序列化字段名,可能与项目内实际使用的 Gson 配置(例如 WxGsonBuilder.create() 或统一的 GsonBuilder 实例)不一致。为避免后续 Gson 配置调整导致测试与真实序列化行为偏离,考虑复用项目里支付模块的 Gson 创建方式。

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

Copy link
Copy Markdown

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 拉起支付参数对象在使用 Gson 序列化时,packageValue 被错误序列化为 packageValue(而非微信端要求的 package)的问题,从而提升与微信 wx.requestPayment / APP 支付拉起参数的兼容性。

Changes:

  • 为多个 V3 支付结果对象中的 packageValue 增加 @SerializedName("package"),确保 JSON key 为 package
  • 新增单元测试,验证 WxPayUnifiedOrderV3ResultJsapiResult / AppResult 序列化后包含 package 字段且不包含 packageValue

Reviewed changes

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

File Description
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayUnifiedOrderV3Result.java JsapiResult / AppResultpackageValue 增加 @SerializedName("package")
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/CombineTransactionsResult.java JsapiResult / AppResultpackageValue 增加 @SerializedName("package")
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/TransactionsResult.java JsapiResult / AppResultpackageValue 增加 @SerializedName("package")
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayUnifiedOrderV3ResultTest.java 新增序列化测试用例,校验 package 字段名输出

Comment on lines 112 to 120
private String appid;
private String partnerId;
private String prepayId;
/**
* 由于package为java保留关键字,因此改为packageValue,序列化时会自动转换为package字段名
*/
@SerializedName("package")
private String packageValue;
private String noncestr;
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

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

WxPayUnifiedOrderV3Result.AppResult 目前只有 packageValue 增加了 @SerializedName("package");但同一个对象里 partnerId / prepayId 字段如果用 Gson 序列化,JSON key 会变成 partnerId / prepayId,而微信 APP 支付参数要求的是 partnerid / prepayid(同模块里 CombineTransactionsResult/TransactionsResult 的 AppResult 也使用了全小写字段名)。建议为 partnerId、prepayId 同样补充 @SerializedName("partnerid") / @SerializedName("prepayid")(或统一字段命名策略),避免调用方直接序列化该对象时仍不兼容微信端。

Copilot uses AI. Check for mistakes.
Comment on lines +239 to +250
WxPayUnifiedOrderV3Result.AppResult appResult =
WxPayUnifiedOrderV3Result.getAppPayInfo(testPrepayId, testAppId, testMchId, privateKey);

// 验证Java字段名仍为packageValue
Assert.assertEquals(appResult.getPackageValue(), "Sign=WXPay");

// 验证JSON序列化后字段名为package(微信官方要求)
Gson gson = new Gson();
JsonObject jsonObject = gson.toJsonTree(appResult).getAsJsonObject();
Assert.assertTrue(jsonObject.has("package"), "JSON中应该包含package字段");
Assert.assertFalse(jsonObject.has("packageValue"), "JSON中不应该包含packageValue字段");
Assert.assertEquals(jsonObject.get("package").getAsString(), "Sign=WXPay");
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

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

新增的 JSON 序列化测试目前只校验了 package/packageValue;但如果调用方直接用 Gson 序列化 AppResult,partnerId/prepayId 仍会输出为驼峰 key(partnerId/prepayId),与微信 APP 支付期望的 partnerid/prepayid 不一致。建议在同一组测试里也断言 partnerid/prepayid 的字段名与值(并在代码侧配合补齐 @SerializedName)。

Copilot uses AI. Check for mistakes.
@binarywang
Copy link
Copy Markdown
Owner

@copilot apply changes based on the comments in this thread

…ame注解,修复与微信官方API字段名不一致问题

Agent-Logs-Url: https://github.com/binarywang/WxJava/sessions/099513d0-4cde-45a3-850c-1afec934741c

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 7, 2026

@copilot apply changes based on the comments in this thread

已在 49c1559 中处理:

  • WxPayUnifiedOrderV3Result.AppResultpartnerId 添加 @SerializedName("partnerid")
  • WxPayUnifiedOrderV3Result.AppResultprepayId 添加 @SerializedName("prepayid")
  • 在测试中增加对 partnerid/prepayid JSON 字段名的断言

现在序列化后与 CombineTransactionsResult/TransactionsResult 保持一致,符合微信官方 APP 支付参数要求。

Copilot AI changed the title Fix V3 payment package field serialized as packageValue instead of WeChat-expected package Fix V3 payment field serialization mismatch with WeChat-expected JSON keys (package, partnerid, prepayid) Apr 7, 2026
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.

V3支付,组装预支付参数和官网字段不一致(packageValue 和 package字段)

3 participants