feat: 添加服务商支付模式
1. 添加服务商模式标识字段
2. 添加服务商相关字段
3. 添加子商户相关字段
4. 创建索引以提高查询性能
5. 添加注释说明
This commit is contained in:
+39
-2
@@ -25,12 +25,12 @@ public class WechatConfig {
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 商户ID
|
||||
* 商户ID(普通商户模式使用)
|
||||
*/
|
||||
private String merchantId;
|
||||
|
||||
/**
|
||||
* 应用ID
|
||||
* 应用ID(普通商户模式使用)
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
@@ -118,4 +118,41 @@ public class WechatConfig {
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
// ========== 服务商模式新增字段 ==========
|
||||
|
||||
/**
|
||||
* 是否服务商模式:0-普通商户,1-服务商
|
||||
*/
|
||||
private Boolean isServiceMode;
|
||||
|
||||
/**
|
||||
* 服务商应用ID
|
||||
*/
|
||||
private String serviceAppId;
|
||||
|
||||
/**
|
||||
* 服务商商户号
|
||||
*/
|
||||
private String serviceMchId;
|
||||
|
||||
/**
|
||||
* 子商户号(服务商模式下的子商户)
|
||||
*/
|
||||
private String subMchId;
|
||||
|
||||
/**
|
||||
* 子商户应用ID(服务商模式下的子商户应用)
|
||||
*/
|
||||
private String subAppId;
|
||||
|
||||
/**
|
||||
* 子商户名称
|
||||
*/
|
||||
private String subMchName;
|
||||
|
||||
/**
|
||||
* 子商户描述
|
||||
*/
|
||||
private String subMchDescription;
|
||||
}
|
||||
|
||||
+30
@@ -8,6 +8,8 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@@ -29,4 +31,32 @@ public interface WechatConfigService {
|
||||
@GetMapping(value = "/wechat/config/serial/{serialNumber}")
|
||||
WechatConfig getConfigBySerialNumber(@PathVariable("serialNumber") String serialNumber);
|
||||
|
||||
// ========== 服务商模式新增方法 ==========
|
||||
|
||||
/**
|
||||
* 获取服务商配置
|
||||
*
|
||||
* @param tenantId 租户ID
|
||||
* @return 服务商配置
|
||||
*/
|
||||
@GetMapping(value = "/wechat/config/service/{tenantId}")
|
||||
WechatConfig getServiceConfig(@PathVariable("tenantId") String tenantId);
|
||||
|
||||
/**
|
||||
* 根据子商户号获取配置
|
||||
*
|
||||
* @param subMchId 子商户号
|
||||
* @return 微信配置
|
||||
*/
|
||||
@GetMapping(value = "/wechat/config/sub/{subMchId}")
|
||||
WechatConfig getConfigBySubMchId(@PathVariable("subMchId") String subMchId);
|
||||
|
||||
/**
|
||||
* 获取指定服务商下的所有子商户配置
|
||||
*
|
||||
* @param serviceMchId 服务商商户号
|
||||
* @return 子商户配置列表
|
||||
*/
|
||||
@GetMapping(value = "/wechat/config/submerchants/{serviceMchId}")
|
||||
List<WechatConfig> getSubMerchantConfigs(@PathVariable("serviceMchId") String serviceMchId);
|
||||
}
|
||||
|
||||
+105
-12
@@ -34,7 +34,6 @@ public class WxPayConfigFactory {
|
||||
private final Map<String, ProfitsharingService> profitsharingServiceCache = new ConcurrentHashMap<>();
|
||||
private final Map<String, Executor> executorCache = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
public Config createConfig(String tenantId) {
|
||||
return configCache.computeIfAbsent(tenantId, this::buildConfig);
|
||||
}
|
||||
@@ -44,8 +43,44 @@ public class WxPayConfigFactory {
|
||||
WechatConfig config = configService.getConfig("PAY", tenantId);
|
||||
PrivateKey privateKey = PrivateKeyUtils.getPrivateKey(config.getPrivateKey());
|
||||
|
||||
RSAAutoCertificateConfig.Builder builder = new RSAAutoCertificateConfig.Builder()
|
||||
.privateKey(privateKey)
|
||||
.merchantSerialNumber(config.getMchSerialNo())
|
||||
.apiV3Key(config.getApiV3Key());
|
||||
|
||||
// 根据是否为服务商模式设置不同的商户ID
|
||||
if (Boolean.TRUE.equals(config.getIsServiceMode())) {
|
||||
// 服务商模式:使用服务商商户号
|
||||
builder.merchantId(config.getServiceMchId());
|
||||
log.info("使用服务商模式,服务商商户号:{}", config.getServiceMchId());
|
||||
} else {
|
||||
// 普通商户模式:使用商户号
|
||||
builder.merchantId(config.getMerchantId());
|
||||
log.info("使用普通商户模式,商户号:{}", config.getMerchantId());
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据子商户号创建配置(服务商模式专用)
|
||||
*/
|
||||
public Config createConfigBySubMchId(String subMchId) {
|
||||
String cacheKey = "sub_" + subMchId;
|
||||
return configCache.computeIfAbsent(cacheKey, key -> buildConfigBySubMchId(subMchId));
|
||||
}
|
||||
|
||||
private Config buildConfigBySubMchId(String subMchId) {
|
||||
log.info("根据子商户号创建微信支付配置,subMchId:{}", subMchId);
|
||||
WechatConfig config = configService.getConfigBySubMchId(subMchId);
|
||||
if (config == null) {
|
||||
throw new RuntimeException("未找到子商户配置: " + subMchId);
|
||||
}
|
||||
|
||||
PrivateKey privateKey = PrivateKeyUtils.getPrivateKey(config.getPrivateKey());
|
||||
|
||||
return new RSAAutoCertificateConfig.Builder()
|
||||
.merchantId(config.getMerchantId())
|
||||
.merchantId(config.getServiceMchId()) // 服务商模式使用服务商商户号
|
||||
.privateKey(privateKey)
|
||||
.merchantSerialNumber(config.getMchSerialNo())
|
||||
.apiV3Key(config.getApiV3Key())
|
||||
@@ -61,6 +96,19 @@ public class WxPayConfigFactory {
|
||||
return new JsapiServiceExtension.Builder().config(config).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据子商户号创建JSAPI服务(服务商模式专用)
|
||||
*/
|
||||
public JsapiServiceExtension createJsapiServiceBySubMchId(String subMchId) {
|
||||
String cacheKey = "sub_" + subMchId;
|
||||
return jsapiServiceCache.computeIfAbsent(cacheKey, key -> buildJsapiServiceBySubMchId(subMchId));
|
||||
}
|
||||
|
||||
private JsapiServiceExtension buildJsapiServiceBySubMchId(String subMchId) {
|
||||
Config config = createConfigBySubMchId(subMchId);
|
||||
return new JsapiServiceExtension.Builder().config(config).build();
|
||||
}
|
||||
|
||||
public NotificationParser createNotificationParser(String tenantId) {
|
||||
return notificationParserCache.computeIfAbsent(tenantId, this::buildNotificationParser);
|
||||
}
|
||||
@@ -70,13 +118,46 @@ public class WxPayConfigFactory {
|
||||
WechatConfig config = configService.getConfig("PAY", tenantId);
|
||||
PrivateKey privateKey = PrivateKeyUtils.getPrivateKey(config.getPrivateKey());
|
||||
|
||||
RSAAutoCertificateConfig.Builder builder = new RSAAutoCertificateConfig.Builder()
|
||||
.privateKey(privateKey)
|
||||
.merchantSerialNumber(config.getMchSerialNo())
|
||||
.apiV3Key(config.getApiV3Key());
|
||||
|
||||
// 根据是否为服务商模式设置不同的商户ID
|
||||
if (Boolean.TRUE.equals(config.getIsServiceMode())) {
|
||||
builder.merchantId(config.getServiceMchId());
|
||||
} else {
|
||||
builder.merchantId(config.getMerchantId());
|
||||
}
|
||||
|
||||
NotificationConfig notificationConfig = builder.build();
|
||||
return new NotificationParser(notificationConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据子商户号创建通知解析器(服务商模式专用)
|
||||
*/
|
||||
public NotificationParser createNotificationParserBySubMchId(String subMchId) {
|
||||
String cacheKey = "sub_" + subMchId;
|
||||
return notificationParserCache.computeIfAbsent(cacheKey, key -> buildNotificationParserBySubMchId(subMchId));
|
||||
}
|
||||
|
||||
private NotificationParser buildNotificationParserBySubMchId(String subMchId) {
|
||||
log.info("根据子商户号创建微信支付通知解析器,subMchId:{}", subMchId);
|
||||
WechatConfig config = configService.getConfigBySubMchId(subMchId);
|
||||
if (config == null) {
|
||||
throw new RuntimeException("未找到子商户配置: " + subMchId);
|
||||
}
|
||||
|
||||
PrivateKey privateKey = PrivateKeyUtils.getPrivateKey(config.getPrivateKey());
|
||||
|
||||
NotificationConfig notificationConfig = new RSAAutoCertificateConfig.Builder()
|
||||
.merchantId(config.getMerchantId())
|
||||
.merchantId(config.getServiceMchId()) // 服务商模式使用服务商商户号
|
||||
.privateKey(privateKey)
|
||||
.merchantSerialNumber(config.getMchSerialNo())
|
||||
.apiV3Key(config.getApiV3Key())
|
||||
.build();
|
||||
|
||||
|
||||
return new NotificationParser(notificationConfig);
|
||||
}
|
||||
|
||||
@@ -90,6 +171,20 @@ public class WxPayConfigFactory {
|
||||
return new RefundService.Builder().config(config).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据子商户号创建退款服务(服务商模式专用)
|
||||
*/
|
||||
public RefundService createRefundServiceBySubMchId(String subMchId) {
|
||||
String cacheKey = "sub_" + subMchId;
|
||||
return refundServiceCache.computeIfAbsent(cacheKey, key -> buildRefundServiceBySubMchId(subMchId));
|
||||
}
|
||||
|
||||
private RefundService buildRefundServiceBySubMchId(String subMchId) {
|
||||
log.info("根据子商户号创建微信退款服务,subMchId:{}", subMchId);
|
||||
Config config = createConfigBySubMchId(subMchId);
|
||||
return new RefundService.Builder().config(config).build();
|
||||
}
|
||||
|
||||
public ProfitsharingService createProfitsharingService(String tenantId) {
|
||||
return profitsharingServiceCache.computeIfAbsent(tenantId, this::buildProfitsharingService);
|
||||
}
|
||||
@@ -105,19 +200,17 @@ public class WxPayConfigFactory {
|
||||
}
|
||||
|
||||
private Executor buildExecutor(String tenantId) {
|
||||
log.info("创建分账异步执行器,tenantId:{}", tenantId);
|
||||
log.info("创建异步处理线程池,tenantId:{}", tenantId);
|
||||
WechatConfig config = configService.getConfig("PAY", tenantId);
|
||||
if (!config.getAsyncEnabled()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
executor.setCorePoolSize(config.getAsyncPoolSize());
|
||||
executor.setMaxPoolSize(config.getAsyncPoolSize());
|
||||
executor.setCorePoolSize(config.getAsyncPoolSize() != null ? config.getAsyncPoolSize() : 5);
|
||||
executor.setMaxPoolSize(config.getAsyncPoolSize() != null ? config.getAsyncPoolSize() * 2 : 10);
|
||||
executor.setQueueCapacity(100);
|
||||
executor.setThreadNamePrefix("profit-sharing-" + tenantId + "-");
|
||||
executor.setThreadNamePrefix("wechat-async-" + tenantId + "-");
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
executor.initialize();
|
||||
|
||||
return executor;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user