|
|
@ -3,6 +3,7 @@ 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.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
@ -19,6 +20,9 @@ public class WechatPayConfigValidator { |
|
|
|
private final CertificateProperties certConfig; |
|
|
|
private final CertificateLoader certificateLoader; |
|
|
|
|
|
|
|
@Value("${spring.profiles.active}") |
|
|
|
private String activeProfile; |
|
|
|
|
|
|
|
public WechatPayConfigValidator(CertificateProperties certConfig, CertificateLoader certificateLoader) { |
|
|
|
this.certConfig = certConfig; |
|
|
|
this.certificateLoader = certificateLoader; |
|
|
@ -111,19 +115,25 @@ public class WechatPayConfigValidator { |
|
|
|
* 验证证书文件 |
|
|
|
*/ |
|
|
|
private void validateCertificateFiles(Integer tenantId, ValidationResult result) { |
|
|
|
String tenantCertPath = "dev/wechat/" + tenantId; |
|
|
|
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile(); |
|
|
|
|
|
|
|
if (!certificateLoader.certificateExists(privateKeyPath)) { |
|
|
|
result.addError("证书文件不存在: " + privateKeyPath); |
|
|
|
return; |
|
|
|
} |
|
|
|
if ("dev".equals(activeProfile)) { |
|
|
|
// 开发环境证书验证
|
|
|
|
String tenantCertPath = "dev/wechat/" + tenantId; |
|
|
|
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile(); |
|
|
|
|
|
|
|
if (!certificateLoader.certificateExists(privateKeyPath)) { |
|
|
|
result.addError("证书文件不存在: " + privateKeyPath); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
String privateKey = certificateLoader.loadCertificatePath(privateKeyPath); |
|
|
|
log.info("✅ 证书文件验证通过: {}", privateKey); |
|
|
|
} catch (Exception e) { |
|
|
|
result.addError("证书文件加载失败: " + e.getMessage()); |
|
|
|
try { |
|
|
|
certificateLoader.loadCertificatePath(privateKeyPath); |
|
|
|
log.info("✅ 开发环境证书文件验证通过: {}", privateKeyPath); |
|
|
|
} catch (Exception e) { |
|
|
|
result.addError("证书文件加载失败: " + e.getMessage()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 生产环境证书验证 - 跳过文件存在性检查,因为证书路径来自数据库
|
|
|
|
log.info("✅ 生产环境跳过证书文件存在性验证,使用数据库配置的证书路径"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -184,12 +194,21 @@ public class WechatPayConfigValidator { |
|
|
|
} |
|
|
|
|
|
|
|
// 证书文件检查
|
|
|
|
String tenantCertPath = "dev/wechat/" + tenantId; |
|
|
|
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile(); |
|
|
|
boolean certExists = certificateLoader.certificateExists(privateKeyPath); |
|
|
|
|
|
|
|
report.append("证书文件路径: ").append(privateKeyPath).append("\n"); |
|
|
|
report.append("证书文件存在: ").append(certExists ? "是" : "否").append("\n"); |
|
|
|
report.append("当前环境: ").append(activeProfile).append("\n"); |
|
|
|
if ("dev".equals(activeProfile)) { |
|
|
|
String tenantCertPath = "dev/wechat/" + tenantId; |
|
|
|
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile(); |
|
|
|
boolean certExists = certificateLoader.certificateExists(privateKeyPath); |
|
|
|
|
|
|
|
report.append("开发环境证书文件路径: ").append(privateKeyPath).append("\n"); |
|
|
|
report.append("证书文件存在: ").append(certExists ? "是" : "否").append("\n"); |
|
|
|
} else { |
|
|
|
report.append("生产环境证书路径: 从数据库配置获取\n"); |
|
|
|
if (payment != null) { |
|
|
|
report.append("私钥文件: ").append(payment.getApiclientKey()).append("\n"); |
|
|
|
report.append("证书文件: ").append(payment.getApiclientCert()).append("\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ValidationResult validation = validateWechatPayConfig(payment, tenantId); |
|
|
|
report.append("配置验证结果: ").append(validation.isValid() ? "通过" : "失败").append("\n"); |
|
|
|