1
0
mirror of synced 2026-05-20 17:28:28 +08:00

🎨 #3954 【微信支付】修复V3支付预支付参数packageValue字段与微信官方API不一致问题,添加@SerializedName("package")注解

This commit is contained in:
Copilot
2026-04-11 21:40:19 +08:00
committed by GitHub
parent fb4583324e
commit f3b8cf689e
4 changed files with 83 additions and 2 deletions

View File

@@ -65,6 +65,10 @@ public class TransactionsResult 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;
@@ -80,6 +84,10 @@ public class TransactionsResult implements Serializable {
private String appid;
private String partnerid;
private String prepayid;
/**
* 由于package为java保留关键字因此改为packageValue序列化时会自动转换为package字段名
*/
@SerializedName("package")
private String packageValue;
private String noncestr;
private String timestamp;

View File

@@ -73,6 +73,10 @@ public class CombineTransactionsResult 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;
@@ -89,6 +93,10 @@ public class CombineTransactionsResult implements Serializable {
private String appid;
private String partnerid;
private String prepayid;
/**
* 由于package为java保留关键字因此改为packageValue序列化时会自动转换为package字段名
*/
@SerializedName("package")
private String packageValue;
private String noncestr;
private String timestamp;

View File

@@ -78,6 +78,10 @@ public class WxPayUnifiedOrderV3Result 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 class WxPayUnifiedOrderV3Result 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;
private String timestamp;

View File

@@ -2,6 +2,8 @@ package com.github.binarywang.wxpay.bean.result;
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 class WxPayUnifiedOrderV3ResultTest {
}
/**
* 测试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();
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");
// 验证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);