对于微信支付,开发人员要做的其实很少,我这里就做了3样:创建二维码,支付成功之后的notify,还有定时查询支付是否成功.
先说第一步:创建二维码.
- <img src="url" >
url为后台创建二维码程序的路径.
我用的是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) {
- }
- }
接着页面上就会显示出你创建的二维码了.
第二步:支付成功之后的通知(项目放到服务器上之后才有用.):
- /**
- * 支付异步接受通知
- * @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("通知签名验证失败");
- }
- }
- /**
- * 支付定时检测(用户支付)
- * @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;
- }
链接:http://pan.baidu.com/s/1eSynXL4 密码:m1wy