對於微信支付,開發人員要作的其實不多,我這裏就作了3樣:建立二維碼,支付成功以後的notify,還有定時查詢支付是否成功.java
先說第一步:建立二維碼.ajax
<img src="url" >
url爲後臺建立二維碼程序的路徑.spring
我用的是spring MVC,實例也就以此爲準,下面是建立二維碼的程序:服務器
/** * 建立二維碼(用戶帳戶充值的二維碼) * @throws UnsupportedEncodingException */ @RequestMapping("/url") public void url(String id) throws RootException, UnsupportedEncodingException { //fee表明須要支付的金錢,1表明1分.下面的100就是1塊錢. Integer fee =100; //生成訂單 .notifyUrl就是支付成功以後,微信會根據你給的路徑,來提醒你,支付成功了,下面具體講. String orderInfo = WeiXinPayCommUtil.createOrderInfo( id, "提示語", fee+"", notifyUrl ,IpUtil.getIpAddr(request) ); //調統一下單API 將返回預支付交易連接(code_url)生成二維碼圖片 String code_url = WeiXinPayCommUtil.httpOrder(orderInfo); try { int width = 230; int height = 230; String format = "png"; Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(code_url, BarcodeFormat.QR_CODE, width, height, hints); ServletOutputStream out = null; out = response.getOutputStream(); MatrixToImageWriter.writeToStream(bitMatrix, format, out); out.flush(); out.close(); } catch (Exception e) { } }
接着頁面上就會顯示出你建立的二維碼了.微信
第二步:支付成功以後的通知(項目放到服務器上以後纔有用.):app
/** * 支付異步接受通知 * @throws RootException * @throws IOException * @throws JDOMException */ @RequestMapping("/payNotify") public void payNotify() throws RootException, IOException, JDOMException{ logger.info("WeiXinPay notify ..."); //讀取參數 InputStream inputStream ; StringBuffer sb = new StringBuffer(); inputStream = request.getInputStream(); String s ; BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); while ((s = in.readLine()) != null){ sb.append(s); } in.close(); inputStream.close(); //解析xml成map Map<String, String> m = new HashMap<String, String>(); m = WeiXinXMLUtil.doXMLParse(sb.toString()); //過濾空 設置 TreeMap SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>(); Iterator<String> it = m.keySet().iterator(); while (it.hasNext()) { String parameter = (String) it.next(); String parameterValue = (String) m.get(parameter); String v = ""; if(null != parameterValue) { v = parameterValue.trim(); } packageParams.put(parameter, v); } // 帳號信息 String key = WeiXinPayConfig.key; // key logger.info(packageParams); //判斷簽名是否正確 if(WeiXinPayCommUtil.isTenpaySign("UTF-8", packageParams,key)) { //------------------------------ //處理業務開始 //------------------------------ String resXml = ""; if("SUCCESS".equals((String)packageParams.get("result_code"))){ // 這裏是支付成功 //////////執行本身的業務邏輯//////////////// String mch_id = (String)packageParams.get("mch_id"); String openid = (String)packageParams.get("openid"); String is_subscribe = (String)packageParams.get("is_subscribe"); String out_trade_no = (String)packageParams.get("out_trade_no"); String total_fee = (String)packageParams.get("total_fee"); logger.info("mch_id:"+mch_id); logger.info("openid:"+openid); logger.info("is_subscribe:"+is_subscribe); logger.info("out_trade_no:"+out_trade_no); logger.info("total_fee:"+total_fee); //////////執行本身的業務邏輯//////////////// BaseVo vo2 = 業務邏輯代碼. if(vo2.getMsg().equals("true")){ logger.info("支付成功"); //通知微信.異步確認成功.必寫.否則會一直通知後臺.八次以後就認爲交易失敗了. resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> "; }else{ logger.info("支付失敗,錯誤信息:" + packageParams.get("err_code")); resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[報文爲空]]></return_msg>" + "</xml> "; } } else { logger.info("支付失敗,錯誤信息:" + packageParams.get("err_code")); resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[報文爲空]]></return_msg>" + "</xml> "; } //------------------------------ //處理業務完畢 //------------------------------ BufferedOutputStream out = new BufferedOutputStream( response.getOutputStream()); out.write(resXml.getBytes()); out.flush(); out.close(); } else{ logger.info("通知簽名驗證失敗"); } }
最後一個就是定時檢測是否支付成功,能夠使用ajax異步
/** * 支付定時檢測(用戶支付) * @throws RootException * @throws IOException * @throws JDOMException * @author dingzefeng */ @RequestMapping("/payCheck") @ResponseBody public BaseVo payCheck(String id) throws RootException, IOException, JDOMException{ BaseVo vo = new BaseVo(); vo.setMsg("error"); try { if(!ValidateUtil.validateBlank(id)){ //生成查詢xml String orderInfo = WeiXinPayCommUtil.selectOrderInfo(id); String result = WeiXinPayCommUtil.httpSelectOrder(orderInfo); if(result != null && result.equals("true")){ //業務邏輯代碼 return vo; } }else{ vo.setMsg("error"); } } catch (Exception e) { e.printStackTrace(); } return vo; }
下面地址爲微信支付的util包下載地址:http://pan.baidu.com/s/1eSmIoYA微信支付