|
@@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.json.JSONArray;
|
|
import cn.hutool.json.JSONArray;
|
|
|
import cn.hutool.json.JSONObject;
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
+import com.github.microservice.core.helper.JsonHelper;
|
|
|
import com.github.microservice.core.util.net.apache.HttpClientUtil;
|
|
import com.github.microservice.core.util.net.apache.HttpClientUtil;
|
|
|
import com.github.microservice.core.util.net.apache.HttpModel;
|
|
import com.github.microservice.core.util.net.apache.HttpModel;
|
|
|
import com.github.microservice.core.util.net.apache.MethodType;
|
|
import com.github.microservice.core.util.net.apache.MethodType;
|
|
@@ -50,6 +51,9 @@ public class UnionRequestService extends SuperService {
|
|
|
|
|
|
|
|
private static final List<String> exitKeys = List.of("signType", "sign", "payKey");
|
|
private static final List<String> exitKeys = List.of("signType", "sign", "payKey");
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ JsonHelper jsonHelper;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 银联请求
|
|
* 银联请求
|
|
|
*
|
|
*
|
|
@@ -71,20 +75,8 @@ public class UnionRequestService extends SuperService {
|
|
|
ApiConfParam apiConfParam = new ApiConfParam();
|
|
ApiConfParam apiConfParam = new ApiConfParam();
|
|
|
apiConfParam.setApiName(url);
|
|
apiConfParam.setApiName(url);
|
|
|
apiConfParam.setMethodType(MethodType.Json);
|
|
apiConfParam.setMethodType(MethodType.Json);
|
|
|
-
|
|
|
|
|
-// data = "A";
|
|
|
|
|
-// JSONObject jsonObject = new JSONObject();
|
|
|
|
|
-// jsonObject.set("requestTimestamp", "2024-08-30 14:54:41");
|
|
|
|
|
-// data = jsonObject.toString();
|
|
|
|
|
-// log.info("data: {}", data);
|
|
|
|
|
-
|
|
|
|
|
-// String sign = sign(JSONUtil.parseObj(JSONUtil.toJsonStr(data)));
|
|
|
|
|
- String str = JSONUtil.toJsonStr(data);
|
|
|
|
|
- log.info("str: {}", str);
|
|
|
|
|
- String sign = AesUtils.signData(str);
|
|
|
|
|
- log.info("sign: {}", sign);
|
|
|
|
|
-// url += "?requestTimestamp=2024-08-30 14:54:41";
|
|
|
|
|
-
|
|
|
|
|
|
|
+ String dataJsonStr = JSONUtil.toJsonStr(data);
|
|
|
|
|
+ String sign = AesUtils.signData(dataJsonStr);
|
|
|
// 请求时间记时
|
|
// 请求时间记时
|
|
|
StopWatch stopWatch = new StopWatch();
|
|
StopWatch stopWatch = new StopWatch();
|
|
|
stopWatch.start();
|
|
stopWatch.start();
|
|
@@ -94,30 +86,100 @@ public class UnionRequestService extends SuperService {
|
|
|
Map<String, Object> header = new HashMap<>();
|
|
Map<String, Object> header = new HashMap<>();
|
|
|
String c = String.format("%s%s%s%s", UnionPaymentConfig.appId, timestamp, nonce, sign);
|
|
String c = String.format("%s%s%s%s", UnionPaymentConfig.appId, timestamp, nonce, sign);
|
|
|
|
|
|
|
|
- log.info("appId: {}", UnionPaymentConfig.appId);
|
|
|
|
|
- log.info("timestamp: {}", timestamp);
|
|
|
|
|
- log.info("nonce: {}", nonce);
|
|
|
|
|
- log.info("appKey: {}", UnionPaymentConfig.appKey);
|
|
|
|
|
-
|
|
|
|
|
String signature = AesUtils.signMacSHA256(c, UnionPaymentConfig.appKey);
|
|
String signature = AesUtils.signMacSHA256(c, UnionPaymentConfig.appKey);
|
|
|
- log.info("signature: {}", signature);
|
|
|
|
|
-
|
|
|
|
|
- String sig = "OPEN-BODY-SIG AppId=" + "\"" + UnionPaymentConfig.appId + "\""
|
|
|
|
|
|
|
+ String authorization = "OPEN-BODY-SIG AppId=" + "\"" + UnionPaymentConfig.appId + "\""
|
|
|
+ ",Timestamp=" + "\"" + timestamp + "\""
|
|
+ ",Timestamp=" + "\"" + timestamp + "\""
|
|
|
+ ",Nonce=" + "\"" + nonce + "\""
|
|
+ ",Nonce=" + "\"" + nonce + "\""
|
|
|
+ ",Signature=" + "\"" + signature + "\"";
|
|
+ ",Signature=" + "\"" + signature + "\"";
|
|
|
- log.info("sig: {}", sig);
|
|
|
|
|
|
|
+ header.put("authorization", authorization);
|
|
|
|
|
+ String response = "";
|
|
|
|
|
+ PrintWriter out = null;
|
|
|
|
|
+ BufferedReader in = null;
|
|
|
|
|
+ int code = 400;
|
|
|
|
|
+ try {
|
|
|
|
|
+ URL realUrl = new URL(url);
|
|
|
|
|
+ URLConnection conn = realUrl.openConnection();
|
|
|
|
|
+ HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;
|
|
|
|
|
+ httpUrlConnection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
|
+ httpUrlConnection.setRequestProperty("authorization", authorization);
|
|
|
|
|
+ httpUrlConnection.setDoOutput(true);
|
|
|
|
|
+ httpUrlConnection.setDoInput(true);
|
|
|
|
|
+ out = new PrintWriter(httpUrlConnection.getOutputStream());
|
|
|
|
|
+ out.write(dataJsonStr);
|
|
|
|
|
+ out.flush();
|
|
|
|
|
+ httpUrlConnection.connect();
|
|
|
|
|
+ code = httpUrlConnection.getResponseCode();
|
|
|
|
|
+ in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
|
|
|
|
|
+ String line;
|
|
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
|
|
+ response += line;
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("response: {}", response);
|
|
|
|
|
+ responseModel = jsonHelper.toObject(response, tClass);
|
|
|
|
|
+ responseModel.setCode(code);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (out != null) {
|
|
|
|
|
+ out.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (in != null) {
|
|
|
|
|
+ in.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("response: {} {}", responseModel.getErrCode(), code);
|
|
|
|
|
+ responseModel.setCode(code);
|
|
|
|
|
+ stopWatch.stop();
|
|
|
|
|
+ responseModel.setMillis(stopWatch.getTotalTimeMillis());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ responseModel.setFailed(String.format("请求出错:%s", e.getMessage()));
|
|
|
|
|
+ return responseModel;
|
|
|
|
|
+ }
|
|
|
|
|
+ param.setMillis(responseModel.getMillis());
|
|
|
|
|
+ param.setIsSuccess(responseModel.isSuccess);
|
|
|
|
|
+ param.setMsg(responseModel.getMsg());
|
|
|
|
|
+ param.setResponse(responseModel);
|
|
|
|
|
+ param.setRequestType(RequestType.UnionFrictionlessPay);
|
|
|
|
|
+ // 请求日志
|
|
|
|
|
+ requestInfoFeignService.saveRequestInfo(param);
|
|
|
|
|
+ return responseModel;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
- header.put("authorization", sig);
|
|
|
|
|
- header.put("Content-Type", "application/json");
|
|
|
|
|
- header.put("Accept_Charset", "UTF-8");
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 通用请求
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param url
|
|
|
|
|
+ * @param data
|
|
|
|
|
+ * @param tClass
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public SuperResponseModel requestAPI(String url,
|
|
|
|
|
+ Object data,
|
|
|
|
|
+ Class<? extends SuperResponseModel> tClass) {
|
|
|
|
|
+ RequestInfoSaveParam param = new RequestInfoSaveParam();
|
|
|
|
|
+ param.setParam(data);
|
|
|
|
|
+ param.setApiParam(url);
|
|
|
|
|
+ param.setUserId(getCurrentUserId());
|
|
|
|
|
+ SuperResponseModel responseModel = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ responseModel = tClass.newInstance();
|
|
|
|
|
+ ApiConfParam apiConfParam = new ApiConfParam();
|
|
|
|
|
+ apiConfParam.setApiName(url);
|
|
|
|
|
+ apiConfParam.setMethodType(MethodType.Json);
|
|
|
|
|
+
|
|
|
|
|
+ // 请求时间记时
|
|
|
|
|
+ StopWatch stopWatch = new StopWatch();
|
|
|
|
|
+ stopWatch.start();
|
|
|
|
|
|
|
|
- log.info("url: {}", url);
|
|
|
|
|
HttpModel httpModel = HttpModel.builder()
|
|
HttpModel httpModel = HttpModel.builder()
|
|
|
.url(url).method(apiConfParam.getMethodType())
|
|
.url(url).method(apiConfParam.getMethodType())
|
|
|
.charset("utf-8").body(data).build();
|
|
.charset("utf-8").body(data).build();
|
|
|
-
|
|
|
|
|
- httpModel.setHeader(header);
|
|
|
|
|
ResponseModel request = HttpClientUtil.request(httpModel);
|
|
ResponseModel request = HttpClientUtil.request(httpModel);
|
|
|
int code = request.getCode();
|
|
int code = request.getCode();
|
|
|
if (code == 200) {
|
|
if (code == 200) {
|
|
@@ -145,12 +207,14 @@ public class UnionRequestService extends SuperService {
|
|
|
return responseModel;
|
|
return responseModel;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
|
public String signParam(UnionSuperParam param) {
|
|
public String signParam(UnionSuperParam param) {
|
|
|
JSONObject entries = JSONUtil.parseObj(param);
|
|
JSONObject entries = JSONUtil.parseObj(param);
|
|
|
return signByJSONObject(entries);
|
|
return signByJSONObject(entries);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
|
public String signByJSONObject(JSONObject entries) {
|
|
public String signByJSONObject(JSONObject entries) {
|
|
|
TreeMap<String, Object> map = new TreeMap<>();
|
|
TreeMap<String, Object> map = new TreeMap<>();
|