获取回调数据

This commit is contained in:
wangw 2025-02-15 15:34:45 +08:00
parent cf63a0ea6f
commit c4bbce7258
1 changed files with 59 additions and 2 deletions

View File

@ -6,10 +6,18 @@ import com.czg.entity.CzgBaseRespParams;
import com.czg.order.service.OrderInfoService;
import com.czg.utils.AssertUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* @author ww
* @description
@ -25,12 +33,61 @@ public class NotifyController {
@RequestMapping("payCallBack")
public String notifyCallBack(CzgBaseRespParams respParams) {
public String notifyCallBack(HttpServletRequest request, @RequestBody CzgBaseRespParams respParams) throws IOException {
Map<String,String> map = getRequestParams(request);
log.info("1支付回调数据为:{}", map);
JSONObject czg = CzgPayUtils.getCzg(respParams);
AssertUtil.isNull(czg, "回调数据为空");
log.info("支付回调数据为:{}", czg);
log.info("2支付回调数据为:{}", czg);
orderInfoService.payCallBackOrder(czg.getString("mchOrderNo"), czg);
return SUCCESS;
}
/**
* HttpServletRequest 中获取参数包括查询参数和请求体参数
*
* @param request HttpServletRequest 对象
* @return 包含所有参数的 Map
* @throws IOException 如果读取请求体时发生错误
*/
public static Map<String, String> getRequestParams(HttpServletRequest request) throws IOException {
Map<String, String> params = new HashMap<>();
// 获取查询参数
Enumeration<String> paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = paramNames.nextElement();
String paramValue = request.getParameter(paramName);
params.put(paramName, paramValue);
}
log.info("param体请求参数为:{}", params);
// 如果是 POST 请求尝试读取请求体
if ("POST" .equalsIgnoreCase(request.getMethod())) {
String contentType = request.getContentType();
if (contentType != null && contentType.startsWith("application/json")) {
// 处理 JSON 格式的请求体
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
if (!sb.isEmpty()) {
// 这里只是简单将 JSON 字符串作为一个整体参数实际使用中可能需要解析 JSON
params.put("requestBody", sb.toString());
}
} else if (contentType != null && contentType.startsWith("application/x-www-form-urlencoded")) {
// 处理表单格式的请求体
Enumeration<String> bodyParamNames = request.getParameterNames();
while (bodyParamNames.hasMoreElements()) {
String paramName = bodyParamNames.nextElement();
String paramValue = request.getParameter(paramName);
params.put(paramName, paramValue);
}
}
}
return params;
}
}