From 021a5b6fa04c3a67d982db3b1733af1e8222b8e7 Mon Sep 17 00:00:00 2001 From: panshuling321 Date: Thu, 16 Jun 2022 15:25:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95=E9=80=80?= =?UTF-8?q?=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xhpc/order/intella/IntellaTest.java | 92 ++++++++++++++++--- 1 file changed, 81 insertions(+), 11 deletions(-) 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 index 610b7ae0..4576578d 100644 --- 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 @@ -6,7 +6,6 @@ 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; @@ -17,7 +16,13 @@ import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; -import java.util.*; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Locale; +import java.util.Map; import static co.intella.net.HttpRequestUtil.getEncryptRequestJson; @@ -31,8 +36,10 @@ public class IntellaTest { final static String MchId = "SLGtest"; // your MchId (login account) final static String RefundKey = "13b7994fae9387c2e1b598524ba1204ae404d02fa67016ed86c74183ab1aabcd"; // SHA256 encoded final static String DeviceId = "01300123"; // for EasyCard API + final static String DeviceInfo = "skb0001"; - public static String SHA256(String data, String key) throws NoSuchAlgorithmException { + + public static String SHA256(String data) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] encodedhash = digest.digest(data.getBytes(StandardCharsets.UTF_8)); return bytesToHex(encodedhash); @@ -50,24 +57,28 @@ public class IntellaTest { return hexString.toString(); } - @Test - public void test() throws Exception { - File der = new File("C:\\intella\\pub.der"); + final static String orderNo = "20222061515350342327"; + final static String fee = "1"; + + @Test + public void paymentTest() throws Exception { + + File der = new File("D:\\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)); + String TradeKey = SHA256("0000").toLowerCase(Locale.ROOT); // SHA256 encoded + rm.put("TradeKey", TradeKey); 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("DeviceInfo", DeviceInfo); + rm.put("StoreOrderNo", orderNo); rm.put("Body","ChargingFee"); - rm.put("TotalFee","1"); + rm.put("TotalFee", fee); rm.put("CardId","4477578739678291"); rm.put("ExtenNo","879"); rm.put("ExpireDate","2703"); @@ -106,4 +117,63 @@ public class IntellaTest { System.out.println(resp); System.out.println("--"); } + + + + @Test + private void refundTest() throws Exception { + File der = new File("D:\\intella\\pub.der"); + InputStream is = new FileInputStream(der); + Map rm = new HashMap<>(); + rm.put("MchId", MchId); + rm.put("Method", "00000"); + rm.put("ServiceType", "Refund"); + String TradeKey = SHA256("0000").toLowerCase(Locale.ROOT); // SHA256 encoded + rm.put("TradeKey", TradeKey); + Date date = Calendar.getInstance().getTime(); + String format = DateUtil.format(date, "yyyyMMddHHmmss"); + rm.put("CreateTime", format); + rm.put("DeviceInfo", DeviceInfo); + rm.put("StoreOrderNo", orderNo); + + rm.put("StoreRefundNo", "RF202206151441003712"); // 长度必须20位 + rm.put("RefundKey", TradeKey.toLowerCase(Locale.ROOT)); + rm.put("RefundFee", fee); + rm.put("RefundedMsg", "退费"); +// 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("--"); + } + }