package com.gkhy.exam.pay.utils;
|
|
import com.alibaba.fastjson2.JSONObject;
|
import org.apache.commons.io.FileUtils;
|
import org.dom4j.*;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.util.Map;
|
|
public class BillSign {
|
ISignCommond signCommond;
|
private static final String HEADER_TAG = "Header";
|
private static final String EINVOICE_TAG = "EInvoiceData";
|
|
|
public BillSign(ISignCommond signCommond) {
|
/* 28 */
|
this.signCommond = signCommond;
|
|
}
|
|
public BillSign(){}
|
|
|
public String signBill(byte[] bytes) throws Exception {
|
/* 41 */
|
return signBill(new String(bytes));
|
|
}
|
|
|
public 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);
|
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
|
}
|
|
|
public String signBill(String xml) throws Exception {
|
/* 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);
|
|
|
/* 92 */
|
addUnitSign(xmlDoc, signNode);
|
|
/* 94 */
|
return xmlDoc.asXML();
|
|
}
|
|
|
private Node genUnitSignNode(String plain) throws Exception {
|
/* 100 */
|
PayUtils payUtils = new PayUtils();
|
JSONObject jsonObject = payUtils.signFileV2(plain);
|
SignDto content = (SignDto) jsonObject.get("content");
|
|
/* 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(content.getSignatureAlgorithm());
|
/* 109 */
|
signedInfo.addElement("SignatureFormat").setText(content.getSignatureFormat());
|
|
/* 111 */
|
signature.addElement("SignatureTime").setText(content.getSignatureTime());
|
/* 112 */
|
signature.addElement("SignatureValue").setText(content.getSignatureValue());
|
|
/* 114 */
|
Element keyInfo = signature.addElement("KeyInfo");
|
/* 115 */
|
keyInfo.addElement("SerialNumber").setText(content.getSerialNumber());
|
/* 116 */
|
keyInfo.addElement("X509IssuerName").setText(content.getIssuerDn());
|
/* 117 */
|
return (Node) signature;
|
|
}
|
|
|
public 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 void addUnitSign(Document xmlDoc, Node signNode) {
|
/* 146 */
|
Element eInvoiceSignature = xmlDoc.getRootElement().addElement("EInvoiceSignature");
|
/* 147 */
|
eInvoiceSignature.add(signNode);
|
|
}
|
|
}
|