Browse Source

feat(payment): 添加获取支付配置功能并优化支付流程

- 在 PaymentMapper 中添加 getByType 方法,用于获取指定类型的支付配置
- 在 PaymentService 中添加 getByType 方法,实现获取支付配置的业务逻辑- 修改 PaymentController 中的 createPayment 方法,增加用户登录验证和设置默认值
- 优化 WxPayConfigService 中的 getPaymentConfig 方法,实现缓存和数据库查询的逻辑
pan
科技小王子 1 month ago
parent
commit
495e6a72c6
  1. 3
      src/main/java/com/gxwebsoft/common/system/mapper/PaymentMapper.java
  2. 10
      src/main/java/com/gxwebsoft/common/system/mapper/xml/PaymentMapper.xml
  3. 1
      src/main/java/com/gxwebsoft/common/system/service/PaymentService.java
  4. 5
      src/main/java/com/gxwebsoft/common/system/service/impl/PaymentServiceImpl.java
  5. 10
      src/main/java/com/gxwebsoft/payment/controller/PaymentController.java
  6. 43
      src/main/java/com/gxwebsoft/payment/service/WxPayConfigService.java

3
src/main/java/com/gxwebsoft/common/system/mapper/PaymentMapper.java

@ -1,5 +1,6 @@
package com.gxwebsoft.common.system.mapper;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxwebsoft.common.system.entity.Payment;
@ -34,4 +35,6 @@ public interface PaymentMapper extends BaseMapper<Payment> {
*/
List<Payment> selectListRel(@Param("param") PaymentParam param);
@InterceptorIgnore(tenantLine = "true")
Payment getByType(@Param("param") PaymentParam paymentParam);
}

10
src/main/java/com/gxwebsoft/common/system/mapper/xml/PaymentMapper.xml

@ -77,4 +77,14 @@
<include refid="selectSql"></include>
</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>

1
src/main/java/com/gxwebsoft/common/system/service/PaymentService.java

@ -39,4 +39,5 @@ public interface PaymentService extends IService<Payment> {
*/
Payment getByIdRel(Integer id);
Payment getByType(PaymentParam paymentParam);
}

5
src/main/java/com/gxwebsoft/common/system/service/impl/PaymentServiceImpl.java

@ -44,4 +44,9 @@ public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> impl
return param.getOne(baseMapper.selectListRel(param));
}
@Override
public Payment getByType(PaymentParam paymentParam) {
return baseMapper.getByType(paymentParam);
}
}

10
src/main/java/com/gxwebsoft/payment/controller/PaymentController.java

@ -2,6 +2,7 @@ package com.gxwebsoft.payment.controller;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.payment.constants.PaymentConstants;
import com.gxwebsoft.payment.dto.PaymentRequest;
import com.gxwebsoft.payment.dto.PaymentResponse;
@ -45,7 +46,16 @@ public class PaymentController extends BaseController {
@PostMapping("/create")
public ApiResult<?> createPayment(@Valid @RequestBody PaymentRequest 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 {
PaymentResponse response = paymentService.createPayment(request);
return this.<PaymentResponse>success("支付订单创建成功", response);

43
src/main/java/com/gxwebsoft/payment/service/WxPayConfigService.java

@ -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,23 +99,42 @@ public class WxPayConfigService {
/**
* 获取支付配置信息
* 优先从缓存获取缓存没有则查询数据库最后兜底到开发环境测试配置
*/
private Payment getPaymentConfig(Integer tenantId) throws PaymentException {
String cacheKey = "Payment:wxPay:" + tenantId;
Payment payment = redisUtil.get(cacheKey, Payment.class);
if (payment == null && !"dev".equals(activeProfile)) {
throw PaymentException.systemError("微信支付配置未找到,租户ID: " + tenantId, null);
}
System.out.println("payment = " + payment);
if (payment != null) {
log.debug("从缓存获取支付配置成功,租户ID: {}", tenantId);
} else {
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 {
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()) {

Loading…
Cancel
Save