-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
在JsapiResult中添加prepayId字段并提供静态工厂方法实现解耦 #3798
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
Merged
+389
−16
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
.../src/test/java/com/github/binarywang/wxpay/bean/result/WxPayUnifiedOrderV3ResultTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| 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 org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.security.KeyPair; | ||
| import java.security.KeyPairGenerator; | ||
| import java.security.PrivateKey; | ||
|
|
||
| /** | ||
| * <pre> | ||
| * WxPayUnifiedOrderV3Result 测试类 | ||
| * 主要测试prepayId字段和静态工厂方法的解耦功能 | ||
| * </pre> | ||
| * | ||
| * @author copilot | ||
| */ | ||
| public class WxPayUnifiedOrderV3ResultTest { | ||
|
|
||
| /** | ||
| * 生成测试用的RSA密钥对 | ||
| */ | ||
| private KeyPair generateKeyPair() throws Exception { | ||
| KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); | ||
| keyPairGenerator.initialize(2048); | ||
| return keyPairGenerator.generateKeyPair(); | ||
| } | ||
|
|
||
| /** | ||
| * 测试JsapiResult中的prepayId字段是否正确设置 | ||
| */ | ||
| @Test | ||
| public void testJsapiResultWithPrepayId() throws Exception { | ||
| // 准备测试数据 | ||
| String testPrepayId = "wx201410272009395522657a690389285100"; | ||
| String testAppId = "wx8888888888888888"; | ||
| KeyPair keyPair = generateKeyPair(); | ||
| PrivateKey privateKey = keyPair.getPrivate(); | ||
|
|
||
| // 创建WxPayUnifiedOrderV3Result对象 | ||
| WxPayUnifiedOrderV3Result result = new WxPayUnifiedOrderV3Result(); | ||
| result.setPrepayId(testPrepayId); | ||
|
|
||
| // 调用getPayInfo生成JsapiResult | ||
| WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = | ||
| result.getPayInfo(TradeTypeEnum.JSAPI, testAppId, null, privateKey); | ||
|
|
||
| // 验证prepayId字段是否正确设置 | ||
| Assert.assertNotNull(jsapiResult.getPrepayId(), "prepayId不应为null"); | ||
| Assert.assertEquals(jsapiResult.getPrepayId(), testPrepayId, "prepayId应该与设置的值相同"); | ||
|
|
||
| // 验证其他字段 | ||
| Assert.assertEquals(jsapiResult.getAppId(), testAppId); | ||
| Assert.assertNotNull(jsapiResult.getTimeStamp()); | ||
| Assert.assertNotNull(jsapiResult.getNonceStr()); | ||
| Assert.assertEquals(jsapiResult.getPackageValue(), "prepay_id=" + testPrepayId); | ||
| Assert.assertEquals(jsapiResult.getSignType(), "RSA"); | ||
| Assert.assertNotNull(jsapiResult.getPaySign()); | ||
| } | ||
|
|
||
| /** | ||
| * 测试使用静态工厂方法生成JsapiResult(解耦场景) | ||
| */ | ||
| @Test | ||
| public void testGetJsapiPayInfoStaticMethod() throws Exception { | ||
| // 准备测试数据 | ||
| String testPrepayId = "wx201410272009395522657a690389285100"; | ||
| String testAppId = "wx8888888888888888"; | ||
| KeyPair keyPair = generateKeyPair(); | ||
| PrivateKey privateKey = keyPair.getPrivate(); | ||
|
|
||
| // 使用静态工厂方法生成JsapiResult | ||
| WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = | ||
| WxPayUnifiedOrderV3Result.getJsapiPayInfo(testPrepayId, testAppId, privateKey); | ||
|
|
||
| // 验证prepayId字段 | ||
| Assert.assertNotNull(jsapiResult.getPrepayId(), "prepayId不应为null"); | ||
| Assert.assertEquals(jsapiResult.getPrepayId(), testPrepayId, "prepayId应该与输入的值相同"); | ||
|
|
||
| // 验证其他字段 | ||
| Assert.assertEquals(jsapiResult.getAppId(), testAppId); | ||
| Assert.assertNotNull(jsapiResult.getTimeStamp()); | ||
| Assert.assertNotNull(jsapiResult.getNonceStr()); | ||
| Assert.assertEquals(jsapiResult.getPackageValue(), "prepay_id=" + testPrepayId); | ||
| Assert.assertEquals(jsapiResult.getSignType(), "RSA"); | ||
| Assert.assertNotNull(jsapiResult.getPaySign()); | ||
| } | ||
|
|
||
| /** | ||
| * 测试使用静态工厂方法生成AppResult(解耦场景) | ||
| */ | ||
| @Test | ||
| public void testGetAppPayInfoStaticMethod() throws Exception { | ||
| // 准备测试数据 | ||
| String testPrepayId = "wx201410272009395522657a690389285100"; | ||
| String testAppId = "wx8888888888888888"; | ||
| String testMchId = "1900000109"; | ||
| KeyPair keyPair = generateKeyPair(); | ||
| PrivateKey privateKey = keyPair.getPrivate(); | ||
|
|
||
| // 使用静态工厂方法生成AppResult | ||
| WxPayUnifiedOrderV3Result.AppResult appResult = | ||
| WxPayUnifiedOrderV3Result.getAppPayInfo(testPrepayId, testAppId, testMchId, privateKey); | ||
|
|
||
| // 验证prepayId字段 | ||
| Assert.assertNotNull(appResult.getPrepayId(), "prepayId不应为null"); | ||
| Assert.assertEquals(appResult.getPrepayId(), testPrepayId, "prepayId应该与输入的值相同"); | ||
|
|
||
| // 验证其他字段 | ||
| Assert.assertEquals(appResult.getAppid(), testAppId); | ||
| Assert.assertEquals(appResult.getPartnerId(), testMchId); | ||
| Assert.assertNotNull(appResult.getTimestamp()); | ||
| Assert.assertNotNull(appResult.getNoncestr()); | ||
| Assert.assertEquals(appResult.getPackageValue(), "Sign=WXPay"); | ||
| Assert.assertNotNull(appResult.getSign()); | ||
| } | ||
|
|
||
| /** | ||
| * 测试解耦场景:先获取prepayId,后续再生成支付信息 | ||
| */ | ||
| @Test | ||
| public void testDecoupledScenario() throws Exception { | ||
| // 模拟场景:先创建订单获取prepayId | ||
| String testPrepayId = "wx201410272009395522657a690389285100"; | ||
| String testAppId = "wx8888888888888888"; | ||
| KeyPair keyPair = generateKeyPair(); | ||
| PrivateKey privateKey = keyPair.getPrivate(); | ||
|
|
||
| // 步骤1:模拟从创建订单接口获取prepayId | ||
| WxPayUnifiedOrderV3Result orderResult = new WxPayUnifiedOrderV3Result(); | ||
| orderResult.setPrepayId(testPrepayId); | ||
|
|
||
| // 获取prepayId用于存储 | ||
| String storedPrepayId = orderResult.getPrepayId(); | ||
| Assert.assertEquals(storedPrepayId, testPrepayId); | ||
|
|
||
| // 步骤2:后续支付失败时,使用存储的prepayId重新生成支付信息 | ||
| WxPayUnifiedOrderV3Result.JsapiResult newPayInfo = | ||
| WxPayUnifiedOrderV3Result.getJsapiPayInfo(storedPrepayId, testAppId, privateKey); | ||
|
|
||
| // 验证重新生成的支付信息 | ||
| Assert.assertEquals(newPayInfo.getPrepayId(), storedPrepayId); | ||
| Assert.assertEquals(newPayInfo.getPackageValue(), "prepay_id=" + storedPrepayId); | ||
| Assert.assertNotNull(newPayInfo.getPaySign()); | ||
| } | ||
|
|
||
| /** | ||
| * 测试多次生成支付信息,签名应该不同(因为timestamp和nonceStr每次都不同) | ||
| */ | ||
| @Test | ||
| public void testMultipleGenerationsHaveDifferentSignatures() throws Exception { | ||
| String testPrepayId = "wx201410272009395522657a690389285100"; | ||
| String testAppId = "wx8888888888888888"; | ||
| KeyPair keyPair = generateKeyPair(); | ||
| PrivateKey privateKey = keyPair.getPrivate(); | ||
|
|
||
| // 生成第一次支付信息 | ||
| WxPayUnifiedOrderV3Result.JsapiResult result1 = | ||
| WxPayUnifiedOrderV3Result.getJsapiPayInfo(testPrepayId, testAppId, privateKey); | ||
|
|
||
| // 等待一秒确保timestamp不同 | ||
| Thread.sleep(1000); | ||
|
|
||
| // 生成第二次支付信息 | ||
| WxPayUnifiedOrderV3Result.JsapiResult result2 = | ||
| WxPayUnifiedOrderV3Result.getJsapiPayInfo(testPrepayId, testAppId, privateKey); | ||
|
|
||
| // prepayId应该相同 | ||
| Assert.assertEquals(result1.getPrepayId(), result2.getPrepayId()); | ||
|
|
||
| // 但是timestamp、nonceStr和签名应该不同 | ||
| Assert.assertNotEquals(result1.getTimeStamp(), result2.getTimeStamp(), "timestamp应该不同"); | ||
| Assert.assertNotEquals(result1.getNonceStr(), result2.getNonceStr(), "nonceStr应该不同"); | ||
| Assert.assertNotEquals(result1.getPaySign(), result2.getPaySign(), "签名应该不同"); | ||
| } | ||
|
|
||
| /** | ||
| * 测试AppResult中的prepayId字段 | ||
| */ | ||
| @Test | ||
| public void testAppResultWithPrepayId() throws Exception { | ||
| String testPrepayId = "wx201410272009395522657a690389285100"; | ||
| String testAppId = "wx8888888888888888"; | ||
| String testMchId = "1900000109"; | ||
| KeyPair keyPair = generateKeyPair(); | ||
| PrivateKey privateKey = keyPair.getPrivate(); | ||
|
|
||
| WxPayUnifiedOrderV3Result result = new WxPayUnifiedOrderV3Result(); | ||
| result.setPrepayId(testPrepayId); | ||
|
|
||
| // 调用getPayInfo生成AppResult | ||
| WxPayUnifiedOrderV3Result.AppResult appResult = | ||
| result.getPayInfo(TradeTypeEnum.APP, testAppId, testMchId, privateKey); | ||
|
|
||
| // 验证prepayId字段 | ||
| Assert.assertNotNull(appResult.getPrepayId(), "prepayId不应为null"); | ||
| Assert.assertEquals(appResult.getPrepayId(), testPrepayId, "prepayId应该与设置的值相同"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.