教育训练处考试制证系统后端
heheng
2025-02-21 5b8607464905c4a585fb8e859985730e1ee61f40
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.gkhy.exam.pay.utils;
 
import cn.com.jit.mof.MOFClient;
import cn.com.jit.mof.bean.MOFSignResult;
import cn.com.jit.mof.bean.MOFVerifyResult;
import cn.com.jit.new_vstk.exception.NewCSSException;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.gkhy.exam.pay.entity.PayReqData;
import com.xjhys.edu.fee.sdk.client.impl.ClientFactory;
import com.xjhys.edu.fee.sdk.model.ResponseDataVo;
import com.xjhys.edu.fee.sdk.model.SignatureFileVo;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Encoder;
 
import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
 
public class DemoUtils {
 
 
    public JSONObject faqiV2(JSONObject res) {
        System.out.println("发起接到参数===" + res.toJSONString());
        String certNo = res.getString("certNo");
        String payerType = res.getString("payerType");
        String payUser = res.getString("payUser");
        String checker = res.getString("checker");
 
        String enterCode = res.getString("enterCode");
        String busCode = res.getString("busCode");
        BigDecimal price = res.getBigDecimal("price");
 
        String handlingPerson = res.getString("handlingPerson");
        String orderDesc = res.getString("orderDesc");
        String invoiceSocialCode = res.getString("invoiceSocialCode");
        int amount = res.getIntValue("amount");
        String orderNo = "dadadad";
        Double money = Double.valueOf(amount * price.doubleValue());
 
        String reqdatastr = "{\"desc\": \"" + orderDesc + "\",\"payerType\": \"" + payerType + "\",\"checker\": \"" + checker + "\",\"amount\": 2,\"orderNo\": \"" + orderNo + "\",\"feeDatas\": [{\"amount\": " + amount + ",\"price\": " + (amount * price.doubleValue()) + ",\"busCode\": \"" + busCode + "\"}],\"money\": " + money + ",\"certNo\": \"" + certNo + "\",\"handlingPerson\": \"" + handlingPerson + "\",\"invoiceSocialCode\": \"" + invoiceSocialCode + "\",\"enterCode\": \"" + enterCode + "\",\"payerName\": \"" + payUser + "\"}";
 
 
        String mac = getMD5("A1749891493E4CDDBFE4506357B1F0AB||" + getBase64(reqdatastr));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appid", "A1749891493E4CDDBFE4506357B1F0AB");
        jsonObject.put("mac", mac);
        jsonObject.put("reqdata", getBase64(reqdatastr));
        Map<String, String> header = new HashMap<>();
        header.put("Accept", "application/json;charset=utf-8");
        header.put("Content-Type", "application/json;charset=utf-8");
        String resultStr = HttpUtil.post("http://finpt.xjcz.gov.cn/fs-service/fs-pay/invoice.do ", (Map) jsonObject);
        System.out.println("发起入参===" + jsonObject);
        JSONObject result = JSONObject.parseObject(resultStr);
        System.out.println("发起回参===" + result);
        result.put("postData", reqdatastr);
 
        JSONObject jsonObject2 = result.getJSONObject("respdata");
 
        String signfile = jsonObject2.getString("fileData");
        String orderNo1 = jsonObject2.getString("orderNo");
        String xmlFilePath = "F:/nginx-1.15.8/files/payH5/" + orderNo1 + ".xml";
        generateBase64StringToFile(signfile, xmlFilePath);
        String trr2 = "";
        try {
            File file2 = new File(xmlFilePath);
            FileReader reader1 = new FileReader(file2);
            BufferedReader bReader = new BufferedReader(reader1);
            StringBuilder sb = new StringBuilder();
            String s = "";
            while ((s = bReader.readLine()) != null) {
                sb.append(s);
            }
            bReader.close();
            trr2 = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        String jmsignfile = signFilejdnew(trr2, null);
        String zuizhongpath = montageXmlV3(xmlFilePath, jmsignfile);
        String zzsignfile = convertFileToBase64(zuizhongpath);
 
        uploadXml(orderNo1, zzsignfile);
        File file = new File(xmlFilePath);
        file.delete();
        File file1 = new File(zuizhongpath);
        file1.delete();
        return result;
    }
 
 
    public void faqiV3(PayReqData payReqData) throws Exception {
 
        PayUtils payUtils = new PayUtils();
        ResultVo resultVo = payUtils.sendApiPost(payReqData);
        String fileData = resultVo.getRespdata().getFileData();
        String orderNo = resultVo.getRespdata().getOrderNo();
 
        String xmlFilePath = "D:/files/" + orderNo + ".xml";
        generateBase64StringToFile(fileData, xmlFilePath);
        String trr2 = "";
        try {
            File file2 = new File(xmlFilePath);
            FileReader reader1 = new FileReader(file2);
            BufferedReader bReader = new BufferedReader(reader1);
            StringBuilder sb = new StringBuilder();
            String s = "";
            while ((s = bReader.readLine()) != null) {
                sb.append(s);
            }
            bReader.close();
            trr2 = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = signFileV2(fileData);
        String jmsignfile = signFilejdnew(trr2, jsonObject);
        String zuizhongpath = montageXmlV3(xmlFilePath, jmsignfile);
        String zzsignfile = convertFileToBase64(zuizhongpath);
 
        uploadXml(orderNo, zzsignfile);
        File file = new File(xmlFilePath);
        file.delete();
        File file1 = new File(zuizhongpath);
        file1.delete();
 
    }
 
    public String getMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                hexString.append(String.format("%02x", new Object[]{Byte.valueOf(b)}));
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
 
    public String getBase64(String str) {
        String encodedStr = Base64.encodeBase64String(str.getBytes());
        return encodedStr;
    }
 
 
    public static boolean generateBase64StringToFile(String fileStr, String fileFilePath) {
        if (fileStr == null) {
            return false;
        }
 
        try {
            byte[] b = Base64.decodeBase64(fileStr);
            for (int i = 0; i < b.length; i++) {
 
                if (b[i] < 0) {
                    b[i] = (byte) (b[i] + 256);
                }
            }
 
            OutputStream out = new FileOutputStream(fileFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
 
            return false;
        }
    }
 
    public String signFilejdnew(String plain, JSONObject job) {
 
        try {
            String s = BillSignUtils.signBill(plain, job);
            return s;
        } catch (BillSignException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
 
    public String uploadXml(String orderNo, String signFile) {
        ResponseDataVo<?> res = ClientFactory.sdkConfig("A1749891493E4CDDBFE4506357B1F0AB", "http://finpt.xjcz.gov.cn/fs-service", "http://finpt.xjcz.gov.cn/fs-service");
        SignatureFileVo signatureFile = new SignatureFileVo();
        signatureFile.setFileData(signFile);
        signatureFile.setOrderNo(orderNo);
//        BillClient billClient = ClientFactory.getBillClient();
//        ResponseDataVo<UploadXmlReponseDataVo> response = billClient.uploadXml(signatureFile);
//
//        String mac = getMD5("A1749891493E4CDDBFE4506357B1F0AB||" + getBase64(signatureFile.toString()));
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("appid", "A1749891493E4CDDBFE4506357B1F0AB");
//        jsonObject.put("mac", mac);
//        jsonObject.put("reqdata", getBase64(signatureFile.toString()));
        return null;
    }
 
    public JSONObject signFileV2(String plain) throws Exception {
        JSONObject xysfResult = new JSONObject();
        String certId = "11650000MB1957293J";
 
        MOFSignResult result = null;
        Map<String, Object> mmp = new HashMap<>();
 
        try {
            String filePath = "F:/cssconfig.properties";
            System.out.println("配置文件路径:" + filePath);
 
            MOFClient client = new MOFClient(filePath);
            System.out.println("证书标识为:" + certId);
            System.out.println("待签名数据:" + plain);
 
 
            result = client.sign(certId, plain.getBytes(StandardCharsets.UTF_8));
            /* 323 */
            System.out.println("签名返回结果:" + JSONObject.toJSONString(result));
 
            /* 325 */
            byte[] signData = result.getSignData();
 
            /* 327 */
            String base64Str = new String(Base64.decodeBase64(signData));
            /* 328 */
            mmp.put("signResult", base64Str);
 
            /* 330 */
            MOFVerifyResult resultVer = client.verifySign(signData, plain.getBytes(StandardCharsets.UTF_8));
            /* 331 */
            System.out.println("验签结果为:" + JSONObject.toJSONString(resultVer));
 
            /* 333 */
            mmp.put("issure", resultVer.getIssure());
            /* 334 */
            mmp.put("sn", resultVer.getSn());
            /* 335 */
            mmp.put("signTime", resultVer.getSignTime());
 
            /* 337 */
            xysfResult.put("success", Boolean.valueOf(true));
            /* 338 */
            xysfResult.put("content", mmp);
            /* 339 */
            return xysfResult;
        }
        /* 341 */ catch (NewCSSException e) {
            /* 342 */
            System.out.println("****签名失败****");
            /* 343 */
            System.out.println("错误号为:" + e.getCode());
            /* 344 */
            System.out.println("错误描述为:" + e.getDescription());
            /* 345 */
            xysfResult.put("success", Boolean.valueOf(false));
            /* 346 */
            xysfResult.put("msg", "系统错误");
            /* 347 */
            return xysfResult;
        }
    }
 
 
    public static String montageXmlV3(String ywfilePath, String xmlStr) {
        try {
            /* 358 */
            String outputxmlpath = ywfilePath.replace(".xml", "_3.xml");
 
            /* 360 */
            xmlStr = xmlStr.trim().replace("\n", "");
            /* 361 */
            FileWriter writer = new FileWriter(outputxmlpath);
            /* 362 */
            writer.write(xmlStr);
            /* 363 */
            writer.close();
            /* 364 */
            return outputxmlpath;
        }
        /* 366 */ catch (Exception e) {
            /* 367 */
            e.printStackTrace();
 
            /* 369 */
            return null;
        }
    }
 
    public static String convertFileToBase64(String imgPath) {
        /* 374 */
        byte[] data = null;
 
        try {
            /* 377 */
            InputStream in = new FileInputStream(imgPath);
            /* 378 */
            data = new byte[in.available()];
            /* 379 */
            in.read(data);
            /* 380 */
            in.close();
            /* 381 */
        } catch (IOException e) {
            /* 382 */
            e.printStackTrace();
        }
 
        /* 385 */
        BASE64Encoder encoder = new BASE64Encoder();
        /* 386 */
        String base64Str = encoder.encode(data);
        /* 387 */
        return base64Str;
    }
 
}