public class JsonUtil { public static ObjectNode warpJsonNodeResponse(JsonNode obj){ ObjectNode objectNode=createObjectNode(); objectNode.put("code", 1); objectNode.put("response", obj); return objectNode; } public static JsonNode objectToJsonNode(Object obj){ try { ObjectMapper objectMapper = new ObjectMapper(); String objJson=objectMapper.writeValueAsString(obj); JsonNode jsonNode = objectMapper.readTree(objJson); return jsonNode; } catch (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }
四、微信H5调起支付
这个url需要后台实现,其实现功能如下:
1、接受微信服务器端发送的支付结果。
2、向微信服务器发送支付结果
具体 参考微信aip()
具体代码如下:
4.1、授权向后台发起生成统一下订单页面
wxrepay.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "https://www.jb51.net/"; long t = System.currentTimeMillis(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="utf-8" /> <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> <meta content="yes" /> <meta content="black" /> <meta content="telephone=no" /> <title>微信公众号支付</title> <link href="../css/css.css?t=<%=t%>" type="text/css"> </head> <body> <div> <div>商品</div> <div> <ul> <li><span>beacon 1分钱 1只</span></li> <li><span>测试支付信息</span></li> </ul> <p><a href="javascript:reppay();">确定购买</a></p> </div> </div> <script type="text/javascript" src="../js/common.js?t=<%=t%>"></script> <script type="text/javascript" > var code = urlparameter("code"); function reppay(){ ajaxUtil({}, mainpath+"/pay/wxprepay.shtml?code=" + code, repay); } function repay(response){ var info = response; var url = "wxpay?appId=" + info.appId + "&timeStamp=" +info.timeStamp + "&nonceStr=" + info.nonceStr + "&pg=" +info.pg + "&signType=" +info.signType + "&paySign=" +info.paySign; window.location.href= url + "&showwxpaytitle=1"; } </script> </body> </html>
首先是请求服务端wxprepay.shml接口,后台向微信支付平台获取支付订单信息,返回前台,wxpay.jsp页面
4.2、确认支付页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "https://www.jb51.net/"; long t = System.currentTimeMillis(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="utf-8" /> <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> <meta content="yes" /> <meta content="black" /> <meta content="telephone=no" /> <title>微信公众号支付</title> <link href="../css/css.css?t=<%=t%>" type="text/css"> </head> <body> <div> <div>微信js支付测试</div> <div> <ul> <li><span>测试支付信息</span></li> </ul> <p><a href="javascript:pay();">立即支付</a></p> </div> </div> <script type="text/javascript" src="../js/common.js?t=<%=t%>"></script> <script type="text/javascript"> var appId = urlparameter("appId"); var timeStamp = urlparameter("timeStamp"); var nonceStr = urlparameter("nonceStr"); var pg = urlparameter("pg"); var signType = urlparameter("signType"); var paySign = urlparameter("paySign"); function onBridgeReady(){ WeixinJSBridge.invoke( 'getBrandWCPayRequest', { "appId" : appId, //公众号名称,由商户传入 "timeStamp": timeStamp, //时间戳,自1970年以来的秒数 "nonceStr" : nonceStr, //随机串 "package" : "prepay_id=" + pg, "signType" : signType, //微信签名方式: "paySign" : paySign //微信签名 }, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ) { alert("支付成功"); } // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。 } ); } function pay(){ if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } }else{ onBridgeReady(); } } </script> </body> </html>
4.2、前台涉及到的工具类
javascript工具类common.js,样式css.css就不贴了没意义。