From 33d850692237ff59bc07710d14817cbbac2f975b Mon Sep 17 00:00:00 2001 From: heheng <heheng@123456> Date: 星期二, 18 二月 2025 16:45:18 +0800 Subject: [PATCH] merge --- exam-system/src/main/java/com/gkhy/exam/pay/utils/BillSignUtils.java | 137 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 137 insertions(+), 0 deletions(-) diff --git a/exam-system/src/main/java/com/gkhy/exam/pay/utils/BillSignUtils.java b/exam-system/src/main/java/com/gkhy/exam/pay/utils/BillSignUtils.java new file mode 100644 index 0000000..a8b9c9c --- /dev/null +++ b/exam-system/src/main/java/com/gkhy/exam/pay/utils/BillSignUtils.java @@ -0,0 +1,137 @@ +package com.gkhy.exam.pay.utils; + +import com.alibaba.fastjson2.JSONObject; +import org.dom4j.*; + +public class BillSignUtils { + + private static final String HEADER_TAG = "Header"; + private static final String EINVOICE_TAG = "EInvoiceData"; + + +// public static String signBill(byte[] bytes) throws BillSignException { +// /* 41 */ +// return signBill(new String(bytes)); +// +// } +// +// +// public static String signBill(File file) throws BillSignException { +// /* 54 */ +// if (!file.exists()) { +// /* 55 */ +// throw new BillSignException("文件不存在。文件名称" + file.getAbsolutePath()); +// +// } +// +// +// try { +// /* 59 */ +// byte[] bytes = FileUtils.readFileToByteArray(file); +// /* 60 */ +// return signBill(new String(bytes)); +// /* 61 */ +// } catch (IOException e) { +// /* 62 */ +// throw new BillSignException("文件读取失败。文件名称" + file.getAbsolutePath(), e); +// +// } +// +// } + + + public static String signBill(String xml, JSONObject data) throws BillSignException { + /* 78 */ + Document xmlDoc = null; + + try { + /* 80 */ + xmlDoc = DocumentHelper.parseText(xml); + /* 81 */ + } catch (DocumentException e) { + /* 82 */ + throw new BillSignException("解析票据文件失败。", e); + + } + + + /* 86 */ + String plain = readRefSignDto(xmlDoc); + + + /* 89 */ + Node signNode = genUnitSignNode(plain, data); + + + /* 92 */ + addUnitSign(xmlDoc, signNode); + + /* 94 */ + return xmlDoc.asXML(); + + } + + + private static Node genUnitSignNode(String plain, JSONObject data) { + /* 100 */ + SignDto signDto = new SignDto(data.getDate("signTime"), data.getString("signResult"), data.getString("issure"), data.getString("sn")); + + /* 102 */ + Document document = DocumentHelper.createDocument(); + /* 103 */ + Element signature = document.addElement("Signature"); + /* 104 */ + signature.addAttribute("id", "InvoicingParty"); + + /* 106 */ + Element signedInfo = signature.addElement("SignedInfo"); + /* 107 */ + signedInfo.addElement("Reference").addAttribute("URI", "/EInvoice/Header|/EInvoice/EInvoiceData"); + /* 108 */ + signedInfo.addElement("SignatureAlgorithm").setText(signDto.getSignatureAlgorithm()); + /* 109 */ + signedInfo.addElement("SignatureFormat").setText(signDto.getSignatureFormat()); + + /* 111 */ + signature.addElement("SignatureTime").setText(signDto.getSignatureTime()); + /* 112 */ + signature.addElement("SignatureValue").setText(signDto.getSignatureValue()); + + /* 114 */ + Element keyInfo = signature.addElement("KeyInfo"); + /* 115 */ + keyInfo.addElement("SerialNumber").setText(signDto.getSerialNumber()); + /* 116 */ + keyInfo.addElement("X509IssuerName").setText(signDto.getIssuerDn()); + /* 117 */ + return (Node) signature; + + } + + + private static String readRefSignDto(Document xmlDoc) throws BillSignException { + /* 130 */ + Element root = xmlDoc.getRootElement(); + /* 131 */ + if (root.element("Header") == null || root.element("EInvoiceData") == null) { + /* 132 */ + throw new BillSignException("票据文件格式不正确"); + + } + /* 134 */ + return root.element("Header").asXML() + root.element("EInvoiceData").asXML(); + + } + + + private static void addUnitSign(Document xmlDoc, Node signNode) { + /* 146 */ + Element eInvoiceSignature = xmlDoc.getRootElement().addElement("EInvoiceSignature"); + /* 147 */ + eInvoiceSignature.add(signNode); + + } + +} + + -- Gitblit v1.9.2