feat(payment): 添加获取支付配置功能并优化支付流程
- 在 PaymentMapper 中添加 getByType 方法,用于获取指定类型的支付配置 - 在 PaymentService 中添加 getByType 方法,实现获取支付配置的业务逻辑- 修改 PaymentController 中的 createPayment 方法,增加用户登录验证和设置默认值 - 优化 WxPayConfigService 中的 getPaymentConfig 方法,实现缓存和数据库查询的逻辑
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.gxwebsoft.common.system.mapper;
|
package com.gxwebsoft.common.system.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.gxwebsoft.common.system.entity.Payment;
|
import com.gxwebsoft.common.system.entity.Payment;
|
||||||
@@ -34,4 +35,6 @@ public interface PaymentMapper extends BaseMapper<Payment> {
|
|||||||
*/
|
*/
|
||||||
List<Payment> selectListRel(@Param("param") PaymentParam param);
|
List<Payment> selectListRel(@Param("param") PaymentParam param);
|
||||||
|
|
||||||
|
@InterceptorIgnore(tenantLine = "true")
|
||||||
|
Payment getByType(@Param("param") PaymentParam paymentParam);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,4 +77,14 @@
|
|||||||
<include refid="selectSql"></include>
|
<include refid="selectSql"></include>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="getByType" resultType="com.gxwebsoft.common.system.entity.Payment">
|
||||||
|
SELECT a.*
|
||||||
|
FROM gxwebsoft_core.sys_payment a
|
||||||
|
WHERE a.type = #{param.type}
|
||||||
|
AND a.deleted = 0
|
||||||
|
AND a.status = 1
|
||||||
|
AND a.tenant_id = #{param.tenantId}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
@@ -39,4 +39,5 @@ public interface PaymentService extends IService<Payment> {
|
|||||||
*/
|
*/
|
||||||
Payment getByIdRel(Integer id);
|
Payment getByIdRel(Integer id);
|
||||||
|
|
||||||
|
Payment getByType(PaymentParam paymentParam);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,4 +44,9 @@ public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> impl
|
|||||||
return param.getOne(baseMapper.selectListRel(param));
|
return param.getOne(baseMapper.selectListRel(param));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Payment getByType(PaymentParam paymentParam) {
|
||||||
|
return baseMapper.getByType(paymentParam);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.gxwebsoft.payment.controller;
|
|||||||
|
|
||||||
import com.gxwebsoft.common.core.web.ApiResult;
|
import com.gxwebsoft.common.core.web.ApiResult;
|
||||||
import com.gxwebsoft.common.core.web.BaseController;
|
import com.gxwebsoft.common.core.web.BaseController;
|
||||||
|
import com.gxwebsoft.common.system.entity.User;
|
||||||
import com.gxwebsoft.payment.constants.PaymentConstants;
|
import com.gxwebsoft.payment.constants.PaymentConstants;
|
||||||
import com.gxwebsoft.payment.dto.PaymentRequest;
|
import com.gxwebsoft.payment.dto.PaymentRequest;
|
||||||
import com.gxwebsoft.payment.dto.PaymentResponse;
|
import com.gxwebsoft.payment.dto.PaymentResponse;
|
||||||
@@ -45,7 +46,16 @@ public class PaymentController extends BaseController {
|
|||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
public ApiResult<?> createPayment(@Valid @RequestBody PaymentRequest request) {
|
public ApiResult<?> createPayment(@Valid @RequestBody PaymentRequest request) {
|
||||||
log.info("收到支付请求: {}", request);
|
log.info("收到支付请求: {}", request);
|
||||||
|
final User loginUser = getLoginUser();
|
||||||
|
|
||||||
|
if(loginUser == null){
|
||||||
|
return fail("请先登录");
|
||||||
|
}
|
||||||
|
|
||||||
|
request.setUserId(loginUser.getUserId());
|
||||||
|
if(request.getTenantId() == null){
|
||||||
|
request.setTenantId(loginUser.getTenantId());
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
PaymentResponse response = paymentService.createPayment(request);
|
PaymentResponse response = paymentService.createPayment(request);
|
||||||
return this.<PaymentResponse>success("支付订单创建成功", response);
|
return this.<PaymentResponse>success("支付订单创建成功", response);
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package com.gxwebsoft.payment.service;
|
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.config.CertificateProperties;
|
||||||
import com.gxwebsoft.common.core.service.CertificateService;
|
import com.gxwebsoft.common.core.service.CertificateService;
|
||||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||||
import com.gxwebsoft.common.core.utils.WxNativeUtil;
|
import com.gxwebsoft.common.core.utils.WxNativeUtil;
|
||||||
import com.gxwebsoft.common.system.entity.Payment;
|
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.gxwebsoft.payment.exception.PaymentException;
|
||||||
import com.wechat.pay.java.core.Config;
|
import com.wechat.pay.java.core.Config;
|
||||||
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
||||||
@@ -15,6 +18,7 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信支付配置服务
|
* 微信支付配置服务
|
||||||
@@ -36,6 +40,9 @@ public class WxPayConfigService {
|
|||||||
@Resource
|
@Resource
|
||||||
private CertificateProperties certificateProperties;
|
private CertificateProperties certificateProperties;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private PaymentService paymentService;
|
||||||
|
|
||||||
@Value("${spring.profiles.active:dev}")
|
@Value("${spring.profiles.active:dev}")
|
||||||
private String activeProfile;
|
private String activeProfile;
|
||||||
|
|
||||||
@@ -92,22 +99,41 @@ public class WxPayConfigService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取支付配置信息
|
* 获取支付配置信息
|
||||||
|
* 优先从缓存获取,缓存没有则查询数据库,最后兜底到开发环境测试配置
|
||||||
*/
|
*/
|
||||||
private Payment getPaymentConfig(Integer tenantId) throws PaymentException {
|
private Payment getPaymentConfig(Integer tenantId) throws PaymentException {
|
||||||
String cacheKey = "Payment:wxPay:" + tenantId;
|
String cacheKey = "Payment:wxPay:" + tenantId;
|
||||||
Payment payment = redisUtil.get(cacheKey, Payment.class);
|
Payment payment = redisUtil.get(cacheKey, Payment.class);
|
||||||
|
System.out.println("payment = " + payment);
|
||||||
if (payment == null && !"dev".equals(activeProfile)) {
|
|
||||||
throw PaymentException.systemError("微信支付配置未找到,租户ID: " + tenantId, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (payment != null) {
|
if (payment != null) {
|
||||||
log.debug("从缓存获取支付配置成功,租户ID: {}", tenantId);
|
log.debug("从缓存获取支付配置成功,租户ID: {}", tenantId);
|
||||||
} else {
|
return payment;
|
||||||
log.debug("开发环境模式,将使用测试配置,租户ID: {}", tenantId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return payment;
|
// 缓存中没有,尝试从数据库查询
|
||||||
|
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 (!"dev".equals(activeProfile)) {
|
||||||
|
throw PaymentException.systemError("微信支付配置未找到,租户ID: " + tenantId + ",请检查数据库配置", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("开发环境模式,将使用测试配置,租户ID: {}", tenantId);
|
||||||
|
return null; // 开发环境返回null,使用测试配置
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -126,7 +152,8 @@ public class WxPayConfigService {
|
|||||||
*/
|
*/
|
||||||
private String getDevCertificatePath(Integer tenantId) throws PaymentException {
|
private String getDevCertificatePath(Integer tenantId) throws PaymentException {
|
||||||
try {
|
try {
|
||||||
String certPath = "cert/wechat-pay/apiclient_key.pem";
|
// 根据租户ID构建证书路径
|
||||||
|
String certPath = "dev/wechat/" + tenantId + "/apiclient_key.pem";
|
||||||
ClassPathResource resource = new ClassPathResource(certPath);
|
ClassPathResource resource = new ClassPathResource(certPath);
|
||||||
|
|
||||||
if (!resource.exists()) {
|
if (!resource.exists()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user