fix:支付宝支付退款以及消息通知

This commit is contained in:
2026-06-25 20:40:43 +08:00
parent 2babee00b1
commit ca41fe080c
8 changed files with 870 additions and 118 deletions
@@ -6,6 +6,7 @@ import com.central.common.feign.UserService;
import com.central.common.model.*;
import com.central.order.constants.EditTaskStatus;
import com.central.order.enums.OrderStatus;
import com.central.order.enums.PayTypeEnum;
import com.central.order.feign.FileCenterService;
import com.central.order.model.*;
import com.central.order.model.params.FileDownloadRequest;
@@ -93,6 +94,9 @@ public class OrderFileController {
@Autowired
private WxMiniSubscribeService wxMiniSubscribeService;
@Autowired
private AlipayMiniSubscribeService alipayMiniSubscribeService;
@Autowired
private IEditTaskService editTaskService;
@@ -418,28 +422,37 @@ public class OrderFileController {
* @param tourism
*/
private void sendMessageForResouresUploaded(Order order, Tourism tourism) {
// 发送小程序订阅消息,用户素材上传完成
SysUser sysUser = userCenterService.findUserById(order.getUserId());
String orderName = tourism.getTourismName();
String pageUrl = "pages/album/album";
// 处理支付时间为空的情况
String payTimeStr = "";
if (order.getPayTime() != null) {
payTimeStr = DateUtils.sdf.format(order.getPayTime());
}
if (sysUser.getOpenId() != null) {
String tip = "素材上传完成";
String orderName = tourism.getTourismName();
String pageUrl = "pages/album/album";
// 处理支付时间为空的情况
String payTimeStr = "";
if (order.getPayTime() != null) {
payTimeStr = DateUtils.sdf.format(order.getPayTime());
// 根据 payType 发送小程序通知(微信 or 支付宝)
Integer payType = order.getPayType();
if (PayTypeEnum.WECHAT_PAY.getCode().equals(payType)) {
SysUser sysUser = userCenterService.findUserById(order.getUserId());
if (sysUser.getOpenId() != null) {
wxMiniSubscribeService.sendServiceProcessMsg(
sysUser.getOpenId(),
orderName,
"素材上传完成",
order.getOrderNo(),
payTimeStr,
"",
pageUrl
);
}
} else if (PayTypeEnum.ALIPAY_PAY.getCode().equals(payType)) {
if (order.getTenantId() != null && !order.getTenantId().isEmpty()) {
alipayMiniSubscribeService.sendServiceProcessMsg(
order.getUserId(), order.getTenantId(),
orderName, "素材上传完成",
order.getOrderNo(), payTimeStr, pageUrl
);
}
wxMiniSubscribeService.sendServiceProcessMsg(
sysUser.getOpenId(),
orderName,
tip,
order.getOrderNo(),
payTimeStr,
"",
pageUrl
);
}
}
@@ -248,7 +248,7 @@ public class RefundController {
return Result.succeed("已处理全额退款");
}
if (refundRequest.getState().equals(RefundState.PART.getId())){
dealCustomRefundAmountRequest(order,refundRequest.getRefundAmount());
routeRefundByPayType(order, refundRequest.getRefundAmount());
refundorder.setApproveReason(refundRequest.getApproveReason());
refundorder.setState(refundRequest.getState()); //部分退款
if(refundRequest.getRefundAmount()!=null){
@@ -11,6 +11,7 @@ import com.central.order.model.Message;
import com.central.order.model.Order;
import com.central.order.model.QrCode;
import com.central.order.model.Tourism;
import com.central.order.enums.PayTypeEnum;
import com.central.order.service.*;
import com.central.order.service.impl.FeipaiNotifyServiceImpl;
import com.central.order.util.DateUtils;
@@ -42,6 +43,8 @@ public class OrderStatusChangeListener {
@Autowired
private WxMiniSubscribeService wxMiniSubscribeService;
@Autowired
private AlipayMiniSubscribeService alipayMiniSubscribeService;
@Autowired
private FeipaiNotifyServiceImpl feipaiNotifyService;
@EventListener
@@ -164,27 +167,15 @@ public class OrderStatusChangeListener {
private void handlePaymentSuccess(Order order, Tourism tourism) {
try {
// 1. 发送微信通知
SysUser sysUser = userCenterService.findUserById(order.getUserId());
// 2. 查询订单排队信息
// 1. 查询订单排队信息
Integer queue = orderService.getOrderRankingByOrderIdAndDevice(order.getId(), order.getDeviceSn());
String orderName = tourism.getTourismName();
String pageUrl = "pages/task/task";
String tip = "支付成功,当前排第" + queue + "位";
String orderTime = DateUtils.sdf.format(order.getPayTime());
// 发送小程序订阅消息
if (sysUser.getOpenId() != null) {
String tip = "支付成功,当前排第"+queue+"位";
String orderName = tourism.getTourismName();
String pageUrl = "pages/task/task";
wxMiniSubscribeService.sendServiceProcessMsg(
sysUser.getOpenId(),
orderName,
tip,
order.getOrderNo(),
DateUtils.sdf.format(order.getPayTime()),
"",
pageUrl
);
}
// 根据 payType 发送小程序通知(微信 or 支付宝)
sendSubscribeMsgByPayType(order, orderName, tip, order.getOrderNo(), orderTime, pageUrl);
// 2. 记录支付成功日志
log.info("订单支付成功: orderNo={}, amount={}, tourismName={}",
@@ -231,108 +222,74 @@ public class OrderStatusChangeListener {
/**
* 退款申请成功(退款中)通知
* @param order
* @param tourism
*/
private void sendMessageForRefunding(Order order, Tourism tourism) {
// 发送小程序订阅消息,用户素材上传完成
SysUser sysUser = userCenterService.findUserById(order.getUserId());
if (sysUser.getOpenId() != null) {
String tip = "您的订单正在退款中";
try {
String orderName = tourism.getTourismName();
String pageUrl = "pages/task/task";
wxMiniSubscribeService.sendServiceProcessMsg(
sysUser.getOpenId(),
orderName,
tip,
order.getOrderNo(),
DateUtils.sdf.format(order.getPayTime()),
"",
pageUrl
);
String orderTime = DateUtils.sdf.format(order.getPayTime());
sendSubscribeMsgByPayType(order, orderName, "您的订单正在退款中",
order.getOrderNo(), orderTime, pageUrl);
} catch (Exception e) {
log.error("退款中通知发送失败,不影响主流程: orderNo={}", order.getOrderNo(), e);
}
}
/**
* 退款完成通知
* @param order
* @param tourism
*/
private void sendMessageForRefunded(Order order, Tourism tourism) {
// 发送小程序订阅消息,用户素材上传完成
SysUser sysUser = userCenterService.findUserById(order.getUserId());
if (sysUser.getOpenId() != null) {
String tip = "您的订单退款已完成";
try {
String orderName = tourism.getTourismName();
String pageUrl = "pages/task/task";
wxMiniSubscribeService.sendServiceProcessMsg(
sysUser.getOpenId(),
orderName,
tip,
order.getOrderNo(),
DateUtils.sdf.format(order.getPayTime()),
"",
pageUrl
);
String orderTime = DateUtils.sdf.format(order.getPayTime());
sendSubscribeMsgByPayType(order, orderName, "您的订单退款已完成",
order.getOrderNo(), orderTime, pageUrl);
} catch (Exception e) {
log.error("退款完成通知发送失败,不影响主流程: orderNo={}", order.getOrderNo(), e);
}
}
/**
* 素材上传完成小程序通知
* @param order
* @param tourism
*/
private void sendMessageForResouresUploaded(Order order, Tourism tourism) {
// 发送小程序订阅消息,用户素材上传完成
SysUser sysUser = userCenterService.findUserById(order.getUserId());
if (sysUser.getOpenId() != null) {
String tip = "素材上传完成";
try {
String orderName = tourism.getTourismName();
String pageUrl = "pages/album/album";
wxMiniSubscribeService.sendServiceProcessMsg(
sysUser.getOpenId(),
orderName,
tip,
order.getOrderNo(),
DateUtils.sdf.format(order.getPayTime()),
"",
pageUrl
);
String orderTime = "";
if (order.getPayTime() != null) {
orderTime = DateUtils.sdf.format(order.getPayTime());
}
sendSubscribeMsgByPayType(order, orderName, "素材上传完成",
order.getOrderNo(), orderTime, pageUrl);
} catch (Exception e) {
log.error("素材上传通知发送失败,不影响主流程: orderNo={}", order.getOrderNo(), e);
}
}
/**
* 订单完成后通知下一位用户
* @param order
* @param tourism
*/
private void sendMessageToNextOrderUser(Order order, Tourism tourism) {
// 新增:查找下一个排队用户
Order nextOrder = orderService.getNextQueueOrder(order.getTourismId(), order.getId());
if (nextOrder != null) {
SysUser nextUser = userCenterService.findUserById(nextOrder.getUserId());
if (nextUser != null && nextUser.getOpenId() != null && tourism != null) {
String activityName = tourism.getTourismName(); // 活动名称
try {
Order nextOrder = orderService.getNextQueueOrder(order.getTourismId(), order.getId());
if (nextOrder != null && tourism != null) {
String activityName = tourism.getTourismName();
String remark = "即将为您进行拍摄服务";
String activityNo = nextOrder.getOrderNo(); // 活动编号
String activityNo = nextOrder.getOrderNo();
String pageUrl = "pages/album/album";
wxMiniSubscribeService.sendServiceProcessMsg(
nextUser.getOpenId(),
activityName,
remark,
activityNo,
DateUtils.sdf.format(nextOrder.getPayTime()),
"",
pageUrl
);
log.info("已通知下一个排队用户: userId={}, openId={}, orderNo={}", nextUser.getId(), nextUser.getOpenId(), activityNo);
String orderTime = DateUtils.sdf.format(nextOrder.getPayTime());
sendSubscribeMsgByPayType(nextOrder, activityName, remark,
activityNo, orderTime, pageUrl);
log.info("已通知下一个排队用户: userId={}, orderNo={}", nextOrder.getUserId(), activityNo);
}
} catch (Exception e) {
log.error("通知下一位排队用户失败,不影响主流程: orderNo={}", order.getOrderNo(), e);
}
}
@@ -345,4 +302,35 @@ public class OrderStatusChangeListener {
&& qrCode.getEnableVideoEdit() == 1
&& qrCode.getTemplateId() != null; // 确保有模板ID
}
/**
* 根据订单支付类型发送小程序订阅消息
* payType=1 → 微信通知
* payType=4 → 支付宝通知
* 其他 → 不发送
*/
private void sendSubscribeMsgByPayType(Order order, String taskName, String processDesc,
String orderNo, String orderTime, String pageUrl) {
Integer payType = order.getPayType();
if (PayTypeEnum.WECHAT_PAY.getCode().equals(payType)) {
// 微信通知
SysUser sysUser = userCenterService.findUserById(order.getUserId());
if (sysUser != null && sysUser.getOpenId() != null) {
wxMiniSubscribeService.sendServiceProcessMsg(
sysUser.getOpenId(), taskName, processDesc,
orderNo, orderTime, "", pageUrl);
}
} else if (PayTypeEnum.ALIPAY_PAY.getCode().equals(payType)) {
// 支付宝通知:服务内部按 tenantId 查询 MINI 配置并获取 appId
if (order.getTenantId() != null && !order.getTenantId().isEmpty()) {
alipayMiniSubscribeService.sendServiceProcessMsg(
order.getUserId(), order.getTenantId(), taskName, processDesc,
orderNo, orderTime, pageUrl);
} else {
log.warn("订单 tenantId 为空,跳过支付宝订阅消息: orderNo={}", orderNo);
}
}
// payType=2(优惠券) / 3(核销) 不发送小程序通知
}
}
@@ -0,0 +1,29 @@
package com.central.order.service;
/**
* 支付宝小程序订阅消息服务
* <p>
* 对标 WxMiniSubscribeService,封装支付宝模板消息发送能力。
* 支持通过系统 userId + 租户ID 自动查询支付宝 userId 并发送消息。
*
* @author duzhichao
* @date 2026-06-24
*/
public interface AlipayMiniSubscribeService {
/**
* 发送服务进度通知(支付成功/退款中/退款完成/排队通知等)
*
* @param userId 系统用户ID
* @param tenantId 租户ID,用于查询 type=MINI 的支付宝小程序配置
* @param taskName 服务名称(如景区名称)
* @param processDesc 服务阶段描述(如"支付成功,当前排第2位")
* @param orderNo 订单编号
* @param orderTime 下单时间
* @param pageUrl 跳转小程序页面路径
*/
void sendServiceProcessMsg(Long userId, String tenantId,
String taskName, String processDesc,
String orderNo, String orderTime,
String pageUrl);
}
@@ -0,0 +1,219 @@
package com.central.order.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayObject;
import com.alipay.api.AlipayRequest;
import com.alipay.api.AlipayResponse;
import com.alipay.api.internal.util.AlipaySignature;
import com.central.alipay.config.AlipayClientFactory;
import com.central.alipay.entity.AlipayConfig;
import com.central.common.feign.UserService;
import com.central.order.service.AlipayMiniSubscribeService;
import com.central.order.service.IAlipayConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* 支付宝小程序订阅消息服务实现
* <p>
* 通过 {@link AlipayClient(AlipayRequest, Map)} 直接调用
* alipay.open.app.message.subscription.send 接口,无需依赖特定 SDK 版本的 Request/Response 类。
* <p>
* 验证方式、参数签名等均由 AlipayClient 统一处理。
*
* @author duzhichao
* @date 2026-06-24
*/
@Slf4j
@Service
public class AlipayMiniSubscribeServiceImpl implements AlipayMiniSubscribeService {
/** 支付宝小程序订阅消息发送接口方法名 */
private static final String API_METHOD = "alipay.open.app.message.subscription.send";
@Value("${alipay.mini.template.service-process:}")
private String serviceProcessTemplateId;
@Autowired
private AlipayClientFactory alipayClientFactory;
@Autowired
private IAlipayConfigService alipayConfigService;
@Autowired
private UserService userService;
@Override
public void sendServiceProcessMsg(Long userId, String tenantId,
String taskName, String processDesc,
String orderNo, String orderTime,
String pageUrl) {
try {
// 0. 校验模板ID配置
if (serviceProcessTemplateId == null || serviceProcessTemplateId.isEmpty()) {
log.warn("支付宝小程序订阅消息模板ID未配置(alipay.mini.template.service-process),跳过通知: orderNo={}", orderNo);
return;
}
// 1. 按租户获取支付宝小程序配置(type=MINI)
AlipayConfig config = alipayConfigService.getConfigByTypeAndTenantId("MINI", tenantId);
if (config == null) {
log.warn("支付宝小程序配置不存在(type=MINI),跳过通知: tenantId={}, orderNo={}", tenantId, orderNo);
return;
}
String appId = config.getAppId();
if (appId == null || appId.isEmpty()) {
log.warn("支付宝小程序 appId 为空,跳过通知: tenantId={}, orderNo={}", tenantId, orderNo);
return;
}
// 2. 通过 userId + appId 反查支付宝 userId
String alipayUserId = userService.getAlipayOpenIdByUserId(userId, appId);
if (alipayUserId == null || alipayUserId.isEmpty()) {
log.warn("用户无支付宝 userId,跳过通知: userId={}, appId={}, orderNo={}", userId, appId, orderNo);
return;
}
// 3. 获取支付宝客户端(MINI 类型配置)
AlipayClient client = alipayClientFactory.createAlipayClient(config);
// 4. 构建订阅消息参数(字段名需与支付宝开放平台申请的模板变量一致)
JSONObject data = new JSONObject();
data.put("taskName", new JSONObject().put("value", taskName != null ? taskName : ""));
data.put("processDesc", new JSONObject().put("value", processDesc != null ? processDesc : ""));
data.put("orderNo", new JSONObject().put("value", orderNo != null ? orderNo : ""));
data.put("orderTime", new JSONObject().put("value", orderTime != null ? orderTime : ""));
JSONObject bizContent = new JSONObject();
bizContent.put("to_user_id", alipayUserId);
bizContent.put("template_id", serviceProcessTemplateId);
if (pageUrl != null && !pageUrl.isEmpty()) {
bizContent.put("page", pageUrl);
}
bizContent.put("data", data);
// 5. 使用 AlipayRequest 接口直接调用,无需依赖特定 SDK 版本的 Request 类
Map<String, String> requestParams = new HashMap<>();
requestParams.put("method", API_METHOD);
requestParams.put("biz_content", bizContent.toJSONString());
AlipayRequest<AlipayResponse> request = new SimpleAlipayRequest(requestParams);
// 6. 发送
AlipayResponse response = client.execute(request);
if (response.isSuccess()) {
log.info("支付宝小程序订阅消息发送成功: userId={}, orderNo={}, templateId={}",
userId, orderNo, serviceProcessTemplateId);
} else {
log.error("支付宝小程序订阅消息发送失败: userId={}, orderNo={}, subCode={}, subMsg={}",
userId, orderNo, response.getSubCode(), response.getSubMsg());
}
} catch (Exception e) {
// 支付宝消息发送失败不影响主业务流程
log.error("支付宝小程序订阅消息发送异常: userId={}, orderNo={}", userId, orderNo, e);
}
}
/**
* 简易 AlipayRequest 实现,通过 Map 传参,避免依赖特定 SDK 版本的 Request 类。
* <p>
* 方法签名严格对齐 alipay-sdk-java 4.40.816.ALL 中 AlipayRequest 接口的 19 个抽象方法。
*/
private static class SimpleAlipayRequest implements AlipayRequest<AlipayResponse> {
private final Map<String, String> requestParams;
SimpleAlipayRequest(Map<String, String> requestParams) {
this.requestParams = requestParams;
}
// ---- AlipayRequest 接口 19 个抽象方法(按 javap 反编译顺序) ----
@Override
public String getApiMethodName() {
return requestParams.get("method");
}
@Override
public Map<String, String> getTextParams() {
return requestParams;
}
@Override
public String getApiVersion() {
return "1.0";
}
@Override
public void setApiVersion(String apiVersion) { }
@Override
public String getTerminalType() {
return null;
}
@Override
public void setTerminalType(String terminalType) { }
@Override
public String getTerminalInfo() {
return null;
}
@Override
public void setTerminalInfo(String terminalInfo) { }
@Override
public String getProdCode() {
return null;
}
@Override
public void setProdCode(String prodCode) { }
@Override
public String getNotifyUrl() {
return null;
}
@Override
public void setNotifyUrl(String notifyUrl) { }
@Override
public String getReturnUrl() {
return null;
}
@Override
public void setReturnUrl(String returnUrl) { }
@Override
public Class<AlipayResponse> getResponseClass() {
return AlipayResponse.class;
}
@Override
public boolean isNeedEncrypt() {
return false;
}
@Override
public void setNeedEncrypt(boolean needEncrypt) { }
@Override
public AlipayObject getBizModel() {
return null;
}
@Override
public void setBizModel(AlipayObject bizModel) { }
}
}