tw intella

This commit is contained in:
ZZ 2022-06-15 10:47:12 +08:00
parent 6024588155
commit 08d85c37a1
4 changed files with 170 additions and 0 deletions

View File

@ -57,5 +57,18 @@
<version>5.7.9</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>co.intella</groupId>
<artifactId>intella-utility</artifactId>
<version>2.1.5</version>
</dependency>
</dependencies>
</project>

View File

@ -108,6 +108,12 @@
<artifactId>EvalEx</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.poi</groupId>-->
<!-- <artifactId>poi-ooxml</artifactId>-->

View File

@ -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<String, String> 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<String, String> dataMap = new HashMap();
ArrayList<String> ignoreList = new ArrayList<String>() {
{
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("--");
}
}

View File

@ -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<String, String> 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<String, String> 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());
}
}
}