更新完成:完成微信支付的功能开发和测试
This commit is contained in:
@@ -29,19 +29,31 @@ public class CertificateTest {
|
||||
public void testCertificateLoading() {
|
||||
try {
|
||||
System.out.println("=== 证书加载测试 ===");
|
||||
|
||||
|
||||
// 测试租户ID
|
||||
String tenantId = "10550";
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
|
||||
|
||||
System.out.println("证书路径: " + privateKeyPath);
|
||||
System.out.println("加载模式: " + certConfig.getLoadMode());
|
||||
|
||||
System.out.println("开发环境证书路径前缀: " + certConfig.getDevCertPath());
|
||||
|
||||
// 检查证书文件是否存在
|
||||
boolean exists = certificateLoader.certificateExists(privateKeyPath);
|
||||
System.out.println("证书文件是否存在: " + exists);
|
||||
|
||||
if (!exists) {
|
||||
System.err.println("❌ 证书文件不存在: " + privateKeyPath);
|
||||
System.out.println("💡 请确认证书文件已放置在正确位置:");
|
||||
System.out.println(" src/main/resources/dev/wechat/10550/apiclient_key.pem");
|
||||
return;
|
||||
}
|
||||
|
||||
// 测试证书加载
|
||||
String privateKeyFile = certificateLoader.loadCertificatePath(privateKeyPath);
|
||||
System.out.println("私钥文件路径: " + privateKeyFile);
|
||||
|
||||
System.out.println("✅ 私钥文件加载成功: " + privateKeyFile);
|
||||
|
||||
// 测试自动证书配置
|
||||
System.out.println("=== 测试自动证书配置 ===");
|
||||
Config config = wechatCertAutoConfig.createAutoConfig(
|
||||
@@ -50,11 +62,45 @@ public class CertificateTest {
|
||||
"test-serial-number", // 测试序列号
|
||||
"test-api-key" // 测试API密钥
|
||||
);
|
||||
|
||||
System.out.println("自动证书配置创建成功: " + (config != null));
|
||||
|
||||
|
||||
System.out.println("✅ 自动证书配置创建成功");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("证书测试失败: " + e.getMessage());
|
||||
System.err.println("❌ 证书测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotificationCertificateConfig() {
|
||||
try {
|
||||
System.out.println("=== 异步通知证书配置测试 ===");
|
||||
|
||||
// 模拟异步通知中的证书配置逻辑
|
||||
String tenantId = "10550";
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
|
||||
System.out.println("租户ID: " + tenantId);
|
||||
System.out.println("证书目录: " + tenantCertPath);
|
||||
System.out.println("私钥路径: " + privateKeyPath);
|
||||
|
||||
// 检查证书文件是否存在
|
||||
if (!certificateLoader.certificateExists(privateKeyPath)) {
|
||||
System.err.println("❌ 证书文件不存在: " + privateKeyPath);
|
||||
throw new RuntimeException("证书文件不存在: " + privateKeyPath);
|
||||
}
|
||||
|
||||
String privateKey = certificateLoader.loadCertificatePath(privateKeyPath);
|
||||
String apiV3Key = certConfig.getWechatPay().getDev().getApiV3Key();
|
||||
|
||||
System.out.println("✅ 私钥文件加载成功: " + privateKey);
|
||||
System.out.println("APIv3密钥配置: " + (apiV3Key != null ? "已配置" : "未配置"));
|
||||
|
||||
System.out.println("✅ 异步通知证书配置测试通过");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("❌ 异步通知证书配置测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.gxwebsoft.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* 异步通知证书修复验证测试
|
||||
*/
|
||||
public class NotificationCertificateFixTest {
|
||||
|
||||
@Test
|
||||
public void testCertificatePathFix() {
|
||||
System.out.println("=== 异步通知证书路径修复验证 ===");
|
||||
|
||||
// 模拟异步通知中的证书路径构建逻辑
|
||||
String tenantId = "10550";
|
||||
String devCertPath = "dev";
|
||||
String certDir = "wechat";
|
||||
String privateKeyFile = "apiclient_key.pem";
|
||||
|
||||
// 修复后的路径构建逻辑
|
||||
String tenantCertPath = devCertPath + "/" + certDir + "/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + privateKeyFile;
|
||||
|
||||
System.out.println("租户ID: " + tenantId);
|
||||
System.out.println("证书目录: " + tenantCertPath);
|
||||
System.out.println("私钥路径: " + privateKeyPath);
|
||||
|
||||
// 测试从classpath加载证书
|
||||
try {
|
||||
Resource resource = new ClassPathResource(privateKeyPath);
|
||||
System.out.println("证书资源存在: " + resource.exists());
|
||||
|
||||
if (resource.exists()) {
|
||||
// 模拟CertificateLoader.loadFromClasspath的逻辑
|
||||
Path tempFile = Files.createTempFile("cert_", ".pem");
|
||||
try (InputStream inputStream = resource.getInputStream()) {
|
||||
Files.copy(inputStream, tempFile, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
String tempPath = tempFile.toAbsolutePath().toString();
|
||||
System.out.println("✅ 证书加载成功: " + tempPath);
|
||||
|
||||
// 验证临时文件
|
||||
File tempCertFile = new File(tempPath);
|
||||
System.out.println("临时证书文件大小: " + tempCertFile.length() + " bytes");
|
||||
|
||||
// 清理临时文件
|
||||
Files.deleteIfExists(tempFile);
|
||||
|
||||
} else {
|
||||
System.err.println("❌ 证书文件不存在: " + privateKeyPath);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("❌ 证书加载失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("=== 测试完成 ===");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllTenantCertificates() {
|
||||
System.out.println("=== 测试所有租户证书 ===");
|
||||
|
||||
String[] tenantIds = {"10398", "10550"};
|
||||
String devCertPath = "dev";
|
||||
String certDir = "wechat";
|
||||
String privateKeyFile = "apiclient_key.pem";
|
||||
|
||||
for (String tenantId : tenantIds) {
|
||||
System.out.println("\n--- 测试租户: " + tenantId + " ---");
|
||||
|
||||
String tenantCertPath = devCertPath + "/" + certDir + "/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + privateKeyFile;
|
||||
|
||||
System.out.println("证书路径: " + privateKeyPath);
|
||||
|
||||
try {
|
||||
Resource resource = new ClassPathResource(privateKeyPath);
|
||||
if (resource.exists()) {
|
||||
System.out.println("✅ 租户 " + tenantId + " 证书存在");
|
||||
} else {
|
||||
System.out.println("❌ 租户 " + tenantId + " 证书不存在");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("❌ 租户 " + tenantId + " 证书检查失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\n=== 所有租户证书测试完成 ===");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package com.gxwebsoft.test;
|
||||
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.core.utils.CertificateLoader;
|
||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
/**
|
||||
* 微信支付配置验证测试
|
||||
*/
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("dev")
|
||||
public class WechatPayConfigValidationTest {
|
||||
|
||||
@Autowired
|
||||
private CertificateProperties certConfig;
|
||||
|
||||
@Autowired
|
||||
private CertificateLoader certificateLoader;
|
||||
|
||||
@Autowired
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Test
|
||||
public void testWechatPayConfigValidation() {
|
||||
System.out.println("=== 微信支付配置验证 ===");
|
||||
|
||||
// 1. 检查配置文件中的 APIv3 密钥
|
||||
String configApiV3Key = certConfig.getWechatPay().getDev().getApiV3Key();
|
||||
System.out.println("配置文件 APIv3 密钥: " + configApiV3Key);
|
||||
System.out.println("配置文件 APIv3 密钥长度: " + (configApiV3Key != null ? configApiV3Key.length() : 0));
|
||||
|
||||
// 2. 检查 Redis 中的支付配置
|
||||
String tenantId = "10550";
|
||||
String redisKey = "Payment:1:" + tenantId;
|
||||
Payment payment = redisUtil.get(redisKey, Payment.class);
|
||||
|
||||
if (payment != null) {
|
||||
System.out.println("\n=== Redis 支付配置 ===");
|
||||
System.out.println("商户号: " + payment.getMchId());
|
||||
System.out.println("应用ID: " + payment.getAppId());
|
||||
System.out.println("数据库 APIv3 密钥: " + payment.getApiKey());
|
||||
System.out.println("数据库 APIv3 密钥长度: " + (payment.getApiKey() != null ? payment.getApiKey().length() : 0));
|
||||
System.out.println("商户证书序列号: " + payment.getMerchantSerialNumber());
|
||||
|
||||
// 3. 比较两个 APIv3 密钥
|
||||
System.out.println("\n=== APIv3 密钥比较 ===");
|
||||
boolean keysMatch = (configApiV3Key != null && configApiV3Key.equals(payment.getApiKey()));
|
||||
System.out.println("配置文件与数据库密钥是否一致: " + keysMatch);
|
||||
|
||||
if (!keysMatch) {
|
||||
System.out.println("⚠️ 警告: 配置文件与数据库中的 APIv3 密钥不一致!");
|
||||
System.out.println("配置文件密钥: " + configApiV3Key);
|
||||
System.out.println("数据库密钥: " + payment.getApiKey());
|
||||
}
|
||||
|
||||
} else {
|
||||
System.out.println("❌ 未找到 Redis 中的支付配置: " + redisKey);
|
||||
|
||||
// 尝试其他可能的键格式
|
||||
String[] possibleKeys = {
|
||||
"Payment:1:10550",
|
||||
"Payment:0:10550",
|
||||
"Payment:10",
|
||||
"Payment:1" + "0" // Payment:10
|
||||
};
|
||||
|
||||
System.out.println("\n=== 尝试其他 Redis 键格式 ===");
|
||||
for (String key : possibleKeys) {
|
||||
Payment p = redisUtil.get(key, Payment.class);
|
||||
if (p != null) {
|
||||
System.out.println("✅ 找到支付配置: " + key);
|
||||
System.out.println(" 商户号: " + p.getMchId());
|
||||
System.out.println(" APIv3密钥: " + p.getApiKey());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 验证证书文件
|
||||
System.out.println("\n=== 证书文件验证 ===");
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
|
||||
boolean certExists = certificateLoader.certificateExists(privateKeyPath);
|
||||
System.out.println("证书文件存在: " + certExists);
|
||||
System.out.println("证书路径: " + privateKeyPath);
|
||||
|
||||
if (certExists) {
|
||||
try {
|
||||
String privateKey = certificateLoader.loadCertificatePath(privateKeyPath);
|
||||
System.out.println("✅ 证书加载成功: " + privateKey);
|
||||
} catch (Exception e) {
|
||||
System.out.println("❌ 证书加载失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\n=== 验证完成 ===");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApiV3KeyValidation() {
|
||||
System.out.println("=== APIv3 密钥格式验证 ===");
|
||||
|
||||
String configKey = certConfig.getWechatPay().getDev().getApiV3Key();
|
||||
|
||||
if (configKey != null) {
|
||||
System.out.println("APIv3 密钥: " + configKey);
|
||||
System.out.println("密钥长度: " + configKey.length());
|
||||
|
||||
// APIv3 密钥应该是32位字符串
|
||||
if (configKey.length() == 32) {
|
||||
System.out.println("✅ APIv3 密钥长度正确 (32位)");
|
||||
} else {
|
||||
System.out.println("❌ APIv3 密钥长度错误,应为32位,实际为: " + configKey.length());
|
||||
}
|
||||
|
||||
// 检查是否包含特殊字符
|
||||
boolean hasSpecialChars = !configKey.matches("^[a-zA-Z0-9]+$");
|
||||
if (hasSpecialChars) {
|
||||
System.out.println("⚠️ APIv3 密钥包含特殊字符,可能导致解密失败");
|
||||
} else {
|
||||
System.out.println("✅ APIv3 密钥格式正确 (仅包含字母和数字)");
|
||||
}
|
||||
|
||||
} else {
|
||||
System.out.println("❌ APIv3 密钥未配置");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user