一、通知接口
我们对一笔微信退款是否成功除了查询以外,还有最保险的一种模式就是对通知的解析,但是微信退款通知居然是密文模式的。
我们要根据他的要求解密出来以后才可以解析。那么先拿到通知吧。
二、接收通知
三、解析通知
这里我就不做详细的介绍,直贴代码
public Map<String, String> getRequestParams(HttpServletRequest request, String payType) throws Exception {
Map<String, String> paramsData = new HashMap<>();
InputStream inputStream = request.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
final StringBuffer sb = new StringBuffer();
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
} finally {
bufferedReader.close();
inputStream.close();
}
paramsData = WXPayUtil.xmlToMap(sb.toString());
return paramsData;
}
那么拿到的是接口中的数据了
四、解密数据
直接上代码
/**
* AES解密
*
* @param base64Data
* @return
* @throws Exception
*/
/**
* 密钥算法
*/
private static final String ALGORITHM = "AES";
/**
* 加解密算法/工作模式/填充方式
*/
private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding";
public static String decryptData(String base64Data, String lowMad5Key) throws Exception {
SecretKeySpec key = new SecretKeySpec(lowMad5Key.getBytes(), ALGORITHM);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
cipher.init(Cipher.DECRYPT_MODE, key);
Base64.Decoder decoder = Base64.getDecoder();
String newData = new String(decoder.decode(base64Data), "ISO-8859-1");
return new String(cipher.doFinal(newData.getBytes("ISO-8859-1")), "utf-8");
}
/**
* MD5加密
*
* @param data
* @return
* @throws Exception
*/
public static String MD5(String data) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] array = md.digest(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
byte[] var4 = array;
int var5 = array.length;
for (int var6 = 0; var6 < var5; ++var6) {
byte item = var4[var6];
sb.append(Integer.toHexString(item & 255 | 256).substring(1, 3));
}
return sb.toString().toUpperCase();
}
//示例:不可以直接用,已经把KEY值修改,需要根据实际进行操作
public static void main(String[] args) throws Exception {
String a = "Czal3idTbyqfr3FJ9EnBRIWX8K6JNgB9RfTpdDfw6H5NATjCZh+JJd+NQTi2YeFeK6Cg+70WUhQWoRl7RcZ6oJ5d8Xxz/ALrKLuko+fkJj4SyN1exOfJAGqT6IR3M+tjtOh/h0nf+w3kbVpwsrQMNTfZO88+1HDZBvpLm3HpRnyRL+8KxBoZGRNIX4Xmk7RE/Q98ghZLaXj3OWk8W1X8NV5UUzofgnNP2/4WgB6TXqQjibFDhPvI1CeJtFOtB8umbS/o4LXaQ14DvjGF37k5FoOz9XBO8/xe4vXRN02GlcmEaC9GH5f/krn9liRNxnKjUdJxvyFDaxJNrwiAXgKC4s7AmUauyvpuEU1g5kol1FwnKyEUJvwjWI1T+7nGasLcwlQ8W1qBB2VKGk49xpS73ePbA9jJB/Z8FT6EEqSFjEc7j7rQT+DLrZvQtK7xl18roJDHIl/nTGCgwosmXpTJ2bW9sYJ6t6bbFf+GQER4A1CX46RryQSNMzVUfjAKW566QPyrP35YCw0rE6mPP1CfXbbWUq+x8po9mXyK9/r0KNbNKWe7MbkMsa2rDg1fcld7hsE6VYzoFkAObkXR6gjrkQ37R70n27AiuqyJmviq42/Hlga7uCH4WN9bPSGYfqLfAC+UkSc9wO1m3uKd7Jw74g+kKukFAhRTBLosfgHMZSjnQJZfrWJIk4gVhWD990Si2W7aoub0nAVK5Lx/jXhG05Ouu9h0zVLsaULRhKmiLmR3NUuxYBTZKEQ7y2n9aFKcMMXAWm+r5WteTx6UtmDYgrqtOUEPNflMw2vclNdVKv1NAngWppk9QYuiW3LaRMz13agYPBxtbQ1nl1RT+0Ng+7DEshc0VMRfI9dNTxYB0+nMJfLXNdUylSEpAYMKv76ck973Ue13VP6AEp6KyhBgdQbgTlpu9CHTc6/N8sUqjprXGwiNu02V2Iqdr2K5uisyOrh6KG1BzAAhp+uTaIr00XZYnrBX/Xg2SRVPCaZTLoWqmTElFmOvwxJy5XDmEwrdni1myT8BxhLVc2Gv/SAUfBjTJJDfvMApMS7TZJBEajj3iSPkz+A4pEk89W3vbVyh0+wtqukPdhVinsHoIQXUKw";
String ab = AESUtil.decryptData(a, AESUtil.MD5("GXXXXXL1236458sskdjsghxc849728").toLowerCase());
Map<String, String> xmlMap = WXPayUtil.xmlToMap(ab);
System.out.println(ab);
System.out.println(xmlMap.get("out_refund_no"));
}
解密后: