微信退款其实跟微信支付差不多,也需要签名,通过签名后请求退款地址。其中需要加载微信退款的证书,关于证书可以上
微信商户平台登录下载。
基本代码如下:
SortedMap<String, String> sm=new TreeMap<>();
sm.put("appid", AppWxConfig.appid);//应用id
sm.put("mch_id", AppWxConfig.mch_id);//商户id
String str=AppPayUtil.create_nonce_str();
sm.put("nonce_str", str);//随机字符串
sm.put("out_trade_no", orders.getOrderNo());//商户订单号
sm.put("out_refund_no", "123321");//商户退款单号
String s=orders.getPayMoney();
int s1=(int)(Double.valueOf(s)*100);
sm.put("total_fee", String.valueOf(s1));//订单金额
sm.put("refund_fee","1");//退款金额
String sign=" ";//定义签名
/*
* 生成签名
*/
sign=AppPayUtil.createSign("UTF-8", sm,AppWxConfig.wx_api_key);
sm.put("sign", sign);//签名
String xml=AppPayUtil.getRequestXml(sm);//封装xml数据
RefundRequest refundRequest = new RefundRequest();
String result1=refundRequest.httpsRequest(AppWxConfig.REFUND_URL, xml, AppWxConfig.certPath);//请求退款地址
System.out.println("------------------------"+result1);
Map<String, Object> getMap = XmlConverUtil.xmltoMap(new String(result1.toString().getBytes()));
if("SUCCESS".equals(getMap.get("return_code")) && "SUCCESS".equals(getMap.get("return_msg"))){
result.put("code", getMap.get("return_code"));
result.put("message", getMap.get("return_msg"));