获取回调数据 终版
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user