package com.nuvole.util.pay.allinPay.yunstNew;
|
|
import cn.hutool.crypto.SecureUtil;
|
|
import cn.hutool.http.HttpUtil;
|
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
import com.alibaba.fastjson.JSON;
|
import com.nuvole.util.pay.allinPay.yunstNew.po.ResultVo;
|
import com.nuvole.util.pay.allinPay.yunstNew.po.YstPayUrl;
|
|
|
import java.util.*;
|
|
/**
|
* Created by chenZhimin
|
* Date:2020/8/24
|
* time:10:09
|
*/
|
public class ApiPost {
|
|
public static ResultVo apiPost(String url, Object obj) {
|
System.out.println(url);
|
System.out.println(getSignBody(obj));
|
ResultVo result=new ResultVo();
|
try {
|
JSONObject data= JSONUtil.xmlToJson(HttpUtil.post(url,getSignBody(obj)));
|
result=JSON.parseObject(JSON.toJSONString(data.get("ResultVo")),ResultVo.class);
|
System.out.println("wwwwwwwwwwwwww"+result);
|
} catch (Exception e) {
|
System.out.println("wwwwwwwwwwwwww"+e);
|
result.setCode("0");
|
result.setMsg("接口请求失败");
|
result.setData("");
|
}
|
return result;
|
}
|
|
/**
|
* 组装带签名数据
|
* @param body
|
* @return
|
*/
|
|
public static String getSignBody(Object body) {
|
Map map= JSON.parseObject(JSON.toJSONString(body));
|
String version=YstPayUrl.version;
|
String terMerchantId= YstPayUrl.terMerchantId;
|
map.put("version",version);
|
map.put("terMerchantId",terMerchantId);
|
String orgSign=createLinkStringByGet(map)+"&key="+YstPayUrl.ked;
|
System.out.println("orgSign--------"+orgSign);
|
String sign=SecureUtil.md5(orgSign);
|
map.put("sign",sign.toUpperCase());
|
return JSON.toJSONString(map);
|
}
|
|
/**
|
* 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
|
* @param params 需要排序并参与字符拼接的参数组
|
* @return 拼接后字符串
|
* @throws UnsupportedEncodingExceptio
|
*/
|
|
public static String createLinkStringByGet(Map<String, String> params) {
|
List<String> keys = new ArrayList<String>(params.keySet());
|
Collections.sort(keys);
|
String prestr = "";
|
for (int i = 0; i < keys.size(); i++) {
|
String key = keys.get(i);
|
Object value = params.get(key);
|
if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符
|
prestr = prestr + key + "=" + value;
|
} else {
|
prestr = prestr + key + "=" + value + "&";
|
}
|
}
|
return prestr;
|
}
|
|
}
|