微信退款结果通知

先理清思路

一  首先在微信商户平台配置,退款结果通知回调路径,这个和支付结果通知一样。微信返回的消息也是流信息,需要解析。

二 接下来按照微信开发文档进行解析流,得到返回数据

三 根据返回数据进行操作,推送模板消息之类的

下面代码,注意编码格式

第一步:解析微信返回的流

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 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);
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。

你可能感兴趣的:(微信公众号支付开发)