|
@@ -0,0 +1,184 @@
|
|
|
+package com.zhongzheng.modules.sdk.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.lang.Validator;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.google.gson.GsonBuilder;
|
|
|
+import com.google.gson.JsonArray;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import com.zhongzheng.common.core.redis.RedisCache;
|
|
|
+import com.zhongzheng.common.exception.CustomException;
|
|
|
+import com.zhongzheng.common.utils.DateUtils;
|
|
|
+import com.zhongzheng.common.utils.bank.DCCryptor;
|
|
|
+import com.zhongzheng.common.utils.bank.DCHelper;
|
|
|
+import com.zhongzheng.modules.exam.bo.ExamApplySiteTimeTwoAddBo;
|
|
|
+import com.zhongzheng.modules.sdk.bo.TopBankBillAddBo;
|
|
|
+import com.zhongzheng.modules.sdk.service.ITopBankBillService;
|
|
|
+import com.zhongzheng.modules.sdk.service.IZsBankService;
|
|
|
+import com.zhongzheng.modules.sdk.service.NuonuoService;
|
|
|
+import nuonuo.open.sdk.NNOpenSDK;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ZsBankServiceImpl implements IZsBankService {
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(ZsBankServiceImpl.class);
|
|
|
+ @Value("${ZsBank.publickey}")
|
|
|
+ private String bankpubkey;
|
|
|
+ @Value("${ZsBank.privatekey}")
|
|
|
+ private String privkey;
|
|
|
+ @Value("${ZsBank.sm4key}")
|
|
|
+ private String sm4key;
|
|
|
+
|
|
|
+ private static Base64.Encoder encoder = Base64.getEncoder();
|
|
|
+ private static Base64.Decoder decoder = Base64.getDecoder();
|
|
|
+
|
|
|
+ private static final String ALG_SM = "SM"; // 采用国密算法
|
|
|
+
|
|
|
+ private static String UID = "N002463271"; // 测试的用户编号
|
|
|
+
|
|
|
+ private static String URL = "http://cdctest.cmburl.cn:80/cdcserver/api/v2"; // 银行服务地址(测试)
|
|
|
+ // private static String bankpubkey = "BNsIe9U0x8IeSe4h/dxUzVEz9pie0hDSfMRINRXc7s1UIXfkExnYECF4QqJ2SnHxLv3z/99gsfDQrQ6dzN5lZj0="; // 银行公钥
|
|
|
+
|
|
|
+ // private static String privkey = "NBtl7WnuUtA2v5FaebEkU0/Jj1IodLGT6lQqwkzmd2E=";
|
|
|
+ // private static String sm4key = "VuAzSWQhsoNqzn0K";//"1234567890123456"; // 用户的对称密钥
|
|
|
+
|
|
|
+ private static String FunCode = "trsQryByBreakPoint"; // 测试的用户编号
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ITopBankBillService iTopBankBillService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean searchBreakPoint(String queryAcctNbr,String cardNbr,String beginDate,String endDate) {
|
|
|
+ // 组织发送报文
|
|
|
+ JsonObject obj = new JsonObject();
|
|
|
+ JsonObject req = new JsonObject();
|
|
|
+ JsonObject body = new JsonObject();
|
|
|
+ JsonObject head = new JsonObject();
|
|
|
+ head.addProperty("funcode", FunCode); //DCLISMOD
|
|
|
+ head.addProperty("userid", UID);
|
|
|
+ head.addProperty("reqid", DCHelper.getTime() + "0000001");
|
|
|
+ Integer transactionSequence = 1;
|
|
|
+
|
|
|
+ JsonArray array = new JsonArray();
|
|
|
+ JsonObject item = new JsonObject();
|
|
|
+ item.addProperty("cardNbr", cardNbr);
|
|
|
+ item.addProperty("beginDate", beginDate);
|
|
|
+ item.addProperty("endDate", endDate);
|
|
|
+ // item.addProperty("transactionSequence", transactionSequence);
|
|
|
+ if(Validator.isNotEmpty(queryAcctNbr)){
|
|
|
+ item.addProperty("queryAcctNbr", queryAcctNbr);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ array.add(item);
|
|
|
+ body.add("TRANSQUERYBYBREAKPOINT_X1", array);
|
|
|
+
|
|
|
+ req.add("head", head);
|
|
|
+ req.add("body", body);
|
|
|
+ obj.add("request", req);
|
|
|
+
|
|
|
+ // 请求发送接收
|
|
|
+ String result = doProcess(obj,FunCode);
|
|
|
+ JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
|
|
|
+ JSONObject resultObject = jsonObject.getJSONObject("response").getJSONObject("body");
|
|
|
+ JSONArray z1 = resultObject.getJSONArray("TRANSQUERYBYBREAKPOINT_Z1");
|
|
|
+ if(resultObject.containsKey("TRANSQUERYBYBREAKPOINT_Z2")){
|
|
|
+ JSONArray z2 = resultObject.getJSONArray("TRANSQUERYBYBREAKPOINT_Z2");
|
|
|
+ List<TopBankBillAddBo> list = JSONArray.parseArray(z2.toJSONString(), TopBankBillAddBo.class);
|
|
|
+ int i = 0;
|
|
|
+ for(TopBankBillAddBo addBo : list){
|
|
|
+ addBo.setResult(z2.getJSONObject(i).toJSONString());
|
|
|
+ iTopBankBillService.insertByAddBo(addBo);
|
|
|
+ }
|
|
|
+ System.out.println(list);
|
|
|
+ }
|
|
|
+ if(z1.size()>0){
|
|
|
+ JSONObject fObject = z1.getJSONObject(0);
|
|
|
+ String ctnFlag =(String) fObject.get("ctnFlag");
|
|
|
+ if("Y".equals(ctnFlag)){
|
|
|
+ String queryAcctNbrNext =(String) fObject.get("queryAcctNbr");
|
|
|
+ searchBreakPoint(queryAcctNbrNext,cardNbr,beginDate,endDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String doProcess(JsonObject jObject,String funCode) {
|
|
|
+ try {
|
|
|
+ JsonObject object = new JsonObject();
|
|
|
+ // 签名
|
|
|
+ object.addProperty("sigdat", "__signature_sigdat__");
|
|
|
+ object.addProperty("sigtim", DCHelper.getTime());
|
|
|
+ jObject.add("signature", object);
|
|
|
+ String source = DCHelper.serialJsonOrdered(jObject);
|
|
|
+ // System.out.println("签名原文: " + source);
|
|
|
+ // System.out.println("签名原文A: " + decoder.decode(privkey));
|
|
|
+ byte[] signature1 = DCCryptor.CMBSM2SignWithSM3(getID_IV(), decoder.decode(privkey), source.getBytes(StandardCharsets.UTF_8));
|
|
|
+ String sigdat1 = new String(encoder.encode(signature1));
|
|
|
+ // System.out.println("签名结果: " + sigdat1);
|
|
|
+ object.addProperty("sigdat", sigdat1);
|
|
|
+
|
|
|
+ // SM4-CBC加密
|
|
|
+ String plaintxt = jObject.toString();
|
|
|
+ // System.out.println("加密前req: " + plaintxt);
|
|
|
+ byte[] enInput = DCCryptor.CMBSM4EncryptWithCBC(sm4key.getBytes(), getID_IV(), plaintxt.getBytes(StandardCharsets.UTF_8));
|
|
|
+
|
|
|
+ String req = new String(encoder.encode(enInput));
|
|
|
+ // System.out.println("加密后req: " + req);
|
|
|
+
|
|
|
+ // 发送请求
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
+ map.put("UID", UID);
|
|
|
+ map.put("ALG", ALG_SM);
|
|
|
+ map.put("DATA", URLEncoder.encode(req, "utf-8"));
|
|
|
+ map.put("FUNCODE", funCode);
|
|
|
+ String res = DCHelper.doPostForm(URL, map);
|
|
|
+ // System.out.println("res: " + res);
|
|
|
+ try {
|
|
|
+ decoder.decode(res);
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println("访问返回错误.");
|
|
|
+ throw new CustomException("网银解码错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解密请求
|
|
|
+ String resplain = new String(DCCryptor.CMBSM4DecryptWithCBC(sm4key.getBytes(), getID_IV(), decoder.decode(res)), StandardCharsets.UTF_8);
|
|
|
+ // System.out.println("res decrypt: " + resplain);
|
|
|
+
|
|
|
+ // 验签
|
|
|
+ JsonObject object2 = new GsonBuilder().create().fromJson(resplain, JsonObject.class);
|
|
|
+ JsonObject object3 = object2.getAsJsonObject("signature");
|
|
|
+ String resSign = object3.get("sigdat").getAsString();
|
|
|
+ object3.addProperty("sigdat", "__signature_sigdat__");
|
|
|
+ object2.add("signature", object3);
|
|
|
+ String resSignSource = DCHelper.serialJsonOrdered(object2);
|
|
|
+ System.out.println("验签原文: " + resSignSource);
|
|
|
+ boolean verify = DCCryptor.CMBSM2VerifyWithSM3(getID_IV(), decoder.decode(bankpubkey), resSignSource.getBytes(StandardCharsets.UTF_8), decoder.decode(resSign));
|
|
|
+ // System.out.println("验签结果: " + verify);
|
|
|
+ if(verify){
|
|
|
+ return resSignSource;
|
|
|
+ }else{
|
|
|
+ throw new CustomException("网银验签错误");
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new CustomException("网银错误"+e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static byte[] getID_IV() {
|
|
|
+ String uid = UID; // 请替换为实际的用户UID
|
|
|
+ String userid = uid + "0000000000000000";
|
|
|
+ return userid.substring(0, 16).getBytes();
|
|
|
+ }
|
|
|
+}
|