教育训练处考试制证系统后端
“djh”
2025-02-05 cff3c43f216c6f01f675e4821832e15f2a77c57c
exam-system/src/main/java/com/gkhy/exam/pay/utils/PayUtils.java
@@ -29,14 +29,21 @@
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;
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;
@@ -63,12 +70,19 @@
    @Value("${finance.apiId}")
    private String appId;
    public ResultVo sendApiPost(PayReqData payReqData) throws IOException {
    @Value("${finance.queryUrl}")
    private String queryUrl;
        //正式
        String proUrl = "http://finpt.xjcz.gov.cn/fs-service/fs-pay/invoice.do";
        //测试
        String testUrl = "http://finpt.xjcz.gov.cn/fs-service-test/fs-pay/invoice.do";
    @Value("${finance.uploadXmlUrl}")
    private String uploadXmlUrl;
    /**
     * 请求开票,发起支付
     * @param payReqData
     * @return
     * @throws IOException
     */
    public ResultVo sendApiPost(PayReqData payReqData) throws IOException {
        Map<String, String> param = new HashMap<>();
@@ -87,9 +101,11 @@
        HttpResultVo execute = httpClient.execute(httpPost, getResponseHandler());
        String stringContent = execute.getStringContent();
        ResultVo resultVo = JSONObject.parseObject(stringContent, ResultVo.class);
        log.info("请求结果为:"+resultVo);
        return resultVo;
    }
    //组装请求体
    private static UrlEncodedFormEntity assemblyFormEntity(Map<String, String> parameters, String charset) {
        List<NameValuePair> formParameters = assemblyParameter(parameters);
        UrlEncodedFormEntity formEntity = null;
@@ -125,6 +141,7 @@
    }
    //在调用SSL之前需要重写验证方法,取消检测SSL 创建ConnectionManager,添加Connection配置信息
    private static HttpClient sslClient() {
        try {
            // 在调用SSL之前需要重写验证方法,取消检测SSL
@@ -167,6 +184,7 @@
    }
    //获取响应结果处理器,将响应结果封装为HttpResult对象
    private static ResponseHandler<HttpResultVo> getResponseHandler() {
        ResponseHandler<HttpResultVo> responseHandler = new ResponseHandler<HttpResultVo>() {
            @Override
@@ -207,6 +225,7 @@
        return responseHandler;
    }
    //判断相应内容是否为文本类型
    private static boolean isTextType(ContentType contentType) {
        if (contentType == null) {
            throw new RuntimeException("ContentType is null");
@@ -282,9 +301,6 @@
    //缴费结果查询
    public JSONObject query(String orderNo) throws IOException {
        String proUrl = "http://finpt.xjcz.gov.cn/fs-service/fs-pay/query.do";
        String testUrl = "http://finpt.xjcz.gov.cn/fs-service-test/fs-pay/query.do";
        HashMap<String, String> param = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
@@ -305,4 +321,86 @@
        log.info("请求结果json为:" + result);
        return result;
    }
    //票据查询
    public ResponseEntity<String> findBill(String billNO) throws IOException {
        Map<String, String> params = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("billNo",billNO);
        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(queryUrl);
        httpPost.setEntity(assemblyFormEntity(params,"utf-8"));
        HttpClient httpClient = getHttpClient(queryUrl);
        HttpResultVo execute = httpClient.execute(httpPost, getResponseHandler());
        String stringContent = execute.getStringContent();
        JSONObject jsonObject1 = JSONObject.parseObject(stringContent);
        log.info("请求结果转json后为:"+jsonObject1);
        String result = (String) jsonObject1.get("reqdata");
        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();
    }
}