feat:功能支付类模块完善
This commit is contained in:
+5
-10
@@ -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;
|
||||
|
||||
+98
-98
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-22
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
-37
@@ -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);
|
||||
}
|
||||
+1
-1
@@ -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);
|
||||
}
|
||||
-61
@@ -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);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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;
|
||||
|
||||
-154
@@ -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;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.central.wechat.wechat.config;
|
||||
package com.central.wechat.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
+6
-1
@@ -1,4 +1,4 @@
|
||||
package com.central.wechat.wechat.entity;
|
||||
package com.central.wechat.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
@@ -54,6 +54,11 @@ public class WechatConfig {
|
||||
*/
|
||||
private String privateKeyPath;
|
||||
|
||||
/**
|
||||
* 商户私钥
|
||||
*/
|
||||
private String privateKey;
|
||||
|
||||
/**
|
||||
* APIv3密钥
|
||||
*/
|
||||
+13
-3
@@ -1,11 +1,12 @@
|
||||
package com.central.wechat.wechat.feign;
|
||||
package com.central.wechat.feign;
|
||||
|
||||
import com.central.common.constant.ServiceNameConstants;
|
||||
import com.central.wechat.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.wechat.feign.fallback.WechatConfigServiceFallbackFactory;
|
||||
import com.central.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.feign.fallback.WechatConfigServiceFallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
@@ -25,5 +26,14 @@ public interface WechatConfigService {
|
||||
@GetMapping(value = "/wechat/config/app/{id}")
|
||||
WechatConfig getConfigByAppId(@PathVariable("id") String appId);
|
||||
|
||||
/**
|
||||
* feign rpc访问远程/wechat/config/{type}接口
|
||||
* 查询微信配置对象WechatConfig
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/wechat/config/{type}")
|
||||
String getPrivateKey(@PathVariable("type") String type, @RequestParam("tenantId") String tenantId);
|
||||
|
||||
}
|
||||
+8
-3
@@ -1,7 +1,7 @@
|
||||
package com.central.wechat.wechat.feign.fallback;
|
||||
package com.central.wechat.feign.fallback;
|
||||
|
||||
import com.central.wechat.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.wechat.feign.WechatConfigService;
|
||||
import com.central.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.feign.WechatConfigService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
|
||||
@@ -27,6 +27,11 @@ public class WechatConfigServiceFallbackFactory implements FallbackFactory<Wecha
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrivateKey(String type, String tenantId) {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
package com.central.wechat.wechat.config;
|
||||
|
||||
import com.central.wechat.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.wechat.feign.PrivateKeyService;
|
||||
import com.central.wechat.wechat.feign.WechatConfigService;
|
||||
import com.wechat.pay.java.core.Config;
|
||||
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
||||
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WxPayConfigFactory {
|
||||
private final WechatConfigService configService;
|
||||
private final PrivateKeyService privateKeyService;
|
||||
|
||||
public WxPayConfigFactory(WechatConfigService configService, PrivateKeyService privateKeyService) {
|
||||
this.configService = configService;
|
||||
this.privateKeyService = privateKeyService;
|
||||
}
|
||||
|
||||
public Config createConfig(String tenantId) {
|
||||
log.info("创建微信支付配置,tenantId:{}", tenantId);
|
||||
WechatConfig config = configService.getConfig("PAY", tenantId);
|
||||
String privateKey = privateKeyService.getPrivateKey(tenantId, "PAY");
|
||||
return new RSAAutoCertificateConfig.Builder()
|
||||
.merchantId(config.getMerchantId())
|
||||
.privateKey(privateKey)
|
||||
.merchantSerialNumber(config.getMchSerialNo())
|
||||
.apiV3Key(config.getApiV3Key())
|
||||
.build();
|
||||
}
|
||||
|
||||
public JsapiServiceExtension createJsapiService(String tenantId) {
|
||||
Config config = createConfig(tenantId);
|
||||
return new JsapiServiceExtension.Builder().config(config).build();
|
||||
}
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.central.wechat.wechat.feign;
|
||||
|
||||
import com.central.common.constant.ServiceNameConstants;
|
||||
import com.central.wechat.wechat.feign.fallback.PrivateKeyServiceFallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@FeignClient(name = ServiceNameConstants.USER_SERVICE, fallbackFactory = PrivateKeyServiceFallbackFactory.class, dismiss404 = true)
|
||||
public interface PrivateKeyService {
|
||||
/**
|
||||
* feign rpc访问远程/wechat/config/{type}接口
|
||||
* 查询微信配置对象WechatConfig
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/wechat/config/{type}")
|
||||
String getPrivateKey(@PathVariable("type") String type, @RequestParam("tenantId") String tenantId);
|
||||
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.central.wechat.wechat.feign.fallback;
|
||||
|
||||
import com.central.wechat.wechat.feign.PrivateKeyService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
|
||||
/**
|
||||
* userService降级工场
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/1/18
|
||||
*/
|
||||
@Slf4j
|
||||
public class PrivateKeyServiceFallbackFactory implements FallbackFactory<PrivateKeyService> {
|
||||
@Override
|
||||
public PrivateKeyService create(Throwable throwable) {
|
||||
return new PrivateKeyService() {
|
||||
@Override
|
||||
public String getPrivateKey(String tenantId,String type) {
|
||||
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -1,4 +1,3 @@
|
||||
com.central.wechat.wechat.feign.fallback.PrivateKeyServiceFallbackFactory
|
||||
com.central.wechat.wechat.feign.fallback.WechatConfigServiceFallbackFactory
|
||||
com.central.wechat.feign.fallback.WechatConfigServiceFallbackFactory
|
||||
|
||||
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.central.wechat.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* 私钥串处理工具类
|
||||
* @author lsl
|
||||
* @date 2025-06-03
|
||||
*/
|
||||
@Slf4j
|
||||
public class PrivateKeyUtils {
|
||||
|
||||
public static PrivateKey getPrivateKey(String privateKeyStr) {
|
||||
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 (Exception e) {
|
||||
log.error("解析私钥失败", e);
|
||||
throw new RuntimeException("解析私钥失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package com.central.wechat.config;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
||||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||
import com.central.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.feign.WechatConfigService;
|
||||
import com.wechat.pay.java.core.Config;
|
||||
import com.wechat.pay.java.core.notification.NotificationParser;
|
||||
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.refund.RefundService;
|
||||
import com.wechat.pay.java.service.profitsharing.ProfitsharingService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
||||
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(WechatProperties.class)
|
||||
@AutoConfigureAfter({FeignAutoConfiguration.class, WebMvcAutoConfiguration.class})
|
||||
public class WechatAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(WechatConfigService.class)
|
||||
public WxMpService wxMpService(WechatConfigService configService) {
|
||||
log.info("WxMpService(公众号相关)被加载,tenantId:{}", getCurrentTenantId());
|
||||
return new WxMpServiceImpl() {
|
||||
@Override
|
||||
public WxMpDefaultConfigImpl getWxMpConfigStorage() {
|
||||
WechatConfig config = configService.getConfig("MP", getCurrentTenantId());
|
||||
WxMpDefaultConfigImpl wxConfig = new WxMpDefaultConfigImpl();
|
||||
wxConfig.setAppId(config.getAppId());
|
||||
wxConfig.setSecret(config.getAppSecret());
|
||||
wxConfig.setToken(config.getToken());
|
||||
wxConfig.setAesKey(config.getAesKey());
|
||||
return wxConfig;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(WechatConfigService.class)
|
||||
public WxMaService wxMaService(WechatConfigService configService) {
|
||||
log.info("WxMaService(小程序通知)被加载,tenantId:{}", getCurrentTenantId());
|
||||
return new WxMaServiceImpl() {
|
||||
@Override
|
||||
public WxMaDefaultConfigImpl getWxMaConfig() {
|
||||
WechatConfig config = configService.getConfig("MINI", getCurrentTenantId());
|
||||
WxMaDefaultConfigImpl wxConfig = new WxMaDefaultConfigImpl();
|
||||
wxConfig.setAppid(config.getAppId());
|
||||
wxConfig.setSecret(config.getAppSecret());
|
||||
return wxConfig;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean({WechatConfigService.class})
|
||||
public WxPayConfigFactory wxPayConfigFactory(WechatConfigService configService) {
|
||||
log.info("WxPayConfigFactory被加载,tenantId:{}", getCurrentTenantId());
|
||||
return new WxPayConfigFactory(configService);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(WxPayConfigFactory.class)
|
||||
public Config wxPayConfig(WxPayConfigFactory factory) {
|
||||
return factory.createConfig(getCurrentTenantId());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(WxPayConfigFactory.class)
|
||||
public JsapiServiceExtension jsapiService(WxPayConfigFactory factory) {
|
||||
return factory.createJsapiService(getCurrentTenantId());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(WxPayConfigFactory.class)
|
||||
public NotificationParser notificationParser(WxPayConfigFactory factory) {
|
||||
return factory.createNotificationParser(getCurrentTenantId());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(WxPayConfigFactory.class)
|
||||
public RefundService refundService(WxPayConfigFactory factory) {
|
||||
return factory.createRefundService(getCurrentTenantId());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(Config.class)
|
||||
public ProfitsharingService profitsharingService(Config config) {
|
||||
return new ProfitsharingService.Builder().config(config).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(WechatConfigService.class)
|
||||
public SettleInfo settleInfo(WechatConfigService configService) {
|
||||
WechatConfig config = configService.getConfig("PAY", getCurrentTenantId());
|
||||
SettleInfo settleInfo = new SettleInfo();
|
||||
settleInfo.setProfitSharing(config.getProfitSharingEnabled());
|
||||
return settleInfo;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(WechatConfigService.class)
|
||||
public Executor profitSharingExecutor(WechatConfigService configService) {
|
||||
log.info("profitSharingExecutor被加载,tenantId:{}", getCurrentTenantId());
|
||||
WechatConfig config = configService.getConfig("PAY", getCurrentTenantId());
|
||||
if (!config.getAsyncEnabled()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
executor.setCorePoolSize(config.getAsyncPoolSize());
|
||||
executor.setMaxPoolSize(config.getAsyncPoolSize());
|
||||
executor.setQueueCapacity(100);
|
||||
executor.setThreadNamePrefix("profit-sharing-");
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
executor.initialize();
|
||||
return executor;
|
||||
}
|
||||
|
||||
private String getCurrentTenantId() {
|
||||
return "webApp";
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package com.central.wechat.config;
|
||||
|
||||
import com.central.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.feign.WechatConfigService;
|
||||
import com.wechat.pay.java.core.Config;
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.security.PrivateKey;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WxPayConfigFactory {
|
||||
private final WechatConfigService configService;
|
||||
|
||||
// 缓存各个服务的实例
|
||||
private final Map<String, Config> configCache = new ConcurrentHashMap<>();
|
||||
private final Map<String, JsapiServiceExtension> jsapiServiceCache = new ConcurrentHashMap<>();
|
||||
private final Map<String, NotificationParser> notificationParserCache = new ConcurrentHashMap<>();
|
||||
private final Map<String, RefundService> refundServiceCache = new ConcurrentHashMap<>();
|
||||
|
||||
public WxPayConfigFactory(WechatConfigService configService) {
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
public Config createConfig(String tenantId) {
|
||||
return configCache.computeIfAbsent(tenantId, this::buildConfig);
|
||||
}
|
||||
|
||||
private Config buildConfig(String tenantId) {
|
||||
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)
|
||||
.merchantSerialNumber(config.getMchSerialNo())
|
||||
.apiV3Key(config.getApiV3Key())
|
||||
.build();
|
||||
}
|
||||
|
||||
public JsapiServiceExtension createJsapiService(String tenantId) {
|
||||
return jsapiServiceCache.computeIfAbsent(tenantId, this::buildJsapiService);
|
||||
}
|
||||
|
||||
private JsapiServiceExtension buildJsapiService(String tenantId) {
|
||||
Config config = createConfig(tenantId);
|
||||
return new JsapiServiceExtension.Builder().config(config).build();
|
||||
}
|
||||
|
||||
public NotificationParser createNotificationParser(String tenantId) {
|
||||
return notificationParserCache.computeIfAbsent(tenantId, this::buildNotificationParser);
|
||||
}
|
||||
|
||||
private NotificationParser buildNotificationParser(String tenantId) {
|
||||
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);
|
||||
}
|
||||
|
||||
public RefundService createRefundService(String tenantId) {
|
||||
return refundServiceCache.computeIfAbsent(tenantId, this::buildRefundService);
|
||||
}
|
||||
|
||||
private RefundService buildRefundService(String tenantId) {
|
||||
log.info("创建微信退款服务,tenantId:{}", tenantId);
|
||||
Config config = createConfig(tenantId);
|
||||
return new RefundService.Builder().config(config).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除指定租户的缓存
|
||||
*/
|
||||
public void clearCache(String tenantId) {
|
||||
configCache.remove(tenantId);
|
||||
jsapiServiceCache.remove(tenantId);
|
||||
notificationParserCache.remove(tenantId);
|
||||
refundServiceCache.remove(tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有缓存
|
||||
*/
|
||||
public void clearAllCache() {
|
||||
configCache.clear();
|
||||
jsapiServiceCache.clear();
|
||||
notificationParserCache.clear();
|
||||
refundServiceCache.clear();
|
||||
}
|
||||
}
|
||||
-166
@@ -1,166 +0,0 @@
|
||||
//package com.zlt.wechat.config;
|
||||
//
|
||||
//import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
//import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
||||
//import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||
//import com.central.common.context.TenantContextHolder;
|
||||
//import com.central.wechat.wechat.config.WechatProperties;
|
||||
//import com.central.wechat.wechat.entity.WechatConfig;
|
||||
//import com.central.wechat.wechat.feign.PrivateKeyService;
|
||||
//import com.central.wechat.wechat.feign.WechatConfigService;
|
||||
//import com.wechat.pay.java.core.Config;
|
||||
//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.payments.jsapi.model.SettleInfo;
|
||||
//import com.wechat.pay.java.service.profitsharing.ProfitsharingService;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import me.chanjar.weixin.mp.api.WxMpService;
|
||||
//import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
||||
//import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
||||
//import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
//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;
|
||||
//import java.util.concurrent.ThreadPoolExecutor;
|
||||
//
|
||||
//@Slf4j
|
||||
//@Configuration
|
||||
//@EnableConfigurationProperties(WechatProperties.class)
|
||||
//public class WechatAutoConfiguration {
|
||||
//
|
||||
// @Bean
|
||||
// @ConditionalOnMissingBean
|
||||
// @ConditionalOnBean(WechatConfigService.class)
|
||||
// public WxMpService wxMpService(WechatConfigService configService) {
|
||||
// log.info("WxMpService(公众号相关)被加载,tenantId:{}", getCurrentTenantId());
|
||||
// return new WxMpServiceImpl() {
|
||||
// @Override
|
||||
// public WxMpDefaultConfigImpl getWxMpConfigStorage() {
|
||||
// WechatConfig config = configService.getConfig("MP", getCurrentTenantId());
|
||||
// WxMpDefaultConfigImpl wxConfig = new WxMpDefaultConfigImpl();
|
||||
// wxConfig.setAppId(config.getAppId());
|
||||
// wxConfig.setSecret(config.getAppSecret());
|
||||
// wxConfig.setToken(config.getToken());
|
||||
// wxConfig.setAesKey(config.getAesKey());
|
||||
// return wxConfig;
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 这个配置类在启动时可能会遇到问题,因为:
|
||||
// * WechatAutoConfiguration是一个自动配置类
|
||||
// * 它依赖于WechatConfigService这个Feign客户端
|
||||
// * 如果Feign客户端还没有被创建,就会导致注入失败
|
||||
// * 因此使用@ConditionalOnBean注解确保配置类只在Feign客户端可用时才创建相关Bean
|
||||
// */
|
||||
// @Bean
|
||||
// @ConditionalOnMissingBean
|
||||
// @ConditionalOnBean(WechatConfigService.class)
|
||||
// public WxMaService wxMaService(WechatConfigService configService) {
|
||||
// log.info("WxMaService(小程序通知)被加载,tenantId:{}", getCurrentTenantId());
|
||||
// // 小程序通知Service配置,type=MINI
|
||||
// return new WxMaServiceImpl() {
|
||||
// @Override
|
||||
// public WxMaDefaultConfigImpl getWxMaConfig() {
|
||||
// WechatConfig config = configService.getConfig("MINI", getCurrentTenantId());
|
||||
// WxMaDefaultConfigImpl wxConfig = new WxMaDefaultConfigImpl();
|
||||
// wxConfig.setAppid(config.getAppId());
|
||||
// wxConfig.setSecret(config.getAppSecret());
|
||||
// return wxConfig;
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @ConditionalOnBean({WechatConfigService.class, PrivateKeyService.class})
|
||||
// public WxPayConfigFactory wxPayConfigFactory(
|
||||
// WechatConfigService configService,
|
||||
// PrivateKeyService privateKeyService) {
|
||||
// log.info("WxPayConfigFactory被加载,tenantId:{}", getCurrentTenantId());
|
||||
// return new WxPayConfigFactory(configService, privateKeyService);
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @ConditionalOnBean(WxPayConfigFactory.class)
|
||||
// public Config wxPayConfig(WxPayConfigFactory factory) {
|
||||
// return factory.createConfig(getCurrentTenantId());
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @ConditionalOnBean(WxPayConfigFactory.class)
|
||||
// public JsapiServiceExtension jsapiService(WxPayConfigFactory factory) {
|
||||
// return factory.createJsapiService(getCurrentTenantId());
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @ConditionalOnMissingBean
|
||||
// @ConditionalOnBean(WechatConfigService.class)
|
||||
// public NotificationConfig notificationConfig(WechatConfigService configService, PrivateKeyService privateKeyService) {
|
||||
// log.info("NotificationConfig被加载,tenantId:{}", getCurrentTenantId());
|
||||
// WechatConfig config = configService.getConfig("PAY", getCurrentTenantId());
|
||||
// String privateKey = privateKeyService.getPrivateKey(getCurrentTenantId(), "PAY");
|
||||
// return new RSAAutoCertificateConfig.Builder()
|
||||
// .merchantId(config.getMerchantId())
|
||||
// .privateKey(privateKey)
|
||||
// .merchantSerialNumber(config.getMchSerialNo())
|
||||
// .apiV3Key(config.getApiV3Key())
|
||||
// .build();
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @ConditionalOnMissingBean
|
||||
// public NotificationParser notificationParser(NotificationConfig config) {
|
||||
// return new NotificationParser(config);
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @ConditionalOnMissingBean
|
||||
// public ProfitsharingService profitsharingService(Config config) {
|
||||
// return new ProfitsharingService.Builder().config(config).build();
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @ConditionalOnMissingBean
|
||||
// @ConditionalOnBean(WechatConfigService.class)
|
||||
// public SettleInfo settleInfo(WechatConfigService configService) {
|
||||
// WechatConfig config = configService.getConfig("PAY", getCurrentTenantId());
|
||||
// SettleInfo settleInfo = new SettleInfo();
|
||||
// settleInfo.setProfitSharing(config.getProfitSharingEnabled());
|
||||
// return settleInfo;
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @ConditionalOnMissingBean
|
||||
// @ConditionalOnBean(WechatConfigService.class)
|
||||
// public Executor profitSharingExecutor(WechatConfigService configService) {
|
||||
// log.info("profitSharingExecutor被加载,tenantId:{}", getCurrentTenantId());
|
||||
// WechatConfig config = configService.getConfig("PAY", getCurrentTenantId());
|
||||
// if (!config.getAsyncEnabled()) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
// executor.setCorePoolSize(config.getAsyncPoolSize());
|
||||
// executor.setMaxPoolSize(config.getAsyncPoolSize());
|
||||
// executor.setQueueCapacity(100);
|
||||
// executor.setThreadNamePrefix("profit-sharing-");
|
||||
// executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
// executor.initialize();
|
||||
// return executor;
|
||||
// }
|
||||
// /**
|
||||
// * 获取当前商户ID
|
||||
// * 这里需要根据实际业务场景实现,可以从ThreadLocal、请求上下文等获取
|
||||
// */
|
||||
// private String getCurrentTenantId() {
|
||||
// // 获取当前租户的逻辑
|
||||
// return TenantContextHolder.getTenant();
|
||||
// }
|
||||
//}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
//package com.zlt.wechat.config;
|
||||
//
|
||||
//import com.central.wechat.wechat.entity.WechatConfig;
|
||||
//import com.central.wechat.wechat.feign.PrivateKeyService;
|
||||
//import com.central.wechat.wechat.feign.WechatConfigService;
|
||||
//import com.wechat.pay.java.core.Config;
|
||||
//import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
||||
//import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.springframework.context.annotation.Bean;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//@Slf4j
|
||||
//@Component
|
||||
//public class WxPayConfigFactory {
|
||||
// private final WechatConfigService configService;
|
||||
// private final PrivateKeyService privateKeyService;
|
||||
//
|
||||
// public WxPayConfigFactory(WechatConfigService configService, PrivateKeyService privateKeyService) {
|
||||
// this.configService = configService;
|
||||
// this.privateKeyService = privateKeyService;
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public Config createConfig(String tenantId) {
|
||||
// log.info("创建微信支付配置,tenantId:{}", tenantId);
|
||||
// WechatConfig config = configService.getConfig("PAY", tenantId);
|
||||
// String privateKey = privateKeyService.getPrivateKey(tenantId, "PAY");
|
||||
// return new RSAAutoCertificateConfig.Builder()
|
||||
// .merchantId(config.getMerchantId())
|
||||
// .privateKey(privateKey)
|
||||
// .merchantSerialNumber(config.getMchSerialNo())
|
||||
// .apiV3Key(config.getApiV3Key())
|
||||
// .build();
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public JsapiServiceExtension createJsapiService(String tenantId) {
|
||||
// Config config = createConfig(tenantId);
|
||||
// return new JsapiServiceExtension.Builder().config(config).build();
|
||||
// }
|
||||
//}
|
||||
-2
@@ -1,2 +0,0 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.zlt.wechat.config.WechatAutoConfiguration
|
||||
+1
@@ -0,0 +1 @@
|
||||
com.central.wechat.config.WechatAutoConfiguration
|
||||
+1
-4
@@ -97,10 +97,7 @@
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-wechat-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.central.oauth.service;
|
||||
|
||||
import com.central.wechat.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.entity.WechatConfig;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
|
||||
@@ -5,8 +5,8 @@ import com.central.common.feign.UserService;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.common.utils.LoginUserUtils;
|
||||
import com.central.oauth.service.ZltUserDetailsService;
|
||||
import com.central.wechat.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.wechat.feign.WechatConfigService;
|
||||
import com.central.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.feign.WechatConfigService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.authentication.DisabledException;
|
||||
import org.springframework.security.authentication.InternalAuthenticationServiceException;
|
||||
|
||||
+7
-1
@@ -18,6 +18,8 @@ import java.util.List;
|
||||
*/
|
||||
public class TencentAuthenticationConverter extends BaseAuthenticationConverter {
|
||||
|
||||
private String appCode = "wx30a3c8de94a85584";
|
||||
|
||||
@Override
|
||||
protected String supportGrantType() {
|
||||
return TencentAuthenticationToken.GRANT_TYPE.getValue();
|
||||
@@ -33,7 +35,11 @@ public class TencentAuthenticationConverter extends BaseAuthenticationConverter
|
||||
String code = OAuthEndpointUtils.getParam(parameters, OAuth2ParameterNames.CODE);
|
||||
String encryptedData = OAuthEndpointUtils.getParam(parameters,"encryptedData");
|
||||
String iv = OAuthEndpointUtils.getParam(parameters,"iv");
|
||||
String appCode = OAuthEndpointUtils.getParam(parameters,"appCode");
|
||||
//如果参数缺省,默认使用平台提供的APPID
|
||||
if (parameters.containsKey("appCode")) {
|
||||
appCode = OAuthEndpointUtils.getParam(parameters,"appCode");
|
||||
}
|
||||
|
||||
return new TencentAuthenticationToken(code, encryptedData, iv, appCode);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import com.central.oauth.service.impl.UserDetailServiceFactory;
|
||||
import com.central.oauth.support.base.BaseAuthenticationProvider;
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import com.central.oauth2.common.token.TencentAuthenticationToken;
|
||||
import com.central.wechat.wechat.entity.WechatConfig;
|
||||
import com.central.wechat.entity.WechatConfig;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@@ -48,5 +48,5 @@ zlt:
|
||||
- com.central.oauth.mapper.ClientMapper.findList
|
||||
uaa:
|
||||
wx:
|
||||
appId:
|
||||
appsecret:
|
||||
# 平台默认的APPID
|
||||
appId: wx0c3cc6f1d297644d
|
||||
|
||||
Reference in New Issue
Block a user