微信公众号开发---实现微信扫一扫支付 (java)

对于微信支付,开发人员要做的其实很少,我这里就做了3样:创建二维码,支付成功之后的notify,还有定时查询支付是否成功.

先说第一步:创建二维码.

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <img src="url"  >  

url为后台创建二维码程序的路径.

我用的是spring MVC,实例也就以此为准,下面是创建二维码的程序:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.  * 创建二维码(用户账户充值的二维码)  
  3.  * @throws UnsupportedEncodingException  
  4.  */    
  5. @RequestMapping("/url")    
  6. public void url(String id) throws RootException, UnsupportedEncodingException {    
  7.   
  8.   
  9.     //fee代表需要支付的金钱,1代表1分.下面的100就是1块钱.  
  10.     Integer fee =100;  
  11.       
  12.       
  13.     //生成订单 .notifyUrl就是支付成功之后,微信会根据你给的路径,来提醒你,支付成功了,下面具体讲.  
  14.     String orderInfo = WeiXinPayCommUtil.createOrderInfo(  
  15.                         id,  
  16.                         "提示语",  
  17.                         fee+"",  
  18.                         notifyUrl  
  19.                         ,IpUtil.getIpAddr(request)  
  20.                         );    
  21.      
  22.     //调统一下单API   将返回预支付交易链接(code_url)生成二维码图片    
  23.     String code_url = WeiXinPayCommUtil.httpOrder(orderInfo);    
  24.       
  25.     try {    
  26.           
  27.         int width = 230;    
  28.         int height = 230;    
  29.         String format = "png";    
  30.         Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();    
  31.         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");    
  32.         BitMatrix bitMatrix = new MultiFormatWriter().encode(code_url, BarcodeFormat.QR_CODE, width, height, hints);    
  33.         ServletOutputStream out = null;    
  34.         out = response.getOutputStream();    
  35.         MatrixToImageWriter.writeToStream(bitMatrix, format, out);    
  36.         out.flush();    
  37.         out.close();    
  38.     } catch (Exception e) {    
  39.           
  40.     }    
  41.     
  42. }    

接着页面上就会显示出你创建的二维码了.

第二步:支付成功之后的通知(项目放到服务器上之后才有用.):

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * 支付异步接受通知 
  3.      * @throws RootException 
  4.      * @throws IOException  
  5.      * @throws JDOMException  
  6.      */  
  7.     @RequestMapping("/payNotify")  
  8.     public void payNotify() throws RootException, IOException, JDOMException{  
  9.           
  10.         logger.info("WeiXinPay notify ...");  
  11.           
  12.         //读取参数    
  13.         InputStream inputStream ;    
  14.         StringBuffer sb = new StringBuffer();    
  15.         inputStream = request.getInputStream();    
  16.         String s ;    
  17.         BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));    
  18.         while ((s = in.readLine()) != null){    
  19.             sb.append(s);    
  20.         }    
  21.           
  22.         in.close();    
  23.         inputStream.close();    
  24.     
  25.         //解析xml成map    
  26.         Map<String, String> m = new HashMap<String, String>();    
  27.         m = WeiXinXMLUtil.doXMLParse(sb.toString());    
  28.             
  29.         //过滤空 设置 TreeMap    
  30.         SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();          
  31.         Iterator<String> it = m.keySet().iterator();    
  32.         while (it.hasNext()) {    
  33.             String parameter = (String) it.next();    
  34.             String parameterValue = (String) m.get(parameter);    
  35.                 
  36.             String v = "";    
  37.             if(null != parameterValue) {    
  38.                 v = parameterValue.trim();    
  39.             }    
  40.             packageParams.put(parameter, v);    
  41.         }    
  42.             
  43.         // 账号信息    
  44.         String key = WeiXinPayConfig.key; // key    
  45.     
  46.         logger.info(packageParams);    
  47.           
  48.           
  49.         //判断签名是否正确    
  50.         if(WeiXinPayCommUtil.isTenpaySign("UTF-8", packageParams,key)) {    
  51.             //------------------------------    
  52.             //处理业务开始    
  53.             //------------------------------    
  54.             String resXml = "";    
  55.             if("SUCCESS".equals((String)packageParams.get("result_code"))){    
  56.                 // 这里是支付成功    
  57.                 //执行自己的业务逻辑    
  58.                 String mch_id = (String)packageParams.get("mch_id");    
  59.                 String openid = (String)packageParams.get("openid");    
  60.                 String is_subscribe = (String)packageParams.get("is_subscribe");    
  61.                 String out_trade_no = (String)packageParams.get("out_trade_no");    
  62.                     
  63.                 String total_fee = (String)packageParams.get("total_fee");    
  64.                     
  65.                 logger.info("mch_id:"+mch_id);    
  66.                 logger.info("openid:"+openid);    
  67.                 logger.info("is_subscribe:"+is_subscribe);    
  68.                 logger.info("out_trade_no:"+out_trade_no);    
  69.                 logger.info("total_fee:"+total_fee);    
  70.                     
  71.                 //执行自己的业务逻辑    
  72.                   
  73.                 BaseVo vo2 = 业务逻辑代码.  
  74.                 if(vo2.getMsg().equals("true")){  
  75.                     logger.info("支付成功");    
  76.                     //通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了.    
  77.                     resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"    
  78.                             + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";    
  79.                 }else{  
  80.                     logger.info("支付失败,错误信息:" + packageParams.get("err_code"));    
  81.                     resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"    
  82.                             + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";  
  83.                 }  
  84.                     
  85.             } else {    
  86.                 logger.info("支付失败,错误信息:" + packageParams.get("err_code"));    
  87.                 resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"    
  88.                         + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";    
  89.             }    
  90.             //------------------------------    
  91.             //处理业务完毕    
  92.             //------------------------------    
  93.             BufferedOutputStream out = new BufferedOutputStream(    
  94.                     response.getOutputStream());    
  95.             out.write(resXml.getBytes());    
  96.             out.flush();    
  97.             out.close();    
  98.         } else{    
  99.             logger.info("通知签名验证失败");    
  100.         }    
  101.           
  102.     }  
  103.       
最后一个就是定时检测是否支付成功,可以使用ajax

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * 支付定时检测(用户支付) 
  3.      * @throws RootException 
  4.      * @throws IOException  
  5.      * @throws JDOMException 
  6.      * @author dingzefeng 
  7.      */  
  8.     @RequestMapping("/payCheck")  
  9.     @ResponseBody  
  10.     public BaseVo payCheck(String id) throws RootException, IOException, JDOMException{  
  11.         BaseVo vo = new BaseVo();  
  12.         vo.setMsg("error");  
  13.         try {  
  14.             if(!ValidateUtil.validateBlank(id)){  
  15.                 //生成查询xml    
  16.                 String orderInfo = WeiXinPayCommUtil.selectOrderInfo(id);  
  17.                   
  18.                 String result = WeiXinPayCommUtil.httpSelectOrder(orderInfo);  
  19.                 if(result != null && result.equals("true")){  
  20.                     //业务逻辑代码  
  21.                       
  22.                     return vo;  
  23.                 }   
  24.             }else{  
  25.                 vo.setMsg("error");  
  26.             }  
  27.               
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.         return vo;  
  32.           
  33.     }  
下面地址为微信支付的util包下载地址
链接:http://pan.baidu.com/s/1eSynXL4 密码:m1wy
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值