| | |
| | | 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; |
| | |
| | | @Value("${finance.queryUrl}") |
| | | private String queryUrl; |
| | | |
| | | @Value("${finance.uploadXmlUrl}") |
| | | private String uploadXmlUrl; |
| | | |
| | | /** |
| | | * 请求开票,发起支付 |
| | | * @param payReqData |
| | |
| | | HttpResultVo execute = httpClient.execute(httpPost, getResponseHandler()); |
| | | String stringContent = execute.getStringContent(); |
| | | ResultVo resultVo = JSONObject.parseObject(stringContent, ResultVo.class); |
| | | log.info("请求结果为:"+resultVo); |
| | | return resultVo; |
| | | } |
| | | |
| | |
| | | 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(); |
| | | } |
| | | |
| | | } |