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
@@ -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);
}
}
}
@@ -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";
}
}
@@ -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();
}
}
@@ -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();
// }
//}
@@ -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();
// }
//}
@@ -1,2 +0,0 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zlt.wechat.config.WechatAutoConfiguration