获取回调数据 终版

This commit is contained in:
wangw 2025-02-15 15:45:06 +08:00
parent 4895beb15d
commit 1f4b33394a
2 changed files with 4 additions and 77 deletions

View File

@ -6,18 +6,11 @@ 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
@ -33,56 +26,11 @@ public class NotifyController {
@RequestMapping("payCallBack")
public String notifyCallBack(HttpServletRequest request, @RequestBody CzgBaseRespParams respParams) throws IOException {
public String notifyCallBack(@RequestBody CzgBaseRespParams respParams){
JSONObject czg = CzgPayUtils.getCzg(respParams);
log.info("2支付回调数据为:{}", czg);
Map<String,String> map = getRequestParams(request);
log.info("1支付回调数据为:{}", map);
AssertUtil.isNull(czg, "回调数据为空");
log.info("支付回调数据为:{}", 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<>();
if ("POST".equalsIgnoreCase(request.getMethod())) {
String contentType = request.getContentType();
if (contentType != null) {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
if (!sb.isEmpty()) {
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);
}
}
}
}
}
}
return params;
}
}

View File

@ -12,7 +12,6 @@ import com.czg.entity.CzgBaseRespParams;
import com.czg.entity.req.*;
import com.czg.entity.resp.*;
import com.czg.enums.CzgPayEnum;
import com.czg.resp.CzgRespCode;
import com.czg.resp.CzgResult;
import com.czg.utils.AssertUtil;
import com.czg.utils.MD5Util;
@ -133,28 +132,8 @@ public class CzgPayUtils {
/**
* 回调数据处理 返回任意实体
*
* @param dataJsonStr 带解析数据
* @param clazz 返回的实体类
* 回调数据处理
*/
public static <T> T getCzg(String dataJsonStr, Class<T> clazz) {
AssertUtil.isBlank(dataJsonStr, "超掌柜交易 回调数据为空");
CzgBaseRespParams respParams = JSONObject.parseObject(dataJsonStr, CzgBaseRespParams.class);
log.info("超掌柜交易请求响应,{}", respParams);
if (!"000000".equals(respParams.getCode())) {
log.error("超掌柜回调响应失败,{}", respParams);
return null;
}
if (StrUtil.isNotBlank(respParams.getSign())) {
if (validateSign(respParams.getSign(), respParams.getBizData())) {
log.error("超掌柜回调 验签失败,{}", respParams);
}
}
return JSONObject.parseObject(respParams.getBizData(), clazz);
}
public static JSONObject getCzg(CzgBaseRespParams respParams) {
AssertUtil.isNull(respParams, "超掌柜交易 回调数据为空");
log.info("超掌柜交易请求响应,{}", respParams);
@ -164,7 +143,7 @@ public class CzgPayUtils {
}
if (StrUtil.isNotBlank(respParams.getSign())) {
if (validateSign(respParams.getSign(), respParams.getBizData())) {
log.error("超掌柜回调 验签失败,{}", respParams);
log.error("超掌柜回调 验签失败");
}
}
return JSONObject.parse(respParams.getBizData());