From dd0dda711a6b54a7124a975e7c9d63cdf7dd776d Mon Sep 17 00:00:00 2001 From: Iamasoldier <1272745473@qq.com> Date: Wed, 16 Apr 2025 11:29:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=88=86=E6=B6=A6=E7=AE=A1=E7=90=86)?= =?UTF-8?q?=EF=BC=9A=E4=BC=98=E5=8C=96=E4=BA=8C=E7=BB=B4=E7=A0=81=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=BC=80=E5=90=AF=E5=88=86=E6=B6=A6=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/controller/OrderController.java | 40 ++- .../order/controller/WxPayController.java | 310 +++++++++++------- .../java/com/central/order/model/QrCode.java | 6 + .../order/service/IProfitSharingService.java | 2 +- .../impl/ProfitSharingServiceImpl.java | 18 +- 5 files changed, 241 insertions(+), 135 deletions(-) diff --git a/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java b/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java index 35565e19..29c191c0 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java +++ b/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java @@ -18,10 +18,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.Amount; -import com.wechat.pay.java.service.payments.jsapi.model.Payer; -import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest; -import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse; +import com.wechat.pay.java.service.payments.jsapi.model.*; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; @@ -243,6 +240,12 @@ public class OrderController { return Result.failed("订单数据不存在"); } + // 检查是否需要开启分账 + QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId()); + if (qrCode == null) { + return Result.failed("二维码不存在"); + } + // 3. 创建支付请求 PrepayRequest request = new PrepayRequest(); @@ -262,12 +265,17 @@ public class OrderController { request.setPayer(payer); // 设置分账信息 -// SettleInfo settleInfo = new SettleInfo(); -// settleInfo.setProfitSharing(true); // 直接设置分账标识为true -// request.setSettleInfo(settleInfo); -// -// // 4. 创建分账记录 -// ProfitSharing profitSharing = profitSharingService.createProfitSharing(order); + 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); @@ -280,6 +288,18 @@ public class OrderController { } } + /** + * 检查是否开启分润 + */ + 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; // 确保有分润比例 + } + @Operation(summary = "获取排队总览信息") @GetMapping("/queue/info") public Result getQueueInfo(@RequestParam String codeId) { diff --git a/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayController.java b/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayController.java index 08729369..4ec10a51 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayController.java +++ b/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayController.java @@ -5,8 +5,10 @@ import com.central.order.enums.OrderStatus; import com.central.order.enums.RefundState; import com.central.order.model.Order; import com.central.order.model.ProfitSharing; +import com.central.order.model.QrCode; import com.central.order.service.IOrderService; import com.central.order.service.IProfitSharingService; +import com.central.order.service.IQrCodeService; import com.central.order.service.IRefundService; import com.wechat.pay.java.core.notification.NotificationConfig; import com.wechat.pay.java.core.notification.NotificationParser; @@ -21,7 +23,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.*; -import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -38,18 +39,14 @@ public class WxPayController { private IOrderService orderService; @Autowired private IRefundService refundService; + @Autowired + private IQrCodeService qrCodeService; + @Autowired + private IProfitSharingService profitSharingService; @Autowired private NotificationConfig config; - @Autowired - private IProfitSharingService profitSharingService; - /** - * 支付回调 - * - * @return - * @throws IOException - */ @PostMapping("/pay/notify") public String handleCallback(@RequestHeader("Wechatpay-Signature") String signature, @RequestHeader("Wechatpay-Timestamp") String timestamp, @@ -85,37 +82,88 @@ public class WxPayController { } private void handlePaymentResult(Transaction transaction) { - // 获取支付通知中的关键信息 String outTradeNo = transaction.getOutTradeNo(); String tradeState = transaction.getTradeState().toString(); - // 2. 根据支付状态进行相应处理 - if ("SUCCESS".equals(tradeState)) { - // 支付成功,更新订单状态等操作 - System.out.println("支付成功,商户订单号:" + outTradeNo); + try { + if ("SUCCESS".equals(tradeState)) { + // 1. 获取订单和二维码信息 + Order order = orderService.getOrderByNumber(outTradeNo); + if (order == null) { + log.error("订单不存在: {}", outTradeNo); + return; + } - // 3. 更新订单状态 - Order order = orderService.getOrderByNumber(transaction.getOutTradeNo()); - order.setOrderStatus(OrderStatus.PAID.getId()); - order.setPayTime(new Date()); - orderService.updateOrder(order); - // 这里可以添加更新数据库订单状态的代码 + QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId()); + if (qrCode == null) { + log.error("二维码不存在: {}", order.getCodeId()); + return; + } - // 4. 更新分账记录 -// profitSharingService.updateTransactionId( -// transaction.getOutTradeNo(), -// transaction.getTransactionId() -// ); + // 2. 更新订单状态 + order.setOrderStatus(OrderStatus.PAID.getId()); + order.setPayTime(new Date()); + orderService.updateOrder(order); - // 5. 发起分账请求 -// profitSharingService.executeProfitSharing(transaction.getOutTradeNo()); - } else { - // 其他支付状态,如支付中、失败等 - System.out.println("支付状态:" + tradeState + ",商户订单号:" + outTradeNo); + // 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()); + } + } + } else { + log.info("支付未成功: orderNo={}, state={}", outTradeNo, tradeState); + } + } catch (Exception e) { + log.error("处理支付结果异常: orderNo={}", outTradeNo, e); + throw e; } } + /** + * 检查是否开启分润 + */ + 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; // 确保有分润比例 + } + /** + * 发送分账异常告警 + */ + private void sendProfitSharingAlert(Order order, String errorMessage) { + try { + String alertMessage = String.format( + "分账处理异常告警\n" + + "订单号:%s\n" + + "异常信息:%s\n" + + "时间:%s", + order.getOrderNo(), + errorMessage, + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ); + + log.error(alertMessage); + // TODO: 实现具体的告警通知逻辑 + } catch (Exception e) { + log.error("发送分账异常告警失败", e); + } + } @PostMapping("/refund/notify") public String refundCallback(@RequestHeader("Wechatpay-Signature") String signature, @@ -206,81 +254,6 @@ public class WxPayController { } } - /** - * 处理分账失败 - */ - private void handleProfitSharingFailure(String orderNo, String failReason) { - try { - log.error("分账失败: orderNo={}, reason={}", orderNo, failReason); - - // 1. 更新分账状态为失败 - profitSharingService.updateProfitSharingStatus( - orderNo, - ProfitSharingConstants.Status.FAILED - ); - - // 2. 获取分账记录 - ProfitSharing profitSharing = profitSharingService.getByOrderNo(orderNo); - if (profitSharing == null) { - log.error("分账记录不存在: {}", orderNo); - return; - } - - // 3. 更新失败原因 - profitSharing.setFailReason(failReason); - profitSharing.setUpdateTime(new Date()); - - // 4. 判断是否需要重试 - if (profitSharing.canRetry()) { - log.info("准备重试分账: orderNo={}, retryTimes={}", - orderNo, profitSharing.getRetryTimes()); - profitSharingService.retryFailedProfitSharing(orderNo); - } else { - log.warn("分账失败且不再重试: orderNo={}, retryTimes={}", - orderNo, profitSharing.getRetryTimes()); - // 可以在这里添加告警通知 - sendFailureAlert(profitSharing); - } - - } catch (Exception e) { - log.error("处理分账失败异常", e); - // 确保状态更新为失败 - profitSharingService.updateProfitSharingStatus( - orderNo, - ProfitSharingConstants.Status.FAILED - ); - } - } - - /** - * 发送失败告警 - */ - private void sendFailureAlert(ProfitSharing profitSharing) { - try { - String alertMessage = String.format( - "分账失败告警\n" + - "订单号:%s\n" + - "交易号:%s\n" + - "失败原因:%s\n" + - "重试次数:%d\n" + - "分账金额:%d分\n" + - "失败时间:%s", - profitSharing.getOrderNo(), - profitSharing.getTransactionId(), - profitSharing.getFailReason(), - profitSharing.getRetryTimes(), - profitSharing.getQrCodeAmount(), - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) - ); - - log.error(alertMessage); - // TODO: 实现具体的告警通知逻辑,如发送邮件、钉钉消息等 - - } catch (Exception e) { - log.error("发送分账失败告警异常", e); - } - } - /** * 优化后的分账回调处理方法 */ @@ -312,18 +285,121 @@ public class WxPayController { String orderNo = notification.getOutOrderNo(); String state = notification.getState(); - switch (state) { - case "FINISHED": - profitSharingService.updateProfitSharingStatus( - orderNo, - ProfitSharingConstants.Status.SUCCESS - ); - break; - case "FAILED": - handleProfitSharingFailure(orderNo, notification.getFailReason()); - break; - default: - log.warn("未知的分账状态: {}", state); + try { + // 1. 获取分账记录 + ProfitSharing profitSharing = profitSharingService.getByOrderNo(orderNo); + if (profitSharing == null) { + log.error("分账记录不存在: {}", orderNo); + return; + } + + // 2. 根据状态处理 + switch (state) { + case "FINISHED": + // 分账成功 + handleProfitSharingSuccess(profitSharing, notification); + break; + + case "FAILED": + // 分账失败 + handleProfitSharingFailure(profitSharing, notification); + break; + + default: + log.warn("未知的分账状态: orderNo={}, state={}", orderNo, state); + } + } catch (Exception e) { + log.error("处理分账回调异常: orderNo={}", orderNo, e); + // 确保更新状态 + profitSharingService.updateProfitSharingStatus( + orderNo, + ProfitSharingConstants.Status.FAILED + ); + } + } + + /** + * 处理分账成功 + */ + private void handleProfitSharingSuccess(ProfitSharing profitSharing, + ProfitSharingNotification notification) { + // 1. 更新分账状态 + profitSharing.setStatus(ProfitSharingConstants.Status.SUCCESS); + profitSharing.setFinishTime(new Date()); + + // 2. 如果有接收方信息,更新接收方结果 + if (notification.getReceivers() != null && !notification.getReceivers().isEmpty()) { + ProfitSharingNotification.Receiver receiver = notification.getReceivers().get(0); + profitSharing.setReceiverAmount(receiver.getAmount()); + profitSharing.setProfitSharingId(receiver.getDetailId()); + } + + // 3. 保存更新 + profitSharingService.updateById(profitSharing); + + log.info("分账成功: orderNo={}", profitSharing.getOrderNo()); + } + + /** + * 处理分账失败 + */ + private void handleProfitSharingFailure(ProfitSharing profitSharing, + ProfitSharingNotification notification) { + // 1. 更新分账状态和失败原因 + profitSharing.setStatus(ProfitSharingConstants.Status.FAILED); + profitSharing.setFailReason(notification.getFailReason()); + profitSharing.setUpdateTime(new Date()); + + // 2. 判断是否需要重试 + if (profitSharing.canRetry()) { + log.info("准备重试分账: orderNo={}, retryTimes={}", + profitSharing.getOrderNo(), profitSharing.getRetryTimes()); + + // 更新重试信息 + profitSharing.setRetryTimes(profitSharing.getRetryTimes() + 1); + profitSharing.setLastRetryTime(new Date()); + profitSharingService.updateById(profitSharing); + + // 发起重试 + profitSharingService.retryFailedProfitSharing(profitSharing.getOrderNo()); + } else { + log.warn("分账失败且不再重试: orderNo={}, retryTimes={}", + profitSharing.getOrderNo(), profitSharing.getRetryTimes()); + + // 发送告警 + sendFailureAlert(profitSharing); + + // 保存最终状态 + profitSharingService.updateById(profitSharing); + } + } + + /** + * 发送失败告警 + */ + private void sendFailureAlert(ProfitSharing profitSharing) { + try { + String alertMessage = String.format( + "分账失败告警\n" + + "订单号:%s\n" + + "交易号:%s\n" + + "失败原因:%s\n" + + "重试次数:%d\n" + + "分账金额:%d分\n" + + "失败时间:%s", + profitSharing.getOrderNo(), + profitSharing.getTransactionId(), + profitSharing.getFailReason(), + profitSharing.getRetryTimes(), + profitSharing.getQrCodeAmount(), + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ); + + log.error(alertMessage); + // TODO: 实现具体的告警通知逻辑,如发送邮件、钉钉消息等 + + } catch (Exception e) { + log.error("发送分账失败告警异常", e); } } diff --git a/zlt-business/order-center/src/main/java/com/central/order/model/QrCode.java b/zlt-business/order-center/src/main/java/com/central/order/model/QrCode.java index 5e89cbc7..34d58362 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/model/QrCode.java +++ b/zlt-business/order-center/src/main/java/com/central/order/model/QrCode.java @@ -80,4 +80,10 @@ public class QrCode extends SuperEntity { @Schema(description = "分账失败原因") private String payeeFailReason; + + /** + * 是否开启分润(0:否,1:是) + */ + @Schema(description = "是否开启分润") + private Integer enableProfit; } diff --git a/zlt-business/order-center/src/main/java/com/central/order/service/IProfitSharingService.java b/zlt-business/order-center/src/main/java/com/central/order/service/IProfitSharingService.java index f58914a9..62458a18 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/service/IProfitSharingService.java +++ b/zlt-business/order-center/src/main/java/com/central/order/service/IProfitSharingService.java @@ -14,7 +14,7 @@ public interface IProfitSharingService extends ISuperService { /** * 创建分账记录 */ - ProfitSharing createProfitSharing(Order order); + ProfitSharing createProfitSharing(Order order, QrCode qrCode); /** * 更新微信支付订单号 diff --git a/zlt-business/order-center/src/main/java/com/central/order/service/impl/ProfitSharingServiceImpl.java b/zlt-business/order-center/src/main/java/com/central/order/service/impl/ProfitSharingServiceImpl.java index f9fa6475..00dd9a88 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/service/impl/ProfitSharingServiceImpl.java +++ b/zlt-business/order-center/src/main/java/com/central/order/service/impl/ProfitSharingServiceImpl.java @@ -66,19 +66,13 @@ public class ProfitSharingServiceImpl extends SuperServiceImpl