修复微信支付,兼容公钥模块
This commit is contained in:
@@ -0,0 +1,319 @@
|
||||
package com.gxwebsoft.common.core.controller;
|
||||
|
||||
import com.gxwebsoft.common.core.utils.WechatPayCertificateDiagnostic;
|
||||
import com.gxwebsoft.common.core.utils.WechatPayConfigChecker;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 微信支付诊断控制器
|
||||
* 用于诊断和解决微信支付证书相关问题
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-29
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/system/wechat-pay-diagnostic")
|
||||
@Tag(name = "微信支付诊断", description = "微信支付证书诊断和问题排查")
|
||||
public class WechatPayDiagnosticController extends com.gxwebsoft.common.core.web.BaseController {
|
||||
|
||||
@Autowired
|
||||
private WechatPayCertificateDiagnostic certificateDiagnostic;
|
||||
|
||||
@Autowired
|
||||
private com.gxwebsoft.common.core.service.PaymentCacheService paymentCacheService;
|
||||
|
||||
@Autowired
|
||||
private com.gxwebsoft.common.core.utils.WechatPayConfigChecker configChecker;
|
||||
|
||||
@Value("${spring.profiles.active:dev}")
|
||||
private String activeProfile;
|
||||
|
||||
@Operation(summary = "诊断租户微信支付证书配置")
|
||||
@GetMapping("/diagnose/{tenantId}")
|
||||
@PreAuthorize("hasAuthority('system:payment:view')")
|
||||
public ApiResult<Map<String, Object>> diagnoseTenantCertificate(
|
||||
@Parameter(description = "租户ID", example = "10550") @PathVariable Integer tenantId) {
|
||||
|
||||
try {
|
||||
log.info("开始诊断租户 {} 的微信支付证书配置", tenantId);
|
||||
|
||||
// 获取支付配置 (微信支付类型为0)
|
||||
Payment payment = paymentCacheService.getWechatPayConfig(tenantId);
|
||||
if (payment == null) {
|
||||
Map<String, Object> errorResponse = new HashMap<>();
|
||||
errorResponse.put("tenantId", tenantId);
|
||||
errorResponse.put("error", "支付配置不存在");
|
||||
return fail("租户 " + tenantId + " 的微信支付配置不存在", errorResponse);
|
||||
}
|
||||
|
||||
// 执行诊断
|
||||
WechatPayCertificateDiagnostic.DiagnosticResult result =
|
||||
certificateDiagnostic.diagnoseCertificateConfig(payment, tenantId, activeProfile);
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("tenantId", tenantId);
|
||||
response.put("environment", activeProfile);
|
||||
response.put("hasErrors", result.hasErrors());
|
||||
response.put("errors", result.getErrors());
|
||||
response.put("warnings", result.getWarnings());
|
||||
response.put("info", result.getInfo());
|
||||
response.put("recommendations", result.getRecommendations());
|
||||
response.put("fullReport", result.getFullReport());
|
||||
|
||||
if (result.hasErrors()) {
|
||||
return fail("诊断发现问题", response);
|
||||
} else {
|
||||
return success("诊断完成", response);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("诊断租户 {} 证书配置时发生异常", tenantId, e);
|
||||
Map<String, Object> errorResponse = new HashMap<>();
|
||||
errorResponse.put("tenantId", tenantId);
|
||||
errorResponse.put("exception", e.getMessage());
|
||||
return fail("诊断过程中发生异常: " + e.getMessage(), errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取证书问题解决方案")
|
||||
@GetMapping("/solutions")
|
||||
@PreAuthorize("hasAuthority('system:payment:view')")
|
||||
public ApiResult<Map<String, Object>> getCertificateSolutions() {
|
||||
Map<String, Object> solutions = new HashMap<>();
|
||||
|
||||
solutions.put("commonIssues", Map.of(
|
||||
"X509Certificate.getSerialNumber() null", "证书对象为空,通常是自动证书配置失败导致",
|
||||
"404错误", "商户平台未开启API安全功能或未申请使用微信支付公钥",
|
||||
"证书序列号错误", "请检查商户平台中的证书序列号是否正确",
|
||||
"APIv3密钥错误", "请确认APIv3密钥是否正确设置",
|
||||
"私钥文件不存在", "请检查私钥文件路径是否正确",
|
||||
"网络连接问题", "请检查网络连接是否正常"
|
||||
));
|
||||
|
||||
solutions.put("stepByStepSolutions", Map.of(
|
||||
"开启API安全", "登录微信商户平台 -> 账户中心 -> API安全 -> 申请使用微信支付公钥",
|
||||
"获取证书序列号", "在API安全页面查看或重新下载证书",
|
||||
"设置APIv3密钥", "在API安全页面设置APIv3密钥(32位字符串)",
|
||||
"检查私钥文件", "确保apiclient_key.pem文件存在且路径正确",
|
||||
"使用自动证书配置", "推荐使用RSAAutoCertificateConfig,可自动管理平台证书"
|
||||
));
|
||||
|
||||
solutions.put("configurationAdvice", Map.of(
|
||||
"开发环境", "证书文件放在 src/main/resources/dev/wechat/{tenantId}/ 目录下",
|
||||
"生产环境", "证书文件放在 Docker 挂载卷或指定的文件系统路径",
|
||||
"证书文件要求", "需要 apiclient_key.pem(私钥)和 apiclient_cert.pem(商户证书)",
|
||||
"自动配置优势", "无需手动管理微信支付平台证书,自动下载和更新"
|
||||
));
|
||||
|
||||
solutions.put("troubleshootingSteps", new String[]{
|
||||
"1. 检查商户平台是否已开启API安全功能",
|
||||
"2. 确认已申请使用微信支付公钥",
|
||||
"3. 验证商户证书序列号是否正确",
|
||||
"4. 检查APIv3密钥是否为32位字符串",
|
||||
"5. 确认私钥文件路径正确且文件存在",
|
||||
"6. 测试网络连接是否正常",
|
||||
"7. 查看详细错误日志进行进一步诊断"
|
||||
});
|
||||
|
||||
return success("获取解决方案成功", solutions);
|
||||
}
|
||||
|
||||
@Operation(summary = "测试证书配置")
|
||||
@PostMapping("/test/{tenantId}")
|
||||
@PreAuthorize("hasAuthority('system:payment:edit')")
|
||||
public ApiResult<Map<String, Object>> testCertificateConfig(
|
||||
@Parameter(description = "租户ID", example = "10550") @PathVariable Integer tenantId) {
|
||||
|
||||
try {
|
||||
log.info("开始测试租户 {} 的证书配置", tenantId);
|
||||
|
||||
// 获取支付配置 (微信支付类型为0)
|
||||
Payment payment = paymentCacheService.getWechatPayConfig(tenantId);
|
||||
if (payment == null) {
|
||||
Map<String, Object> errorResponse = new HashMap<>();
|
||||
errorResponse.put("tenantId", tenantId);
|
||||
errorResponse.put("error", "支付配置不存在");
|
||||
return fail("租户 " + tenantId + " 的微信支付配置不存在", errorResponse);
|
||||
}
|
||||
|
||||
// 执行诊断
|
||||
WechatPayCertificateDiagnostic.DiagnosticResult result =
|
||||
certificateDiagnostic.diagnoseCertificateConfig(payment, tenantId, activeProfile);
|
||||
|
||||
Map<String, Object> testResult = new HashMap<>();
|
||||
testResult.put("tenantId", tenantId);
|
||||
testResult.put("configurationValid", !result.hasErrors());
|
||||
testResult.put("testTime", System.currentTimeMillis());
|
||||
testResult.put("environment", activeProfile);
|
||||
|
||||
if (result.hasErrors()) {
|
||||
testResult.put("status", "FAILED");
|
||||
testResult.put("errors", result.getErrors());
|
||||
testResult.put("recommendations", result.getRecommendations());
|
||||
return fail("证书配置测试失败", testResult);
|
||||
} else {
|
||||
testResult.put("status", "SUCCESS");
|
||||
testResult.put("message", "证书配置正常");
|
||||
return success("证书配置测试通过", testResult);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("测试租户 {} 证书配置时发生异常", tenantId, e);
|
||||
Map<String, Object> errorResponse = new HashMap<>();
|
||||
errorResponse.put("tenantId", tenantId);
|
||||
errorResponse.put("exception", e.getMessage());
|
||||
return fail("测试过程中发生异常: " + e.getMessage(), errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取环境信息")
|
||||
@GetMapping("/environment")
|
||||
@PreAuthorize("hasAuthority('system:payment:view')")
|
||||
public ApiResult<Map<String, Object>> getEnvironmentInfo() {
|
||||
Map<String, Object> envInfo = new HashMap<>();
|
||||
envInfo.put("activeProfile", activeProfile);
|
||||
envInfo.put("javaVersion", System.getProperty("java.version"));
|
||||
envInfo.put("osName", System.getProperty("os.name"));
|
||||
envInfo.put("userDir", System.getProperty("user.dir"));
|
||||
envInfo.put("timestamp", System.currentTimeMillis());
|
||||
|
||||
return success("获取环境信息成功", envInfo);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取证书配置指南")
|
||||
@GetMapping("/guide")
|
||||
public ApiResult<Map<String, Object>> getCertificateGuide() {
|
||||
Map<String, Object> guide = new HashMap<>();
|
||||
|
||||
guide.put("overview", "微信支付证书配置完整指南");
|
||||
|
||||
guide.put("prerequisites", new String[]{
|
||||
"1. 拥有微信支付商户账号",
|
||||
"2. 已完成商户入驻和资质审核",
|
||||
"3. 具备开发者权限"
|
||||
});
|
||||
|
||||
guide.put("merchantPlatformSteps", new String[]{
|
||||
"1. 登录微信商户平台 (pay.weixin.qq.com)",
|
||||
"2. 进入【账户中心】->【API安全】",
|
||||
"3. 点击【申请使用微信支付公钥】",
|
||||
"4. 下载商户证书(apiclient_cert.pem 和 apiclient_key.pem)",
|
||||
"5. 设置APIv3密钥(32位字符串)",
|
||||
"6. 记录商户证书序列号"
|
||||
});
|
||||
|
||||
guide.put("developmentSetup", new String[]{
|
||||
"1. 在项目中创建证书目录:src/main/resources/dev/wechat/{tenantId}/",
|
||||
"2. 将 apiclient_key.pem 放入该目录",
|
||||
"3. 将 apiclient_cert.pem 放入该目录(可选,自动配置不需要)",
|
||||
"4. 在数据库中配置支付信息:商户号、应用ID、证书序列号、APIv3密钥"
|
||||
});
|
||||
|
||||
guide.put("productionSetup", new String[]{
|
||||
"1. 将证书文件上传到服务器指定目录",
|
||||
"2. 确保应用有读取证书文件的权限",
|
||||
"3. 在数据库中配置正确的证书文件路径",
|
||||
"4. 测试证书配置是否正常"
|
||||
});
|
||||
|
||||
guide.put("bestPractices", new String[]{
|
||||
"1. 使用RSAAutoCertificateConfig自动证书配置",
|
||||
"2. 定期检查证书有效期",
|
||||
"3. 妥善保管私钥文件",
|
||||
"4. 使用HTTPS传输敏感信息",
|
||||
"5. 定期更新微信支付SDK版本"
|
||||
});
|
||||
|
||||
return success("获取配置指南成功", guide);
|
||||
}
|
||||
|
||||
@Operation(summary = "快速检查租户配置状态")
|
||||
@GetMapping("/check/{tenantId}")
|
||||
@PreAuthorize("hasAuthority('system:payment:view')")
|
||||
public ApiResult<Map<String, Object>> quickCheckConfig(
|
||||
@Parameter(description = "租户ID", example = "10547") @PathVariable Integer tenantId) {
|
||||
|
||||
try {
|
||||
log.info("快速检查租户 {} 的配置状态", tenantId);
|
||||
|
||||
WechatPayConfigChecker.ConfigStatus status = configChecker.checkTenantConfig(tenantId);
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("tenantId", status.tenantId);
|
||||
response.put("environment", status.environment);
|
||||
response.put("configMode", status.configMode);
|
||||
response.put("configComplete", status.configComplete);
|
||||
response.put("hasError", status.hasError);
|
||||
response.put("errorMessage", status.errorMessage);
|
||||
response.put("recommendation", status.recommendation);
|
||||
response.put("issues", status.issues);
|
||||
|
||||
// 详细配置信息
|
||||
Map<String, Object> configDetails = new HashMap<>();
|
||||
configDetails.put("merchantId", status.merchantId);
|
||||
configDetails.put("appId", status.appId);
|
||||
configDetails.put("serialNumber", status.serialNumber);
|
||||
configDetails.put("hasApiKey", status.hasApiKey);
|
||||
configDetails.put("apiKeyLength", status.apiKeyLength);
|
||||
configDetails.put("hasPublicKey", status.hasPublicKey);
|
||||
configDetails.put("publicKeyFile", status.publicKeyFile);
|
||||
configDetails.put("publicKeyId", status.publicKeyId);
|
||||
configDetails.put("publicKeyExists", status.publicKeyExists);
|
||||
configDetails.put("privateKeyExists", status.privateKeyExists);
|
||||
configDetails.put("merchantCertExists", status.merchantCertExists);
|
||||
response.put("configDetails", configDetails);
|
||||
|
||||
if (status.hasError || !status.configComplete) {
|
||||
return fail("配置检查发现问题", response);
|
||||
} else {
|
||||
return success("配置检查通过", response);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("检查租户 {} 配置状态时发生异常", tenantId, e);
|
||||
Map<String, Object> errorResponse = new HashMap<>();
|
||||
errorResponse.put("tenantId", tenantId);
|
||||
errorResponse.put("exception", e.getMessage());
|
||||
return fail("检查过程中发生异常: " + e.getMessage(), errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取配置建议")
|
||||
@GetMapping("/advice/{tenantId}")
|
||||
@PreAuthorize("hasAuthority('system:payment:view')")
|
||||
public ApiResult<Map<String, Object>> getConfigAdvice(
|
||||
@Parameter(description = "租户ID", example = "10547") @PathVariable Integer tenantId) {
|
||||
|
||||
try {
|
||||
String advice = configChecker.generateConfigAdvice(tenantId);
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("tenantId", tenantId);
|
||||
response.put("advice", advice);
|
||||
response.put("timestamp", System.currentTimeMillis());
|
||||
|
||||
return success("获取配置建议成功", response);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取租户 {} 配置建议时发生异常", tenantId, e);
|
||||
Map<String, Object> errorResponse = new HashMap<>();
|
||||
errorResponse.put("tenantId", tenantId);
|
||||
errorResponse.put("exception", e.getMessage());
|
||||
return fail("获取建议过程中发生异常: " + e.getMessage(), errorResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
|
||||
/**
|
||||
* 微信支付证书诊断工具
|
||||
* 专门用于诊断和解决证书相关问题
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-29
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WechatPayCertificateDiagnostic {
|
||||
|
||||
private final CertificateProperties certConfig;
|
||||
private final CertificateLoader certificateLoader;
|
||||
|
||||
public WechatPayCertificateDiagnostic(CertificateProperties certConfig, CertificateLoader certificateLoader) {
|
||||
this.certConfig = certConfig;
|
||||
this.certificateLoader = certificateLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* 全面诊断微信支付证书配置
|
||||
*
|
||||
* @param payment 支付配置
|
||||
* @param tenantId 租户ID
|
||||
* @param environment 环境(dev/prod)
|
||||
* @return 诊断结果
|
||||
*/
|
||||
public DiagnosticResult diagnoseCertificateConfig(Payment payment, Integer tenantId, String environment) {
|
||||
DiagnosticResult result = new DiagnosticResult();
|
||||
|
||||
log.info("=== 开始微信支付证书诊断 ===");
|
||||
log.info("租户ID: {}, 环境: {}", tenantId, environment);
|
||||
|
||||
try {
|
||||
// 1. 检查基本配置
|
||||
checkBasicConfig(payment, result);
|
||||
|
||||
// 2. 检查证书文件
|
||||
checkCertificateFiles(payment, tenantId, environment, result);
|
||||
|
||||
// 3. 检查证书内容
|
||||
validateCertificateContent(payment, tenantId, environment, result);
|
||||
|
||||
// 4. 生成建议
|
||||
generateRecommendations(result);
|
||||
|
||||
} catch (Exception e) {
|
||||
result.addError("诊断过程中发生异常: " + e.getMessage());
|
||||
log.error("证书诊断异常", e);
|
||||
}
|
||||
|
||||
log.info("=== 证书诊断完成 ===");
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查基本配置
|
||||
*/
|
||||
private void checkBasicConfig(Payment payment, DiagnosticResult result) {
|
||||
if (payment == null) {
|
||||
result.addError("支付配置为空");
|
||||
return;
|
||||
}
|
||||
|
||||
if (payment.getMchId() == null || payment.getMchId().trim().isEmpty()) {
|
||||
result.addError("商户号未配置");
|
||||
} else {
|
||||
result.addInfo("商户号: " + payment.getMchId());
|
||||
}
|
||||
|
||||
if (payment.getAppId() == null || payment.getAppId().trim().isEmpty()) {
|
||||
result.addError("应用ID未配置");
|
||||
} else {
|
||||
result.addInfo("应用ID: " + payment.getAppId());
|
||||
}
|
||||
|
||||
if (payment.getMerchantSerialNumber() == null || payment.getMerchantSerialNumber().trim().isEmpty()) {
|
||||
result.addError("商户证书序列号未配置");
|
||||
} else {
|
||||
result.addInfo("商户证书序列号: " + payment.getMerchantSerialNumber());
|
||||
}
|
||||
|
||||
if (payment.getApiKey() == null || payment.getApiKey().trim().isEmpty()) {
|
||||
result.addWarning("数据库中APIv3密钥未配置,将使用配置文件默认值");
|
||||
} else {
|
||||
result.addInfo("APIv3密钥: 已配置(" + payment.getApiKey().length() + "位)");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查证书文件
|
||||
*/
|
||||
private void checkCertificateFiles(Payment payment, Integer tenantId, String environment, DiagnosticResult result) {
|
||||
if ("dev".equals(environment)) {
|
||||
// 开发环境证书检查
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
String apiclientCertPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getApiclientCertFile();
|
||||
|
||||
// 检查私钥文件
|
||||
if (certificateLoader.certificateExists(privateKeyPath)) {
|
||||
result.addInfo("✅ 私钥文件存在: " + privateKeyPath);
|
||||
try {
|
||||
String privateKeyFile = certificateLoader.loadCertificatePath(privateKeyPath);
|
||||
result.addInfo("私钥文件路径: " + privateKeyFile);
|
||||
} catch (Exception e) {
|
||||
result.addError("私钥文件加载失败: " + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
result.addError("❌ 私钥文件不存在: " + privateKeyPath);
|
||||
}
|
||||
|
||||
// 检查商户证书文件
|
||||
if (certificateLoader.certificateExists(apiclientCertPath)) {
|
||||
result.addInfo("✅ 商户证书文件存在: " + apiclientCertPath);
|
||||
} else {
|
||||
result.addWarning("⚠️ 商户证书文件不存在: " + apiclientCertPath + " (自动证书配置不需要此文件)");
|
||||
}
|
||||
|
||||
} else {
|
||||
// 生产环境证书检查
|
||||
if (payment.getApiclientKey() != null) {
|
||||
result.addInfo("私钥文件配置: " + payment.getApiclientKey());
|
||||
} else {
|
||||
result.addError("生产环境私钥文件路径未配置");
|
||||
}
|
||||
|
||||
if (payment.getApiclientCert() != null) {
|
||||
result.addInfo("商户证书文件配置: " + payment.getApiclientCert());
|
||||
} else {
|
||||
result.addWarning("生产环境商户证书文件路径未配置 (自动证书配置不需要此文件)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证证书内容
|
||||
*/
|
||||
private void validateCertificateContent(Payment payment, Integer tenantId, String environment, DiagnosticResult result) {
|
||||
try {
|
||||
if ("dev".equals(environment)) {
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String apiclientCertPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getApiclientCertFile();
|
||||
|
||||
if (certificateLoader.certificateExists(apiclientCertPath)) {
|
||||
validateX509Certificate(apiclientCertPath, payment.getMerchantSerialNumber(), result);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.addWarning("证书内容验证失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证X509证书
|
||||
*/
|
||||
private void validateX509Certificate(String certPath, String expectedSerialNumber, DiagnosticResult result) {
|
||||
try {
|
||||
String actualCertPath = certificateLoader.loadCertificatePath(certPath);
|
||||
|
||||
try (InputStream inputStream = new FileInputStream(new File(actualCertPath))) {
|
||||
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
||||
X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);
|
||||
|
||||
if (cert != null) {
|
||||
String actualSerialNumber = cert.getSerialNumber().toString(16).toUpperCase();
|
||||
result.addInfo("证书序列号: " + actualSerialNumber);
|
||||
result.addInfo("证书有效期: " + cert.getNotBefore() + " 至 " + cert.getNotAfter());
|
||||
result.addInfo("证书主体: " + cert.getSubjectX500Principal().toString());
|
||||
|
||||
// 检查序列号是否匹配
|
||||
if (expectedSerialNumber != null && !expectedSerialNumber.equalsIgnoreCase(actualSerialNumber)) {
|
||||
result.addError("证书序列号不匹配! 配置: " + expectedSerialNumber + ", 实际: " + actualSerialNumber);
|
||||
} else {
|
||||
result.addInfo("✅ 证书序列号匹配");
|
||||
}
|
||||
|
||||
// 检查证书是否过期
|
||||
long now = System.currentTimeMillis();
|
||||
if (now < cert.getNotBefore().getTime()) {
|
||||
result.addError("证书尚未生效");
|
||||
} else if (now > cert.getNotAfter().getTime()) {
|
||||
result.addError("证书已过期");
|
||||
} else {
|
||||
result.addInfo("✅ 证书在有效期内");
|
||||
}
|
||||
} else {
|
||||
result.addError("无法解析证书文件");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.addError("证书验证失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成建议
|
||||
*/
|
||||
private void generateRecommendations(DiagnosticResult result) {
|
||||
if (result.hasErrors()) {
|
||||
result.addRecommendation("🔧 修复建议:");
|
||||
|
||||
String errorText = result.getErrors();
|
||||
if (errorText.contains("商户号")) {
|
||||
result.addRecommendation("1. 请在支付配置中设置正确的商户号");
|
||||
}
|
||||
|
||||
if (errorText.contains("序列号")) {
|
||||
result.addRecommendation("2. 请检查商户证书序列号是否正确,可在微信商户平台查看");
|
||||
}
|
||||
|
||||
if (errorText.contains("证书文件")) {
|
||||
result.addRecommendation("3. 请确保证书文件已正确放置在指定目录");
|
||||
}
|
||||
|
||||
if (errorText.contains("过期")) {
|
||||
result.addRecommendation("4. 请更新过期的证书文件");
|
||||
}
|
||||
|
||||
result.addRecommendation("5. 建议使用RSAAutoCertificateConfig自动证书配置,可避免手动管理证书");
|
||||
result.addRecommendation("6. 确保在微信商户平台开启API安全功能并申请使用微信支付公钥");
|
||||
} else {
|
||||
result.addRecommendation("✅ 证书配置正常,建议使用自动证书配置以获得最佳体验");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 诊断结果类
|
||||
*/
|
||||
public static class DiagnosticResult {
|
||||
private final StringBuilder errors = new StringBuilder();
|
||||
private final StringBuilder warnings = new StringBuilder();
|
||||
private final StringBuilder info = new StringBuilder();
|
||||
private final StringBuilder recommendations = new StringBuilder();
|
||||
|
||||
public void addError(String error) {
|
||||
if (errors.length() > 0) errors.append("\n");
|
||||
errors.append(error);
|
||||
}
|
||||
|
||||
public void addWarning(String warning) {
|
||||
if (warnings.length() > 0) warnings.append("\n");
|
||||
warnings.append(warning);
|
||||
}
|
||||
|
||||
public void addInfo(String information) {
|
||||
if (info.length() > 0) info.append("\n");
|
||||
info.append(information);
|
||||
}
|
||||
|
||||
public void addRecommendation(String recommendation) {
|
||||
if (recommendations.length() > 0) recommendations.append("\n");
|
||||
recommendations.append(recommendation);
|
||||
}
|
||||
|
||||
public boolean hasErrors() {
|
||||
return errors.length() > 0;
|
||||
}
|
||||
|
||||
public String getErrors() {
|
||||
return errors.toString();
|
||||
}
|
||||
|
||||
public String getWarnings() {
|
||||
return warnings.toString();
|
||||
}
|
||||
|
||||
public String getInfo() {
|
||||
return info.toString();
|
||||
}
|
||||
|
||||
public String getRecommendations() {
|
||||
return recommendations.toString();
|
||||
}
|
||||
|
||||
public String getFullReport() {
|
||||
StringBuilder report = new StringBuilder();
|
||||
report.append("=== 微信支付证书诊断报告 ===\n\n");
|
||||
|
||||
if (info.length() > 0) {
|
||||
report.append("📋 基本信息:\n").append(info).append("\n\n");
|
||||
}
|
||||
|
||||
if (warnings.length() > 0) {
|
||||
report.append("⚠️ 警告:\n").append(warnings).append("\n\n");
|
||||
}
|
||||
|
||||
if (errors.length() > 0) {
|
||||
report.append("❌ 错误:\n").append(errors).append("\n\n");
|
||||
}
|
||||
|
||||
if (recommendations.length() > 0) {
|
||||
report.append("💡 建议:\n").append(recommendations).append("\n\n");
|
||||
}
|
||||
|
||||
report.append("=== 诊断报告结束 ===");
|
||||
return report.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 微信支付证书修复工具
|
||||
* 自动检测和修复常见的证书配置问题
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-29
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WechatPayCertificateFixer {
|
||||
|
||||
private final CertificateProperties certConfig;
|
||||
private final CertificateLoader certificateLoader;
|
||||
|
||||
public WechatPayCertificateFixer(CertificateProperties certConfig, CertificateLoader certificateLoader) {
|
||||
this.certConfig = certConfig;
|
||||
this.certificateLoader = certificateLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动修复证书配置问题
|
||||
*
|
||||
* @param payment 支付配置
|
||||
* @param tenantId 租户ID
|
||||
* @param environment 环境
|
||||
* @return 修复结果
|
||||
*/
|
||||
public FixResult autoFixCertificateIssues(Payment payment, Integer tenantId, String environment) {
|
||||
FixResult result = new FixResult();
|
||||
|
||||
log.info("开始自动修复租户 {} 的证书配置问题", tenantId);
|
||||
|
||||
try {
|
||||
// 1. 检查并修复基本配置
|
||||
fixBasicConfiguration(payment, result);
|
||||
|
||||
// 2. 检查并修复证书文件问题
|
||||
fixCertificateFiles(payment, tenantId, environment, result);
|
||||
|
||||
// 3. 检查并修复序列号问题
|
||||
fixSerialNumberIssues(payment, tenantId, environment, result);
|
||||
|
||||
// 4. 生成修复建议
|
||||
generateFixRecommendations(result);
|
||||
|
||||
} catch (Exception e) {
|
||||
result.addError("修复过程中发生异常: " + e.getMessage());
|
||||
log.error("证书修复异常", e);
|
||||
}
|
||||
|
||||
log.info("证书配置修复完成,成功修复 {} 个问题", result.getFixedIssues().size());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复基本配置问题
|
||||
*/
|
||||
private void fixBasicConfiguration(Payment payment, FixResult result) {
|
||||
if (payment == null) {
|
||||
result.addError("支付配置为空,无法修复");
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查商户号
|
||||
if (payment.getMchId() == null || payment.getMchId().trim().isEmpty()) {
|
||||
result.addError("商户号未配置,需要手动设置");
|
||||
}
|
||||
|
||||
// 检查应用ID
|
||||
if (payment.getAppId() == null || payment.getAppId().trim().isEmpty()) {
|
||||
result.addError("应用ID未配置,需要手动设置");
|
||||
}
|
||||
|
||||
// 检查APIv3密钥
|
||||
if (payment.getApiKey() == null || payment.getApiKey().trim().isEmpty()) {
|
||||
result.addWarning("APIv3密钥未配置,将使用配置文件默认值");
|
||||
} else if (payment.getApiKey().length() != 32) {
|
||||
result.addError("APIv3密钥长度错误,应为32位,实际为: " + payment.getApiKey().length());
|
||||
}
|
||||
|
||||
// 检查商户证书序列号
|
||||
if (payment.getMerchantSerialNumber() == null || payment.getMerchantSerialNumber().trim().isEmpty()) {
|
||||
result.addError("商户证书序列号未配置,需要手动设置");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复证书文件问题
|
||||
*/
|
||||
private void fixCertificateFiles(Payment payment, Integer tenantId, String environment, FixResult result) {
|
||||
if ("dev".equals(environment)) {
|
||||
fixDevCertificateFiles(tenantId, result);
|
||||
} else {
|
||||
fixProdCertificateFiles(payment, result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复开发环境证书文件问题
|
||||
*/
|
||||
private void fixDevCertificateFiles(Integer tenantId, FixResult result) {
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
String apiclientCertPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getApiclientCertFile();
|
||||
|
||||
// 检查私钥文件
|
||||
if (!certificateLoader.certificateExists(privateKeyPath)) {
|
||||
result.addError("私钥文件不存在: " + privateKeyPath);
|
||||
result.addRecommendation("请将 apiclient_key.pem 文件放置到 src/main/resources/" + privateKeyPath);
|
||||
} else {
|
||||
result.addFixed("私钥文件存在: " + privateKeyPath);
|
||||
|
||||
// 尝试加载私钥文件
|
||||
try {
|
||||
String privateKeyFile = certificateLoader.loadCertificatePath(privateKeyPath);
|
||||
result.addFixed("私钥文件加载成功: " + privateKeyFile);
|
||||
} catch (Exception e) {
|
||||
result.addError("私钥文件加载失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 检查商户证书文件(可选)
|
||||
if (!certificateLoader.certificateExists(apiclientCertPath)) {
|
||||
result.addWarning("商户证书文件不存在: " + apiclientCertPath + " (自动证书配置不需要此文件)");
|
||||
} else {
|
||||
result.addFixed("商户证书文件存在: " + apiclientCertPath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复生产环境证书文件问题
|
||||
*/
|
||||
private void fixProdCertificateFiles(Payment payment, FixResult result) {
|
||||
if (payment.getApiclientKey() == null || payment.getApiclientKey().trim().isEmpty()) {
|
||||
result.addError("生产环境私钥文件路径未配置");
|
||||
} else {
|
||||
result.addFixed("生产环境私钥文件路径已配置: " + payment.getApiclientKey());
|
||||
}
|
||||
|
||||
if (payment.getApiclientCert() == null || payment.getApiclientCert().trim().isEmpty()) {
|
||||
result.addWarning("生产环境商户证书文件路径未配置 (自动证书配置不需要此文件)");
|
||||
} else {
|
||||
result.addFixed("生产环境商户证书文件路径已配置: " + payment.getApiclientCert());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复序列号问题
|
||||
*/
|
||||
private void fixSerialNumberIssues(Payment payment, Integer tenantId, String environment, FixResult result) {
|
||||
if (payment.getMerchantSerialNumber() == null || payment.getMerchantSerialNumber().trim().isEmpty()) {
|
||||
result.addError("商户证书序列号未配置");
|
||||
return;
|
||||
}
|
||||
|
||||
// 在开发环境中,尝试从证书文件中提取序列号进行验证
|
||||
if ("dev".equals(environment)) {
|
||||
try {
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String apiclientCertPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getApiclientCertFile();
|
||||
|
||||
if (certificateLoader.certificateExists(apiclientCertPath)) {
|
||||
String actualCertPath = certificateLoader.loadCertificatePath(apiclientCertPath);
|
||||
|
||||
try (InputStream inputStream = new FileInputStream(new File(actualCertPath))) {
|
||||
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
||||
X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);
|
||||
|
||||
if (cert != null) {
|
||||
String actualSerialNumber = cert.getSerialNumber().toString(16).toUpperCase();
|
||||
String configuredSerialNumber = payment.getMerchantSerialNumber();
|
||||
|
||||
if (!configuredSerialNumber.equalsIgnoreCase(actualSerialNumber)) {
|
||||
result.addError("证书序列号不匹配! 配置: " + configuredSerialNumber + ", 实际: " + actualSerialNumber);
|
||||
result.addRecommendation("建议将商户证书序列号更新为: " + actualSerialNumber);
|
||||
} else {
|
||||
result.addFixed("证书序列号匹配: " + actualSerialNumber);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.addWarning("无法验证证书序列号: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成修复建议
|
||||
*/
|
||||
private void generateFixRecommendations(FixResult result) {
|
||||
if (result.hasErrors()) {
|
||||
result.addRecommendation("=== 修复建议 ===");
|
||||
|
||||
if (result.getErrors().stream().anyMatch(e -> e.contains("商户号"))) {
|
||||
result.addRecommendation("1. 请在微信商户平台获取商户号并在系统中配置");
|
||||
}
|
||||
|
||||
if (result.getErrors().stream().anyMatch(e -> e.contains("应用ID"))) {
|
||||
result.addRecommendation("2. 请在微信开放平台获取应用ID并在系统中配置");
|
||||
}
|
||||
|
||||
if (result.getErrors().stream().anyMatch(e -> e.contains("APIv3密钥"))) {
|
||||
result.addRecommendation("3. 请在微信商户平台设置32位APIv3密钥");
|
||||
}
|
||||
|
||||
if (result.getErrors().stream().anyMatch(e -> e.contains("证书序列号"))) {
|
||||
result.addRecommendation("4. 请在微信商户平台查看正确的商户证书序列号");
|
||||
}
|
||||
|
||||
if (result.getErrors().stream().anyMatch(e -> e.contains("私钥文件"))) {
|
||||
result.addRecommendation("5. 请从微信商户平台下载私钥文件并放置到正确位置");
|
||||
}
|
||||
|
||||
result.addRecommendation("6. 建议使用RSAAutoCertificateConfig自动证书配置");
|
||||
result.addRecommendation("7. 确保在微信商户平台开启API安全功能");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复结果类
|
||||
*/
|
||||
public static class FixResult {
|
||||
private final List<String> errors = new ArrayList<>();
|
||||
private final List<String> warnings = new ArrayList<>();
|
||||
private final List<String> fixedIssues = new ArrayList<>();
|
||||
private final List<String> recommendations = new ArrayList<>();
|
||||
|
||||
public void addError(String error) {
|
||||
errors.add(error);
|
||||
}
|
||||
|
||||
public void addWarning(String warning) {
|
||||
warnings.add(warning);
|
||||
}
|
||||
|
||||
public void addFixed(String fixed) {
|
||||
fixedIssues.add(fixed);
|
||||
}
|
||||
|
||||
public void addRecommendation(String recommendation) {
|
||||
recommendations.add(recommendation);
|
||||
}
|
||||
|
||||
public boolean hasErrors() {
|
||||
return !errors.isEmpty();
|
||||
}
|
||||
|
||||
public List<String> getErrors() {
|
||||
return errors;
|
||||
}
|
||||
|
||||
public List<String> getWarnings() {
|
||||
return warnings;
|
||||
}
|
||||
|
||||
public List<String> getFixedIssues() {
|
||||
return fixedIssues;
|
||||
}
|
||||
|
||||
public List<String> getRecommendations() {
|
||||
return recommendations;
|
||||
}
|
||||
|
||||
public String getFullReport() {
|
||||
StringBuilder report = new StringBuilder();
|
||||
report.append("=== 微信支付证书修复报告 ===\n\n");
|
||||
|
||||
if (!fixedIssues.isEmpty()) {
|
||||
report.append("✅ 已修复的问题:\n");
|
||||
fixedIssues.forEach(issue -> report.append(" - ").append(issue).append("\n"));
|
||||
report.append("\n");
|
||||
}
|
||||
|
||||
if (!warnings.isEmpty()) {
|
||||
report.append("⚠️ 警告:\n");
|
||||
warnings.forEach(warning -> report.append(" - ").append(warning).append("\n"));
|
||||
report.append("\n");
|
||||
}
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
report.append("❌ 需要手动修复的问题:\n");
|
||||
errors.forEach(error -> report.append(" - ").append(error).append("\n"));
|
||||
report.append("\n");
|
||||
}
|
||||
|
||||
if (!recommendations.isEmpty()) {
|
||||
report.append("💡 修复建议:\n");
|
||||
recommendations.forEach(rec -> report.append(" ").append(rec).append("\n"));
|
||||
report.append("\n");
|
||||
}
|
||||
|
||||
report.append("=== 修复报告结束 ===");
|
||||
return report.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.core.service.PaymentCacheService;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 微信支付配置检查器
|
||||
* 用于快速检查和验证微信支付配置状态
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-29
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WechatPayConfigChecker {
|
||||
|
||||
@Autowired
|
||||
private PaymentCacheService paymentCacheService;
|
||||
|
||||
@Autowired
|
||||
private CertificateLoader certificateLoader;
|
||||
|
||||
@Autowired
|
||||
private CertificateProperties certConfig;
|
||||
|
||||
@Value("${spring.profiles.active:dev}")
|
||||
private String activeProfile;
|
||||
|
||||
/**
|
||||
* 检查租户的微信支付配置状态
|
||||
*
|
||||
* @param tenantId 租户ID
|
||||
* @return 配置状态报告
|
||||
*/
|
||||
public ConfigStatus checkTenantConfig(Integer tenantId) {
|
||||
ConfigStatus status = new ConfigStatus();
|
||||
status.tenantId = tenantId;
|
||||
status.environment = activeProfile;
|
||||
|
||||
try {
|
||||
// 获取支付配置
|
||||
Payment payment = paymentCacheService.getWechatPayConfig(tenantId);
|
||||
if (payment == null) {
|
||||
status.hasError = true;
|
||||
status.errorMessage = "支付配置不存在";
|
||||
return status;
|
||||
}
|
||||
|
||||
status.merchantId = payment.getMchId();
|
||||
status.appId = payment.getAppId();
|
||||
status.serialNumber = payment.getMerchantSerialNumber();
|
||||
status.hasApiKey = payment.getApiKey() != null && !payment.getApiKey().isEmpty();
|
||||
status.apiKeyLength = payment.getApiKey() != null ? payment.getApiKey().length() : 0;
|
||||
|
||||
// 检查公钥配置
|
||||
if (payment.getPubKey() != null && !payment.getPubKey().isEmpty() &&
|
||||
payment.getPubKeyId() != null && !payment.getPubKeyId().isEmpty()) {
|
||||
|
||||
status.hasPublicKey = true;
|
||||
status.publicKeyFile = payment.getPubKey();
|
||||
status.publicKeyId = payment.getPubKeyId();
|
||||
status.configMode = "公钥模式";
|
||||
|
||||
// 检查公钥文件是否存在
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String pubKeyPath = tenantCertPath + "/" + payment.getPubKey();
|
||||
status.publicKeyExists = certificateLoader.certificateExists(pubKeyPath);
|
||||
|
||||
} else {
|
||||
status.hasPublicKey = false;
|
||||
status.configMode = "自动证书模式";
|
||||
}
|
||||
|
||||
// 检查私钥文件
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
status.privateKeyExists = certificateLoader.certificateExists(privateKeyPath);
|
||||
|
||||
// 检查商户证书文件
|
||||
String apiclientCertPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getApiclientCertFile();
|
||||
status.merchantCertExists = certificateLoader.certificateExists(apiclientCertPath);
|
||||
|
||||
// 评估配置完整性
|
||||
evaluateConfigCompleteness(status);
|
||||
|
||||
} catch (Exception e) {
|
||||
status.hasError = true;
|
||||
status.errorMessage = "检查配置时发生异常: " + e.getMessage();
|
||||
log.error("检查租户 {} 配置时发生异常", tenantId, e);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 评估配置完整性
|
||||
*/
|
||||
private void evaluateConfigCompleteness(ConfigStatus status) {
|
||||
if (status.hasError) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 基本配置检查
|
||||
if (status.merchantId == null || status.merchantId.isEmpty()) {
|
||||
status.addIssue("商户号未配置");
|
||||
}
|
||||
|
||||
if (status.appId == null || status.appId.isEmpty()) {
|
||||
status.addIssue("应用ID未配置");
|
||||
}
|
||||
|
||||
if (status.serialNumber == null || status.serialNumber.isEmpty()) {
|
||||
status.addIssue("商户证书序列号未配置");
|
||||
}
|
||||
|
||||
if (!status.hasApiKey) {
|
||||
status.addIssue("APIv3密钥未配置");
|
||||
} else if (status.apiKeyLength != 32) {
|
||||
status.addIssue("APIv3密钥长度错误,应为32位,实际为" + status.apiKeyLength + "位");
|
||||
}
|
||||
|
||||
if (!status.privateKeyExists) {
|
||||
status.addIssue("私钥文件不存在");
|
||||
}
|
||||
|
||||
// 公钥模式特定检查
|
||||
if (status.hasPublicKey) {
|
||||
if (!status.publicKeyExists) {
|
||||
status.addIssue("公钥文件不存在");
|
||||
}
|
||||
}
|
||||
|
||||
// 设置配置状态
|
||||
if (status.issues.isEmpty()) {
|
||||
status.configComplete = true;
|
||||
status.recommendation = "✅ 配置完整,建议使用当前配置";
|
||||
} else {
|
||||
status.configComplete = false;
|
||||
if (status.hasPublicKey) {
|
||||
status.recommendation = "⚠️ 公钥配置不完整,建议修复问题或回退到自动证书模式";
|
||||
} else {
|
||||
status.recommendation = "⚠️ 自动证书配置不完整,建议配置公钥模式或修复当前问题";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成配置建议
|
||||
*/
|
||||
public String generateConfigAdvice(Integer tenantId) {
|
||||
ConfigStatus status = checkTenantConfig(tenantId);
|
||||
|
||||
StringBuilder advice = new StringBuilder();
|
||||
advice.append("=== 租户 ").append(tenantId).append(" 微信支付配置建议 ===\n\n");
|
||||
|
||||
advice.append("当前配置模式: ").append(status.configMode).append("\n");
|
||||
advice.append("配置完整性: ").append(status.configComplete ? "完整" : "不完整").append("\n\n");
|
||||
|
||||
if (status.hasError) {
|
||||
advice.append("❌ 错误: ").append(status.errorMessage).append("\n\n");
|
||||
return advice.toString();
|
||||
}
|
||||
|
||||
// 基本信息
|
||||
advice.append("📋 基本信息:\n");
|
||||
advice.append(" 商户号: ").append(status.merchantId).append("\n");
|
||||
advice.append(" 应用ID: ").append(status.appId).append("\n");
|
||||
advice.append(" 序列号: ").append(status.serialNumber).append("\n");
|
||||
advice.append(" API密钥: ").append(status.hasApiKey ? "已配置(" + status.apiKeyLength + "位)" : "未配置").append("\n\n");
|
||||
|
||||
// 证书文件状态
|
||||
advice.append("📁 证书文件状态:\n");
|
||||
advice.append(" 私钥文件: ").append(status.privateKeyExists ? "✅ 存在" : "❌ 不存在").append("\n");
|
||||
advice.append(" 商户证书: ").append(status.merchantCertExists ? "✅ 存在" : "⚠️ 不存在").append("\n");
|
||||
|
||||
if (status.hasPublicKey) {
|
||||
advice.append(" 公钥文件: ").append(status.publicKeyExists ? "✅ 存在" : "❌ 不存在").append("\n");
|
||||
advice.append(" 公钥ID: ").append(status.publicKeyId).append("\n");
|
||||
}
|
||||
advice.append("\n");
|
||||
|
||||
// 问题列表
|
||||
if (!status.issues.isEmpty()) {
|
||||
advice.append("⚠️ 发现的问题:\n");
|
||||
for (String issue : status.issues) {
|
||||
advice.append(" - ").append(issue).append("\n");
|
||||
}
|
||||
advice.append("\n");
|
||||
}
|
||||
|
||||
// 建议
|
||||
advice.append("💡 建议:\n");
|
||||
advice.append(" ").append(status.recommendation).append("\n\n");
|
||||
|
||||
if (!status.hasPublicKey) {
|
||||
advice.append("🔧 配置公钥模式的步骤:\n");
|
||||
advice.append(" 1. 获取微信支付平台公钥文件和公钥ID\n");
|
||||
advice.append(" 2. 将公钥文件放置到: src/main/resources/dev/wechat/").append(tenantId).append("/\n");
|
||||
advice.append(" 3. 执行SQL更新数据库配置:\n");
|
||||
advice.append(" UPDATE sys_payment SET \n");
|
||||
advice.append(" pub_key = 'wechatpay_public_key.pem',\n");
|
||||
advice.append(" pub_key_id = 'YOUR_PUBLIC_KEY_ID'\n");
|
||||
advice.append(" WHERE tenant_id = ").append(tenantId).append(" AND type = 0;\n\n");
|
||||
}
|
||||
|
||||
advice.append("=== 配置建议结束 ===");
|
||||
return advice.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置状态类
|
||||
*/
|
||||
public static class ConfigStatus {
|
||||
public Integer tenantId;
|
||||
public String environment;
|
||||
public String merchantId;
|
||||
public String appId;
|
||||
public String serialNumber;
|
||||
public boolean hasApiKey;
|
||||
public int apiKeyLength;
|
||||
public boolean hasPublicKey;
|
||||
public String publicKeyFile;
|
||||
public String publicKeyId;
|
||||
public boolean publicKeyExists;
|
||||
public boolean privateKeyExists;
|
||||
public boolean merchantCertExists;
|
||||
public String configMode;
|
||||
public boolean configComplete;
|
||||
public boolean hasError;
|
||||
public String errorMessage;
|
||||
public String recommendation;
|
||||
public java.util.List<String> issues = new java.util.ArrayList<>();
|
||||
|
||||
public void addIssue(String issue) {
|
||||
issues.add(issue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
import com.gxwebsoft.common.core.utils.CertificateLoader;
|
||||
import com.gxwebsoft.common.core.service.PaymentCacheService;
|
||||
import com.gxwebsoft.common.core.utils.WechatPayDiagnostic;
|
||||
import com.gxwebsoft.common.core.utils.WechatPayCertificateDiagnostic;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.gxwebsoft.common.system.service.PaymentService;
|
||||
@@ -73,6 +74,8 @@ import com.gxwebsoft.common.core.service.PaymentCacheService;
|
||||
private WechatCertAutoConfig wechatCertAutoConfig;
|
||||
@Resource
|
||||
private WechatPayDiagnostic wechatPayDiagnostic;
|
||||
@Resource
|
||||
private WechatPayCertificateDiagnostic certificateDiagnostic;
|
||||
|
||||
@Override
|
||||
public PageResult<ShopOrder> pageRel(ShopOrderParam param) {
|
||||
@@ -289,6 +292,12 @@ import com.gxwebsoft.common.core.service.PaymentCacheService;
|
||||
System.out.println("=== 运行微信支付配置诊断 ===");
|
||||
wechatPayDiagnostic.diagnosePaymentConfig(payment, null, active);
|
||||
|
||||
// 运行证书诊断
|
||||
System.out.println("=== 运行证书诊断 ===");
|
||||
WechatPayCertificateDiagnostic.DiagnosticResult diagnosticResult =
|
||||
certificateDiagnostic.diagnoseCertificateConfig(payment, order.getTenantId(), active);
|
||||
System.out.println(diagnosticResult.getFullReport());
|
||||
|
||||
// 开发环境配置 - 使用自动证书配置
|
||||
if (active.equals("dev")) {
|
||||
// 构建包含租户号的证书路径: dev/wechat/{tenantId}/
|
||||
@@ -377,67 +386,243 @@ import com.gxwebsoft.common.core.service.PaymentCacheService;
|
||||
System.out.println("=== 注意:使用RSA配置替代自动证书配置 ===");
|
||||
System.out.println("原因:商户平台可能未开启API安全功能或未申请微信支付公钥");
|
||||
|
||||
try {
|
||||
config = wechatCertAutoConfig.createAutoConfig(
|
||||
payment.getMchId(),
|
||||
privateKey,
|
||||
payment.getMerchantSerialNumber(),
|
||||
payment.getApiKey()
|
||||
);
|
||||
} catch (Exception e) {
|
||||
System.err.println("自动证书配置失败: " + e.getMessage());
|
||||
System.err.println("请在微信支付商户平台完成以下配置:");
|
||||
System.err.println("1. 开启API安全功能");
|
||||
System.err.println("2. 申请使用微信支付公钥");
|
||||
System.err.println("3. 参考文档:https://pay.weixin.qq.com/doc/v3/merchant/4012153196");
|
||||
throw new RuntimeException("微信支付配置失败,请先在商户平台开启API安全功能", e);
|
||||
// 首先检查是否配置了公钥,如果有则优先使用公钥模式
|
||||
if (payment.getPubKey() != null && !payment.getPubKey().isEmpty() &&
|
||||
payment.getPubKeyId() != null && !payment.getPubKeyId().isEmpty()) {
|
||||
|
||||
System.out.println("=== 检测到公钥配置,使用RSA公钥模式 ===");
|
||||
System.out.println("公钥文件: " + payment.getPubKey());
|
||||
System.out.println("公钥ID: " + payment.getPubKeyId());
|
||||
|
||||
try {
|
||||
// 开发环境固定使用 wechatpay_public_key.pem
|
||||
String tenantCertPath = "dev/wechat/" + order.getTenantId();
|
||||
String pubKeyPath = tenantCertPath + "/wechatpay_public_key.pem";
|
||||
|
||||
System.out.println("开发环境公钥文件路径: " + pubKeyPath);
|
||||
|
||||
if (certificateLoader.certificateExists(pubKeyPath)) {
|
||||
String pubKeyFile = certificateLoader.loadCertificatePath(pubKeyPath);
|
||||
System.out.println("✅ 开发环境公钥文件加载成功: " + pubKeyFile);
|
||||
|
||||
config = new RSAPublicKeyConfig.Builder()
|
||||
.merchantId(payment.getMchId())
|
||||
.privateKeyFromPath(privateKey)
|
||||
.publicKeyFromPath(pubKeyFile)
|
||||
.publicKeyId(payment.getPubKeyId())
|
||||
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||
.apiV3Key(payment.getApiKey())
|
||||
.build();
|
||||
System.out.println("✅ 开发环境RSA公钥配置成功");
|
||||
} else {
|
||||
System.err.println("❌ 公钥文件不存在: " + pubKeyPath);
|
||||
throw new RuntimeException("公钥文件不存在: " + pubKeyPath);
|
||||
}
|
||||
} catch (Exception pubKeyException) {
|
||||
System.err.println("❌ RSA公钥配置失败: " + pubKeyException.getMessage());
|
||||
pubKeyException.printStackTrace();
|
||||
throw new RuntimeException("RSA公钥配置失败: " + pubKeyException.getMessage(), pubKeyException);
|
||||
}
|
||||
} else {
|
||||
// 没有公钥配置,尝试自动证书配置
|
||||
try {
|
||||
System.out.println("=== 尝试创建自动证书配置 ===");
|
||||
System.out.println("商户号: " + payment.getMchId());
|
||||
System.out.println("私钥路径: " + privateKey);
|
||||
System.out.println("序列号: " + payment.getMerchantSerialNumber());
|
||||
System.out.println("API密钥长度: " + (payment.getApiKey() != null ? payment.getApiKey().length() : 0));
|
||||
|
||||
config = wechatCertAutoConfig.createAutoConfig(
|
||||
payment.getMchId(),
|
||||
privateKey,
|
||||
payment.getMerchantSerialNumber(),
|
||||
payment.getApiKey()
|
||||
);
|
||||
System.out.println("✅ 开发环境自动证书配置成功");
|
||||
} catch (Exception e) {
|
||||
System.err.println("❌ 自动证书配置失败: " + e.getMessage());
|
||||
System.err.println("错误类型: " + e.getClass().getName());
|
||||
e.printStackTrace();
|
||||
|
||||
// 检查是否是证书相关的错误
|
||||
if (e.getMessage() != null && (
|
||||
e.getMessage().contains("certificate") ||
|
||||
e.getMessage().contains("X509Certificate") ||
|
||||
e.getMessage().contains("getSerialNumber") ||
|
||||
e.getMessage().contains("404") ||
|
||||
e.getMessage().contains("API安全"))) {
|
||||
|
||||
System.err.println("🔍 证书问题诊断:");
|
||||
System.err.println("1. 检查商户平台是否已开启API安全功能");
|
||||
System.err.println("2. 检查是否已申请使用微信支付公钥");
|
||||
System.err.println("3. 检查网络连接是否正常");
|
||||
System.err.println("4. 检查商户证书序列号是否正确");
|
||||
System.err.println("5. 参考文档:https://pay.weixin.qq.com/doc/v3/merchant/4012153196");
|
||||
|
||||
// 开发环境回退到基础RSA配置
|
||||
System.err.println("⚠️ 开发环境回退到基础RSA配置...");
|
||||
try {
|
||||
// 方案1:尝试使用RSA证书配置(需要商户证书文件)
|
||||
String tenantCertPath = "dev/wechat/" + order.getTenantId();
|
||||
String apiclientCertPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getApiclientCertFile();
|
||||
|
||||
if (certificateLoader.certificateExists(apiclientCertPath)) {
|
||||
String apiclientCertFile = certificateLoader.loadCertificatePath(apiclientCertPath);
|
||||
System.out.println("方案1: 使用RSA证书配置作为回退方案");
|
||||
System.out.println("商户证书路径: " + apiclientCertFile);
|
||||
|
||||
try {
|
||||
config = new RSAConfig.Builder()
|
||||
.merchantId(payment.getMchId())
|
||||
.privateKeyFromPath(privateKey)
|
||||
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||
.wechatPayCertificatesFromPath(apiclientCertFile)
|
||||
.build();
|
||||
System.out.println("✅ 开发环境RSA证书配置成功");
|
||||
} catch (Exception rsaException) {
|
||||
System.err.println("RSA证书配置失败: " + rsaException.getMessage());
|
||||
throw rsaException;
|
||||
}
|
||||
} else {
|
||||
System.err.println("❌ 商户证书文件不存在: " + apiclientCertPath);
|
||||
System.err.println("⚠️ 尝试方案2: 使用最小化配置...");
|
||||
|
||||
// 方案2:使用最小化的RSA配置(仅私钥和序列号)
|
||||
try {
|
||||
// 创建一个最基础的配置,不依赖平台证书
|
||||
config = new com.wechat.pay.java.core.RSAConfig.Builder()
|
||||
.merchantId(payment.getMchId())
|
||||
.privateKeyFromPath(privateKey)
|
||||
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||
.build();
|
||||
System.out.println("✅ 开发环境最小化RSA配置成功");
|
||||
} catch (Exception minimalException) {
|
||||
System.err.println("最小化配置也失败: " + minimalException.getMessage());
|
||||
throw new RuntimeException("所有配置方案都失败,请检查私钥文件和商户配置", e);
|
||||
}
|
||||
}
|
||||
} catch (Exception fallbackException) {
|
||||
System.err.println("❌ 手动证书配置失败: " + fallbackException.getMessage());
|
||||
fallbackException.printStackTrace();
|
||||
|
||||
// 最后的回退:抛出详细错误信息
|
||||
System.err.println("=== 最终错误诊断 ===");
|
||||
System.err.println("1. 自动证书配置失败原因: " + e.getMessage());
|
||||
System.err.println("2. 手动证书配置失败原因: " + fallbackException.getMessage());
|
||||
System.err.println("3. 建议解决方案:");
|
||||
System.err.println(" - 检查微信商户平台是否开启API安全功能");
|
||||
System.err.println(" - 确认已申请使用微信支付公钥");
|
||||
System.err.println(" - 验证商户证书序列号是否正确");
|
||||
System.err.println(" - 检查私钥文件是否完整且格式正确");
|
||||
|
||||
throw new RuntimeException("微信支付配置失败,请检查商户平台API安全设置和证书配置。原始错误: " + e.getMessage() + ", 回退错误: " + fallbackException.getMessage(), e);
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("微信支付配置失败:" + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 生产环境优先使用自动证书配置
|
||||
System.out.println("=== 生产环境使用自动证书配置 ===");
|
||||
System.out.println("商户号: " + payment.getMchId());
|
||||
System.out.println("序列号: " + payment.getMerchantSerialNumber());
|
||||
System.out.println("API密钥: 已配置(长度:" + payment.getApiKey().length() + ")");
|
||||
// 生产环境也优先检查公钥配置
|
||||
if (payment.getPubKey() != null && !payment.getPubKey().isEmpty() &&
|
||||
payment.getPubKeyId() != null && !payment.getPubKeyId().isEmpty()) {
|
||||
|
||||
try {
|
||||
// 优先使用自动证书配置,避免证书过期和序列号不匹配问题
|
||||
config = wechatCertAutoConfig.createAutoConfig(
|
||||
payment.getMchId(),
|
||||
privateKey,
|
||||
payment.getMerchantSerialNumber(),
|
||||
payment.getApiKey()
|
||||
);
|
||||
System.out.println("✅ 生产环境自动证书配置成功");
|
||||
} catch (Exception autoConfigException) {
|
||||
System.err.println("⚠️ 自动证书配置失败,回退到手动证书配置");
|
||||
System.err.println("自动配置错误: " + autoConfigException.getMessage());
|
||||
System.out.println("=== 生产环境检测到公钥配置,使用RSA公钥模式 ===");
|
||||
System.out.println("公钥文件路径: " + payment.getPubKey());
|
||||
System.out.println("公钥ID: " + payment.getPubKeyId());
|
||||
|
||||
try {
|
||||
// 生产环境直接使用数据库中存储的完整路径
|
||||
String pubKeyFile = certificateLoader.loadCertificatePath(payment.getPubKey());
|
||||
System.out.println("✅ 生产环境公钥文件加载成功: " + pubKeyFile);
|
||||
|
||||
// 回退到手动证书配置
|
||||
if (payment.getPubKey() != null && !payment.getPubKey().isEmpty()) {
|
||||
System.out.println("使用RSA公钥配置");
|
||||
config = new RSAPublicKeyConfig.Builder()
|
||||
.merchantId(payment.getMchId())
|
||||
.privateKeyFromPath(privateKey)
|
||||
.publicKeyFromPath(pubKey)
|
||||
.publicKeyFromPath(pubKeyFile)
|
||||
.publicKeyId(payment.getPubKeyId())
|
||||
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||
.apiV3Key(payment.getApiKey())
|
||||
.build();
|
||||
} else {
|
||||
System.out.println("使用RSA证书配置");
|
||||
config = new RSAConfig.Builder()
|
||||
.merchantId(payment.getMchId())
|
||||
.privateKeyFromPath(privateKey)
|
||||
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||
.wechatPayCertificatesFromPath(apiclientCert)
|
||||
.build();
|
||||
System.out.println("✅ 生产环境RSA公钥配置成功");
|
||||
} catch (Exception pubKeyException) {
|
||||
System.err.println("❌ 生产环境RSA公钥配置失败: " + pubKeyException.getMessage());
|
||||
pubKeyException.printStackTrace();
|
||||
throw new RuntimeException("生产环境RSA公钥配置失败: " + pubKeyException.getMessage(), pubKeyException);
|
||||
}
|
||||
} else {
|
||||
// 没有公钥配置,使用自动证书配置
|
||||
System.out.println("=== 生产环境使用自动证书配置 ===");
|
||||
System.out.println("商户号: " + payment.getMchId());
|
||||
System.out.println("序列号: " + payment.getMerchantSerialNumber());
|
||||
System.out.println("API密钥: 已配置(长度:" + payment.getApiKey().length() + ")");
|
||||
|
||||
try {
|
||||
// 优先使用自动证书配置,避免证书过期和序列号不匹配问题
|
||||
config = wechatCertAutoConfig.createAutoConfig(
|
||||
payment.getMchId(),
|
||||
privateKey,
|
||||
payment.getMerchantSerialNumber(),
|
||||
payment.getApiKey()
|
||||
);
|
||||
System.out.println("✅ 生产环境自动证书配置成功");
|
||||
} catch (Exception autoConfigException) {
|
||||
System.err.println("⚠️ 自动证书配置失败,回退到手动证书配置");
|
||||
System.err.println("自动配置错误: " + autoConfigException.getMessage());
|
||||
System.err.println("错误类型: " + autoConfigException.getClass().getName());
|
||||
autoConfigException.printStackTrace();
|
||||
|
||||
// 检查是否是证书相关的错误
|
||||
if (autoConfigException.getMessage() != null && (
|
||||
autoConfigException.getMessage().contains("certificate") ||
|
||||
autoConfigException.getMessage().contains("X509Certificate") ||
|
||||
autoConfigException.getMessage().contains("getSerialNumber") ||
|
||||
autoConfigException.getMessage().contains("404") ||
|
||||
autoConfigException.getMessage().contains("API安全"))) {
|
||||
|
||||
System.err.println("🔍 生产环境证书问题诊断:");
|
||||
System.err.println("1. 检查商户平台是否已开启API安全功能");
|
||||
System.err.println("2. 检查是否已申请使用微信支付公钥");
|
||||
System.err.println("3. 检查网络连接是否正常");
|
||||
System.err.println("4. 检查商户证书序列号是否正确");
|
||||
}
|
||||
|
||||
try {
|
||||
// 回退到手动证书配置
|
||||
if (payment.getPubKey() != null && !payment.getPubKey().isEmpty()) {
|
||||
System.out.println("使用RSA公钥配置作为回退方案");
|
||||
config = new RSAPublicKeyConfig.Builder()
|
||||
.merchantId(payment.getMchId())
|
||||
.privateKeyFromPath(privateKey)
|
||||
.publicKeyFromPath(pubKey)
|
||||
.publicKeyId(payment.getPubKeyId())
|
||||
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||
.apiV3Key(payment.getApiKey())
|
||||
.build();
|
||||
System.out.println("✅ 生产环境RSA公钥配置成功");
|
||||
} else if (apiclientCert != null && !apiclientCert.isEmpty()) {
|
||||
System.out.println("使用RSA证书配置作为回退方案");
|
||||
config = new RSAConfig.Builder()
|
||||
.merchantId(payment.getMchId())
|
||||
.privateKeyFromPath(privateKey)
|
||||
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||
.wechatPayCertificatesFromPath(apiclientCert)
|
||||
.build();
|
||||
System.out.println("✅ 生产环境RSA证书配置成功");
|
||||
} else {
|
||||
throw new RuntimeException("生产环境缺少必要的证书文件,无法创建手动证书配置", autoConfigException);
|
||||
}
|
||||
} catch (Exception fallbackException) {
|
||||
System.err.println("❌ 生产环境手动证书配置也失败: " + fallbackException.getMessage());
|
||||
throw new RuntimeException("生产环境微信支付配置失败,请检查商户平台API安全设置和证书配置", autoConfigException);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 构建service
|
||||
return new JsapiServiceExtension.Builder().config(config).build();
|
||||
} catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
System.err.println("=== 构建微信支付服务失败 ===");
|
||||
System.err.println("错误信息: " + e.getMessage());
|
||||
System.err.println("错误类型: " + e.getClass().getName());
|
||||
|
||||
@@ -53,7 +53,7 @@ mqtt:
|
||||
# 框架配置
|
||||
config:
|
||||
# 开发环境接口
|
||||
server-url: http://127.0.0.1:9200/api
|
||||
server-url: https://server.websoft.top/api
|
||||
upload-path: /Users/gxwebsoft/Documents/uploads/ # window(D:\Temp)
|
||||
|
||||
# 开发环境证书配置
|
||||
|
||||
@@ -44,7 +44,7 @@ mqtt:
|
||||
# 框架配置
|
||||
config:
|
||||
# 生产环境接口
|
||||
server-url: https://server.s209.websoft.top/api
|
||||
server-url: https://server.websoft.top/api
|
||||
upload-path: /www/wwwroot/file.ws/
|
||||
|
||||
# 阿里云OSS云存储
|
||||
|
||||
@@ -101,7 +101,7 @@ config:
|
||||
swagger-version: 2.0
|
||||
token-key: WLgNsWJ8rPjRtnjzX/Gx2RGS80Kwnm/ZeLbvIL+NrBs=
|
||||
# 主服务器
|
||||
server-url: https://server.s209.websoft.top/api
|
||||
server-url: https://server.websoft.top/api
|
||||
# 文件服务器
|
||||
file-server: https://file.wsdns.cn
|
||||
upload-path: /Users/gxwebsoft/Documents/uploads/
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCUhkLLcEzSnxFF
|
||||
hhjUXcwBuvdTypKWu/CFe0jQGgFnQVKIb0hkAR8YNiXE45zYn6sVgFY99NrtPjhD
|
||||
xE0PrXky3i4EXxqeG/FgSnYm5/Uc2Q/euMRoTDZHDBMFMXanwStvJBmimFtMwYn5
|
||||
M0tSwxrhFZlKPYxO6QLsw8+ZicHHdX2bODbIw+zutEC8OEmQlyDVuiGFXONS5/kZ
|
||||
WNlz5Jv5HgcnY637zCO2fg6lv0xFjlrITcnGK94alXoqc9hZ1MCuK7hsRDqk14WL
|
||||
Z9ELb2GjHK33FydVPs7sLWX/pId0SPy5PstqyZH57sWWfj+StqBvoXAWqWEt6teZ
|
||||
Jn/UaTB9AgMBAAECggEAb1Nvj5OeUaUfShBoXg3sU0O0DR9i3w8CCttMyYcklCO3
|
||||
XEKlbSgWCYzUpI7DSu/rSdOHUStOSdOAUvM5m824cbNtpKMwjWB+fWFyzFjDNhtR
|
||||
NO0jctXlPT3Ep/jaaoV1K/pQKLqwfIj5BUw4YlGRvTL2Ulpt59vp8FQZMIm8MOcw
|
||||
rNwYcUbMPaNKk4q3GF0LGvzW8k+S6wAWcAbx1KINRsLE0127o+shjGIlBiZgMJGZ
|
||||
nTMz4xdvVbojsMhdM8aEhq6GtmSHgBFKjETQPXiOjRDCGOM5yC/9R/9WsMGJmJ4m
|
||||
6Ec/RM4k9TZlnMZFsOZYO8S/kM+xgQUcAD8uGT1UgQKBgQDDGVZiqsDjudFcRkG/
|
||||
5pJN9PAC/Dk0Wzt6uRPZIhyFo2tDC/uL210Z5QR4hhB2nUSK8ANfAnepTotNzPHO
|
||||
DC/sO2NzLuZz5EZTLeg9ij9BZDK+0/6AiBT2XdBKR/uGZAffjFCDh+ujm44lbrRK
|
||||
7MUb9LtvDjPru1WVR0WhpFIwXQKBgQDC4xTQv6x3cPSW2SEglLVrl9CA68yO1g4T
|
||||
MphCav64Cl9UDk1ov5C2SCvshFbWlIBv2g7tqb/bUk8nj42GuZWBu1spkUt2y7HS
|
||||
eO89BmnaRNkVtWT8GtSMYYrYYAd23IGiOHPQqMnw/6HXkpjonpBa9c9CfEPwNtdq
|
||||
84pgqed+oQKBgC6rV/PAPuX6pC87iyzZffPx/JvqM9DnZgIEVdAiDcqV/emK60BY
|
||||
WBwCoaAnCbcmBahqo5PNpkw0wrP4q3sLhUcwKaj69huQ5pWtLJnUAS+mRVFKqt2a
|
||||
L9GDPXkXYP6T3SJHkVb1Y5O+eTFRGwW1P61hTJjTP+5K4L0V0H1LLnHtAoGAEDBU
|
||||
1lJVvUZAyxcWTWKM/3cI9uyffW4ClU2qoDnLFvalnJHjlEP1fW7ZVzhXDlQfpyrx
|
||||
+oQTT+CyepLOKtbXuIMbu4Q6RI//IYCyPtt9h4gYkFkVHmwMI+0mX3r6o8EFc7hE
|
||||
xpx+yeoyQ3oGAazKSQQKR3eTHS0xD81TPVxfwoECgYEAvBi3fPvIQ08pxk6kxj+S
|
||||
bypHo06JHT1Fi8pmKtKCGLduK85dCeBZqHmsorWC/qg4RgCFWFFKfrFTGTxC4nf8
|
||||
MRQHmKxq+SAh4SvFgRDA0lyaUWmw7H/JpolbBDIGnXhoDI0CmQU3s2xsQdJnNPIL
|
||||
azgaJXtOu+wr1MPR7Ij5OTU=
|
||||
-----END PRIVATE KEY-----
|
||||
@@ -1,24 +0,0 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIEFDCCAvygAwIBAgIUSjIxWE6Ttq53ggB00H6t6sy34iMwDQYJKoZIhvcNAQEL
|
||||
BQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsT
|
||||
FFRlbnBheS5jb20gQ0EgQ2VudGVyMRswGQYDVQQDExJUZW5wYXkuY29tIFJvb3Qg
|
||||
Q0EwHhcNMjMwOTE5MTUxNTQ4WhcNMjgwOTE3MTUxNTQ4WjBuMRgwFgYDVQQDDA9U
|
||||
ZW5wYXkuY29tIHNpZ24xEzARBgNVBAoMClRlbnBheS5jb20xHTAbBgNVBAsMFFRl
|
||||
bnBheS5jb20gQ0EgQ2VudGVyMQswCQYDVQQGDAJDTjERMA8GA1UEBwwIU2hlblpo
|
||||
ZW4wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC5AOeoNuRReVPfOodE
|
||||
TTE/wq96OK/7MI1lkwgtRlOIqwFGbGPxPx+wiLweWOWAeGrd0h+YuktIZezLhMhB
|
||||
8pkJBzxz4U+zoQ9n3yY4CgDUBeAO8eNHhEQTTOTFUIvIlRxyr0EVuTHNP7pIkxA6
|
||||
gUvajWjJFbvU393vDHA9RflWrBNw1qVjkPPUx6axizD3wS8f77bwuspEWGTza/pS
|
||||
g96HWUwFh9OSlQNPOjPG4km2YRQwGhoRMlLeZsUB+a0PtV0HI/B0TbY5u2EmPt0R
|
||||
uNZLmcwImtiANYmySww6gp5uo4+im0P/kHKynPrW1SkS+8M9JP5T7Xck3bnHNv4S
|
||||
9UOPAgMBAAGjgbkwgbYwCQYDVR0TBAIwADALBgNVHQ8EBAMCA/gwgZsGA1UdHwSB
|
||||
kzCBkDCBjaCBiqCBh4aBhGh0dHA6Ly9ldmNhLml0cnVzLmNvbS5jbi9wdWJsaWMv
|
||||
aXRydXNjcmw/Q0E9MUJENDIyMEU1MERCQzA0QjA2QUQzOTc1NDk4NDZDMDFDM0U4
|
||||
RUJEMiZzZz1IQUNDNDcxQjY1NDIyRTEyQjI3QTlEMzNBODdBRDFDREY1OTI2RTE0
|
||||
MDM3MTANBgkqhkiG9w0BAQsFAAOCAQEAHd42YyvxvvjYEIkCURHweCSQpWQrdsnP
|
||||
AU2rcVzOx0kDxjq7+W2HkIkYeAZfE8pyBs5c39tgK+qospiDsKa9WYeBNyIRcCvN
|
||||
q9ObLqSV4Cy/8lQMNh4Q37cdc3X+pAlTr7MtKka8ZcXYvbMBqus0dfJZayZvW7Ak
|
||||
nnaXXJ4k7urgHOGYsZlZ+HC+DC/sYoN3DXzvg3XPlL0SNEQH0cWrRbaQnpOKVMk5
|
||||
TptbeNHKJfUxVW5jh4GtBFqvLeiOruY+gFYg7UkCKWo9ZIYe7/mEvJeHYh3acTTl
|
||||
DOl3L0fR5KR7vqFMwZEUZxVOs6K2ANSCr57OQPBV++MG4DPr3yOhCQ==
|
||||
-----END CERTIFICATE-----
|
||||
158
src/test/java/com/gxwebsoft/test/WechatPayPathTest.java
Normal file
158
src/test/java/com/gxwebsoft/test/WechatPayPathTest.java
Normal file
@@ -0,0 +1,158 @@
|
||||
package com.gxwebsoft.test;
|
||||
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.core.service.PaymentCacheService;
|
||||
import com.gxwebsoft.common.core.utils.CertificateLoader;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 微信支付路径处理测试
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-29
|
||||
*/
|
||||
@SpringBootTest
|
||||
public class WechatPayPathTest {
|
||||
|
||||
@Autowired
|
||||
private PaymentCacheService paymentCacheService;
|
||||
|
||||
@Autowired
|
||||
private CertificateLoader certificateLoader;
|
||||
|
||||
@Autowired
|
||||
private CertificateProperties certConfig;
|
||||
|
||||
@Test
|
||||
public void testPublicKeyPathHandling() {
|
||||
try {
|
||||
System.out.println("=== 微信支付公钥路径处理测试 ===");
|
||||
|
||||
// 测试租户ID
|
||||
Integer tenantId = 10547;
|
||||
|
||||
// 获取支付配置
|
||||
Payment payment = paymentCacheService.getWechatPayConfig(tenantId);
|
||||
if (payment == null) {
|
||||
System.err.println("❌ 支付配置不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("数据库配置信息:");
|
||||
System.out.println("租户ID: " + tenantId);
|
||||
System.out.println("商户号: " + payment.getMchId());
|
||||
System.out.println("公钥文件配置: " + payment.getPubKey());
|
||||
System.out.println("公钥ID: " + payment.getPubKeyId());
|
||||
|
||||
// 测试路径处理逻辑
|
||||
if (payment.getPubKey() != null && !payment.getPubKey().isEmpty()) {
|
||||
System.out.println("\n=== 路径处理测试 ===");
|
||||
|
||||
String pubKeyPath;
|
||||
if (payment.getPubKey().startsWith("dev/wechat/") || payment.getPubKey().startsWith("/")) {
|
||||
// 如果数据库中存储的是完整路径,直接使用
|
||||
pubKeyPath = payment.getPubKey();
|
||||
System.out.println("✅ 检测到完整路径,直接使用: " + pubKeyPath);
|
||||
} else {
|
||||
// 如果是相对路径,需要拼接租户目录
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
pubKeyPath = tenantCertPath + "/" + payment.getPubKey();
|
||||
System.out.println("✅ 检测到相对路径,拼接后: " + pubKeyPath);
|
||||
}
|
||||
|
||||
// 测试文件是否存在
|
||||
System.out.println("\n=== 文件存在性检查 ===");
|
||||
if (certificateLoader.certificateExists(pubKeyPath)) {
|
||||
try {
|
||||
String actualPath = certificateLoader.loadCertificatePath(pubKeyPath);
|
||||
System.out.println("✅ 公钥文件存在: " + actualPath);
|
||||
|
||||
// 检查文件大小
|
||||
java.io.File file = new java.io.File(actualPath);
|
||||
if (file.exists()) {
|
||||
System.out.println("文件大小: " + file.length() + " 字节");
|
||||
System.out.println("文件可读: " + file.canRead());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("❌ 加载公钥文件失败: " + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
System.err.println("❌ 公钥文件不存在: " + pubKeyPath);
|
||||
|
||||
// 提供修复建议
|
||||
System.out.println("\n=== 修复建议 ===");
|
||||
if (payment.getPubKey().contains("/")) {
|
||||
System.out.println("1. 检查数据库中的路径是否正确");
|
||||
System.out.println("2. 确认文件是否已上传到指定位置");
|
||||
System.out.println("3. 当前配置的路径: " + payment.getPubKey());
|
||||
} else {
|
||||
System.out.println("1. 将公钥文件放置到: src/main/resources/dev/wechat/" + tenantId + "/");
|
||||
System.out.println("2. 或者更新数据库配置为完整路径");
|
||||
}
|
||||
}
|
||||
|
||||
// 测试其他证书文件
|
||||
System.out.println("\n=== 其他证书文件检查 ===");
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
String merchantCertPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getApiclientCertFile();
|
||||
|
||||
System.out.println("私钥文件: " + privateKeyPath + " - " +
|
||||
(certificateLoader.certificateExists(privateKeyPath) ? "✅ 存在" : "❌ 不存在"));
|
||||
System.out.println("商户证书: " + merchantCertPath + " - " +
|
||||
(certificateLoader.certificateExists(merchantCertPath) ? "✅ 存在" : "❌ 不存在"));
|
||||
|
||||
} else {
|
||||
System.out.println("⚠️ 未配置公钥信息");
|
||||
}
|
||||
|
||||
System.out.println("\n=== 测试完成 ===");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("❌ 测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateCorrectDirectoryStructure() {
|
||||
System.out.println("=== 创建正确的目录结构建议 ===");
|
||||
|
||||
try {
|
||||
Integer tenantId = 10547;
|
||||
Payment payment = paymentCacheService.getWechatPayConfig(tenantId);
|
||||
|
||||
if (payment != null && payment.getPubKey() != null) {
|
||||
System.out.println("当前数据库配置的公钥路径: " + payment.getPubKey());
|
||||
|
||||
if (payment.getPubKey().contains("/")) {
|
||||
// 包含路径分隔符,说明是完整路径
|
||||
System.out.println("\n建议的文件放置位置:");
|
||||
System.out.println("src/main/resources/" + payment.getPubKey());
|
||||
|
||||
// 创建目录的命令
|
||||
String dirPath = payment.getPubKey().substring(0, payment.getPubKey().lastIndexOf("/"));
|
||||
System.out.println("\n创建目录的命令:");
|
||||
System.out.println("mkdir -p src/main/resources/" + dirPath);
|
||||
|
||||
// 复制文件的命令
|
||||
System.out.println("\n如果您有公钥文件,可以这样复制:");
|
||||
System.out.println("cp your_public_key.pem src/main/resources/" + payment.getPubKey());
|
||||
} else {
|
||||
// 只是文件名,需要放在租户目录下
|
||||
System.out.println("\n建议的文件放置位置:");
|
||||
System.out.println("src/main/resources/dev/wechat/" + tenantId + "/" + payment.getPubKey());
|
||||
|
||||
System.out.println("\n或者更新数据库配置为:");
|
||||
System.out.println("UPDATE sys_payment SET pub_key = 'dev/wechat/" + tenantId + "/" + payment.getPubKey() + "' WHERE tenant_id = " + tenantId + " AND type = 0;");
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("获取配置失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
150
src/test/java/com/gxwebsoft/test/WechatPayPublicKeyTest.java
Normal file
150
src/test/java/com/gxwebsoft/test/WechatPayPublicKeyTest.java
Normal file
@@ -0,0 +1,150 @@
|
||||
package com.gxwebsoft.test;
|
||||
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.core.service.PaymentCacheService;
|
||||
import com.gxwebsoft.common.core.utils.CertificateLoader;
|
||||
import com.gxwebsoft.common.core.utils.WechatCertAutoConfig;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import com.wechat.pay.java.core.Config;
|
||||
import com.wechat.pay.java.core.RSAPublicKeyConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
/**
|
||||
* 微信支付公钥配置测试
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-29
|
||||
*/
|
||||
@SpringBootTest
|
||||
public class WechatPayPublicKeyTest {
|
||||
|
||||
@Autowired
|
||||
private PaymentCacheService paymentCacheService;
|
||||
|
||||
@Autowired
|
||||
private CertificateLoader certificateLoader;
|
||||
|
||||
@Autowired
|
||||
private CertificateProperties certConfig;
|
||||
|
||||
@Autowired
|
||||
private WechatCertAutoConfig wechatCertAutoConfig;
|
||||
|
||||
@Test
|
||||
public void testPublicKeyConfiguration() {
|
||||
try {
|
||||
System.out.println("=== 微信支付公钥配置测试 ===");
|
||||
|
||||
// 测试租户ID
|
||||
Integer tenantId = 10547;
|
||||
|
||||
// 获取支付配置
|
||||
Payment payment = paymentCacheService.getWechatPayConfig(tenantId);
|
||||
System.out.println("支付配置获取成功:");
|
||||
System.out.println("商户号: " + payment.getMchId());
|
||||
System.out.println("应用ID: " + payment.getAppId());
|
||||
System.out.println("序列号: " + payment.getMerchantSerialNumber());
|
||||
System.out.println("API密钥: " + (payment.getApiKey() != null ? "已配置(长度:" + payment.getApiKey().length() + ")" : "未配置"));
|
||||
System.out.println("公钥文件: " + payment.getPubKey());
|
||||
System.out.println("公钥ID: " + payment.getPubKeyId());
|
||||
|
||||
// 测试证书文件加载
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
|
||||
System.out.println("\n=== 证书文件检查 ===");
|
||||
System.out.println("私钥路径: " + privateKeyPath);
|
||||
|
||||
if (certificateLoader.certificateExists(privateKeyPath)) {
|
||||
String privateKey = certificateLoader.loadCertificatePath(privateKeyPath);
|
||||
System.out.println("✅ 私钥文件加载成功: " + privateKey);
|
||||
|
||||
// 检查是否配置了公钥
|
||||
if (payment.getPubKey() != null && !payment.getPubKey().isEmpty() &&
|
||||
payment.getPubKeyId() != null && !payment.getPubKeyId().isEmpty()) {
|
||||
|
||||
System.out.println("\n=== 公钥配置测试 ===");
|
||||
String pubKeyPath = tenantCertPath + "/" + payment.getPubKey();
|
||||
System.out.println("公钥路径: " + pubKeyPath);
|
||||
|
||||
if (certificateLoader.certificateExists(pubKeyPath)) {
|
||||
String pubKeyFile = certificateLoader.loadCertificatePath(pubKeyPath);
|
||||
System.out.println("✅ 公钥文件加载成功: " + pubKeyFile);
|
||||
|
||||
// 测试RSA公钥配置
|
||||
try {
|
||||
Config config = new RSAPublicKeyConfig.Builder()
|
||||
.merchantId(payment.getMchId())
|
||||
.privateKeyFromPath(privateKey)
|
||||
.publicKeyFromPath(pubKeyFile)
|
||||
.publicKeyId(payment.getPubKeyId())
|
||||
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||
.apiV3Key(payment.getApiKey())
|
||||
.build();
|
||||
|
||||
System.out.println("✅ RSA公钥配置创建成功");
|
||||
System.out.println("配置类型: " + config.getClass().getSimpleName());
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("❌ RSA公钥配置失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} else {
|
||||
System.err.println("❌ 公钥文件不存在: " + pubKeyPath);
|
||||
System.out.println("💡 建议: 请将公钥文件放置到指定位置");
|
||||
}
|
||||
|
||||
} else {
|
||||
System.out.println("\n⚠️ 未配置公钥信息");
|
||||
System.out.println("💡 建议: 在数据库中配置 pubKey 和 pubKeyId 字段");
|
||||
|
||||
// 测试自动证书配置
|
||||
System.out.println("\n=== 自动证书配置测试 ===");
|
||||
try {
|
||||
Config autoConfig = wechatCertAutoConfig.createAutoConfig(
|
||||
payment.getMchId(),
|
||||
privateKey,
|
||||
payment.getMerchantSerialNumber(),
|
||||
payment.getApiKey()
|
||||
);
|
||||
System.out.println("✅ 自动证书配置创建成功");
|
||||
System.out.println("配置类型: " + autoConfig.getClass().getSimpleName());
|
||||
} catch (Exception e) {
|
||||
System.err.println("❌ 自动证书配置失败: " + e.getMessage());
|
||||
System.err.println("错误类型: " + e.getClass().getName());
|
||||
|
||||
if (e.getMessage() != null && e.getMessage().contains("certificate")) {
|
||||
System.err.println("🔍 这是证书相关错误,建议使用公钥模式");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
System.err.println("❌ 私钥文件不存在: " + privateKeyPath);
|
||||
}
|
||||
|
||||
System.out.println("\n=== 测试完成 ===");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("❌ 测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSamplePublicKeyConfig() {
|
||||
System.out.println("=== 创建示例公钥配置 ===");
|
||||
System.out.println("如果您的后台使用公钥模式,请在数据库中配置以下字段:");
|
||||
System.out.println("1. pubKey: 公钥文件名,例如 'wechatpay_public_key.pem'");
|
||||
System.out.println("2. pubKeyId: 公钥ID,例如 'PUB_KEY_ID_0112422897022025011300326200001208'");
|
||||
System.out.println("3. 将公钥文件放置到: src/main/resources/dev/wechat/{tenantId}/");
|
||||
System.out.println("\n示例SQL更新语句:");
|
||||
System.out.println("UPDATE sys_payment SET ");
|
||||
System.out.println(" pub_key = 'wechatpay_public_key.pem',");
|
||||
System.out.println(" pub_key_id = 'PUB_KEY_ID_0112422897022025011300326200001208'");
|
||||
System.out.println("WHERE tenant_id = 10547 AND type = 0;");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user