修复:商品描述过长导致支付失败的bug

This commit is contained in:
2025-08-10 13:33:11 +08:00
parent a5c9dda2a1
commit 591ed6a2fe

View File

@@ -19,13 +19,13 @@ public class WechatPayDescriptionTest {
public void testTruncateToByteLimit() { public void testTruncateToByteLimit() {
// 测试正常情况 - 字符串长度在限制内 // 测试正常情况 - 字符串长度在限制内
String shortText = "正常商品描述"; String shortText = "正常商品描述";
String result1 = truncateToByteLimit(shortText, 127); String result1 = WechatPayUtils.truncateToByteLimit(shortText, 127);
assertEquals(shortText, result1); assertEquals(shortText, result1);
assertTrue(result1.getBytes(StandardCharsets.UTF_8).length <= 127); assertTrue(result1.getBytes(StandardCharsets.UTF_8).length <= 127);
// 测试超长中文字符串 // 测试超长中文字符串
String longText = "【湾区认证】【百千万工程帮扶产品 通过远方320项检测 湾区认证 丰江桥佛手瓜面】独特风味低脂轻食便捷速食非油炸 720g/袋"; String longText = "【湾区认证】【百千万工程帮扶产品 通过远方320项检测 湾区认证 丰江桥佛手瓜面】独特风味低脂轻食便捷速食非油炸 720g/袋";
String result2 = truncateToByteLimit(longText, 127); String result2 = WechatPayUtils.truncateToByteLimit(longText, 127);
assertNotNull(result2); assertNotNull(result2);
assertTrue(result2.getBytes(StandardCharsets.UTF_8).length <= 127); assertTrue(result2.getBytes(StandardCharsets.UTF_8).length <= 127);
System.out.println("原始文本字节数: " + longText.getBytes(StandardCharsets.UTF_8).length); System.out.println("原始文本字节数: " + longText.getBytes(StandardCharsets.UTF_8).length);
@@ -33,18 +33,18 @@ public class WechatPayDescriptionTest {
System.out.println("截断后字节数: " + result2.getBytes(StandardCharsets.UTF_8).length); System.out.println("截断后字节数: " + result2.getBytes(StandardCharsets.UTF_8).length);
// 测试null和空字符串 // 测试null和空字符串
assertNull(truncateToByteLimit(null, 127)); assertNull(WechatPayUtils.truncateToByteLimit(null, 127));
assertEquals("", truncateToByteLimit("", 127)); assertEquals("", WechatPayUtils.truncateToByteLimit("", 127));
// 测试英文字符串 // 测试英文字符串
String englishText = "This is a very long English description that might exceed the byte limit for WeChat Pay description field"; String englishText = "This is a very long English description that might exceed the byte limit for WeChat Pay description field";
String result3 = truncateToByteLimit(englishText, 127); String result3 = WechatPayUtils.truncateToByteLimit(englishText, 127);
assertNotNull(result3); assertNotNull(result3);
assertTrue(result3.getBytes(StandardCharsets.UTF_8).length <= 127); assertTrue(result3.getBytes(StandardCharsets.UTF_8).length <= 127);
// 测试混合字符串 // 测试混合字符串
String mixedText = "Product Name 产品名称 with special characters @#$%^&*()"; String mixedText = "Product Name 产品名称 with special characters @#$%^&*()";
String result4 = truncateToByteLimit(mixedText, 50); String result4 = WechatPayUtils.truncateToByteLimit(mixedText, 50);
assertNotNull(result4); assertNotNull(result4);
assertTrue(result4.getBytes(StandardCharsets.UTF_8).length <= 50); assertTrue(result4.getBytes(StandardCharsets.UTF_8).length <= 50);
} }
@@ -61,7 +61,7 @@ public class WechatPayDescriptionTest {
assertTrue(originalBytes > 127, "原始文本应该超过127字节"); assertTrue(originalBytes > 127, "原始文本应该超过127字节");
// 测试截断 // 测试截断
String truncated = truncateToByteLimit(errorText, 127); String truncated = WechatPayUtils.processDescription(errorText);
int truncatedBytes = truncated.getBytes(StandardCharsets.UTF_8).length; int truncatedBytes = truncated.getBytes(StandardCharsets.UTF_8).length;
System.out.println("截断后文本: " + truncated); System.out.println("截断后文本: " + truncated);