微信退款結果通知
阿新 • • 發佈:2019-02-10
先理清思路
一 首先在微信商戶平臺配置,退款結果通知回撥路徑,這個和支付結果通知一樣。微信返回的訊息也是流資訊,需要解析。
二 接下來按照微信開發文件進行解析流,得到返回資料
三 根據返回資料進行操作,推送模板訊息之類的
下面程式碼,注意編碼格式
第一步:解析微信返回的流
InputStream inStream;
inStream = request.getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0 ;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
String result = new String(outSteam.toByteArray(), "utf-8");// 獲取微信呼叫我們notify_url的返回資訊
Map<Object, Object> m = null;
try {
//將xml格式轉換為map格式
m = XmlUtil.xml2map(result, false );
} catch (DocumentException e) {
e.printStackTrace();
}
//獲取加密資訊
String a = m.get("req_info").toString();
第二部 接下來解碼加密資訊
(1)對加密串A做base64解碼,得到加密串B
public static byte[] decode(String key) throws Exception { System.out.println(new BASE64Decoder().decodeBuffer(key)); return new BASE64Decoder().decodeBuffer(key); }(2)對商戶key做md5,得到32位小寫key* ( key設定路徑:微信商戶平臺(pay.weixin.qq.com)-->賬戶設定-->API安全-->金鑰設定 )
private static SecretKeySpec key = new SecretKeySpec(MD5Util.MD5Encode(password, "UTF-8").toLowerCase().getBytes(), ALGORITHM);
(3)用key*對加密串B做AES-256-ECB解密
public static String decryptData(String base64Data) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(decode(base64Data)),"UTF-8");
}
這樣得到String B = decryptData(a);B就是解密後的資訊,是xml格式,需要再次轉化成map。