本文介绍的是微信H5支付功能开发,也就是在微信之外的H5页面支付。
首先我们需要先看微信的官方文档https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_1,这里面几乎介绍了全部流程了。等你了解大概流程之后,需要在微信公众平台和微信商户平台拿到或者配置一下参数。
appid:公众平台的appid
商户号:公众平台的商户号
商户密钥:key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
最后在商户平台后台产品中心-->开发配置 中添加H5支付回调域名,域名必须是通过备案的。(下图借用了某个兄弟的图,我懒得上后台截图了)
花了不少时间去填这些坑,填完了我们就可以吭哧吭哧开工搬砖了!
写完支付别忘了还有订单查询功能需要开发,主要为了查询出哪些王八蛋就只是来看看不买东西还占库存。不给钱的我们需要把库存要回来。
/**
* 保存订单并发起支付请求
* @param request
* @param response
* @throws IOException
*/
@RequestMapping(value = "/save")
public void save(HttpServletRequest request, HttpServletResponse response) throws IOException{
//判断用户是否登录
String mobileNumber = (String)request.getSession().getAttribute("mobileNumber");
if(StringUtils.isEmpty(mobileNumber)){
return;
}
response.setContentType("text/xml;charset=utf-8");
PrintWriter out = response.getWriter();
ResponMsg msg = new ResponMsg();
msg.setSuccess(true);
String content = "提交成功";
try{
//获取用户选择的票务信息,邮寄信息
String ticketId = request.getParameter("ticketId");
String ticketCount = request.getParameter("ticketCount");
String userName = request.getParameter("userName");
String userAddress = request.getParameter("userAddress");
String userMobile = request.getParameter("userMobile");
//参数验证
if(StringUtils.isEmpty(ticketId) || StringUtils.isEmpty(ticketCount) || StringUtils.isEmpty(userAddress)
|| StringUtils.isEmpty(userMobile) || StringUtils.isEmpty(userName)){
content = "请正确且完整填写收货信息!";
msg.setCode(-2);
return;
}
//判断是否有可售票 有则创建订单预留票并发起支付
ActTicket actTicket = showService.queryTicketById(Integer.valueOf(ticketId));
if(actTicket.getTicketStock() - Integer.valueOf(ticketCount) < 0){
content = "抱歉,已售完!感谢关注!";
msg.setCode(-1);
return;
}
//创建订单
// 自己网站上的订单号
int randomNum = (int) (Math.random() * 1999+5000);
//String out_trade_no = TimeUtils.getSysTime("yyyyMMddHHmmss") + randomNum;
String orderNO = TimeUtils.getSysTime("yyyyMMddHHmmss") + randomNum;
ActUserAddress actUserAddress = new ActUserAddress(mobileNumber, userName, userMobile,userAddress, orderNO);
ActOrder actOrder = new ActOrder();
actOrder.setOrderNumber(orderNO);
actOrder.setTicketId(actTicket.getId());
actOrder.setProductName(actTicket.getTicketLevel());
actOrder.setProductPrice(actTicket.getTicketPrice());
actOrder.setProductCount(Integer.valueOf(ticketCount));
actOrder.setTotalPay(actTicket.getTicketPrice() * Integer.valueOf(ticketCount));
actOrder.setPayType(1);
actOrder.setStatement(1);
actOrder.setUserAccount(mobileNumber);
actOrder.setActUserAddress(actUserAddress);
System.out.println(mobileNumber + " 创建订单:" + actOrder.toString());
//缓存数据
request.getSession(true).setAttribute("actOrder", actOrder);
showService.createOrder(actOrder);
//发起支付
content = "/show/wxPayH5";
msg.setCode(9);
}catch(Exception e){
msg.setCode(0);
msg.setSuccess(false);
msg.setMessage("当前人数过多,请稍后再来。");
e.printStackTrace();
}finally{
msg.setMessage(content);
out.print(JsonUtil.toJson(msg));
out.flush();
out.close();
}
}
/**
* 微信H5支付
* 统一下单
* @param request
* @param response
* @param model
* @throws Exception
*/
@RequestMapping("/wxPayH5")
public void wxPayH5(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
//System.out.println("pay ===== start===");
//System.out.println("pay mobile=== " + request.getSession().getAttribute("mobileNumber").toString());
ActOrder actOrder = (ActOrder)request.getSession(true).getAttribute("actOrder");
System.out.println("===wxPayH5===start===actOrder===" + actOrder.toString());
request.getSession().removeAttribute("actOrder");
Map
result.put("success", false);
try {
// 付款金额,必填
String total_fee = String.valueOf(actOrder.getTotalPay()); //"0.01"
// 账号信息
String appid = PayConfig.APP_ID; // appid
String mch_id = PayConfig.MCH_ID; // 商业号
String key = PayConfig.API_KEY; // key
String currTime = PayCommonUtil.getCurrTime();
String strTime = currTime.substring(8, currTime.length());
String strRandom = PayCommonUtil.buildRandom(4) + "";
String nonce_str = strTime + strRandom;
// 价格 注意:价格的单位是分
String order_price = new BigDecimal(total_fee).multiply(new BigDecimal(100)).toString().split("\\.")[0];
// 自己网站上的订单号
String out_trade_no = actOrder.getOrderNumber();
// 获取发起电脑 ip
String spbill_create_ip = HttpUtil.getRealIp(request);
// 回调接口
String notify_url = "http://wx.xxx.com/xx/xx/payNotifyMe";
// 页面跳转同步通知页面路径
String trade_type = "MWEB";
// 设置package订单参数
SortedMap
packageParams.put("appid", appid);
packageParams.put("mch_id", mch_id);
// 生成签名的时候需要你自己设置随机字符串
packageParams.put("nonce_str", nonce_str);
packageParams.put("out_trade_no", out_trade_no);
packageParams.put("total_fee", order_price);
packageParams.put("spbill_create_ip", spbill_create_ip);
packageParams.put("notify_url", notify_url);
packageParams.put("trade_type", trade_type);
packageParams.put("body", PayConfig.BODY);
packageParams.put("scene_info", "{\"h5_info\": {\"type\":\"Wap\",\"wap_url\": \"http://wx.xxxx.com\",\"wap_name\": \"测试\"}}");
String sign = PayCommonUtil.createSign("UTF-8", packageParams, key);
System.out.println("订单号:" + out_trade_no + " 的签名:" + sign);
packageParams.put("sign", sign);
String requestXML = PayCommonUtil.getRequestXml(packageParams);
String resXml = HttpUtil.postData(PayConfig.UFDODER_URL, requestXML);
Map map = XMLUtil.doXMLParse(resXml);
//String urlCode = (String) map.get("code_url");
System.out.println("out_trade_no=" + map.get("out_trade_no"));
System.out.println("prepay_id=" + map.get("prepay_id"));
//确认支付过后跳的地址,需要经过urlencode处理
String urlString = URLEncoder.encode("http://wx.xxx.com/xx/xx/xx/list.html", "GBK");
String mweb_url = map.get("mweb_url")+"&redirect_url="+urlString;
//更新订单状态
actOrder.setPayOrderNO(map.get("prepay_id").toString());
actOrder.setStatement(2);
actOrder.setNote(map.get("sign").toString());
showService.updateOrderStatement(actOrder);
System.out.println("==========mweb_url==========" + mweb_url);
response.sendRedirect(mweb_url);
//result.put("sHtmlText", urlCode);
result.put("success", true);
} catch (Exception e) {
e.printStackTrace();
result.put("errormsg", e.getMessage());
}
}
/**
* 执行回调 确认支付后处理事件 例如添加金额到数据库等操作
*
* @param request
* @param response
* @throws Exception
*/
@RequestMapping("/payNotifyMe")
public void weixin_notify(HttpServletRequest request, HttpServletResponse response, ModelMap model)throws Exception {
System.out.println("进入支付h5回调=====================");
String xmlMsg = readData(request);
System.out.println("pay notice---------"+xmlMsg);
Map params = XMLUtil.doXMLParse(xmlMsg);
try {
// 过滤空 设置 TreeMap
SortedMap
Iterator it = params.keySet().iterator();
while (it.hasNext()) {
String parameter = (String) it.next();
String parameterValue = params.get(parameter)+"";
String v = "";
if (null != parameterValue) {
v = parameterValue.trim();
}
//System.out.println( parameter + " value==============="+v);
packageParams.put(parameter, v);
}
//订单号
String orderNO = packageParams.get("out_trade_no").toString().trim();
ActOrder actOrder = new ActOrder();
actOrder.setOrderNumber(orderNO);
actOrder.setPayOrderNO(packageParams.get("sign").toString().trim());
actOrder.setPayOrderNO(packageParams.get("transaction_id").toString());
String resXml = "";
// 处理业务开始
if ("SUCCESS".equals((String) packageParams.get("result_code"))) {
// 这里是支付成功
model.put("msg", "付款成功");
// 执行自己的业务逻辑 更新订单状态
actOrder.setStatement(3);
// 通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了.
resXml = "
} else {
model.put("msg", "付款失败");
actOrder.setStatement(0);
resXml = "
}
//更新订单
showService.updateOrderStatement(actOrder);
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(resXml.getBytes());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String readData(HttpServletRequest request) {
BufferedReader br = null;
try {
StringBuilder result = new StringBuilder();
br = request.getReader();
for (String line; (line=br.readLine())!=null;) {
if (result.length() > 0) {
result.append("\n");
}
result.append(line);
}
return result.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
finally {
if (br != null)
try {br.close();} catch (IOException e) {e.printStackTrace();}
}
}
需要完整的代码可以留言哦!可有偿指导开发完整个流程!