diff --git a/xhpc-modules/xhpc-common/pom.xml b/xhpc-modules/xhpc-common/pom.xml index a6442b59..36682a4f 100644 --- a/xhpc-modules/xhpc-common/pom.xml +++ b/xhpc-modules/xhpc-common/pom.xml @@ -57,5 +57,18 @@ 5.7.9 compile + + + + com.google.code.gson + gson + 2.9.0 + + + co.intella + intella-utility + 2.1.5 + + diff --git a/xhpc-modules/xhpc-order/pom.xml b/xhpc-modules/xhpc-order/pom.xml index d5157f70..03ba629f 100644 --- a/xhpc-modules/xhpc-order/pom.xml +++ b/xhpc-modules/xhpc-order/pom.xml @@ -108,6 +108,12 @@ EvalEx 2.7 + + org.testng + testng + RELEASE + compile + diff --git a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/intella/IntellaTest.java b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/intella/IntellaTest.java new file mode 100644 index 00000000..610b7ae0 --- /dev/null +++ b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/intella/IntellaTest.java @@ -0,0 +1,109 @@ +package com.xhpc.order.intella; + +import cn.hutool.core.date.DateUtil; +import co.intella.crypto.AesCryptoUtil; +import co.intella.crypto.KeyReader; +import co.intella.model.IntegratedRequestBody; +import co.intella.model.RequestHeader; +import com.google.gson.Gson; +import com.xhpc.order.intella.Utility; +import org.testng.annotations.Test; + +import javax.crypto.SecretKey; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; +import java.util.*; + +import static co.intella.net.HttpRequestUtil.getEncryptRequestJson; + + +public class IntellaTest { + + final static int publicKeyResId = 1;//R.raw.stage_pub; + final static String ServiceType = "Payment";//credit card "OLPay";//消費者主掃 + final static String API_URL = "https://a.intella.co/allpaypass/api/general"; // for test server + final static String Method = "20000";//信用卡 + final static String MchId = "SLGtest"; // your MchId (login account) + final static String RefundKey = "13b7994fae9387c2e1b598524ba1204ae404d02fa67016ed86c74183ab1aabcd"; // SHA256 encoded + final static String DeviceId = "01300123"; // for EasyCard API + + public static String SHA256(String data, String key) throws NoSuchAlgorithmException { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] encodedhash = digest.digest(data.getBytes(StandardCharsets.UTF_8)); + return bytesToHex(encodedhash); + } + + private static String bytesToHex(byte[] hash) { + StringBuilder hexString = new StringBuilder(2 * hash.length); + for (int i = 0; i < hash.length; i++) { + String hex = Integer.toHexString(0xff & hash[i]); + if(hex.length() == 1) { + hexString.append('0'); + } + hexString.append(hex); + } + return hexString.toString(); + } + + @Test + public void test() throws Exception { + + File der = new File("C:\\intella\\pub.der"); + InputStream is = new FileInputStream(der); + Map rm = new HashMap<>(); + rm.put("MchId", MchId); + rm.put("Method", Method); + rm.put("ServiceType", ServiceType); + String TradeKey = SHA256("0000", "8651731586517315"); // SHA256 encoded + rm.put("TradeKey", TradeKey.toLowerCase(Locale.ROOT)); + Date date = Calendar.getInstance().getTime(); + String format = DateUtil.format(date, "yyyyMMddHHmmss"); + rm.put("CreateTime", format); + rm.put("DeviceInfo","80836000050001"); + rm.put("StoreOrderNo","80836000050001022205251204372344"); + rm.put("Body","ChargingFee"); + rm.put("TotalFee","1"); + rm.put("CardId","4477578739678291"); + rm.put("ExtenNo","879"); + rm.put("ExpireDate","2703"); +// rm.put("Data", "{\"DeviceInfo\":\"skb0001\",\"StoreOrderNo\":\"PO-20180715-001\",\"Body\":\"Chicken Rice\",\"TotalFee\":\"1\"}");//消費者主掃 + PublicKey rsaPubKey = KeyReader.loadPublicKeyFromDER(der); + SecretKey aesKey = AesCryptoUtil.generateSecreteKey(); + System.out.println(getEncryptRequestJson(rm, rsaPubKey, aesKey)); + String rawJson = (new Gson()).toJson(rm); + RequestHeader header = (RequestHeader)(new Gson()).fromJson(rawJson, RequestHeader.class); + Map dataMap = new HashMap(); + ArrayList ignoreList = new ArrayList() { + { + this.add("Method"); + this.add("ServiceType"); + this.add("MchId"); + this.add("CreateTime"); + this.add("TradeKey"); + } + }; + Iterator var10 = rm.keySet().iterator(); + + String key; + while(var10.hasNext()) { + key = (String)var10.next(); + if (!ignoreList.contains(key)) { + dataMap.put(key, rm.get(key)); + } + } + IntegratedRequestBody body = new IntegratedRequestBody(); + body.setRequestData((new Gson()).toJson(dataMap)); + body.setRequestHeader(header); + String json = (new Gson()).toJson(body); + System.out.println(json); + System.out.println("--"); + String resp = Utility.doRequest(API_URL, is, rm); + System.out.println(resp); + System.out.println("--"); + } +} diff --git a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/intella/Utility.java b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/intella/Utility.java new file mode 100644 index 00000000..1c9d246e --- /dev/null +++ b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/intella/Utility.java @@ -0,0 +1,42 @@ +package com.xhpc.order.intella; + +import java.io.InputStream; +import java.security.PublicKey; +import java.util.Map; + +import javax.crypto.SecretKey; + +import co.intella.crypto.AesCryptoUtil; +import co.intella.crypto.KeyReader; +import co.intella.net.Constant; +import co.intella.net.HttpRequestUtil; + +public class Utility { + + public static String doRequest(String url, InputStream rsaPubKeyStream, Map requestMap) { + + try { + PublicKey rsaPubKey = KeyReader.loadPublicKeyFromDER(rsaPubKeyStream); + SecretKey aesKey = AesCryptoUtil.generateSecreteKey(); + return doPost(url, rsaPubKey, aesKey, requestMap); + + } catch (Exception e) { + + e.printStackTrace(); + return String.format("{\"Header\": {\"StatusCode\": \"9000\",\"StatusDesc\": \"%s\"}}", e.getMessage()); + } + } + + private static String doPost(String url, PublicKey rsaPubKey, SecretKey aesKey, Map requestMap) { + + try { + String _response = HttpRequestUtil.httpsPost(url, requestMap, rsaPubKey, aesKey); + return AesCryptoUtil.decryptResponse(aesKey, Constant.IV, _response); + + } catch (Exception e) { + e.printStackTrace(); + return String.format("{\"Header\": {\"StatusCode\": \"9000\",\"StatusDesc\": \"%s\"}}", e.getMessage()); + } + } + +}