|
2 | 2 |
|
3 | 3 | import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum; |
4 | 4 | import com.github.binarywang.wxpay.v3.util.SignUtils; |
| 5 | +import com.google.gson.Gson; |
| 6 | +import com.google.gson.JsonObject; |
5 | 7 | import org.testng.Assert; |
6 | 8 | import org.testng.annotations.Test; |
7 | 9 |
|
@@ -200,9 +202,62 @@ public void testAppResultWithPrepayId() throws Exception { |
200 | 202 | } |
201 | 203 |
|
202 | 204 | /** |
203 | | - * 测试getJsapiPayInfo方法的空值验证 |
| 205 | + * 测试JsapiResult序列化为JSON时,packageValue字段名应为package(兼容微信官方API要求) |
204 | 206 | */ |
205 | | - @Test(expectedExceptions = IllegalArgumentException.class, |
| 207 | + @Test |
| 208 | + public void testJsapiResultJsonSerializationPackageFieldName() throws Exception { |
| 209 | + String testPrepayId = "wx201410272009395522657a690389285100"; |
| 210 | + String testAppId = "wx8888888888888888"; |
| 211 | + KeyPair keyPair = generateKeyPair(); |
| 212 | + PrivateKey privateKey = keyPair.getPrivate(); |
| 213 | + |
| 214 | + WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = |
| 215 | + WxPayUnifiedOrderV3Result.getJsapiPayInfo(testPrepayId, testAppId, privateKey); |
| 216 | + |
| 217 | + // 验证Java字段名仍为packageValue |
| 218 | + Assert.assertEquals(jsapiResult.getPackageValue(), "prepay_id=" + testPrepayId); |
| 219 | + |
| 220 | + // 验证JSON序列化后字段名为package(微信官方要求) |
| 221 | + Gson gson = new Gson(); |
| 222 | + JsonObject jsonObject = gson.toJsonTree(jsapiResult).getAsJsonObject(); |
| 223 | + Assert.assertTrue(jsonObject.has("package"), "JSON中应该包含package字段"); |
| 224 | + Assert.assertFalse(jsonObject.has("packageValue"), "JSON中不应该包含packageValue字段"); |
| 225 | + Assert.assertEquals(jsonObject.get("package").getAsString(), "prepay_id=" + testPrepayId); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * 测试AppResult序列化为JSON时,packageValue字段名应为package(兼容微信官方API要求) |
| 230 | + */ |
| 231 | + @Test |
| 232 | + public void testAppResultJsonSerializationPackageFieldName() throws Exception { |
| 233 | + String testPrepayId = "wx201410272009395522657a690389285100"; |
| 234 | + String testAppId = "wx8888888888888888"; |
| 235 | + String testMchId = "1900000109"; |
| 236 | + KeyPair keyPair = generateKeyPair(); |
| 237 | + PrivateKey privateKey = keyPair.getPrivate(); |
| 238 | + |
| 239 | + WxPayUnifiedOrderV3Result.AppResult appResult = |
| 240 | + WxPayUnifiedOrderV3Result.getAppPayInfo(testPrepayId, testAppId, testMchId, privateKey); |
| 241 | + |
| 242 | + // 验证Java字段名仍为packageValue |
| 243 | + Assert.assertEquals(appResult.getPackageValue(), "Sign=WXPay"); |
| 244 | + |
| 245 | + // 验证JSON序列化后字段名为package(微信官方要求) |
| 246 | + Gson gson = new Gson(); |
| 247 | + JsonObject jsonObject = gson.toJsonTree(appResult).getAsJsonObject(); |
| 248 | + Assert.assertTrue(jsonObject.has("package"), "JSON中应该包含package字段"); |
| 249 | + Assert.assertFalse(jsonObject.has("packageValue"), "JSON中不应该包含packageValue字段"); |
| 250 | + Assert.assertEquals(jsonObject.get("package").getAsString(), "Sign=WXPay"); |
| 251 | + // 验证JSON序列化后partnerid和prepayid字段名为全小写(微信官方要求) |
| 252 | + Assert.assertTrue(jsonObject.has("partnerid"), "JSON中应该包含partnerid字段"); |
| 253 | + Assert.assertFalse(jsonObject.has("partnerId"), "JSON中不应该包含驼峰的partnerId字段"); |
| 254 | + Assert.assertEquals(jsonObject.get("partnerid").getAsString(), testMchId); |
| 255 | + Assert.assertTrue(jsonObject.has("prepayid"), "JSON中应该包含prepayid字段"); |
| 256 | + Assert.assertFalse(jsonObject.has("prepayId"), "JSON中不应该包含驼峰的prepayId字段"); |
| 257 | + Assert.assertEquals(jsonObject.get("prepayid").getAsString(), testPrepayId); |
| 258 | + } |
| 259 | + |
| 260 | + @Test(expectedExceptions = IllegalArgumentException.class, |
206 | 261 | expectedExceptionsMessageRegExp = "prepayId, appId 和 privateKey 不能为空") |
207 | 262 | public void testGetJsapiPayInfoWithNullPrepayId() { |
208 | 263 | WxPayUnifiedOrderV3Result.getJsapiPayInfo(null, "appId", null); |
|
0 commit comments