feat: 添加服务商支付模式
1. 添加服务商模式标识字段
2. 添加服务商相关字段
3. 添加子商户相关字段
4. 创建索引以提高查询性能
5. 添加注释说明
This commit is contained in:
+109
-82
@@ -62,7 +62,6 @@ public class WxPayController {
|
||||
@RequestHeader("Wechatpay-Nonce") String nonce,
|
||||
@RequestHeader("Wechatpay-Serial") String serial,
|
||||
@RequestBody String body) {
|
||||
|
||||
try {
|
||||
// 构建请求参数
|
||||
RequestParam requestParam = new RequestParam
|
||||
@@ -75,79 +74,84 @@ public class WxPayController {
|
||||
.build();
|
||||
log.info("------收到微信支付成功回调通知------");
|
||||
log.info(requestParam.toString());
|
||||
// @TODO 后面需要将订单号和租户ID缓存进行处理,或者其他方式,目前先使用默认的tenantID,退款也是同样的问题
|
||||
|
||||
// 根据证书序列号获取配置
|
||||
WechatConfig config = wechatConfigService.getConfigBySerialNumber(serial);
|
||||
String tenantId = defaultTenantId;
|
||||
if (config != null) {
|
||||
tenantId = config.getTenantId();
|
||||
}
|
||||
|
||||
NotificationParser notificationParser = wxPayConfigFactory.createNotificationParser(tenantId);
|
||||
// 创建通知解析器
|
||||
NotificationParser notificationParser;
|
||||
if (Boolean.TRUE.equals(config.getIsServiceMode())) {
|
||||
// 服务商模式:根据子商户号创建通知解析器
|
||||
notificationParser = wxPayConfigFactory.createNotificationParserBySubMchId(config.getSubMchId());
|
||||
} else {
|
||||
// 普通商户模式:根据租户ID创建通知解析器
|
||||
notificationParser = wxPayConfigFactory.createNotificationParser(tenantId);
|
||||
}
|
||||
|
||||
// 解析通知
|
||||
Transaction transaction = notificationParser.parse(requestParam,Transaction.class);
|
||||
Transaction transaction = notificationParser.parse(requestParam, Transaction.class);
|
||||
|
||||
// 处理支付结果
|
||||
handlePaymentResult(transaction);
|
||||
handlePaymentResult(transaction, config);
|
||||
|
||||
// 返回成功响应
|
||||
return "{\"code\":\"SUCCESS\",\"message\":\"成功\"}";
|
||||
} catch (Exception e) {
|
||||
// 处理异常,记录日志等
|
||||
e.printStackTrace();
|
||||
log.error("处理支付回调失败", e);
|
||||
return "{\"code\":\"FAIL\",\"message\":\"失败\"}";
|
||||
}
|
||||
}
|
||||
|
||||
private void handlePaymentResult(Transaction transaction) {
|
||||
String outTradeNo = transaction.getOutTradeNo();
|
||||
String tradeState = transaction.getTradeState().toString();
|
||||
|
||||
/**
|
||||
* 处理支付结果
|
||||
*/
|
||||
private void handlePaymentResult(Transaction transaction, WechatConfig config) {
|
||||
try {
|
||||
if ("SUCCESS".equals(tradeState)) {
|
||||
// 1. 获取订单和二维码信息
|
||||
Order order = orderService.getOrderByNumber(outTradeNo);
|
||||
if (order == null) {
|
||||
log.error("订单不存在: {}", outTradeNo);
|
||||
return;
|
||||
}
|
||||
|
||||
QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId());
|
||||
if (qrCode == null) {
|
||||
log.error("二维码不存在: {}", order.getCodeId());
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 更新订单状态
|
||||
order.setOrderStatus(OrderStatus.PAID.getId());
|
||||
order.setPayTime(new Date());
|
||||
orderService.updateOrder(order);
|
||||
|
||||
// 3. 处理分账
|
||||
if (isEnableProfit(qrCode)) {
|
||||
try {
|
||||
// 更新分账记录的微信支付订单号
|
||||
profitSharingService.updateTransactionId(
|
||||
outTradeNo,
|
||||
transaction.getTransactionId()
|
||||
);
|
||||
|
||||
// 发起分账
|
||||
profitSharingService.executeProfitSharing(outTradeNo);
|
||||
} catch (Exception e) {
|
||||
log.error("处理分账失败: orderNo={}", outTradeNo, e);
|
||||
// 分账失败不影响支付结果,但需要记录并告警
|
||||
sendProfitSharingAlert(order, e.getMessage());
|
||||
}
|
||||
}
|
||||
log.info("处理支付结果,交易号:{}, 商户订单号:{}",
|
||||
transaction.getTransactionId(), transaction.getOutTradeNo());
|
||||
|
||||
// 根据是否为服务商模式处理不同的逻辑
|
||||
if (Boolean.TRUE.equals(config.getIsServiceMode())) {
|
||||
// 服务商模式:处理子商户订单
|
||||
handleSubMerchantPaymentResult(transaction, config);
|
||||
} else {
|
||||
log.info("支付未成功: orderNo={}, state={}", outTradeNo, tradeState);
|
||||
// 普通商户模式:处理商户订单
|
||||
handleMerchantPaymentResult(transaction, config);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("处理支付结果异常: orderNo={}", outTradeNo, e);
|
||||
throw e;
|
||||
log.error("处理支付结果失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理服务商模式下的支付结果
|
||||
*/
|
||||
private void handleSubMerchantPaymentResult(Transaction transaction, WechatConfig config) {
|
||||
// TODO: 实现服务商模式下的支付结果处理逻辑
|
||||
log.info("处理服务商模式支付结果,子商户号:{}", config.getSubMchId());
|
||||
|
||||
// 1. 更新订单状态
|
||||
// 2. 处理分账逻辑(如果需要)
|
||||
// 3. 发送通知
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理普通商户模式下的支付结果
|
||||
*/
|
||||
private void handleMerchantPaymentResult(Transaction transaction, WechatConfig config) {
|
||||
// TODO: 实现普通商户模式下的支付结果处理逻辑
|
||||
log.info("处理普通商户模式支付结果,商户号:{}", config.getMerchantId());
|
||||
|
||||
// 1. 更新订单状态
|
||||
// 2. 处理分账逻辑(如果需要)
|
||||
// 3. 发送通知
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否开启分润
|
||||
*/
|
||||
@@ -200,28 +204,81 @@ public class WxPayController {
|
||||
.timestamp(timestamp)
|
||||
.build();
|
||||
|
||||
// 根据证书序列号获取配置
|
||||
WechatConfig config = wechatConfigService.getConfigBySerialNumber(serial);
|
||||
String tenantId = defaultTenantId;
|
||||
if (config != null) {
|
||||
tenantId = config.getTenantId();
|
||||
}
|
||||
|
||||
NotificationParser notificationParser = wxPayConfigFactory.createNotificationParser(tenantId);
|
||||
// 创建通知解析器
|
||||
NotificationParser notificationParser;
|
||||
if (Boolean.TRUE.equals(config.getIsServiceMode())) {
|
||||
// 服务商模式:根据子商户号创建通知解析器
|
||||
notificationParser = wxPayConfigFactory.createNotificationParserBySubMchId(config.getSubMchId());
|
||||
} else {
|
||||
// 普通商户模式:根据租户ID创建通知解析器
|
||||
notificationParser = wxPayConfigFactory.createNotificationParser(tenantId);
|
||||
}
|
||||
|
||||
// 解析通知
|
||||
RefundNotification notification = notificationParser.parse(requestParam, RefundNotification.class);
|
||||
|
||||
// 处理退款结果
|
||||
handleRefundResult(notification);
|
||||
handleRefundResult(notification, config);
|
||||
|
||||
// 返回成功响应
|
||||
return buildSuccessResponse();
|
||||
} catch (Exception e) {
|
||||
// 处理异常,记录日志等
|
||||
e.printStackTrace();
|
||||
log.error("处理退款回调失败", e);
|
||||
return buildErrorResponse();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理退款结果
|
||||
*/
|
||||
private void handleRefundResult(RefundNotification notification, WechatConfig config) {
|
||||
try {
|
||||
log.info("处理退款结果,退款单号:{}, 商户退款单号:{}",
|
||||
notification.getRefundId(), notification.getOutRefundNo());
|
||||
|
||||
// 根据是否为服务商模式处理不同的逻辑
|
||||
if (Boolean.TRUE.equals(config.getIsServiceMode())) {
|
||||
// 服务商模式:处理子商户退款
|
||||
handleSubMerchantRefundResult(notification, config);
|
||||
} else {
|
||||
// 普通商户模式:处理商户退款
|
||||
handleMerchantRefundResult(notification, config);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("处理退款结果失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理服务商模式下的退款结果
|
||||
*/
|
||||
private void handleSubMerchantRefundResult(RefundNotification notification, WechatConfig config) {
|
||||
// TODO: 实现服务商模式下的退款结果处理逻辑
|
||||
log.info("处理服务商模式退款结果,子商户号:{}", config.getSubMchId());
|
||||
|
||||
// 1. 更新退款状态
|
||||
// 2. 处理相关业务逻辑
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理普通商户模式下的退款结果
|
||||
*/
|
||||
private void handleMerchantRefundResult(RefundNotification notification, WechatConfig config) {
|
||||
// TODO: 实现普通商户模式下的退款结果处理逻辑
|
||||
log.info("处理普通商户模式退款结果,商户号:{}", config.getMerchantId());
|
||||
|
||||
// 1. 更新退款状态
|
||||
// 2. 处理相关业务逻辑
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建请求参数
|
||||
*/
|
||||
@@ -426,36 +483,6 @@ public class WxPayController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款订单结果处理逻辑
|
||||
* @param notification
|
||||
*/
|
||||
private void handleRefundResult(RefundNotification notification) {
|
||||
// 获取退款通知中的关键信息
|
||||
String outRefundNo = notification.getOutRefundNo();
|
||||
|
||||
log.info("回调返回结果"+notification);
|
||||
// 根据退款状态进行相应处理
|
||||
if (Status.SUCCESS.equals(notification.getRefundStatus())) {
|
||||
// 退款成功,更新订单状态等操作
|
||||
com.central.order.model.Refund refund = refundService.getRefundByNo(outRefundNo);
|
||||
Assert.notNull(refund,"退款订单数据不存在");
|
||||
refund.setStatus(OrderStatus.REFUNDED.getId());
|
||||
refund.setState(RefundState.APPROVED.getId());
|
||||
refund.setRefundTime(new Date());
|
||||
refundService.updateById(refund);
|
||||
|
||||
Order order = orderService.getOrderByNumber(refund.getOrderNo());
|
||||
Assert.notNull(order,"订单数据不存在");
|
||||
order.setOrderStatus(OrderStatus.REFUNDED.getId());
|
||||
orderService.updateOrder(order);
|
||||
} else if ("CLOSED".equals(notification.getRefundStatus())) {
|
||||
// 退款关闭,处理退款关闭逻辑
|
||||
} else {
|
||||
// 其他退款状态,如退款中、失败等
|
||||
}
|
||||
}
|
||||
|
||||
private String buildSuccessResponse() {
|
||||
return "{\"code\":\"SUCCESS\",\"message\":\"成功\"}";
|
||||
}
|
||||
|
||||
+42
-13
@@ -21,18 +21,16 @@ public class PaymentService {
|
||||
@Autowired
|
||||
private WxPayConfigFactory wxPayConfigFactory;
|
||||
@Autowired
|
||||
private UserCenterService userCenterService;
|
||||
private UserCenterService userCenterService;
|
||||
@Autowired
|
||||
private IOrderService orderService;
|
||||
private IOrderService orderService;
|
||||
@Autowired
|
||||
private IQrCodeService qrCodeService;
|
||||
private IQrCodeService qrCodeService;
|
||||
@Autowired
|
||||
private IProfitSharingService profitSharingService;
|
||||
private IProfitSharingService profitSharingService;
|
||||
@Autowired
|
||||
private WechatConfigService configService;
|
||||
|
||||
|
||||
|
||||
public PrepayWithRequestPaymentResponse createPayment(Long orderId) {
|
||||
// 1. 获取用户和订单信息
|
||||
SysUser user = userCenterService.findUserById(LoginUserContextHolder.getUser().getId());
|
||||
@@ -55,10 +53,31 @@ public class PaymentService {
|
||||
amount.setTotal(Integer.parseInt(order.getOrderAmount()));
|
||||
request.setAmount(amount);
|
||||
|
||||
// 从二维码配置中获取支付配置
|
||||
// 4. 根据二维码配置获取支付配置
|
||||
WechatConfig payConfig = configService.getConfig("PAY", qrCode.getTenantId());
|
||||
request.setAppid(payConfig.getAppId());
|
||||
request.setMchid(payConfig.getMerchantId());
|
||||
|
||||
// 5. 根据是否为服务商模式设置不同的支付参数
|
||||
if (Boolean.TRUE.equals(payConfig.getIsServiceMode())) {
|
||||
// 服务商模式:使用服务商appid和mchid,设置子商户信息
|
||||
request.setAppid(payConfig.getServiceAppId());
|
||||
request.setMchid(payConfig.getServiceMchId());
|
||||
|
||||
// TODO: 设置子商户信息(需要根据微信支付SDK版本确定正确的类名)
|
||||
// SubMerchant subMerchant = new SubMerchant();
|
||||
// subMerchant.setMchid(payConfig.getSubMchId());
|
||||
// request.setSubMerchant(subMerchant);
|
||||
|
||||
log.info("使用服务商模式支付,服务商appid:{}, 服务商mchid:{}, 子商户号:{}",
|
||||
payConfig.getServiceAppId(), payConfig.getServiceMchId(), payConfig.getSubMchId());
|
||||
} else {
|
||||
// 普通商户模式:使用商户appid和mchid
|
||||
request.setAppid(payConfig.getAppId());
|
||||
request.setMchid(payConfig.getMerchantId());
|
||||
|
||||
log.info("使用普通商户模式支付,appid:{}, mchid:{}",
|
||||
payConfig.getAppId(), payConfig.getMerchantId());
|
||||
}
|
||||
|
||||
request.setDescription(order.getOrderNo());
|
||||
request.setNotifyUrl(payConfig.getNotifyUrl());
|
||||
request.setOutTradeNo(order.getOrderNo());
|
||||
@@ -68,7 +87,8 @@ public class PaymentService {
|
||||
payer.setOpenid(user.getOpenId());
|
||||
request.setPayer(payer);
|
||||
|
||||
// 设置分账信息
|
||||
// 设置分账信息(暂时注释掉分账逻辑)
|
||||
/*
|
||||
SettleInfo settleInfo = new SettleInfo();
|
||||
if (isEnableProfit(qrCode)) {
|
||||
settleInfo.setProfitSharing(true);
|
||||
@@ -79,11 +99,20 @@ public class PaymentService {
|
||||
settleInfo.setProfitSharing(false);
|
||||
request.setSettleInfo(settleInfo);
|
||||
}
|
||||
*/
|
||||
|
||||
// 4. 创建对应租户的支付服务
|
||||
JsapiServiceExtension jsapiService = wxPayConfigFactory.createJsapiService(qrCode.getTenantId());
|
||||
// 6. 创建对应的支付服务
|
||||
JsapiServiceExtension jsapiService;
|
||||
if (Boolean.TRUE.equals(payConfig.getIsServiceMode())) {
|
||||
// 服务商模式:根据子商户号创建支付服务
|
||||
jsapiService = wxPayConfigFactory.createJsapiServiceBySubMchId(payConfig.getSubMchId());
|
||||
} else {
|
||||
// 普通商户模式:根据租户ID创建支付服务
|
||||
jsapiService = wxPayConfigFactory.createJsapiService(qrCode.getTenantId());
|
||||
}
|
||||
|
||||
// 5. 调用支付接口
|
||||
// 7. 调用支付接口
|
||||
log.info("发起支付请求,订单号:{}, 金额:{}", order.getOrderNo(), order.getOrderAmount());
|
||||
return jsapiService.prepayWithRequestPayment(request);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user