前面做了app微信支付的回调处理,现在需要做微信公众号的支付,花了一天多时间,终于折腾出来了!鉴于坑爹的微信官方没有提供Java版的demo,所以全靠自己按照同样坑爹的文档敲敲敲,所以记录下来,以供自己及后来人参考,不足之处,还请指正。
首先,我们贴出调用支付接口的H5页面,当然,在这个页面之前,还需要做很多其他的操作,我们一步一步的来。
坑爹的官方文档给了两个不同的支付接口,在微信公众平台开发中文档的“微信JS-SDK说明文档”中,给出的支付方式是下面被屏蔽的那一部分,而在商户平台的“H5调起支付API”中,又给了一份不同的接口,即下面未屏蔽正常使用的接口。关于坑爹的微信提供了两个不同的支付接口,网上搜索结果也是众说纷纷,所以,只有自己试了。当然,为了简单,我直接试了下面这一种,然后奇迹般的成功了。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
微信网页支付
上面h5页面中,支付接口所需的参数都是由后台传过来的,除此之外,在进行上面一步之前,我们还需要获取一个预支付标识,下面贴上后台传参,及获取预支付标识和参数加密等方法(获取预支付标识之前需要网页授权获取用户openid,鉴于这个比较简单,所以不贴代码了)
首先是后台参数封装并对其签名(关键部分代码):
if(payway.equals("1")){
System.out.println("----------支付宝支付-------------");
request.setAttribute("WIDout_trade_no", WIDout_trade_no);//订单号
request.setAttribute("WIDsubject", WIDsubject);//订单名称
request.setAttribute("WIDtotal_fee", WIDtotal_fee);//付款金额
request.setAttribute("WIDshow_url", WIDshow_url);//商品链接
request.setAttribute("WIDbody", "");//商品描述,可空
return "alipayapi";
}else if(payway.equals("2")){
System.out.println("----------微信支付-------------");
//1、通过网页授权接口,获取到的openid
String openid=request.getSession().getAttribute("openid")+"";
//处理价格单位为:分(请自行处理)
WIDtotal_fee="1";
String preid=getPrepayid(WIDout_trade_no, WIDtotal_fee, openid);//获取预支付标示
System.out.println("预支付标示:----------------"+preid);
//APPID
String appId=Common.appid;
request.setAttribute("appId", appId);
//时间戳
String timeStamp=(System.currentTimeMillis()/1000)+"";
request.setAttribute("timeStamp", timeStamp);
//随机字符串
String nonceStr=Common.randString(16).toString();
request.setAttribute("nonceStr", nonceStr);
//预支付标识
request.setAttribute("prepay_id", "prepay_id="+preid);
//加密方式
request.setAttribute("signType", "MD5");
//组装map用于生成sign
Map map=new HashMap();
map.put("appId", appId);
map.put("timeStamp", timeStamp);
map.put("nonceStr", nonceStr);
map.put("package", "prepay_id="+preid);
map.put("signType", "MD5");
request.setAttribute("paySign", Common.sign(map, Common.MchSecret));//签名
return "weixinpay";
}else {
return "error";
}
接下是获取预支付标识的方法getPrepayid:
@ResponseBody
public String getPrepayid(String out_trade_no1,String total_fee1,String openid1){
String result = "";
String appid = Common.appid;
String mch_id = Common.mch_id;
String nonce_str = Common.randString(16);//生成随机数,可直接用系统提供的方法
String body = "E光学-商品订单";
String out_trade_no = out_trade_no1;
String total_fee = total_fee1;
String spbill_create_ip = "xxx.xxx.38.47";//用户端ip,这里随意输入的
String notify_url = "http://www.xxxxxxx.cn/egxwx/wxpay_notify_url.jsp";//支付回调地址
String trade_type = "JSAPI";
String openid = openid1;
HashMap map = new HashMap();
map.put("appid", appid);
map.put("mch_id", mch_id);
map.put("attach", "支付测试");
map.put("device_info", "WEB");
map.put("nonce_str", nonce_str);
map.put("body", body);
map.put("out_trade_no", out_trade_no);
map.put("total_fee", total_fee);
map.put("spbill_create_ip", spbill_create_ip);
map.put("trade_type", trade_type);
map.put("notify_url", notify_url);
map.put("openid", openid);
String sign = Common.sign(map, Common.MchSecret);//参数加密
System.out.println("sign秘钥:-----------"+sign);
map.put("sign", sign);
//组装xml(wx就这么变态,非得加点xml在里面)
String content=Common.MapToXml(map);
//System.out.println(content);
String PostResult=HttpClient.HttpsPost("https://api.mch.weixin.qq.com/pay/unifiedorder", content);
JSonObject jsonObject=XmlUtil.XmlToJson(PostResult);//返回的的结果
if(jsonObject.getString("return_code").equals("SUCCESS")&&jsonObject.getString("result_code").equals("SUCCESS")){
result=jsonObject.get("prepay_id")+"";//这就是预支付id
}
return result;
}
接下是签名的方法(MD5加密是调用微信一个jar里面的,你也可以自己写一个,网上很多参考):
public static String sign(Mapmap,String key) { //排序 String sort=sortParameters(map); //拼接API秘钥 sort=sort+"&key="+key; //System.out.println(sort); //MD5加密 String sign=MD5.MD5Encode(sort).toUpperCase(); return sign; } private static String sortParameters(Map map) { Set keys = map.keySet(); List paramsBuf = new ArrayList (); for (String k : keys) { paramsBuf.add((k + "=" + getParamString(map, k))); } // 对参数排序 Collections.sort(paramsBuf); String result=""; int count=paramsBuf.size(); for(int i=0;i sort(List data) { Collections.sort(data, new Comparator () { public int compare(String obj1, String obj2) { return obj1.compareTo(obj2); } }); return data; }
Map转XML的方法:
public static String MapToXml(Maparr) { String xml = " "; Iterator "; return xml; } private static boolean IsNumeric(String str) { if (str.matches("\d *")) { return true; } else { return false; } }> iter = arr.entrySet().iterator(); while (iter.hasNext()) { Entry entry = iter.next(); String key = entry.getKey(); String val = entry.getValue(); if (IsNumeric(val)) { xml += "<" + key + ">" + val + "" + key + ">"; } else xml += "<" + key + ">" + key + ">"; } xml += "
以上就是java实现微信H5支付的主要代码了,大部分都有注释,也没有什么好解释的了。希望对大家的学习有所帮助,也希望大家多多支持考高分网。



