|
|
@ -1,10 +1,13 @@ |
|
|
|
package com.gxwebsoft.payment.service; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.gxwebsoft.common.core.config.CertificateProperties; |
|
|
|
import com.gxwebsoft.common.core.service.CertificateService; |
|
|
|
import com.gxwebsoft.common.core.utils.RedisUtil; |
|
|
|
import com.gxwebsoft.common.core.utils.WxNativeUtil; |
|
|
|
import com.gxwebsoft.common.system.entity.Payment; |
|
|
|
import com.gxwebsoft.common.system.param.PaymentParam; |
|
|
|
import com.gxwebsoft.common.system.service.PaymentService; |
|
|
|
import com.gxwebsoft.payment.exception.PaymentException; |
|
|
|
import com.wechat.pay.java.core.Config; |
|
|
|
import com.wechat.pay.java.core.RSAAutoCertificateConfig; |
|
|
@ -15,6 +18,7 @@ import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
/** |
|
|
|
* 微信支付配置服务 |
|
|
@ -36,6 +40,9 @@ public class WxPayConfigService { |
|
|
|
@Resource |
|
|
|
private CertificateProperties certificateProperties; |
|
|
|
|
|
|
|
@Resource |
|
|
|
private PaymentService paymentService; |
|
|
|
|
|
|
|
@Value("${spring.profiles.active:dev}") |
|
|
|
private String activeProfile; |
|
|
|
|
|
|
@ -92,22 +99,41 @@ public class WxPayConfigService { |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取支付配置信息 |
|
|
|
* 优先从缓存获取,缓存没有则查询数据库,最后兜底到开发环境测试配置 |
|
|
|
*/ |
|
|
|
private Payment getPaymentConfig(Integer tenantId) throws PaymentException { |
|
|
|
String cacheKey = "Payment:wxPay:" + tenantId; |
|
|
|
Payment payment = redisUtil.get(cacheKey, Payment.class); |
|
|
|
System.out.println("payment = " + payment); |
|
|
|
if (payment != null) { |
|
|
|
log.debug("从缓存获取支付配置成功,租户ID: {}", tenantId); |
|
|
|
return payment; |
|
|
|
} |
|
|
|
|
|
|
|
if (payment == null && !"dev".equals(activeProfile)) { |
|
|
|
throw PaymentException.systemError("微信支付配置未找到,租户ID: " + tenantId, null); |
|
|
|
// 缓存中没有,尝试从数据库查询
|
|
|
|
try { |
|
|
|
final PaymentParam paymentParam = new PaymentParam(); |
|
|
|
paymentParam.setType(1); |
|
|
|
paymentParam.setTenantId(tenantId); |
|
|
|
payment = paymentService.getByType(paymentParam); |
|
|
|
System.out.println("payment1 = " + payment); |
|
|
|
if (payment != null) { |
|
|
|
log.info("从数据库获取支付配置成功,租户ID: {},将缓存配置", tenantId); |
|
|
|
// 将查询到的配置缓存起来,缓存1小时
|
|
|
|
redisUtil.set(cacheKey, payment, 1L,TimeUnit.DAYS); |
|
|
|
return payment; |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.warn("从数据库查询支付配置失败,租户ID: {},错误: {}", tenantId, e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
if (payment != null) { |
|
|
|
log.debug("从缓存获取支付配置成功,租户ID: {}", tenantId); |
|
|
|
} else { |
|
|
|
log.debug("开发环境模式,将使用测试配置,租户ID: {}", tenantId); |
|
|
|
// 数据库也没有配置
|
|
|
|
if (!"dev".equals(activeProfile)) { |
|
|
|
throw PaymentException.systemError("微信支付配置未找到,租户ID: " + tenantId + ",请检查数据库配置", null); |
|
|
|
} |
|
|
|
|
|
|
|
return payment; |
|
|
|
log.debug("开发环境模式,将使用测试配置,租户ID: {}", tenantId); |
|
|
|
return null; // 开发环境返回null,使用测试配置
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -126,7 +152,8 @@ public class WxPayConfigService { |
|
|
|
*/ |
|
|
|
private String getDevCertificatePath(Integer tenantId) throws PaymentException { |
|
|
|
try { |
|
|
|
String certPath = "cert/wechat-pay/apiclient_key.pem"; |
|
|
|
// 根据租户ID构建证书路径
|
|
|
|
String certPath = "dev/wechat/" + tenantId + "/apiclient_key.pem"; |
|
|
|
ClassPathResource resource = new ClassPathResource(certPath); |
|
|
|
|
|
|
|
if (!resource.exists()) { |
|
|
|