获取回调数据

This commit is contained in:
2025-02-15 15:39:44 +08:00
parent c4bbce7258
commit 4895beb15d

View File

@@ -34,11 +34,11 @@ public class NotifyController {
@RequestMapping("payCallBack")
public String notifyCallBack(HttpServletRequest request, @RequestBody CzgBaseRespParams respParams) throws IOException {
JSONObject czg = CzgPayUtils.getCzg(respParams);
log.info("2支付回调数据为:{}", czg);
Map<String,String> map = getRequestParams(request);
log.info("1支付回调数据为:{}", map);
JSONObject czg = CzgPayUtils.getCzg(respParams);
AssertUtil.isNull(czg, "回调数据为空");
log.info("2支付回调数据为:{}", czg);
orderInfoService.payCallBackOrder(czg.getString("mchOrderNo"), czg);
return SUCCESS;
}
@@ -53,20 +53,9 @@ public class NotifyController {
*/
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())) {
if ("POST".equalsIgnoreCase(request.getMethod())) {
String contentType = request.getContentType();
if (contentType != null && contentType.startsWith("application/json")) {
// 处理 JSON 格式的请求体
if (contentType != null) {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
String line;
@@ -74,17 +63,23 @@ public class NotifyController {
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);
if (contentType.startsWith("application/json")) {
// 处理 JSON 格式的请求体
params.put("requestBody", sb.toString());
} else if (contentType.startsWith("application/x-www-form-urlencoded")) {
// 处理表单格式的请求体
String[] pairs = sb.toString().split("&");
for (String pair : pairs) {
String[] keyValue = pair.split("=", 2);
if (keyValue.length == 2) {
String paramName = keyValue[0];
String paramValue = keyValue[1];
params.put(paramName, paramValue);
}
}
}
}
}
}