点击支付在js中处理请求:
var callbackUrl = 'http://www.xxxxx.com/reader/charges.html?uid='+localUser.id+"&price="+price;
window.location.href = callbackUrl;
ajax提交请求:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>微信充值中...</title> </head> <body> </body> <script src="javascripts/config.js" type="text/javascript"></script> <script src="javascripts/md5.min.js"></script> <script src="javascripts/jquery-3.2.1.min.js"></script> <script src="javascripts/util.js"></script> <script> var uid = getQueryString('uid'); var price = getQueryString('price'); $.ajax({ type: 'post', url: '/reader/wxpay/wxCharges.dom', data: {uid:uid,price:price}, success: function (data) {
//后台返回调起微信支付的链接,直接跳转过去即可 window.location.href=data; } }) function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } return null; } </script> </html>
后台处理请求:
public String wxCharges(String price,String uid) { try{ logger.info("before insert charge..."); Charge charge = new Charge(); charge.setId(IdGenerate.getId()); charge.setCdate(sdf.format(new Date())); charge.setCoin(price); charge.setStatus("0"); charge.setMoney(String.valueOf(Integer.valueOf(price)/100)); charge.setUid(uid); charge.setZhifutype("wx"); charge.setXstype("web"); chargeMapper.insertCharge(charge); logger.info("after insert charge..."); //订单号 String tradeNo = charge.getId(); String payUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; String ip = SpringUtils.getRequestIP(); System.out.println("ip:"+ip); String stringA = "appid="+WxPayParam.APPID+"&attach=XXXX在线充值&body=xxxx在线充值&" + "mch_id="+WxPayParam.PAY_MCH_ID+"&nonce_str=1add1a30ac87aa2db72f57a2375d8fec¬ify_url=" + "http://www.xxxx.com/reader/wxpay/wxChargeCallback.dom&out_trade_no=" + tradeNo + "&scene_info={"h5_info": {"type":"Wap","wap_url": "http://www.xxxx.com/reader/home.html","wap_name": "xxxx在线充值"}}&spbill_create_ip=" + ip + "&total_fee=" + price +"&trade_type=MWEB&key=" + WxPayParam.API_KEY; System.out.println(stringA); String sign = DigestUtils.md5Hex(stringA).toUpperCase(); // String sign = MD5(stringA).toUpperCase(); String xml = "<xml> " + " <appid>"+WxPayParam.APPID+"</appid> " + " <attach>xxxx在线充值</attach> " + " <body>xxxx在线充值</body> " + " <mch_id>"+WxPayParam.PAY_MCH_ID+"</mch_id> " + " <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str> " + " <notify_url>http://www.xxxx.com/reader/wxpay/wxChargeCallback.dom</notify_url> " + " <out_trade_no>" + tradeNo + "</out_trade_no> " + " <spbill_create_ip>" + ip + "</spbill_create_ip> " + " <total_fee>" + price + "</total_fee> " + " <trade_type>MWEB</trade_type> " + " <scene_info>{"h5_info": {"type":"Wap","wap_url": "http://www.xxxx.com/reader/home.html","wap_name": "xxxx在线充值"}}</scene_info> " + " <sign>" + sign + "</sign> " + " </xml>"; System.out.println(xml); HttpClient client = new HttpClient(); client.getParams().setContentCharset("utf-8"); PostMethod post1 = new PostMethod(payUrl); post1.setRequestBody(xml); post1.addRequestHeader("accept", "application/xml"); // 视具体情况而定,可以从浏览器或postman查看下请求的header post1.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8"); //这里设置具体编码,视具体接口而定 post1.setRequestHeader("Content-type", "text/xml; charset=UTF-8"); client.executeMethod(post1); String response1 = post1.getResponseBodyAsString(); logger.info("response1: " + response1); String str=response1.substring(response1.indexOf("<mweb_url><![CDATA[")+"<mweb_url><![CDATA[".length(),response1.indexOf("]]></mweb_url>"));
//redirect_url是支付成功之后你自己想要跳转的页面,默认是没有的,需要自己添加,如果不添加,支付完之后还会回到支付页面,一直循环
return (str +"&redirect_url=http://www.xxxxx.com/reader/home.html");
} catch (Exception e){
logger.error("error: ", e);
return null;
}
}
微信支付回调:
public String wxChargeCallback() { logger.info("wx charge call back..."); try { Map<String, String> map = getWxCallbackParams(); String result_code = map.get("result_code"); String out_trade_no = map.get("out_trade_no"); String total_fee = map.get("total_fee"); logger.info("result_code: " + result_code); logger.info("out_trade_no: " + out_trade_no); logger.info("total_fee: " + total_fee); //处理业务逻辑 if ("SUCCESS".equals(result_code)) { //微信支付成功,设置支付金额 Charge charge = new Charge(); charge.setId(out_trade_no); List<Charge> charges = chargeMapper.selectCharge(charge); if (charges.size() > 0) { Charge c = charges.get(0); logger.info(c); //实际支付金额(元) logger.info("before callback update charge"); c.setStatus("1"); chargeMapper.updateCharge(c); logger.info("after callback update charge"); //更新用户币量 logger.info("before callback update user"); User user = new User(); user.setId(c.getUid()); List<User> users = userMapper.selectUser(user); if (users.size() > 0) { User user1 = users.get(0); logger.info(user1); user1.setBalance("" + (Integer.parseInt(user1.getBalance()) + Integer.parseInt(c.getCoin()))); userMapper.updateUser(user1); } logger.info("after callback update user"); } return returnXML(result_code); } return returnXML("FAIL"); } catch (Exception e) { e.printStackTrace(); return returnXML("FAIL"); } }
/** * 获取微信回调参数 */ private Map getWxCallbackParams() throws IOException, JDOMException { HttpServletRequest request = SpringUtils.getRequest(); BufferedReader reader; reader = request.getReader(); String line; String xmlString; StringBuffer inputString = new StringBuffer(); while ((line = reader.readLine()) != null) { inputString.append(line); } xmlString = inputString.toString(); request.getReader().close(); logger.info("微信回调数据:" + xmlString); return XmlUtils.doXMLParse(xmlString); }