ebike-plus/ebike-payment/src/main/java/com/cdzy/payment/controller/EbikeWxPayNotifyController.java
2025-10-30 09:21:18 +08:00

98 lines
3.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.cdzy.payment.controller;
import com.cdzy.payment.model.vo.EbikeWxHandleNotifyVo;
import com.cdzy.payment.service.EbikeWxPayService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 微信支付通知回调接口
* <br>通知接口不能鉴权
*
* @author: yanglei
* @since: 2025-10-21 15:00
*/
@RestController
@RequestMapping("/wxPayment/notify")
public class EbikeWxPayNotifyController {
@Resource
private EbikeWxPayService wxPayService;
@Resource
private ObjectMapper objectMapper;
/**
* 支付回调通知
*
* @param request 支付回调请求
* @param response 支付回调响应
* @return 支付回调响应
*/
@PostMapping("/pay")
public String payNotify(HttpServletRequest request, HttpServletResponse response) {
EbikeWxHandleNotifyVo r = wxPayService.handlePayNotify(request);
if (!r.isSuccess()) {
response.setStatus(500);
return createErrorResponse(r.getMessage());
}
response.setStatus(200);
return createSuccessResponse();
}
/**
* 退款回调通知
*
* @param request 退款回调请求
* @param response 退款回调响应
* @return 退款回调响应
*/
@PostMapping("/refund")
public String refundNotify(HttpServletRequest request, HttpServletResponse response) {
EbikeWxHandleNotifyVo r = wxPayService.handleRefundNotify(request);
if (!r.isSuccess()) {
response.setStatus(500);
return createErrorResponse(r.getMessage());
}
response.setStatus(200);
return createSuccessResponse();
}
/**
* 创建成功响应
*/
private String createSuccessResponse() {
try {
ObjectNode jsonObject = objectMapper.createObjectNode();
jsonObject.put("code", "SUCCESS");
jsonObject.put("message", "OK");
return objectMapper.writeValueAsString(jsonObject);
} catch (JsonProcessingException e) {
// 如果JSON序列化失败返回简单的成功响应
return "{\"code\":\"SUCCESS\",\"message\":\"OK\"}";
}
}
/**
* 创建错误响应
*/
private String createErrorResponse(String message) {
try {
ObjectNode jsonObject = objectMapper.createObjectNode();
jsonObject.put("code", "SYSTEM_ERROR");
jsonObject.put("message", message);
return objectMapper.writeValueAsString(jsonObject);
} catch (JsonProcessingException e) {
// 如果JSON序列化失败返回简单的错误响应
return "{\"code\":\"SYSTEM_ERROR\",\"message\":\"" + message + "\"}";
}
}
}