大改:重构项目

This commit is contained in:
2025-07-27 11:31:59 +08:00
parent 0c027252f0
commit e16ba79b10
4 changed files with 390 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
package com.gxwebsoft.test;
import com.gxwebsoft.common.core.config.CertificateProperties;
import com.gxwebsoft.common.core.utils.CertificateLoader;
import com.gxwebsoft.common.core.utils.WechatCertAutoConfig;
import com.wechat.pay.java.core.Config;
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 CertificateTest {
@Autowired
private CertificateProperties certConfig;
@Autowired
private CertificateLoader certificateLoader;
@Autowired
private WechatCertAutoConfig wechatCertAutoConfig;
@Test
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());
// 测试证书加载
String privateKeyFile = certificateLoader.loadCertificatePath(privateKeyPath);
System.out.println("私钥文件路径: " + privateKeyFile);
// 测试自动证书配置
System.out.println("=== 测试自动证书配置 ===");
Config config = wechatCertAutoConfig.createAutoConfig(
"1723321338", // 测试商户号
privateKeyFile,
"test-serial-number", // 测试序列号
"test-api-key" // 测试API密钥
);
System.out.println("自动证书配置创建成功: " + (config != null));
} catch (Exception e) {
System.err.println("证书测试失败: " + e.getMessage());
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,89 @@
package com.gxwebsoft.test;
import com.gxwebsoft.common.core.config.CertificateProperties;
import com.gxwebsoft.common.core.utils.CertificateLoader;
import com.gxwebsoft.common.core.utils.WechatCertAutoConfig;
import com.gxwebsoft.common.core.service.PaymentCacheService;
import com.gxwebsoft.common.system.entity.Payment;
import com.wechat.pay.java.core.Config;
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 WechatPayConfigTest {
@Autowired
private CertificateProperties certConfig;
@Autowired
private CertificateLoader certificateLoader;
@Autowired
private WechatCertAutoConfig wechatCertAutoConfig;
@Autowired
private PaymentCacheService paymentCacheService;
@Test
public void testWechatPayConfig() {
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());
// 测试证书加载
String privateKeyFile = certificateLoader.loadCertificatePath(privateKeyPath);
System.out.println("私钥文件路径: " + privateKeyFile);
// 测试数据库支付配置
System.out.println("=== 测试数据库支付配置 ===");
try {
Payment payment = paymentCacheService.getPaymentConfig(0, 10550); // 微信支付租户ID 10550
System.out.println("数据库配置获取成功:");
System.out.println("商户号: " + payment.getMchId());
System.out.println("序列号: " + payment.getMerchantSerialNumber());
System.out.println("API密钥: " + (payment.getApiKey() != null ? "已配置(长度:" + payment.getApiKey().length() + ")" : "未配置"));
System.out.println("应用ID: " + payment.getAppId());
// 使用数据库配置进行测试
if (payment.getMchId() != null && payment.getMerchantSerialNumber() != null && payment.getApiKey() != null) {
Config dbConfig = wechatCertAutoConfig.createAutoConfig(
payment.getMchId(),
privateKeyFile,
payment.getMerchantSerialNumber(),
payment.getApiKey()
);
System.out.println("使用数据库配置创建成功: " + (dbConfig != null));
} else {
System.out.println("数据库配置不完整,无法创建微信支付配置");
}
} catch (Exception e) {
System.err.println("数据库配置获取失败: " + e.getMessage());
// 回退到配置文件参数
System.out.println("=== 回退到配置文件参数 ===");
String devApiKey = certConfig.getWechatPay().getDev().getApiV3Key();
System.out.println("API密钥: " + (devApiKey != null ? "已配置(长度:" + devApiKey.length() + ")" : "未配置"));
}
System.out.println("=== 测试完成 ===");
} catch (Exception e) {
System.err.println("微信支付配置测试失败: " + e.getMessage());
e.printStackTrace();
}
}
}