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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user