xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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;
    }
 
}