教育训练处考试制证系统后端
“djh”
2025-02-05 cff3c43f216c6f01f675e4821832e15f2a77c57c
exam-system/src/main/java/com/gkhy/exam/pay/utils/PayUtils.java
@@ -29,6 +29,9 @@
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
@@ -36,8 +39,11 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
@@ -67,6 +73,9 @@
    @Value("${finance.queryUrl}")
    private String queryUrl;
    @Value("${finance.uploadXmlUrl}")
    private String uploadXmlUrl;
    /**
     * 请求开票,发起支付
     * @param payReqData
@@ -92,6 +101,7 @@
        HttpResultVo execute = httpClient.execute(httpPost, getResponseHandler());
        String stringContent = execute.getStringContent();
        ResultVo resultVo = JSONObject.parseObject(stringContent, ResultVo.class);
        log.info("请求结果为:"+resultVo);
        return resultVo;
    }
@@ -337,4 +347,60 @@
        return ResponseEntity.ok(result);
    }
    //上传财政电子票据签名文件
    public ResultVo uploadXml(String orderId, String plain) throws IOException {
        Map<String, String> params = new HashMap<>();
        Sign sign = new Sign();
        Element signature = sign.getSignature(plain);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderNo",orderId);
        jsonObject.put("fileData", Base64.getEncoder().encodeToString(convertElementToByteArray(signature)));
        String reqdata = Base64.getEncoder().encodeToString(jsonObject.toJSONString().getBytes());
        String mac = appId+"||" +reqdata;
        mac = DigestUtils.md5Hex(mac.getBytes());
        params.put("appid",appId);
        params.put("reqdata",reqdata);
        params.put("mac",mac);
        HttpPost httpPost = new HttpPost(uploadXmlUrl);
        httpPost.setEntity(assemblyFormEntity(params,"utf-8"));
        HttpClient httpClient = getHttpClient(uploadXmlUrl);
        HttpResultVo execute = httpClient.execute(httpPost, getResponseHandler());
        String stringContent = execute.getStringContent();
        ResultVo resultVo = JSONObject.parseObject(stringContent, ResultVo.class);
        log.info("请求结果转为:"+resultVo);
        return resultVo;
    }
    //获取XML文件字节码
    public static byte[] convertElementToByteArray(Element element) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            // 设置输出格式
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8"); // 设置编码为UTF-8
            // 创建 XMLWriter
            XMLWriter writer = new XMLWriter(new OutputStreamWriter(byteArrayOutputStream, StandardCharsets.UTF_8), format);
            // 写入 Element 对象
            writer.write(element.getDocument());
            writer.close(); // 关闭 writer,确保所有数据都被写入
        } catch (IOException e) {
            e.printStackTrace(); // 打印异常信息
            // 处理异常,比如返回一个空的字节数组或重新抛出异常
            return new byte[0];
        }
        // 返回字节数组
        return byteArrayOutputStream.toByteArray();
    }
}