package com.nuvole.util;
|
|
import cn.hutool.crypto.SecureUtil;
|
import jakarta.servlet.http.HttpServletRequest;
|
import org.dom4j.Document;
|
import org.dom4j.DocumentException;
|
import org.dom4j.DocumentHelper;
|
import org.dom4j.Element;
|
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.lang.reflect.Field;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.TreeMap;
|
|
/**
|
* @author ChenLong
|
* @version 1.0
|
* @ClassName PayUtil
|
* @date 2019/7/30 21:51
|
*/
|
public class PayUtil {
|
|
public static Map getSignMap(Object f, String appKey) {
|
TreeMap<String, String> map = new TreeMap<String,String>();
|
Field[] fields = f.getClass().getDeclaredFields();
|
for (int i = 0, len = fields.length; i < len; i++) {
|
String varName = fields[i].getName();
|
try {
|
boolean accessFlag = fields[i].isAccessible();
|
//fields[i].setAccessible(true);
|
Object o = fields[i].get(f);
|
if (!"0".equals(String.valueOf(o)) && !"null".equals(String.valueOf(o)) && !String.valueOf(o).isEmpty()) {
|
map.put(varName, String.valueOf(o));
|
}
|
System.out.println("传入的对象中包含一个如下的变量:" + varName + " = " + o);
|
//fields[i].setAccessible(accessFlag);
|
} catch (IllegalArgumentException ex) {
|
ex.printStackTrace();
|
} catch (IllegalAccessException ex) {
|
ex.printStackTrace();
|
}
|
}
|
map.remove("serialVersionUID");
|
if (map.containsKey("validtime") && map.get("validtime").equals("null")) {
|
map.remove("validtime");
|
}
|
map.put("sign", sign(map, appKey));
|
return map;
|
}
|
|
public static String sign(TreeMap<String, String> params, String appkey) {
|
if (params.containsKey("sign"))// 签名明文组装不包含sign字段
|
params.remove("sign");
|
params.put("key", appkey);
|
StringBuilder sb = new StringBuilder(512);
|
for (Map.Entry<String, String> entry : params.entrySet()) {
|
if (entry.getValue() != null && entry.getValue().length() > 0) {
|
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
|
}
|
}
|
if (sb.length() > 0) {
|
sb.deleteCharAt(sb.length() - 1);
|
}
|
String sign = SecureUtil.md5(sb.toString());
|
params.remove("key");
|
return sign;
|
}
|
public static Map<String,String> notify(HttpServletRequest request){
|
InputStream inStream = null;
|
ByteArrayOutputStream outSteam=null;
|
Map<String, String> params =null;
|
try {
|
inStream = request.getInputStream();
|
outSteam = new ByteArrayOutputStream();
|
byte[] buffer = new byte[1024];
|
int len = 0;
|
while ((len = inStream.read(buffer)) != -1) {
|
outSteam.write(buffer, 0, len);
|
}
|
String resultxml = new String(outSteam.toByteArray(), "utf-8");
|
params = xmlStrToMap(resultxml);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}finally {
|
try {
|
outSteam.close();
|
inStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return params;
|
}
|
public static String getNotifyParam(HttpServletRequest request) {
|
InputStream inStream = null;
|
ByteArrayOutputStream outSteam = null;
|
String resultStr = null;
|
try {
|
inStream = request.getInputStream();
|
outSteam = new ByteArrayOutputStream();
|
byte[] buffer = new byte[1024];
|
int len = 0;
|
while ((len = inStream.read(buffer)) != -1) {
|
outSteam.write(buffer, 0, len);
|
}
|
resultStr = new String(outSteam.toByteArray(), "utf-8");
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
outSteam.close();
|
inStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return resultStr;
|
}
|
|
/**
|
* Xml字符串转换为Map
|
*
|
* @param xmlStr
|
* @return
|
*/
|
public static Map<String, String> xmlStrToMap(String xmlStr) {
|
Map<String, String> map = new HashMap<String, String>();
|
Document doc;
|
try {
|
doc = DocumentHelper.parseText(xmlStr);
|
Element root = doc.getRootElement();
|
List children = root.elements();
|
if (children != null && children.size() > 0) {
|
for (int i = 0; i < children.size(); i++) {
|
Element child = (Element) children.get(i);
|
map.put(child.getName(), child.getTextTrim());
|
}
|
}
|
} catch (DocumentException e) {
|
e.printStackTrace();
|
}
|
return map;
|
}
|
|
}
|