feat:功能支付类模块完善

This commit is contained in:
2025-06-03 15:56:31 +08:00
parent ee840a2df1
commit 1dec9d2035
31 changed files with 433 additions and 798 deletions
@@ -8,14 +8,9 @@ import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.SettleInfo;
import com.wechat.pay.java.service.profitsharing.ProfitsharingService;
import com.wechat.pay.java.service.refund.RefundService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@@ -28,11 +23,11 @@ import java.util.concurrent.ThreadPoolExecutor;
* @date 2025-01-14
*
*/
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "tencent")
@EnableConfigurationProperties(ProfitSharingProperties.class)
//@Getter
//@Setter
//@Configuration
//@ConfigurationProperties(prefix = "tencent")
//@EnableConfigurationProperties(ProfitSharingProperties.class)
public class TencentPayConfiguration {
private String merchantId;
@@ -1,98 +1,98 @@
//package com.central.order.service;
//
//import com.central.common.context.LoginUserContextHolder;
//import com.central.common.exception.BusinessException;
//import com.central.common.model.SysUser;
//import com.central.order.feign.UserCenterService;
//import com.central.order.model.Order;
//import com.central.order.model.QrCode;
//import com.central.wechat.wechat.entity.WechatConfig;
//import com.central.wechat.wechat.feign.WechatConfigService;
//import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
//import com.wechat.pay.java.service.payments.jsapi.model.*;
//import com.zlt.wechat.config.WxPayConfigFactory;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//@Slf4j
//@Service
//public class PaymentService {
// @Autowired
// private WxPayConfigFactory wxPayConfigFactory;
// @Autowired
// private UserCenterService userCenterService;
// @Autowired
// private IOrderService orderService;
// @Autowired
// private IQrCodeService qrCodeService;
// @Autowired
// private IProfitSharingService profitSharingService;
// @Autowired
// private WechatConfigService configService;
//
//
//
// public PrepayWithRequestPaymentResponse createPayment(Long orderId) {
// // 1. 获取用户和订单信息
// SysUser user = userCenterService.findUserById(LoginUserContextHolder.getUser().getId());
// Order order = orderService.getById(orderId);
// if (order == null) {
// throw new BusinessException("订单数据不存在");
// }
//
// // 2. 获取二维码信息
// QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId());
// if (qrCode == null) {
// throw new BusinessException("二维码不存在");
// }
//
// // 3. 创建支付请求
// PrepayRequest request = new PrepayRequest();
//
// // 设置基本支付信息
// Amount amount = new Amount();
// amount.setTotal(Integer.parseInt(order.getOrderAmount()));
// request.setAmount(amount);
//
// // 从二维码配置中获取支付配置
// WechatConfig payConfig = configService.getConfig("PAY", qrCode.getTenantId());
// request.setAppid(payConfig.getAppId());
// request.setMchid(payConfig.getMerchantId());
// request.setDescription(order.getOrderNo());
// request.setNotifyUrl(payConfig.getNotifyUrl());
// request.setOutTradeNo(order.getOrderNo());
//
// // 设置支付者信息
// Payer payer = new Payer();
// payer.setOpenid(user.getOpenId());
// request.setPayer(payer);
//
// // 设置分账信息
// SettleInfo settleInfo = new SettleInfo();
// if (isEnableProfit(qrCode)) {
// settleInfo.setProfitSharing(true);
// request.setSettleInfo(settleInfo);
// // 创建分账记录
// profitSharingService.createProfitSharing(order, qrCode);
// } else {
// settleInfo.setProfitSharing(false);
// request.setSettleInfo(settleInfo);
// }
//
// // 4. 创建对应租户的支付服务
// JsapiServiceExtension jsapiService = wxPayConfigFactory.createJsapiService(qrCode.getTenantId());
//
// // 5. 调用支付接口
// return jsapiService.prepayWithRequestPayment(request);
// }
//
// private boolean isEnableProfit(QrCode qrCode) {
// return qrCode != null &&
// Integer.valueOf(1).equals(qrCode.getEnableProfit()) &&
// qrCode.getPayeeStatus() != null &&
// qrCode.getPayeeStatus() == 1 &&
// qrCode.getPayeeAccount() != null &&
// qrCode.getProfitRate() != null;
// }
//}
package com.central.order.service;
import com.central.common.context.LoginUserContextHolder;
import com.central.common.exception.BusinessException;
import com.central.common.model.SysUser;
import com.central.order.feign.UserCenterService;
import com.central.order.model.Order;
import com.central.order.model.QrCode;
import com.central.wechat.config.WxPayConfigFactory;
import com.central.wechat.entity.WechatConfig;
import com.central.wechat.feign.WechatConfigService;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class PaymentService {
@Autowired
private WxPayConfigFactory wxPayConfigFactory;
@Autowired
private UserCenterService userCenterService;
@Autowired
private IOrderService orderService;
@Autowired
private IQrCodeService qrCodeService;
@Autowired
private IProfitSharingService profitSharingService;
@Autowired
private WechatConfigService configService;
public PrepayWithRequestPaymentResponse createPayment(Long orderId) {
// 1. 获取用户和订单信息
SysUser user = userCenterService.findUserById(LoginUserContextHolder.getUser().getId());
Order order = orderService.getById(orderId);
if (order == null) {
throw new BusinessException("订单数据不存在");
}
// 2. 获取二维码信息
QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId());
if (qrCode == null) {
throw new BusinessException("二维码不存在");
}
// 3. 创建支付请求
PrepayRequest request = new PrepayRequest();
// 设置基本支付信息
Amount amount = new Amount();
amount.setTotal(Integer.parseInt(order.getOrderAmount()));
request.setAmount(amount);
// 从二维码配置中获取支付配置
WechatConfig payConfig = configService.getConfig("PAY", qrCode.getTenantId());
request.setAppid(payConfig.getAppId());
request.setMchid(payConfig.getMerchantId());
request.setDescription(order.getOrderNo());
request.setNotifyUrl(payConfig.getNotifyUrl());
request.setOutTradeNo(order.getOrderNo());
// 设置支付者信息
Payer payer = new Payer();
payer.setOpenid(user.getOpenId());
request.setPayer(payer);
// 设置分账信息
SettleInfo settleInfo = new SettleInfo();
if (isEnableProfit(qrCode)) {
settleInfo.setProfitSharing(true);
request.setSettleInfo(settleInfo);
// 创建分账记录
profitSharingService.createProfitSharing(order, qrCode);
} else {
settleInfo.setProfitSharing(false);
request.setSettleInfo(settleInfo);
}
// 4. 创建对应租户的支付服务
JsapiServiceExtension jsapiService = wxPayConfigFactory.createJsapiService(qrCode.getTenantId());
// 5. 调用支付接口
return jsapiService.prepayWithRequestPayment(request);
}
private boolean isEnableProfit(QrCode qrCode) {
return qrCode != null &&
Integer.valueOf(1).equals(qrCode.getEnableProfit()) &&
qrCode.getPayeeStatus() != null &&
qrCode.getPayeeStatus() == 1 &&
qrCode.getPayeeAccount() != null &&
qrCode.getProfitRate() != null;
}
}
@@ -3,10 +3,8 @@ package com.central.user.controller;
import com.central.common.context.TenantContextHolder;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.user.service.PrivateKeyService;
import com.central.user.service.WechatConfigService;
import com.central.user.service.WechatPayService;
import com.central.wechat.wechat.entity.WechatConfig;
import com.central.wechat.entity.WechatConfig;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
@@ -30,11 +28,6 @@ public class SysWechatConfigController {
@Resource
private WechatConfigService wechatConfigService;
@Resource
private WechatPayService wechatPayService;
@Resource
private PrivateKeyService privateKeyService;
@Operation(summary = "查询列表")
@Parameters({
@@ -84,21 +77,15 @@ public class SysWechatConfigController {
public Result<Boolean> uploadPrivateKey(@RequestParam("file") MultipartFile file) {
try {
String privateKey = new String(file.getBytes());
return Result.succeed(wechatPayService.uploadPrivateKey(
TenantContextHolder.getTenant(), privateKey));
WechatConfig wechatConfig = wechatConfigService.getConfig("PAY",TenantContextHolder.getTenant());
wechatConfig.setPrivateKey(privateKey);
return Result.succeed(wechatConfigService.updateConfig(wechatConfig));
} catch (Exception e) {
log.error("上传私钥文件失败", e);
return Result.failed("上传私钥文件失败");
}
}
@Operation(summary = "更新支付配置")
@PostMapping("/pay-config")
public Result<Boolean> updatePayConfig(@RequestBody Map<String, String> config) {
return Result.succeed(wechatPayService.updatePayConfig(
TenantContextHolder.getTenant(), config));
}
@Operation(summary = "切换微信配置")
@PostMapping("/switch/{type}")
public Result<Boolean> switchConfig(@PathVariable String type) {
@@ -111,9 +98,4 @@ public class SysWechatConfigController {
return Result.succeed(wechatConfigService.getConfig(type, TenantContextHolder.getTenant()));
}
@Operation(summary = "获取租户密钥")
@GetMapping("/tenant/private-key")
public String getPrivateKey(@RequestParam("tenantId")String tenantId,@RequestParam String type) {
return privateKeyService.getPrivateKey(tenantId, type);
}
}
@@ -2,7 +2,7 @@ package com.central.user.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.wechat.wechat.entity.WechatConfig;
import com.central.wechat.entity.WechatConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@@ -1,37 +0,0 @@
package com.central.user.service;
import java.io.InputStream;
/**
* 私钥文件管理服务
*/
public interface PrivateKeyService {
/**
* 上传私钥文件
*
* @param tenantId 租户ID
* @param type 配置类型
* @param inputStream 文件输入流
* @return 文件访问路径
*/
String uploadPrivateKey(String tenantId, String type, InputStream inputStream);
/**
* 获取私钥文件
*
* @param tenantId 租户ID
* @param type 配置类型
* @return 私钥文件内容
*/
String getPrivateKey(String tenantId, String type);
/**
* 删除私钥文件
*
* @param tenantId 租户ID
* @param type 配置类型
* @return 是否成功
*/
boolean deletePrivateKey(String tenantId, String type);
}
@@ -2,7 +2,7 @@ package com.central.user.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.central.common.model.PageResult;
import com.central.wechat.wechat.entity.WechatConfig;
import com.central.wechat.entity.WechatConfig;
import java.util.Map;
@@ -1,93 +0,0 @@
package com.central.user.service;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.Refund;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import java.util.Map;
/**
* 微信支付服务
*/
public interface WechatPayService {
/**
* 创建JSAPI支付订单
*
* @param tenantId 租户ID
* @param openId 用户openId
* @param amount 支付金额(分)
* @param description 商品描述
* @param outTradeNo 商户订单号
* @return 支付参数
*/
PrepayWithRequestPaymentResponse createJsapiOrder(String tenantId, String openId, int amount,
String description, String outTradeNo);
/**
* 查询订单
*
* @param tenantId 租户ID
* @param outTradeNo 商户订单号
* @return 订单信息
*/
Transaction queryOrder(String tenantId, String outTradeNo);
/**
* 关闭订单
*
* @param tenantId 租户ID
* @param outTradeNo 商户订单号
*/
void closeOrder(String tenantId, String outTradeNo);
/**
* 申请退款
*
* @param tenantId 租户ID
* @param outTradeNo 商户订单号
* @param outRefundNo 商户退款单号
* @param amount 退款金额(分)
* @param reason 退款原因
* @return 退款结果
*/
// RefundResponse refund(String tenantId, String outTradeNo, String outRefundNo,
// int amount, String reason);
/**
* 查询退款
*
* @param tenantId 租户ID
* @param outRefundNo 商户退款单号
* @return 退款信息
*/
Refund queryRefund(String tenantId, String outRefundNo);
/**
* 解析退款通知
*
* @param tenantId 租户ID
* @param notifyData 通知数据
* @return 退款通知信息
*/
RefundNotification parseRefundNotify(String tenantId, String notifyData);
/**
* 上传私钥文件
*
* @param tenantId 租户ID
* @param privateKey 私钥内容
* @return 是否成功
*/
boolean uploadPrivateKey(String tenantId, String privateKey);
/**
* 更新支付配置
*
* @param tenantId 租户ID
* @param config 配置信息
* @return 是否成功
*/
boolean updatePayConfig(String tenantId, Map<String, String> config);
}
@@ -1,61 +0,0 @@
package com.central.user.service.impl;
import com.central.user.service.PrivateKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
@Service
public class PrivateKeyServiceImpl implements PrivateKeyService {
private static final String CACHE_KEY_PATTERN = "wechat:private_key:%s:%s";
private static final long CACHE_EXPIRE_DAYS = 30;
@Autowired
private StringRedisTemplate redisTemplate;
@Override
public String uploadPrivateKey(String tenantId, String type, InputStream inputStream) {
try {
// 读取私钥内容
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
// 缓存私钥内容
String cacheKey = String.format(CACHE_KEY_PATTERN, tenantId, type);
redisTemplate.opsForValue().set(cacheKey, content.toString(), CACHE_EXPIRE_DAYS, TimeUnit.DAYS);
return cacheKey;
} catch (Exception e) {
throw new RuntimeException("上传私钥文件失败", e);
}
}
@Override
public String getPrivateKey(String tenantId, String type) {
String cacheKey = String.format(CACHE_KEY_PATTERN, tenantId, type);
String privateKey = redisTemplate.opsForValue().get(cacheKey);
if (!StringUtils.hasText(privateKey)) {
throw new RuntimeException("私钥文件不存在");
}
return privateKey;
}
@Override
public boolean deletePrivateKey(String tenantId, String type) {
String cacheKey = String.format(CACHE_KEY_PATTERN, tenantId, type);
return redisTemplate.delete(cacheKey);
}
}
@@ -6,7 +6,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.common.model.PageResult;
import com.central.user.mapper.WechatConfigMapper;
import com.central.user.service.WechatConfigService;
import com.central.wechat.wechat.entity.WechatConfig;
import com.central.wechat.entity.WechatConfig;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.MapUtils;
import org.springframework.cache.annotation.CacheEvict;
@@ -1,154 +0,0 @@
package com.central.user.service.impl;//package com.zlt.wechat.service.impl;
import com.central.user.service.WechatPayService;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.Refund;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class WechatPayServiceImpl implements WechatPayService {
@Override
public PrepayWithRequestPaymentResponse createJsapiOrder(String tenantId, String openId, int amount, String description, String outTradeNo) {
return null;
}
@Override
public Transaction queryOrder(String tenantId, String outTradeNo) {
return null;
}
@Override
public void closeOrder(String tenantId, String outTradeNo) {
}
@Override
public Refund queryRefund(String tenantId, String outRefundNo) {
return null;
}
@Override
public RefundNotification parseRefundNotify(String tenantId, String notifyData) {
return null;
}
@Override
public boolean uploadPrivateKey(String tenantId, String privateKey) {
return false;
}
@Override
public boolean updatePayConfig(String tenantId, Map<String, String> config) {
return false;
}
/*@Autowired
private WechatConfigService configService;
@Autowired
private PrivateKeyService privateKeyService;
@Autowired
private JsapiServiceExtension jsapiService;
@Autowired
private RefundService refundService;
@Autowired
private Config wxPayConfig;
@Override
public PrepayWithRequestPaymentResponse createJsapiOrder(String tenantId, String openId,
int amount, String description, String outTradeNo) {
// 切换租户配置
configService.switchConfig("PAY", tenantId);
// 创建支付订单
*//*return jsapiService.prepayWithRequestPayment(
openId,
description,
outTradeNo,
amount,
"CNY"
);*//*
return new PrepayWithRequestPaymentResponse();
}
@Override
public Transaction queryOrder(String tenantId, String outTradeNo) {
configService.switchConfig("PAY", tenantId);
return jsapiService.queryOrderByOutTradeNo(outTradeNo);
}
@Override
public void closeOrder(String tenantId, String outTradeNo) {
configService.switchConfig("PAY", tenantId);
jsapiService.closeOrder(outTradeNo);
}
@Override
public RefundResponse refund(String tenantId, String outTradeNo, String outRefundNo,
int amount, String reason) {
configService.switchConfig("PAY", tenantId);
RefundRequest request = new RefundRequest();
request.setOutTradeNo(outTradeNo);
request.setOutRefundNo(outRefundNo);
request.setAmount(new RefundRequest.Amount(amount, "CNY"));
request.setReason(reason);
return refundService.refund(request);
}
@Override
public Refund queryRefund(String tenantId, String outRefundNo) {
configService.switchConfig("PAY", tenantId);
return refundService.queryByOutRefundNo(outRefundNo);
}
@Override
public RefundNotification parseRefundNotify(String tenantId, String notifyData) {
configService.switchConfig("PAY", tenantId);
return refundService.parseRefundNotification(notifyData);
}
@Override
public boolean uploadPrivateKey(String tenantId, String privateKey) {
try {
privateKeyService.uploadPrivateKey(tenantId, "PAY",
new ByteArrayInputStream(privateKey.getBytes()));
return true;
} catch (Exception e) {
return false;
}
}
@Override
public boolean updatePayConfig(String tenantId, Map<String, String> config) {
try {
WechatConfig wechatConfig = configService.getConfig("PAY", tenantId);
if (wechatConfig == null) {
wechatConfig = new WechatConfig();
wechatConfig.setType("PAY");
wechatConfig.setTenantId(tenantId);
}
// 更新配置信息
wechatConfig.setMerchantId(config.get("merchantId"));
wechatConfig.setAppId(config.get("appId"));
wechatConfig.setMchKey(config.get("mchKey"));
wechatConfig.setMchSerialNo(config.get("mchSerialNo"));
wechatConfig.setApiV3Key(config.get("apiV3Key"));
wechatConfig.setNotifyUrl(config.get("notifyUrl"));
wechatConfig.setStatus(1);
return configService.saveOrUpdate(wechatConfig);
} catch (Exception e) {
return false;
}
}*/
}