退款失败返回详细原因

This commit is contained in:
yanglei 2026-01-20 13:48:58 +08:00
parent ecb9d7f0d1
commit a8b24cf4e3
5 changed files with 45 additions and 17 deletions

View File

@ -10,6 +10,7 @@ import com.ebike.feign.model.vo.FeignEbikeRefundOrderDetailVo;
import com.ebike.feign.model.vo.FeignEbikeRefundReviewVo; import com.ebike.feign.model.vo.FeignEbikeRefundReviewVo;
import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -38,7 +39,7 @@ public class EbikeRefundReviewController {
* @return 审核成功返回true否则返回false * @return 审核成功返回true否则返回false
*/ */
@PostMapping("/reviewRefund") @PostMapping("/reviewRefund")
public JsonResult<?> reviewRefund(@RequestBody EbikeRefundReviewDto processDto) { public JsonResult<?> reviewRefund(@RequestBody @Validated EbikeRefundReviewDto processDto) {
boolean result = refundReviewService.refundReview(processDto); boolean result = refundReviewService.refundReview(processDto);
return JsonResult.success(result); return JsonResult.success(result);
} }

View File

@ -21,11 +21,13 @@ public class EbikeRefundReviewDto {
/** /**
* 主键id * 主键id
*/ */
@NotNull(message = "主键id不能为空")
private Long reviewId; private Long reviewId;
/** /**
* 审核操作, 1-通过 3-驳回 * 审核操作, 1-通过 3-驳回
*/ */
@NotNull(message = "审核操作不能为空")
private Integer operate; private Integer operate;
/** /**

View File

@ -87,8 +87,8 @@ public class EbikeWxPaymentController {
*/ */
@PostMapping("/api/refund") @PostMapping("/api/refund")
public JsonResult<?> refund(@RequestBody FeignEbikeRefundDto refundDto) { public JsonResult<?> refund(@RequestBody FeignEbikeRefundDto refundDto) {
EbikeWxHandleNotifyVo notifyVo = wxPayService.refund(refundDto); wxPayService.refund(refundDto);
return JsonResult.success(notifyVo); return JsonResult.success();
} }
/** /**

View File

@ -72,9 +72,8 @@ public interface EbikeWxPayService {
* 调用微信退款接口 * 调用微信退款接口
* *
* @param refundDto 退款请求 * @param refundDto 退款请求
* @return 退款信息id
*/ */
EbikeWxHandleNotifyVo refund(FeignEbikeRefundDto refundDto); void refund(FeignEbikeRefundDto refundDto);
/** /**

View File

@ -406,9 +406,7 @@ public class EbikeWxPayServiceImpl implements EbikeWxPayService {
} }
@Override @Override
public EbikeWxHandleNotifyVo refund(FeignEbikeRefundDto refundDto) { public void refund(FeignEbikeRefundDto refundDto) {
EbikeWxHandleNotifyVo notifyVo = new EbikeWxHandleNotifyVo();
notifyVo.setSuccess(false);
// 检查退款记录是否存在 // 检查退款记录是否存在
EbikeRefund ebikeRefund = ebikeRefundService.getById(refundDto.getRefundId()); EbikeRefund ebikeRefund = ebikeRefundService.getById(refundDto.getRefundId());
if (Objects.isNull(ebikeRefund)) { if (Objects.isNull(ebikeRefund)) {
@ -447,8 +445,7 @@ public class EbikeWxPayServiceImpl implements EbikeWxPayService {
Refund result = wxRefundService.create(request); Refund result = wxRefundService.create(request);
if (result == null) { if (result == null) {
log.error("调用微信退款接口返回空,订单号: {}", orderId); log.error("调用微信退款接口返回空,订单号: {}", orderId);
notifyVo.setMessage("退款请求失败"); throw new EbikeException("退款失败:微信服务无响应");
return notifyVo;
} }
orderService.refund(orderId); orderService.refund(orderId);
// 更新退款信息 // 更新退款信息
@ -462,14 +459,17 @@ public class EbikeWxPayServiceImpl implements EbikeWxPayService {
ebikeRefund.setRefundTime(LocalDateTime.now()); ebikeRefund.setRefundTime(LocalDateTime.now());
ebikeRefund.setRefundTransactionId(result.getRefundId()); ebikeRefund.setRefundTransactionId(result.getRefundId());
ebikeRefundService.updateById(ebikeRefund); ebikeRefundService.updateById(ebikeRefund);
notifyVo.setSuccess(true); } catch (ServiceException e) {
notifyVo.setMessage("退款已提交"); String httpResponseBody = e.getResponseBody();
return notifyVo; log.error("微信退款失败 | refundId: {}, orderId: {} | HTTP状态码: {}, 响应体: {}",
refundDto.getRefundId(), orderId, e.getHttpStatusCode(), httpResponseBody, e);
// 解析微信退款回调参数
String userMessage = parseWechatErrorMessage(httpResponseBody);
throw new EbikeException(userMessage);
} catch (Exception e) { } catch (Exception e) {
log.error("发起退款异常refundId: {}, orderId: {}, error: {}", log.error("发起退款发生系统异常refundId: {}, orderId: {}",
refundDto.getRefundId(), orderId, e.getMessage(), e); refundDto.getRefundId(), orderId, e);
notifyVo.setMessage("退款失败: " + e.getMessage()); throw new EbikeException("退款失败:系统内部错误,请稍后重试");
return notifyVo;
} }
} }
@ -603,4 +603,30 @@ public class EbikeWxPayServiceImpl implements EbikeWxPayService {
.setScale(0, RoundingMode.HALF_UP) .setScale(0, RoundingMode.HALF_UP)
.longValue(); .longValue();
} }
/**
* 解析微信返回的错误信息提取用户友好提示
*/
private String parseWechatErrorMessage(String responseBody) {
try {
JsonNode root = objectMapper.readTree(responseBody);
JsonNode codeNode = root.get("code");
JsonNode messageNode = root.get("message");
String code = codeNode != null ? codeNode.asText() : "";
String msg = messageNode != null ? messageNode.asText() : "未知错误";
return switch (code) {
case "NOT_ENOUGH" -> "商户账户余额不足,请联系客服处理";
case "RESOURCE_NOT_EXISTS" -> "原支付订单不存在或已过期";
case "ORDER_PAID" -> "订单已支付,不能重复退款";
case "REFUND_NOT_ALLOWED" -> "该订单不支持退款";
case "FREQ_LIMIT" -> "退款频率过高,请稍后再试";
default -> msg;
};
} catch (Exception ex) {
log.warn("解析微信错误信息失败: {}", ex.getMessage());
return "微信服务返回异常,请稍后重试";
}
}
} }