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 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 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 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; } }