-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Fix V3 payment field serialization mismatch with WeChat-expected JSON keys (package, partnerid, prepayid)
#3954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,6 +78,10 @@ public static class JsapiResult implements Serializable { | |
| private String appId; | ||
| private String timeStamp; | ||
| private String nonceStr; | ||
| /** | ||
| * 由于package为java保留关键字,因此改为packageValue,序列化时会自动转换为package字段名 | ||
| */ | ||
| @SerializedName("package") | ||
| private String packageValue; | ||
| private String signType; | ||
| private String paySign; | ||
|
|
@@ -106,8 +110,14 @@ public static class AppResult implements Serializable { | |
| private static final long serialVersionUID = 5465773025172875110L; | ||
|
|
||
| private String appid; | ||
| @SerializedName("partnerid") | ||
| private String partnerId; | ||
| @SerializedName("prepayid") | ||
| private String prepayId; | ||
| /** | ||
| * 由于package为java保留关键字,因此改为packageValue,序列化时会自动转换为package字段名 | ||
| */ | ||
| @SerializedName("package") | ||
| private String packageValue; | ||
| private String noncestr; | ||
|
Comment on lines
112
to
122
|
||
| private String timestamp; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum; | ||
| import com.github.binarywang.wxpay.v3.util.SignUtils; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonObject; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
|
|
@@ -200,9 +202,62 @@ public void testAppResultWithPrepayId() throws Exception { | |
| } | ||
|
|
||
| /** | ||
| * 测试getJsapiPayInfo方法的空值验证 | ||
| * 测试JsapiResult序列化为JSON时,packageValue字段名应为package(兼容微信官方API要求) | ||
| */ | ||
| @Test(expectedExceptions = IllegalArgumentException.class, | ||
| @Test | ||
| public void testJsapiResultJsonSerializationPackageFieldName() throws Exception { | ||
| String testPrepayId = "wx201410272009395522657a690389285100"; | ||
| String testAppId = "wx8888888888888888"; | ||
| KeyPair keyPair = generateKeyPair(); | ||
| PrivateKey privateKey = keyPair.getPrivate(); | ||
|
|
||
| WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = | ||
| WxPayUnifiedOrderV3Result.getJsapiPayInfo(testPrepayId, testAppId, privateKey); | ||
|
|
||
| // 验证Java字段名仍为packageValue | ||
| Assert.assertEquals(jsapiResult.getPackageValue(), "prepay_id=" + testPrepayId); | ||
|
|
||
| // 验证JSON序列化后字段名为package(微信官方要求) | ||
| Gson gson = new Gson(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| JsonObject jsonObject = gson.toJsonTree(jsapiResult).getAsJsonObject(); | ||
| Assert.assertTrue(jsonObject.has("package"), "JSON中应该包含package字段"); | ||
| Assert.assertFalse(jsonObject.has("packageValue"), "JSON中不应该包含packageValue字段"); | ||
| Assert.assertEquals(jsonObject.get("package").getAsString(), "prepay_id=" + testPrepayId); | ||
| } | ||
|
|
||
| /** | ||
| * 测试AppResult序列化为JSON时,packageValue字段名应为package(兼容微信官方API要求) | ||
| */ | ||
| @Test | ||
| public void testAppResultJsonSerializationPackageFieldName() throws Exception { | ||
| String testPrepayId = "wx201410272009395522657a690389285100"; | ||
| String testAppId = "wx8888888888888888"; | ||
| String testMchId = "1900000109"; | ||
| KeyPair keyPair = generateKeyPair(); | ||
| PrivateKey privateKey = keyPair.getPrivate(); | ||
|
|
||
| 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"); | ||
|
Comment on lines
+239
to
+250
|
||
| // 验证JSON序列化后partnerid和prepayid字段名为全小写(微信官方要求) | ||
| Assert.assertTrue(jsonObject.has("partnerid"), "JSON中应该包含partnerid字段"); | ||
| Assert.assertFalse(jsonObject.has("partnerId"), "JSON中不应该包含驼峰的partnerId字段"); | ||
| Assert.assertEquals(jsonObject.get("partnerid").getAsString(), testMchId); | ||
| Assert.assertTrue(jsonObject.has("prepayid"), "JSON中应该包含prepayid字段"); | ||
| Assert.assertFalse(jsonObject.has("prepayId"), "JSON中不应该包含驼峰的prepayId字段"); | ||
| Assert.assertEquals(jsonObject.get("prepayid").getAsString(), testPrepayId); | ||
| } | ||
|
|
||
| @Test(expectedExceptions = IllegalArgumentException.class, | ||
| expectedExceptionsMessageRegExp = "prepayId, appId 和 privateKey 不能为空") | ||
| public void testGetJsapiPayInfoWithNullPrepayId() { | ||
| WxPayUnifiedOrderV3Result.getJsapiPayInfo(null, "appId", null); | ||
|
|
||
There was a problem hiding this comment.
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: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.