From 6f8ece5da1b0cb8d247a18dcc444d3ed43aad2c4 Mon Sep 17 00:00:00 2001
From: “djh” <“3298565835@qq.com”>
Date: Tue, 25 Feb 2025 16:32:45 +0800
Subject: [PATCH] 煤矿缴费修改
---
exam-system/src/main/java/com/gkhy/exam/pay/utils/PayUtils.java | 407 +++++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 346 insertions(+), 61 deletions(-)
diff --git a/exam-system/src/main/java/com/gkhy/exam/pay/utils/PayUtils.java b/exam-system/src/main/java/com/gkhy/exam/pay/utils/PayUtils.java
index f96042f..1a707b2 100644
--- a/exam-system/src/main/java/com/gkhy/exam/pay/utils/PayUtils.java
+++ b/exam-system/src/main/java/com/gkhy/exam/pay/utils/PayUtils.java
@@ -1,11 +1,19 @@
package com.gkhy.exam.pay.utils;
+import cn.com.jit.mof.MOFClient;
+import cn.com.jit.mof.bean.MOFSignResult;
+import cn.com.jit.mof.bean.MOFVerifyResult;
import cn.com.jit.new_vstk.AdvanceSignClient;
import cn.com.jit.new_vstk.Bean.*;
-import cn.com.jit.new_vstk.SignClient;
import cn.com.jit.new_vstk.exception.NewCSSException;
+import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.gkhy.exam.pay.entity.PayReqData;
+import com.xjhys.edu.fee.sdk.client.BillClient;
+import com.xjhys.edu.fee.sdk.client.impl.ClientFactory;
+import com.xjhys.edu.fee.sdk.model.ResponseDataVo;
+import com.xjhys.edu.fee.sdk.model.SignatureFileVo;
+import com.xjhys.edu.fee.sdk.model.UploadXmlReponseDataVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
@@ -39,16 +47,15 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
+import sun.misc.BASE64Encoder;
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.io.*;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
+import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.*;
@@ -80,8 +87,12 @@
@Value("${finance.uploadXmlUrl}")
private String uploadXmlUrl;
+// @Value(("${finance.path}"))
+// private String path;
+
/**
* 请求开票,发起支付
+ *
* @param payReqData
* @return
* @throws IOException
@@ -89,23 +100,26 @@
public ResultVo sendApiPost(PayReqData payReqData) throws IOException {
Map<String, String> param = new HashMap<>();
+ String appid = "A1749891493E4CDDBFE4506357B1F0AB";
+ String orderUrl = "http://finpt.xjcz.gov.cn/fs-service/fs-pay/invoice.do";
- HttpPost httpPost = new HttpPost("http://finpt.xjcz.gov.cn/fs-service-test/fs-pay/invoice.do");
-
+ HttpPost httpPost = new HttpPost(orderUrl);
+ log.info("请求地址:" + orderUrl);
//请求参数转为json格式base64编码
String reqData = Base64.getEncoder().encodeToString(JSONObject.toJSONString(payReqData).getBytes());
- String mac = "ED76A5F1703540BE977D34780B371FEB" + "||" + reqData;
+ String mac = appid + "||" + reqData;
mac = DigestUtils.md5Hex(mac.getBytes());
- param.put("appid", "ED76A5F1703540BE977D34780B371FEB");
+ param.put("appid", appid);
+ log.info("请求appid:" + appid);
param.put("reqdata", reqData);
param.put("mac", mac);
httpPost.setEntity(assemblyFormEntity(param, "utf-8"));
- HttpClient httpClient = getHttpClient("http://finpt.xjcz.gov.cn/fs-service-test/fs-pay/invoice.do");
+ HttpClient httpClient = getHttpClient(orderUrl);
HttpResultVo execute = httpClient.execute(httpPost, getResponseHandler());
String stringContent = execute.getStringContent();
ResultVo resultVo = JSONObject.parseObject(stringContent, ResultVo.class);
- log.info("请求结果为:"+resultVo);
+ log.info("请求结果为:" + resultVo);
return resultVo;
}
@@ -297,7 +311,7 @@
String stringContent = execute.getStringContent();
ResultVo resultVo = JSONObject.parseObject(stringContent, ResultVo.class);
log.info("请求结果为:" + resultVo);
- if (resultVo.getRespcode().equals("BUS0000")) {
+ if (resultVo.getRespcode().equals(CaiZhengConstans.CAI_ZHENG_SUCCESS)) {
return "success";
}
return "fail";
@@ -332,21 +346,21 @@
Map<String, String> params = new HashMap<>();
JSONObject jsonObject = new JSONObject();
- jsonObject.put("billNo",billNO);
+ jsonObject.put("billNo", billNO);
String reqdata = Base64.getEncoder().encodeToString(jsonObject.toJSONString().getBytes());
- String mac = appId+"||"+reqdata;
+ String mac = appId + "||" + reqdata;
mac = DigestUtils.md5Hex(mac.getBytes());
- params.put("appid",appId);
- params.put("reqdata",reqdata);
- params.put("mac",mac);
+ params.put("appid", appId);
+ params.put("reqdata", reqdata);
+ params.put("mac", mac);
HttpPost httpPost = new HttpPost(queryUrl);
- httpPost.setEntity(assemblyFormEntity(params,"utf-8"));
+ 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);
+ log.info("请求结果转json后为:" + jsonObject1);
String result = (String) jsonObject1.get("reqdata");
return ResponseEntity.ok(result);
@@ -356,34 +370,276 @@
public ResultVo uploadXml(String orderId, String plain) throws IOException {
Map<String, String> params = new HashMap<>();
- Sign sign = new Sign();
- SignResult sign1 = this.sign(plain);
- log.info("签名结果为:"+ Arrays.toString(sign1.getSignData()));
- VerifyResult verify = this.verify(plain, new String(sign1.getSignData()));
- SignDto signDto = new SignDto("SM3", "Base64", new Date(), verify.getReturnData(), verify.getSerialNumber(), verify.getIssure());
- Element signature = sign.getSignature(signDto);
+// Sign sign = new Sign();
+// SignResult sign1 = this.sign(plain);
+// log.info("签名结果为:"+ Arrays.toString(sign1.getSignData()));
+// VerifyResult verify = this.verify(plain, sign1.getSignData());
+// SignDto signDto = new SignDto("SM3", "Base64", new Date(), verify.getReturnData(), verify.getSerialNumber(), verify.getIssure());
+// Element signature = sign.getSignature(signDto);
+
+ String appid = "A1749891493E4CDDBFE4506357B1F0AB";
+ String orderUrl = "http://finpt.xjcz.gov.cn/fs-service/fs-pay/uploadXml.do";
JSONObject jsonObject = new JSONObject();
- jsonObject.put("orderNo",orderId);
- jsonObject.put("fileData", Base64.getEncoder().encodeToString(convertElementToByteArray(signature)));
+ jsonObject.put("orderNo", orderId);
+ jsonObject.put("fileData", Base64.getEncoder().encodeToString(plain.getBytes()));
String reqdata = Base64.getEncoder().encodeToString(jsonObject.toJSONString().getBytes());
- String mac = appId+"||" +reqdata;
+ String mac = appid + "||" + reqdata;
mac = DigestUtils.md5Hex(mac.getBytes());
- params.put("appid",appId);
- params.put("reqdata",reqdata);
- params.put("mac",mac);
+ 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);
+ HttpPost httpPost = new HttpPost(orderUrl);
+ httpPost.setEntity(assemblyFormEntity(params, "utf-8"));
+ HttpClient httpClient = getHttpClient(orderUrl);
HttpResultVo execute = httpClient.execute(httpPost, getResponseHandler());
String stringContent = execute.getStringContent();
ResultVo resultVo = JSONObject.parseObject(stringContent, ResultVo.class);
- log.info("请求结果转为:"+resultVo);
+ log.info("请求结果转为:" + resultVo);
return resultVo;
}
+
+
+ public ResultVo faqiV2(PayReqData payReqData) {
+ System.out.println("发起接到参数===" + payReqData.toString());
+
+ String reqdatastr = JSONObject.toJSONString(payReqData);
+
+ String mac = this.getMD5(appId+"||"+ this.getBase64(payReqData));
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("appid", appId);
+ jsonObject.put("mac", mac);
+ jsonObject.put("reqdata", this.getBase64(payReqData));
+ Map<String, String> header = new HashMap();
+ header.put("Accept", "application/json;charset=utf-8");
+ header.put("Content-Type", "application/json;charset=utf-8");
+ String resultStr = HttpUtil.post(orderUrl, jsonObject);
+ System.out.println("发起入参===" + jsonObject);
+ JSONObject result = JSONObject.parseObject(resultStr);
+ ResultVo resultVo = JSONObject.parseObject(resultStr, ResultVo.class);
+ System.out.println("发起回参===" + result);
+ result.put("postData", reqdatastr);
+ JSONObject jsonObject2 = result.getJSONObject("respdata");
+ String signfile = jsonObject2.getString("fileData");
+ String orderNo1 = jsonObject2.getString("orderNo");
+ // /home/www/swspkmas/files/
+// String xmlFilePath = "/home/www/swspkmas/files/" + orderNo1 + ".xml";
+ String xmlFilePath = "F:\\text\\" + orderNo1 + ".xml";
+ generateBase64StringToFile(signfile, xmlFilePath);
+ String trr2 = "";
+
+ try {
+ File file = new File(xmlFilePath);
+ FileReader reader1 = new FileReader(file);
+ BufferedReader bReader = new BufferedReader(reader1);
+ StringBuilder sb = new StringBuilder();
+ String s = "";
+
+ while ((s = bReader.readLine()) != null) {
+ sb.append(s);
+ }
+
+ bReader.close();
+ trr2 = sb.toString();
+ } catch (Exception var34) {
+ Exception e = var34;
+ e.printStackTrace();
+ }
+
+ String jmsignfile = this.signFilejdnew(trr2);
+ String zuizhongpath = montageXmlV3(xmlFilePath, jmsignfile);
+ String zzsignfile = convertFileToBase64(zuizhongpath);
+ Boolean b = this.uploadXmlV2(orderNo1, zzsignfile);
+
+ File file = new File(xmlFilePath);
+ file.delete();
+ File file1 = new File(zuizhongpath);
+ file1.delete();
+ if (b) {
+ return resultVo;
+ } else {
+ resultVo.setRespcode("BUS8888");
+ }
+ return resultVo;
+ }
+
+ public static String montageXmlV3(String ywfilePath, String xmlStr) {
+ try {
+ String outputxmlpath = ywfilePath.replace(".xml", "_3.xml");
+ xmlStr = xmlStr.trim().replace("\n", "");
+ FileWriter writer = new FileWriter(outputxmlpath);
+ writer.write(xmlStr);
+ writer.close();
+ return outputxmlpath;
+ } catch (Exception var4) {
+ Exception e = var4;
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public static String convertFileToBase64(String imgPath) {
+ byte[] data = null;
+
+ try {
+ InputStream in = new FileInputStream(imgPath);
+ data = new byte[((InputStream) in).available()];
+ ((InputStream) in).read(data);
+ ((InputStream) in).close();
+ } catch (IOException var4) {
+ IOException e = var4;
+ e.printStackTrace();
+ }
+
+ BASE64Encoder encoder = new BASE64Encoder();
+ String base64Str = encoder.encode(data);
+ return base64Str;
+ }
+
+ public static boolean generateBase64StringToFile(String fileStr, String fileFilePath) {
+ if (fileStr == null) {
+ return false;
+ } else {
+ try {
+ byte[] b = org.apache.commons.codec.binary.Base64.decodeBase64(fileStr);
+
+ for (int i = 0; i < b.length; ++i) {
+ if (b[i] < 0) {
+ b[i] = (byte) (b[i] + 256);
+ }
+ }
+
+ OutputStream out = new FileOutputStream(fileFilePath);
+ ((OutputStream) out).write(b);
+ ((OutputStream) out).flush();
+ ((OutputStream) out).close();
+ return true;
+ } catch (Exception var4) {
+ return false;
+ }
+ }
+ }
+
+ public String signFilejdnew(String plain) {
+ BillSign sign = new BillSign(new ISignCommond() {
+ @Override
+ public SignDto signSm3Detach(String plain) {
+ SignDto dto = null;
+
+ try {
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("data", plain);
+ JSONObject Result = signFileV2(jsonObject);
+ dto = new SignDto(Result.getJSONObject("content").getDate("signTime"), Result.getJSONObject("content").getString("signResult"), "CN=Private Certificate Authority Of MOF SM2,O=MOF,C=CN", "142FA2FA197E349C");
+ } catch (Exception var5) {
+ Exception exception = var5;
+ exception.printStackTrace();
+ }
+
+ return dto;
+ }
+ });
+
+ try {
+ String xx = sign.signBill(plain);
+ return xx;
+ } catch (Exception var4) {
+ var4.printStackTrace();
+ return null;
+ }
+ }
+
+
+ public JSONObject signFileV2(JSONObject job) throws Exception {
+ JSONObject xysfResult = new JSONObject();
+ String certId = "11650000MB1957293J";
+ String plain = job.getString("data").trim();
+ MOFSignResult result = null;
+ Map<String, Object> mmp = new HashMap();
+
+ try {
+ String filePath = "F:\\text/cssconfig.properties";
+ System.out.println("配置文件路径:" + filePath);
+ MOFClient client = new MOFClient(filePath);
+ System.out.println("证书标识为:" + certId);
+ System.out.println("待签名数据:" + plain);
+ result = client.sign(certId, plain.getBytes(StandardCharsets.UTF_8));
+ System.out.println("签名返回结果:" + JSONObject.toJSONString(result));
+ byte[] signData = result.getSignData();
+ String base64Str = new String(cn.com.jit.ida.util.pki.encoders.Base64.encode(signData));
+ mmp.put("signResult", base64Str);
+ MOFVerifyResult resultVer = client.verifySign(signData, plain.getBytes(StandardCharsets.UTF_8));
+ System.out.println("验签结果为:" + JSONObject.toJSONString(resultVer));
+ mmp.put("issure", resultVer.getIssure());
+ mmp.put("sn", resultVer.getSn());
+ mmp.put("signTime", resultVer.getSignTime());
+ xysfResult.put("success", true);
+ xysfResult.put("content", mmp);
+ return xysfResult;
+ } catch (NewCSSException var12) {
+ NewCSSException e = var12;
+ System.out.println("****签名失败****");
+ System.out.println("错误号为:" + e.getCode());
+ System.out.println("错误描述为:" + e.getDescription());
+ xysfResult.put("success", false);
+ xysfResult.put("msg", "系统错误");
+ return xysfResult;
+ }
+ }
+
+ public Boolean uploadXmlV2(String orderNo, String signFile) {
+ log.info("上传验签开始");
+ log.info("上传验签参数订单号:" + orderNo + "签名文件:" + signFile);
+ ResponseDataVo<?> res = ClientFactory.sdkConfig(appId, "http://finpt.xjcz.gov.cn/fs-service", "http://finpt.xjcz.gov.cn/fs-service");
+ SignatureFileVo signatureFile = new SignatureFileVo();
+ signatureFile.setFileData(signFile);
+ signatureFile.setOrderNo(orderNo);
+ BillClient billClient = ClientFactory.getBillClient();
+ ResponseDataVo<UploadXmlReponseDataVo> dataRes = billClient.uploadXml(signatureFile);
+
+ if (!dataRes.getRespcode().equals(CaiZhengConstans.CAI_ZHENG_SUCCESS)) {
+ log.info("上传验签失败:" + dataRes);
+ return false;
+ }
+// String mac = this.getMD5("A1749891493E4CDDBFE4506357B1F0AB||" + this.getBase64(signatureFile.toString()));
+// JSONObject jsonObject = new JSONObject();
+// jsonObject.put("appid", "A1749891493E4CDDBFE4506357B1F0AB");
+// jsonObject.put("mac", mac);
+// jsonObject.put("reqdata", this.getBase64(signatureFile.toString()));
+ return true;
+ }
+
+ public String getMD5(String input) {
+ try {
+ MessageDigest md = MessageDigest.getInstance("MD5");
+ byte[] messageDigest = md.digest(input.getBytes());
+ StringBuilder hexString = new StringBuilder();
+ byte[] var5 = messageDigest;
+ int var6 = messageDigest.length;
+
+ for (int var7 = 0; var7 < var6; ++var7) {
+ byte b = var5[var7];
+ hexString.append(String.format("%02x", b));
+ }
+
+ return hexString.toString();
+ } catch (NoSuchAlgorithmException var9) {
+ NoSuchAlgorithmException e = var9;
+ throw new RuntimeException(e);
+ }
+ }
+
+ public String getBase64(PayReqData str) {
+// String encodedStr = org.apache.commons.codec.binary.Base64.encodeBase64String(str.getBytes());
+ String reqData = Base64.getEncoder().encodeToString(JSONObject.toJSONString(str).getBytes());
+
+ return reqData;
+
+ }
+
//获取XML文件字节码
public static byte[] convertElementToByteArray(Element element) {
@@ -412,17 +668,56 @@
}
+ public JSONObject signFileV2(String fileData) throws Exception {
+ JSONObject xysfResult = new JSONObject();
+ String certId = "11650000MB1957293J";
+ MOFSignResult result = null;
+// Map<String, Object> mmp = new HashMap<>();
+
+ try {
+// String filePath = "/home/www/swspkmas/cssconfig.properties";
+ String filePath = "F:\\text/cssconfig.properties";
+ System.out.println("配置文件路径:" + filePath);
+ MOFClient client = new MOFClient(filePath);
+ System.out.println("证书标识为:" + certId);
+ System.out.println("待签名数据:" + fileData);
+ result = client.sign(certId, fileData.getBytes(StandardCharsets.UTF_8));
+ System.out.println("签名返回结果:" + JSONObject.toJSONString(result));
+ byte[] signData = result.getSignData();
+ String base64Str = new String(Base64.getEncoder().encode(signData));
+// mmp.put("signResult", base64Str);
+ MOFVerifyResult resultVer = client.verifySign(signData, fileData.getBytes(StandardCharsets.UTF_8));
+ System.out.println("验签结果为:" + JSONObject.toJSONString(resultVer));
+// mmp.put("issure", resultVer.getIssure());
+// mmp.put("sn", resultVer.getSn());
+// mmp.put("signTime", resultVer.getSignTime());
+ SignDto signDto = new SignDto(resultVer.getSignTime(), base64Str, resultVer.getIssure(), resultVer.getSn());
+ xysfResult.put("success", Boolean.valueOf(true));
+ xysfResult.put("content", signDto);
+ return xysfResult;
+ } catch (NewCSSException e) {
+ System.out.println("****签名失败****");
+ System.out.println("错误号为:" + e.getCode());
+ System.out.println("错误描述为:" + e.getDescription());
+ xysfResult.put("success", Boolean.valueOf(false));
+ xysfResult.put("msg", "系统错误");
+ return xysfResult;
+ }
+ }
+
+
//签名
public SignResult sign(String strData) {
SignResult result = null;
- String certId = "rsa";
+ String certId = "11650000MB1957293J";
try {
- AdvanceSignClient client = new AdvanceSignClient("cssconfig.properties");
- result = client.sign(certId,strData.getBytes());
+ MOFClient client = new MOFClient("/home/www/swspkmas/cssconfig.properties");
+ result = client.sign(certId, strData.getBytes(StandardCharsets.UTF_8));
log.info("***签名成功***");
+ log.info("签名返回结果为:" + result);
byte[] signData = result.getSignData();
String s = new String(Base64.getEncoder().encode(signData));
- log.info("签名结果为:"+s);
+ log.info("签名结果为:" + s);
} catch (NewCSSException e) {
log.info("签名失败");
log.info("错误号为:" + e.getCode());
@@ -435,14 +730,12 @@
/**
* 验签名
*/
- public VerifyResult verify(String strData,String signData){
- VerifyResult verify = null;
+ public MOFVerifyResult verify(byte[] strData, String signData) {
+ MOFVerifyResult verify = null;
try {
- AdvanceSignClient client = new AdvanceSignClient("cssconfig.properties");
- byte[] plain = strData.getBytes();
- byte[] sign = signData.getBytes();
+ MOFClient client = new MOFClient("/home/www/swspkmas/cssconfig.properties");
- verify = client.verify(sign, plain);
+ verify = client.verifySign(strData, signData.getBytes(StandardCharsets.UTF_8));
log.info("验签成功");
log.info(" 证书subjectdn: " + verify.getSubjectdn());
log.info(" 证书issuer: " + verify.getIssure());
@@ -461,19 +754,18 @@
/**
* 制作数字信封
*/
- public EnvelopResult encryptEnvelop(String plain){
+ public EnvelopResult encryptEnvelop(byte[] plain) {
EnvelopResult envelopResult = null;
try {
- AdvanceSignClient client = new AdvanceSignClient("cssconfig.properties");
+ AdvanceSignClient client = new AdvanceSignClient("/home/www/swspkmas/cssconfig.properties");
String certId = "rsa";
CertIdParams certIdParams = new CertIdParams(certId);
//将封装好的加密证书参数对象放入 数组中
- CertParams[] envelopCerts = new CertIdParams[]{certIdParams};
- byte[] bytes = plain.getBytes();
+ CertParams[] envelopCerts = new CertIdParams[]{certIdParams};
/* 制作信封 */
- envelopResult = client.encryptEnvelop(envelopCerts, bytes);
+ envelopResult = client.encryptEnvelop(envelopCerts, plain);
byte[] envelopData = envelopResult.getEnvelopData();
- log.info("信封结果:["+envelopData+"]");
+ log.info("信封结果:[" + envelopData + "]");
} catch (NewCSSException e) {
throw new RuntimeException(e);
}
@@ -483,15 +775,15 @@
/**
* 解密数字信封
*/
- public DecryptResult decryptEnvelop(String signData){
+ public DecryptResult decryptEnvelop(String signData) {
DecryptResult result = null;
try {
- AdvanceSignClient client = new AdvanceSignClient("cssconfig.properties");
+ AdvanceSignClient client = new AdvanceSignClient("/home/www/swspkmas/cssconfig.properties");
byte[] enc_data = signData.getBytes();
result = client.decryptEnvelop(enc_data);
log.info("解密数字信封结果成功");
String plainData = new String(result.getPlainData());
- log.info("原文:"+plainData);
+ log.info("原文:" + plainData);
log.info(" 证书subjectdn: " + result.getSubjectdn());
log.info(" 证书issuer: " + result.getIssure());
log.info(" 证书SN: " + result.getSn());
@@ -505,13 +797,6 @@
}
return result;
}
-
-
-
-
-
-
-
}
--
Gitblit v1.9.2