fix:完善订单支付逻辑动态配置微信商户支付配置
This commit is contained in:
@@ -145,6 +145,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-wechat-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
+5
-50
@@ -7,7 +7,6 @@ import com.central.common.context.TenantContextHolder;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.log.annotation.AuditLog;
|
||||
import com.central.order.config.TencentPayProperties;
|
||||
import com.central.order.enums.CommonStatus;
|
||||
@@ -18,7 +17,7 @@ import com.central.order.feign.UserCenterService;
|
||||
import com.central.order.model.*;
|
||||
import com.central.order.service.*;
|
||||
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
|
||||
import com.wechat.pay.java.service.payments.jsapi.model.*;
|
||||
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
@@ -97,6 +96,9 @@ public class OrderController {
|
||||
@Autowired
|
||||
private IProfitSharingService profitSharingService;
|
||||
|
||||
@Autowired
|
||||
private PaymentService paymentService;
|
||||
|
||||
/**
|
||||
* 列表
|
||||
*/
|
||||
@@ -227,59 +229,12 @@ public class OrderController {
|
||||
* @param orderId 微信公众号对应微信付款用户的唯一标识
|
||||
* @return 返回jsapi所需参数
|
||||
*/
|
||||
|
||||
@Operation(summary = "生成预支付订单")
|
||||
@PutMapping("/{orderId}/pay")
|
||||
public Result<PrepayWithRequestPaymentResponse> payOrder(@PathVariable("orderId") Long orderId) {
|
||||
try {
|
||||
// 1. 获取用户和订单信息
|
||||
SysUser user = userCenterService.findUserById(LoginUserContextHolder.getUser().getId());
|
||||
PrepayWithRequestPaymentResponse response = paymentService.createPayment(orderId);
|
||||
Order order = orderService.getById(orderId);
|
||||
if (order == null) {
|
||||
return Result.failed("订单数据不存在");
|
||||
}
|
||||
|
||||
// 检查是否需要开启分账
|
||||
QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId());
|
||||
if (qrCode == null) {
|
||||
return Result.failed("二维码不存在");
|
||||
}
|
||||
|
||||
// 3. 创建支付请求
|
||||
PrepayRequest request = new PrepayRequest();
|
||||
|
||||
// 设置基本支付信息
|
||||
Amount amount = new Amount();
|
||||
amount.setTotal(Integer.parseInt(order.getOrderAmount()));
|
||||
request.setAmount(amount);
|
||||
request.setAppid(tencentPayProperties.getAppId());
|
||||
request.setMchid(tencentPayProperties.getMerchantId());
|
||||
request.setDescription(order.getOrderNo());
|
||||
request.setNotifyUrl(tencentPayProperties.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);
|
||||
|
||||
// 创建分账记录
|
||||
ProfitSharing profitSharing = profitSharingService.createProfitSharing(order,qrCode);
|
||||
} else {
|
||||
settleInfo.setProfitSharing(false);
|
||||
request.setSettleInfo(settleInfo);
|
||||
}
|
||||
|
||||
// 5. 调用支付接口
|
||||
PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(request);
|
||||
|
||||
// 6. 返回结果
|
||||
return Result.succeed(response, order.getOrderNo());
|
||||
} catch (Exception e) {
|
||||
log.error("创建预支付订单失败", e);
|
||||
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
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.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class PaymentService {
|
||||
private final WxPayConfigFactory wxPayConfigFactory;
|
||||
private final UserCenterService userCenterService;
|
||||
private final IOrderService orderService;
|
||||
private final IQrCodeService qrCodeService;
|
||||
private final IProfitSharingService profitSharingService;
|
||||
private final WechatConfigService configService;
|
||||
|
||||
public PaymentService(WxPayConfigFactory wxPayConfigFactory,
|
||||
UserCenterService userCenterService,
|
||||
IOrderService orderService,
|
||||
IQrCodeService qrCodeService,
|
||||
IProfitSharingService profitSharingService,
|
||||
WechatConfigService configService) {
|
||||
this.wxPayConfigFactory = wxPayConfigFactory;
|
||||
this.userCenterService = userCenterService;
|
||||
this.orderService = orderService;
|
||||
this.qrCodeService = qrCodeService;
|
||||
this.profitSharingService = profitSharingService;
|
||||
this.configService = 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;
|
||||
}
|
||||
}
|
||||
+12
-45
@@ -16,6 +16,7 @@ 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.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;
|
||||
@@ -24,11 +25,13 @@ 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.context.annotation.Lazy;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(WechatProperties.class)
|
||||
public class WechatAutoConfiguration {
|
||||
@@ -37,6 +40,7 @@ public class WechatAutoConfiguration {
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnBean(WechatConfigService.class)
|
||||
public WxMpService wxMpService(WechatConfigService configService) {
|
||||
log.info("WxMpService(公众号相关)被加载,tenantId:{}", getCurrentTenantId());
|
||||
return new WxMpServiceImpl() {
|
||||
@Override
|
||||
public WxMpDefaultConfigImpl getWxMpConfigStorage() {
|
||||
@@ -62,6 +66,8 @@ public class WechatAutoConfiguration {
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnBean(WechatConfigService.class)
|
||||
public WxMaService wxMaService(WechatConfigService configService) {
|
||||
log.info("WxMaService(小程序通知)被加载,tenantId:{}", getCurrentTenantId());
|
||||
// 小程序通知Service配置,type=MINI
|
||||
return new WxMaServiceImpl() {
|
||||
@Override
|
||||
public WxMaDefaultConfigImpl getWxMaConfig() {
|
||||
@@ -74,58 +80,18 @@ public class WechatAutoConfiguration {
|
||||
};
|
||||
}
|
||||
|
||||
/*@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public WxPayService wxPayService(WechatConfigService configService) {
|
||||
return new WxPayServiceImpl() {
|
||||
@Override
|
||||
public WxPayConfig getConfig() {
|
||||
WechatConfig config = configService.getConfig("PAY", getCurrentMerchantId());
|
||||
WxPayConfig payConfig = new WxPayConfig();
|
||||
payConfig.setAppId(config.getAppId());
|
||||
payConfig.setMchId(config.getMerchantId());
|
||||
payConfig.setMchKey(config.getMchKey());
|
||||
payConfig.setKeyPath(config.getPrivateKeyPath());
|
||||
payConfig.setNotifyUrl(config.getNotifyUrl());
|
||||
payConfig.setApiV3Key(config.getApiV3Key());
|
||||
payConfig.setPrivateKeyPath(config.getPrivateKeyPath());
|
||||
payConfig.setPrivateCertPath(config.getPrivateKeyPath());
|
||||
payConfig.setCertSerialNo(config.getMchSerialNo());
|
||||
return payConfig;
|
||||
}
|
||||
};
|
||||
}*/
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnBean(WechatConfigService.class)
|
||||
public Config wxPayConfig(WechatConfigService configService, PrivateKeyService privateKeyService) {
|
||||
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 JsapiServiceExtension jsapiServiceExtension(Config config) {
|
||||
return new JsapiServiceExtension.Builder().config(config).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RefundService refundService(Config config) {
|
||||
return new RefundService.Builder().config(config).build();
|
||||
public WxPayConfigFactory wxPayConfigFactory(
|
||||
WechatConfigService configService,
|
||||
PrivateKeyService privateKeyService) {
|
||||
return new WxPayConfigFactory(configService, privateKeyService);
|
||||
}
|
||||
|
||||
@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()
|
||||
@@ -162,6 +128,7 @@ public class WechatAutoConfiguration {
|
||||
@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;
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
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.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();
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -33,8 +33,8 @@ 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 appId = OAuthEndpointUtils.getParam(parameters,"appId");
|
||||
return new TencentAuthenticationToken(code, encryptedData, iv, appId);
|
||||
String appCode = OAuthEndpointUtils.getParam(parameters,"appCode");
|
||||
return new TencentAuthenticationToken(code, encryptedData, iv, appCode);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user