mitao
2024-06-18 fd97cde3d994a3aa6898ba862cd20d0c081012c3
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
package com.ruoyi.order.util;
 
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
 
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.util.*;
 
 
public class WxPayUtils {
    public static final String serial_no = "xxx";
    // 商户号
    public static final String mchId = "xxx";
    // appid
    public static final String appId = "xxx";
 
    // 证书私钥
    public static final String privateKey = "-----BEGIN PRIVATE KEY-----\n" +
            "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDBJn3bVt9BbLhs\n" +
            "+ZjULV7Odh4rtBBd2wc7uZ8RlN/9E8sJfAefowiSTPwOkTYKDlyhpG131a4VKj+N\n" +
            "zaTu0Y5zQGyl29ElHVVuPw4mVni7xyqVdJ5VUxyJMKHmQsGpngHS4sF28c5Aihfi\n" +
            "Gkydt6BRF8xwFC9u+D0r4PY9dwHTn4jyaFd25CMUxVO+rfjOWFuGa/rEjtQw03KU\n" +
            "UVR6np6FmNXdWalQbmbETyUqGjCac6J7hDZ1LIRU4YtqMqqsXhnk3FaoS23MOgaZ\n" +
            "RE3nIgpYqGgfXfLOKDt5n4zLALJkV0xmk3VErbheWpzRVFae/JMtsf/8ySgtiOga\n" +
            "n8fvShJNAgMBAAECggEAf+P+vbb9yJI2Y2G5QfRwrAAl5gYqOBsI5RD5NGkBDs+G\n" +
            "RtdrLNaEnGqBExwvTeVIjHcVTJ2d0MHSpxAdP0xeKA3mYsPz7cFIieESe2wSMTOl\n" +
            "DKTVGeYYJPagnJhjJu5KgtpGA34EdVN6kOmdBWlKq6c4ZJXY+n8/8cfZA8XC3d3B\n" +
            "favo104podp8cC2IVNMs21OzbjD4V+6VV9X/5BA6x7bDcarlJXSyCehdXgjhjNmT\n" +
            "/9gz7MFhOsUlAG7P03easIRcZCQL6z9MebD3xfVYcXnS4L3CcVw1FWEdLXxe/3IG\n" +
            "Q8UPTPHhTLFRbURHI00x1jEvcD9qKXYgKeU/N7IY+QKBgQD93r2SotKM5EDxM3L2\n" +
            "5UFrmEVnf4bwg5QDyZFpjAJ+/uMSsEX3f1m8Mst82Y4Mybz8XVMNsUJmoY8eJoM+\n" +
            "sqRbI5HXySsFEbDqh7jrtBoN7R1rl3jQw4qxqWYR8GQJ6VqAXuEFbrxgE1YFgqw5\n" +
            "40HAYbv417h0h5A1rcRKegoW9wKBgQDCxVaCmBzcYYFc2leRy6qH965OEedqbSCV\n" +
            "QPprCDBoBM6jrq97ZfWyfmLNSjjFhTSz9h8z5dQcePZrrI3+evlE8V18hUmWN6rM\n" +
            "CvPCb+4q8CwiHmLFqVp+m11MR8EOBcWCW/HlioqBKTLglijY53RUjW7No/T+32sf\n" +
            "5R1qYq672wKBgQC3dztFN4o169bK+UWCDBgFK9wsecsJEe3r9sWxo09Ce+2aWe2W\n" +
            "eWBeU88fARJZR4neT4tv/8Re6y7EuUxsCSoh+0iwy17doPVb6I3JOTUDD3MNiD/1\n" +
            "jvsyfZuYJ0QErbGLyAWSqX5VaGPoQ5E3nHauE3OG2E8jV7zuLhAHSr7z7QKBgAF8\n" +
            "Z/CPIIk95TLEJ67hEuf+p8HIuS9CreD1ofN3GIdyofD1wDj8yicWd8KBMnWvUnud\n" +
            "ARfwRPICqj6gDmVGoug3vzLYAXu36QGtg7aUDAkf0/ZerPo9FIeqv8d5NKvat2sL\n" +
            "MIlDyVK68bxs6NreyTBr89B108SuB68ynErXfeXJAoGBAMCoJyWdFAQKVvycHyv4\n" +
            "914+v7niQahws23wxHlzvup8/doCS1XVQAoEyQ4iIPDHYmiOCcmzCQnb1CWgMbIl\n" +
            "oBY3baP1GWOwEWw/HYKOwHW3fLkYC/xaRi+b5IxyEzR7hlCm/A6gCnCUYqIdU8gd\n" +
            "jYE4kGB2O/5vdbITdSMai47o\n" +
            "-----END PRIVATE KEY-----";
 
 
    */
/**
     * 获取微信预支付订单号
     * @return
     *//*
 
    public JSONObject getPrepayId(Integer apptype, String outTradeNo, String body1,String openId, Double price){
 
        JSONObject jsonObject = new JSONObject();
        JSONObject amountJsonObject = new JSONObject();
        JSONObject payerJsonObject = new JSONObject();
        amountJsonObject.put("total",price);
        payerJsonObject.put("openid",openId);
        // 应用ID
        jsonObject.put("appid",appId);
        // 商户号
        jsonObject.put("mchid",mchId);
        // 商品描述
        jsonObject.put("description",body1);
        // 商户订单号
        jsonObject.put("out_trade_no",outTradeNo);
        // 通知地址
        jsonObject.put("notify_url","https://zc.xxx.com/wx/api/pay/notifyUrl");
        // 订单金额信息
        jsonObject.put("amount",amountJsonObject);
        // 支付者信息
        jsonObject.put("payer",payerJsonObject);
        String body = jsonObject.toString();
        System.out.println(body);
        String authorization = signStr("POST", "/v3/pay/transactions/jsapi", body);
 
        String result = HttpRequest.post("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi")
                .body(body)
                .header("Content-Type","application/json")
                .header("Accept","application/json")
                .header("Authorization","WECHATPAY2-SHA256-RSA2048 " + authorization)//头信息,多个头信息多次调用此方法即可
                .timeout(10000)//超时,毫秒
                .execute().body();
        JSONObject jsonObject1 = JSONUtil.parseObj(result);
        String prepay_id = jsonObject1 == null ? "":(String) jsonObject1.get("prepay_id");
        JSONObject resultJsonObject = new JSONObject();
        resultJsonObject.put("prepay_id",prepay_id);
        resultJsonObject.put("chatPrice",wxOrder.getTotalPrice());
        return resultJsonObject;
    }
 
    */
/**
     * 成功获取微信预支付订单号后, 返回给前端调起微信支付的必要参数
     * @param orderNo   商户订单号
     * @param prepay_id 预支付订单号
     * @return
     *//*
 
    public Map<String, Object> returnToReceptionJson(String orderNo, String prepay_id){
        HashMap<String, Object> map = new HashMap<>();
        long timeStamp = System.currentTimeMillis() / 1000;
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String paySign = getPaySign(timeStamp, uuid, prepay_id);
        map.put("orderId", orderNo);
        map.put("timeStamp", timeStamp);
        map.put("nonceStr", uuid);
        map.put("package", "prepay_id=" + prepay_id);
        map.put("signType", "RSA");
        map.put("paySign", paySign);
        return map;
    }
 
    public String getPaySign(long timestamp, String randString, String prepay_id){
        String data =  appId + "\n" +
                timestamp + "\n" +
                randString + "\n" +
                "prepay_id=" + prepay_id + "\n";
        String signature = null;
        try {
            signature = sign(data.getBytes("utf-8"));
        } catch (SignatureException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return signature;
    }
 
    public String signStr2(String method, String url){
        String nonceStr = UUID.randomUUID().toString();
        long timestamp = System.currentTimeMillis() / 1000;
        String message = buildMessage2(method, url, timestamp, nonceStr);
        String signature = null;
        try {
            signature = sign(message.getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("签名后:[" + signature + "]");
        return  "mchid=\"" + mchId + "\","
                + "serial_no=\"" + serial_no + "\","
                + "nonce_str=\"" + nonceStr + "\","
                + "timestamp=\"" + timestamp + "\","
                + "signature=\"" + signature + "\"";
    }
 
    public String buildMessage2(String method, String url, long timestamp, String nonceStr) {
        String str = method + "\n"
                + url + "\n"
                + timestamp + "\n"
                + nonceStr + "\n\n";
        System.out.println("签名数据[" + str + "]");
        return str;
    }
 
    public String signStr(String method, String url, String body){
        String nonceStr = UUID.randomUUID().toString();
        long timestamp = System.currentTimeMillis() / 1000;
        String message = buildMessage(method, url, timestamp, nonceStr, body);
        System.out.println("message:[" + message + "]");
        String signature = null;
        try {
            signature = sign(message.getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("signature=[" + signature + "]");
        return  "mchid=\"" + mchId + "\","
                + "nonce_str=\"" + nonceStr + "\","
                + "timestamp=\"" + timestamp + "\","
                + "serial_no=\"" + serial_no + "\","
                + "signature=\"" + signature + "\"";
    }
 
    public String buildMessage(String method, String url, long timestamp, String nonceStr, String body) {
        String str = method + "\n"
                + url + "\n"
                + timestamp + "\n"
                + nonceStr + "\n"
                + body + "\n";
        return str;
    }
 
    public String sign(byte[] message) throws Exception {
        Signature sign = null;
        try {
            sign = Signature.getInstance("SHA256withRSA");
            PrivateKey privateKey = getPrivateKey();
            sign.initSign(privateKey);
            sign.update(message);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (SignatureException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Base64.getEncoder().encodeToString(sign.sign());
    }
    */
/**
     * 获取私钥。
     *//*
 
    public static PrivateKey getPrivateKey() throws IOException {
        PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(
                new ByteArrayInputStream(privateKey.getBytes("utf-8")));
        return merchantPrivateKey;
    }
 
    // xml解析
    public static Map doXMLParse(String strxml) throws JDOMException, IOException {
        strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");
        if(null == strxml || "".equals(strxml)) {
            return null;
        }
        Map m = new HashMap();
        InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(in);
        Element root = doc.getRootElement();
        List list = root.getChildren();
        Iterator it = list.iterator();
        while(it.hasNext()) {
            Element e = (Element) it.next();
            String k = e.getName();
            String v = "";
            List children = e.getChildren();
            if(children.isEmpty()) {
                v = e.getTextNormalize();
            } else {
                v = getChildrenText(children);
            }
            m.put(k, v);
        }
        //关闭流
        in.close();
        return m;
    }
 
    */
/**
     * 获取子结点的xml
     *
     * @param children
     * @return String
     *//*
 
    public static String getChildrenText(List children) {
        StringBuffer sb = new StringBuffer();
        if(!children.isEmpty()) {
            Iterator it = children.iterator();
            while(it.hasNext()) {
                Element e = (Element) it.next();
                String name = e.getName();
                String value = e.getTextNormalize();
                List list = e.getChildren();
                sb.append("<" + name + ">");
                if(!list.isEmpty()) {
                    sb.append(getChildrenText(list));
                }
                sb.append(value);
                sb.append("</" + name + ">");
            }
        }
        return sb.toString();
    }
}
*/