今天更新一波微信退款的操作,微信退款,最主要的数据就是订单号,如果有了订单号我们就能从数据库中获取之前我们所支付成功的订单所有的数据,废话不多少来码!
Controller
// @RequestMapping("refund")
// public void refund(HttpServletRequest request) throws Exception{
// String tradeno = request.getParameter("tradeno");
// paymanager.reFund(tradeno);
// }
这里为什么要注释起来呢?原因很简单,怕被别人恶意获取到你的订单号直接请求退款,那事情岂不是大条了!!!
Service
//退款
public Map<String, String> reFund(String tradeno){
WXPayConfigImpl payconfig;
try {
payconfig = WXPayConfigImpl.getInstance(config.getKey(), config.getAppID(),
config.getMchID(), config.getCertLocalPath());
WXPay wxpay = new WXPay(payconfig);
HashMap<String, String> data = new HashMap<String, String>();
WXPayOrders order = wxpayDao.getOrder(tradeno);
String refundno = "T" + tradeno;
data.put("out_trade_no", tradeno);
data.put("out_refund_no", refundno);//退款订单号
data.put("total_fee", order.getTotalfee()+"");//总价格
data.put("refund_fee", order.getTotalfee()+"");//退款价格
data.put("refund_fee_type", "CNY");
data.put("op_user_id", config.getMchID());
Map<String, String> r = wxpay.refund(data);
if(r.get("return_code").equals("SUCCESS") && r.get("result_code").equals("SUCCESS")){
wxpayDao.reFund(tradeno,refundno);
}
return r;
} catch (Exception e) {
// e.printStackTrace();
Util.logException("微信退款失败", e);
return null;
}
}
WXpay
/**
* 作用:申请退款<br>
* 场景:刷卡支付、公共号支付、扫码支付、APP支付
* @param reqData 向wxpay post的请求数据
* @return API返回数据
* @throws Exception
*/
public Map<String, String> refund(Map<String, String> reqData) throws Exception {
return this.refund(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
上面那就是微信提供的代码。
走到这一部,你就能发现退款成功了。
有时候需要退回到某些页面比如
WeixinJSBridge.call('closeWindow');
直接退回到公众号页面上了,同时给出个友好提示就好了嘛!!!!