Fix V3 payment field serialization mismatch with WeChat-expected JSON keys (package, partnerid, prepayid)#3954
Fix V3 payment field serialization mismatch with WeChat-expected JSON keys (package, partnerid, prepayid)#3954
package, partnerid, prepayid)#3954Conversation
Agent-Logs-Url: https://github.com/binarywang/WxJava/sessions/4f76043e-c058-4838-8d60-a43a1c878544 Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Agent-Logs-Url: https://github.com/binarywang/WxJava/sessions/4f76043e-c058-4838-8d60-a43a1c878544 Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
package field serialized as packageValue instead of WeChat-expected package
🤖 Augment PR SummarySummary: Fixes WeChat Pay V3 payment parameter JSON output so the key is Changes:
Technical Notes: This change is Gson-specific (via 🤖 Was this summary useful? React with 👍 or 👎 |
| private String timeStamp; | ||
| private String nonceStr; | ||
| /** | ||
| * 由于package为java保留关键字,因此改为packageValue,序列化时会自动转换为package字段名 |
There was a problem hiding this comment.
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:116weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/CombineTransactionsResult.java:77weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/CombineTransactionsResult.java:97weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/TransactionsResult.java:69weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/TransactionsResult.java:88
🤖 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(); |
There was a problem hiding this comment.
Pull request overview
该 PR 修复微信支付 V3 拉起支付参数对象在使用 Gson 序列化时,packageValue 被错误序列化为 packageValue(而非微信端要求的 package)的问题,从而提升与微信 wx.requestPayment / APP 支付拉起参数的兼容性。
Changes:
- 为多个 V3 支付结果对象中的
packageValue增加@SerializedName("package"),确保 JSON key 为package - 新增单元测试,验证
WxPayUnifiedOrderV3Result的JsapiResult/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 / AppResult 的 packageValue 增加 @SerializedName("package") |
| weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/CombineTransactionsResult.java | 为 JsapiResult / AppResult 的 packageValue 增加 @SerializedName("package") |
| weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/TransactionsResult.java | 为 JsapiResult / AppResult 的 packageValue 增加 @SerializedName("package") |
| weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayUnifiedOrderV3ResultTest.java | 新增序列化测试用例,校验 package 字段名输出 |
| private String appid; | ||
| private String partnerId; | ||
| private String prepayId; | ||
| /** | ||
| * 由于package为java保留关键字,因此改为packageValue,序列化时会自动转换为package字段名 | ||
| */ | ||
| @SerializedName("package") | ||
| private String packageValue; | ||
| private String noncestr; |
There was a problem hiding this comment.
WxPayUnifiedOrderV3Result.AppResult 目前只有 packageValue 增加了 @SerializedName("package");但同一个对象里 partnerId / prepayId 字段如果用 Gson 序列化,JSON key 会变成 partnerId / prepayId,而微信 APP 支付参数要求的是 partnerid / prepayid(同模块里 CombineTransactionsResult/TransactionsResult 的 AppResult 也使用了全小写字段名)。建议为 partnerId、prepayId 同样补充 @SerializedName("partnerid") / @SerializedName("prepayid")(或统一字段命名策略),避免调用方直接序列化该对象时仍不兼容微信端。
| 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"); |
There was a problem hiding this comment.
新增的 JSON 序列化测试目前只校验了 package/packageValue;但如果调用方直接用 Gson 序列化 AppResult,partnerId/prepayId 仍会输出为驼峰 key(partnerId/prepayId),与微信 APP 支付期望的 partnerid/prepayid 不一致。建议在同一组测试里也断言 partnerid/prepayid 的字段名与值(并在代码侧配合补齐 @SerializedName)。
|
@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>
已在 49c1559 中处理:
现在序列化后与 |
package field serialized as packageValue instead of WeChat-expected packagepackage, partnerid, prepayid)
V3 payment result objects used Java field names that, when serialized by Gson, produced JSON keys incompatible with WeChat's payment APIs. Specifically:
packageValuewas serialized aspackageValueinstead of the requiredpackagepartnerId/prepayIdinWxPayUnifiedOrderV3Result.AppResultwere serialized as camelCase instead of the required all-lowercasepartnerid/prepayidChanges
WxPayUnifiedOrderV3Result(JsapiResult) — add@SerializedName("package")topackageValueWxPayUnifiedOrderV3Result(AppResult) — add@SerializedName("package")topackageValue; add@SerializedName("partnerid")topartnerId; add@SerializedName("prepayid")toprepayIdCombineTransactionsResult(JsapiResult,AppResult) — add@SerializedName("package")topackageValueTransactionsResult(JsapiResult,AppResult) — add@SerializedName("package")topackageValueResult
Java getter/setter names (
getPackageValue(),getPartnerId(),getPrepayId(), etc.) are unchanged — only the serialized JSON keys are affected.New tests in
WxPayUnifiedOrderV3ResultTestverify that Gson output for bothJsapiResultandAppResultuses the correct WeChat-expected field names (package,partnerid,prepayid) and does not contain the old camelCase keys.