|
| 1 | +package com.github.binarywang.wxpay.bean.result; |
| 2 | + |
| 3 | +import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum; |
| 4 | +import com.github.binarywang.wxpay.v3.util.SignUtils; |
| 5 | +import org.testng.Assert; |
| 6 | +import org.testng.annotations.Test; |
| 7 | + |
| 8 | +import java.security.KeyPair; |
| 9 | +import java.security.KeyPairGenerator; |
| 10 | +import java.security.PrivateKey; |
| 11 | + |
| 12 | +/** |
| 13 | + * <pre> |
| 14 | + * WxPayUnifiedOrderV3Result 测试类 |
| 15 | + * 主要测试prepayId字段和静态工厂方法的解耦功能 |
| 16 | + * </pre> |
| 17 | + * |
| 18 | + * @author copilot |
| 19 | + */ |
| 20 | +public class WxPayUnifiedOrderV3ResultTest { |
| 21 | + |
| 22 | + /** |
| 23 | + * 生成测试用的RSA密钥对 |
| 24 | + */ |
| 25 | + private KeyPair generateKeyPair() throws Exception { |
| 26 | + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); |
| 27 | + keyPairGenerator.initialize(2048); |
| 28 | + return keyPairGenerator.generateKeyPair(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * 测试JsapiResult中的prepayId字段是否正确设置 |
| 33 | + */ |
| 34 | + @Test |
| 35 | + public void testJsapiResultWithPrepayId() throws Exception { |
| 36 | + // 准备测试数据 |
| 37 | + String testPrepayId = "wx201410272009395522657a690389285100"; |
| 38 | + String testAppId = "wx8888888888888888"; |
| 39 | + KeyPair keyPair = generateKeyPair(); |
| 40 | + PrivateKey privateKey = keyPair.getPrivate(); |
| 41 | + |
| 42 | + // 创建WxPayUnifiedOrderV3Result对象 |
| 43 | + WxPayUnifiedOrderV3Result result = new WxPayUnifiedOrderV3Result(); |
| 44 | + result.setPrepayId(testPrepayId); |
| 45 | + |
| 46 | + // 调用getPayInfo生成JsapiResult |
| 47 | + WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = |
| 48 | + result.getPayInfo(TradeTypeEnum.JSAPI, testAppId, null, privateKey); |
| 49 | + |
| 50 | + // 验证prepayId字段是否正确设置 |
| 51 | + Assert.assertNotNull(jsapiResult.getPrepayId(), "prepayId不应为null"); |
| 52 | + Assert.assertEquals(jsapiResult.getPrepayId(), testPrepayId, "prepayId应该与设置的值相同"); |
| 53 | + |
| 54 | + // 验证其他字段 |
| 55 | + Assert.assertEquals(jsapiResult.getAppId(), testAppId); |
| 56 | + Assert.assertNotNull(jsapiResult.getTimeStamp()); |
| 57 | + Assert.assertNotNull(jsapiResult.getNonceStr()); |
| 58 | + Assert.assertEquals(jsapiResult.getPackageValue(), "prepay_id=" + testPrepayId); |
| 59 | + Assert.assertEquals(jsapiResult.getSignType(), "RSA"); |
| 60 | + Assert.assertNotNull(jsapiResult.getPaySign()); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * 测试使用静态工厂方法生成JsapiResult(解耦场景) |
| 65 | + */ |
| 66 | + @Test |
| 67 | + public void testGetJsapiPayInfoStaticMethod() throws Exception { |
| 68 | + // 准备测试数据 |
| 69 | + String testPrepayId = "wx201410272009395522657a690389285100"; |
| 70 | + String testAppId = "wx8888888888888888"; |
| 71 | + KeyPair keyPair = generateKeyPair(); |
| 72 | + PrivateKey privateKey = keyPair.getPrivate(); |
| 73 | + |
| 74 | + // 使用静态工厂方法生成JsapiResult |
| 75 | + WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = |
| 76 | + WxPayUnifiedOrderV3Result.getJsapiPayInfo(testPrepayId, testAppId, privateKey); |
| 77 | + |
| 78 | + // 验证prepayId字段 |
| 79 | + Assert.assertNotNull(jsapiResult.getPrepayId(), "prepayId不应为null"); |
| 80 | + Assert.assertEquals(jsapiResult.getPrepayId(), testPrepayId, "prepayId应该与输入的值相同"); |
| 81 | + |
| 82 | + // 验证其他字段 |
| 83 | + Assert.assertEquals(jsapiResult.getAppId(), testAppId); |
| 84 | + Assert.assertNotNull(jsapiResult.getTimeStamp()); |
| 85 | + Assert.assertNotNull(jsapiResult.getNonceStr()); |
| 86 | + Assert.assertEquals(jsapiResult.getPackageValue(), "prepay_id=" + testPrepayId); |
| 87 | + Assert.assertEquals(jsapiResult.getSignType(), "RSA"); |
| 88 | + Assert.assertNotNull(jsapiResult.getPaySign()); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * 测试使用静态工厂方法生成AppResult(解耦场景) |
| 93 | + */ |
| 94 | + @Test |
| 95 | + public void testGetAppPayInfoStaticMethod() throws Exception { |
| 96 | + // 准备测试数据 |
| 97 | + String testPrepayId = "wx201410272009395522657a690389285100"; |
| 98 | + String testAppId = "wx8888888888888888"; |
| 99 | + String testMchId = "1900000109"; |
| 100 | + KeyPair keyPair = generateKeyPair(); |
| 101 | + PrivateKey privateKey = keyPair.getPrivate(); |
| 102 | + |
| 103 | + // 使用静态工厂方法生成AppResult |
| 104 | + WxPayUnifiedOrderV3Result.AppResult appResult = |
| 105 | + WxPayUnifiedOrderV3Result.getAppPayInfo(testPrepayId, testAppId, testMchId, privateKey); |
| 106 | + |
| 107 | + // 验证prepayId字段 |
| 108 | + Assert.assertNotNull(appResult.getPrepayId(), "prepayId不应为null"); |
| 109 | + Assert.assertEquals(appResult.getPrepayId(), testPrepayId, "prepayId应该与输入的值相同"); |
| 110 | + |
| 111 | + // 验证其他字段 |
| 112 | + Assert.assertEquals(appResult.getAppid(), testAppId); |
| 113 | + Assert.assertEquals(appResult.getPartnerId(), testMchId); |
| 114 | + Assert.assertNotNull(appResult.getTimestamp()); |
| 115 | + Assert.assertNotNull(appResult.getNoncestr()); |
| 116 | + Assert.assertEquals(appResult.getPackageValue(), "Sign=WXPay"); |
| 117 | + Assert.assertNotNull(appResult.getSign()); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * 测试解耦场景:先获取prepayId,后续再生成支付信息 |
| 122 | + */ |
| 123 | + @Test |
| 124 | + public void testDecoupledScenario() throws Exception { |
| 125 | + // 模拟场景:先创建订单获取prepayId |
| 126 | + String testPrepayId = "wx201410272009395522657a690389285100"; |
| 127 | + String testAppId = "wx8888888888888888"; |
| 128 | + KeyPair keyPair = generateKeyPair(); |
| 129 | + PrivateKey privateKey = keyPair.getPrivate(); |
| 130 | + |
| 131 | + // 步骤1:模拟从创建订单接口获取prepayId |
| 132 | + WxPayUnifiedOrderV3Result orderResult = new WxPayUnifiedOrderV3Result(); |
| 133 | + orderResult.setPrepayId(testPrepayId); |
| 134 | + |
| 135 | + // 获取prepayId用于存储 |
| 136 | + String storedPrepayId = orderResult.getPrepayId(); |
| 137 | + Assert.assertEquals(storedPrepayId, testPrepayId); |
| 138 | + |
| 139 | + // 步骤2:后续支付失败时,使用存储的prepayId重新生成支付信息 |
| 140 | + WxPayUnifiedOrderV3Result.JsapiResult newPayInfo = |
| 141 | + WxPayUnifiedOrderV3Result.getJsapiPayInfo(storedPrepayId, testAppId, privateKey); |
| 142 | + |
| 143 | + // 验证重新生成的支付信息 |
| 144 | + Assert.assertEquals(newPayInfo.getPrepayId(), storedPrepayId); |
| 145 | + Assert.assertEquals(newPayInfo.getPackageValue(), "prepay_id=" + storedPrepayId); |
| 146 | + Assert.assertNotNull(newPayInfo.getPaySign()); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * 测试多次生成支付信息,签名应该不同(因为timestamp和nonceStr每次都不同) |
| 151 | + */ |
| 152 | + @Test |
| 153 | + public void testMultipleGenerationsHaveDifferentSignatures() throws Exception { |
| 154 | + String testPrepayId = "wx201410272009395522657a690389285100"; |
| 155 | + String testAppId = "wx8888888888888888"; |
| 156 | + KeyPair keyPair = generateKeyPair(); |
| 157 | + PrivateKey privateKey = keyPair.getPrivate(); |
| 158 | + |
| 159 | + // 生成第一次支付信息 |
| 160 | + WxPayUnifiedOrderV3Result.JsapiResult result1 = |
| 161 | + WxPayUnifiedOrderV3Result.getJsapiPayInfo(testPrepayId, testAppId, privateKey); |
| 162 | + |
| 163 | + // 等待一秒确保timestamp不同 |
| 164 | + Thread.sleep(1000); |
| 165 | + |
| 166 | + // 生成第二次支付信息 |
| 167 | + WxPayUnifiedOrderV3Result.JsapiResult result2 = |
| 168 | + WxPayUnifiedOrderV3Result.getJsapiPayInfo(testPrepayId, testAppId, privateKey); |
| 169 | + |
| 170 | + // prepayId应该相同 |
| 171 | + Assert.assertEquals(result1.getPrepayId(), result2.getPrepayId()); |
| 172 | + |
| 173 | + // 但是timestamp、nonceStr和签名应该不同 |
| 174 | + Assert.assertNotEquals(result1.getTimeStamp(), result2.getTimeStamp(), "timestamp应该不同"); |
| 175 | + Assert.assertNotEquals(result1.getNonceStr(), result2.getNonceStr(), "nonceStr应该不同"); |
| 176 | + Assert.assertNotEquals(result1.getPaySign(), result2.getPaySign(), "签名应该不同"); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * 测试AppResult中的prepayId字段 |
| 181 | + */ |
| 182 | + @Test |
| 183 | + public void testAppResultWithPrepayId() throws Exception { |
| 184 | + String testPrepayId = "wx201410272009395522657a690389285100"; |
| 185 | + String testAppId = "wx8888888888888888"; |
| 186 | + String testMchId = "1900000109"; |
| 187 | + KeyPair keyPair = generateKeyPair(); |
| 188 | + PrivateKey privateKey = keyPair.getPrivate(); |
| 189 | + |
| 190 | + WxPayUnifiedOrderV3Result result = new WxPayUnifiedOrderV3Result(); |
| 191 | + result.setPrepayId(testPrepayId); |
| 192 | + |
| 193 | + // 调用getPayInfo生成AppResult |
| 194 | + WxPayUnifiedOrderV3Result.AppResult appResult = |
| 195 | + result.getPayInfo(TradeTypeEnum.APP, testAppId, testMchId, privateKey); |
| 196 | + |
| 197 | + // 验证prepayId字段 |
| 198 | + Assert.assertNotNull(appResult.getPrepayId(), "prepayId不应为null"); |
| 199 | + Assert.assertEquals(appResult.getPrepayId(), testPrepayId, "prepayId应该与设置的值相同"); |
| 200 | + } |
| 201 | +} |
0 commit comments