fix:支付流程调试通过
This commit is contained in:
+45
-2
@@ -4,6 +4,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
|
||||
@@ -16,19 +19,59 @@ import java.util.Base64;
|
||||
public class PrivateKeyUtils {
|
||||
|
||||
public static PrivateKey getPrivateKey(String privateKeyStr) {
|
||||
if (privateKeyStr == null || privateKeyStr.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("私钥字符串不能为空");
|
||||
}
|
||||
|
||||
// 检查是否是证书而不是私钥
|
||||
if (privateKeyStr.contains("-----BEGIN CERTIFICATE-----")) {
|
||||
throw new IllegalArgumentException("输入的是证书而不是私钥,请提供正确的私钥");
|
||||
}
|
||||
|
||||
try {
|
||||
// 移除所有可能的头部和尾部标记
|
||||
String privateKeyPEM = privateKeyStr
|
||||
.replace("-----BEGIN PRIVATE KEY-----", "")
|
||||
.replace("-----END PRIVATE KEY-----", "")
|
||||
// 移除所有空白字符(包括换行符、制表符等)
|
||||
.replaceAll("\\s+", "");
|
||||
|
||||
byte[] decoded = Base64.getDecoder().decode(privateKeyPEM);
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decoded);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
return keyFactory.generatePrivate(keySpec);
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.error("私钥格式错误: {}", e.getMessage());
|
||||
throw new IllegalArgumentException("私钥格式错误: " + e.getMessage(), e);
|
||||
} catch (Exception e) {
|
||||
log.error("解析私钥失败", e);
|
||||
throw new RuntimeException("解析私钥失败", e);
|
||||
log.error("解析私钥失败,原始私钥字符串: {}", privateKeyStr, e);
|
||||
throw new RuntimeException("解析私钥失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public static X509Certificate getCertificate(String certificateStr) {
|
||||
if (certificateStr == null || certificateStr.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("证书字符串不能为空");
|
||||
}
|
||||
|
||||
// 检查是否是私钥而不是证书
|
||||
if (certificateStr.contains("-----BEGIN PRIVATE KEY-----") ||
|
||||
certificateStr.contains("-----BEGIN RSA PRIVATE KEY-----")) {
|
||||
throw new IllegalArgumentException("输入的是私钥而不是证书,请提供正确的证书");
|
||||
}
|
||||
|
||||
try {
|
||||
String certificatePEM = certificateStr
|
||||
.replace("-----BEGIN CERTIFICATE-----", "")
|
||||
.replace("-----END CERTIFICATE-----", "")
|
||||
.replaceAll("\\s+", "");
|
||||
|
||||
byte[] decoded = Base64.getDecoder().decode(certificatePEM);
|
||||
CertificateFactory factory = CertificateFactory.getInstance("X.509");
|
||||
return (X509Certificate) factory.generateCertificate(new java.io.ByteArrayInputStream(decoded));
|
||||
} catch (CertificateException e) {
|
||||
log.error("解析证书失败,原始证书字符串: {}", certificateStr, e);
|
||||
throw new RuntimeException("解析证书失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -49,7 +49,7 @@ public class WechatAutoConfiguration {
|
||||
return new WxMaServiceImpl() {
|
||||
@Override
|
||||
public WxMaDefaultConfigImpl getWxMaConfig() {
|
||||
WechatConfig config = configService.getConfig("MINI", getCurrentTenantId());
|
||||
WechatConfig config = configService.getConfig("PAY", getCurrentTenantId());
|
||||
WxMaDefaultConfigImpl wxConfig = new WxMaDefaultConfigImpl();
|
||||
wxConfig.setAppid(config.getAppId());
|
||||
wxConfig.setSecret(config.getAppSecret());
|
||||
@@ -60,9 +60,9 @@ public class WechatAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean({WechatConfigService.class})
|
||||
public WxPayConfigFactory wxPayConfigFactory(WechatConfigService configService) {
|
||||
public WxPayConfigFactory wxPayConfigFactory() {
|
||||
log.info("WxPayConfigFactory被加载,tenantId:{}", getCurrentTenantId());
|
||||
return new WxPayConfigFactory(configService);
|
||||
return new WxPayConfigFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
+8
-9
@@ -7,9 +7,10 @@ import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
||||
import com.wechat.pay.java.core.notification.NotificationConfig;
|
||||
import com.wechat.pay.java.core.notification.NotificationParser;
|
||||
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
|
||||
import com.wechat.pay.java.service.refund.RefundService;
|
||||
import com.wechat.pay.java.service.profitsharing.ProfitsharingService;
|
||||
import com.wechat.pay.java.service.refund.RefundService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -22,8 +23,9 @@ import java.util.concurrent.ThreadPoolExecutor;
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WxPayConfigFactory {
|
||||
private final WechatConfigService configService;
|
||||
|
||||
@Autowired
|
||||
private WechatConfigService configService;
|
||||
|
||||
// 缓存各个服务的实例
|
||||
private final Map<String, Config> configCache = new ConcurrentHashMap<>();
|
||||
private final Map<String, JsapiServiceExtension> jsapiServiceCache = new ConcurrentHashMap<>();
|
||||
@@ -32,9 +34,6 @@ public class WxPayConfigFactory {
|
||||
private final Map<String, ProfitsharingService> profitsharingServiceCache = new ConcurrentHashMap<>();
|
||||
private final Map<String, Executor> executorCache = new ConcurrentHashMap<>();
|
||||
|
||||
public WxPayConfigFactory(WechatConfigService configService) {
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
public Config createConfig(String tenantId) {
|
||||
return configCache.computeIfAbsent(tenantId, this::buildConfig);
|
||||
@@ -44,7 +43,7 @@ public class WxPayConfigFactory {
|
||||
log.info("创建微信支付配置,tenantId:{}", tenantId);
|
||||
WechatConfig config = configService.getConfig("PAY", tenantId);
|
||||
PrivateKey privateKey = PrivateKeyUtils.getPrivateKey(config.getPrivateKey());
|
||||
|
||||
|
||||
return new RSAAutoCertificateConfig.Builder()
|
||||
.merchantId(config.getMerchantId())
|
||||
.privateKey(privateKey)
|
||||
@@ -70,14 +69,14 @@ public class WxPayConfigFactory {
|
||||
log.info("创建微信支付通知解析器,tenantId:{}", tenantId);
|
||||
WechatConfig config = configService.getConfig("PAY", tenantId);
|
||||
PrivateKey privateKey = PrivateKeyUtils.getPrivateKey(config.getPrivateKey());
|
||||
|
||||
|
||||
NotificationConfig notificationConfig = new RSAAutoCertificateConfig.Builder()
|
||||
.merchantId(config.getMerchantId())
|
||||
.privateKey(privateKey)
|
||||
.merchantSerialNumber(config.getMchSerialNo())
|
||||
.apiV3Key(config.getApiV3Key())
|
||||
.build();
|
||||
|
||||
|
||||
return new NotificationParser(notificationConfig);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user