微信支付之扫码支付(java版 native原生支付)

来源:互联网 发布:淘宝上化妆品正品店铺 编辑:程序博客网 时间:2024/06/10 03:59

本文直接从代码调用微信扫码支付讲起。账号配置,参数生成等请参考官方文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1

微信扫码支付。简单来说,就是你把微信支付需要的信息,生成到二维码图片中。通过微信扫一扫,发起支付。我们需要做的就是二件事:

  一是:按照微信扫码支付规则生成二维码信息.

  二是:微信没有提供生成二维码图片的接口。需要我们自己把二维码信息生成到二维码图片中。

1.模式选择:

微信扫码支付,有两种模式,文档中有介绍。第二种模式,微信接口会返回二维码信息给我们。而第一种模式则需要我们自己去生成二维码信息。会有些麻烦。尤其是参数大小写,还有签名的问题,容易出错。总的来说第二种模式比第一种模式简单。所有我采用的是第二种模式,比较通用。京东与携程亦用的是第二种模式。

2.调用统一下单接口获取带有二维码信息的url:(模式二)

模式二的微信扫码支付,需要先调用微信的统一下单接口,生成预交易单。(参数传递与接收都是XML 数据格式。)

正确调用后,会返回含有交易标示ID,和二维码链接的URL。

HashMap<String, String> paramMap = Maps.newHashMap(); paramMap.put("trade_type", "NATIVE"); //交易类型paramMap.put("spbill_create_ip",localIp()); //本机的IpparamMap.put("product_id", payOrderIdsStr); // 商户根据自己业务传递的参数 必填paramMap.put("body", orderSubject);         //描述paramMap.put("out_trade_no", payOrderIdsStr); //商户 后台的贸易单号paramMap.put("total_fee", "" + totalCount); //金额必须为整数  单位为分paramMap.put("notify_url", "http://" + getAccessDomain() + "/wx_pay_notify"); //支付成功后,回调地址     paramMap.put("appid", siteConfig.getWxPayAppId()); //appidparamMap.put("mch_id", siteConfig.getWxPayMchId()); //商户号      paramMap.put("nonce_str", CommonUtilPub.createNoncestr(32));  //随机数  paramMap.put("sign",CommonUtilPub.getSign(paramMap,siteConfig.getWxPayPartnerKey()));//根据微信签名规则,生成签名 
String xmlData = CommonUtilPub.mapToXml(paramMap);//把参数转换成XML数据格式
 1 /** 2      * 获取本机Ip  3      *   4      *  通过 获取系统所有的networkInterface网络接口 然后遍历 每个网络下的InterfaceAddress组。 5      *  获得符合 <code>InetAddress instanceof Inet4Address</code> 条件的一个IpV4地址 6      * @return 7      */ 8     @SuppressWarnings("rawtypes") 9     private String localIp(){10         String ip = null;11         Enumeration allNetInterfaces;12         try {13             allNetInterfaces = NetworkInterface.getNetworkInterfaces();            14             while (allNetInterfaces.hasMoreElements()) {15                 NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();16                 List<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses();17                 for (InterfaceAddress add : InterfaceAddress) {18                     InetAddress Ip = add.getAddress();19                     if (Ip != null && Ip instanceof Inet4Address) {20                         ip = Ip.getHostAddress();21                     }22                 }23             }24         } catch (SocketException e) {25             // TODO Auto-generated catch block        26             logger.warn("获取本机Ip失败:异常信息:"+e.getMessage());27         }28         return ip;29     }

 

 成功时返回的XML数据为:

 1 <xml><return_code><![CDATA[SUCCESS]]></return_code> 2 <return_msg><![CDATA[OK]]></return_msg> 3 <appid><![CDATA[wx49342bda0ef105dd]]></appid> 4 <mch_id><![CDATA[10019460]]></mch_id> 5 <nonce_str><![CDATA[UneMQd4qWQd0hJ4L]]></nonce_str> 6 <sign><![CDATA[C621A9C586C1F0397D4C6B8003E0CBCE]]></sign> 7 <result_code><![CDATA[SUCCESS]]></result_code> 8 <prepay_id><![CDATA[wx2015070818251790742fea5e0865034508]]></prepay_id> 9 <trade_type><![CDATA[NATIVE]]></trade_type>10 <code_url><![CDATA[weixin://wxpay/bizpayurl?pr=AOFEsxf]]></code_url>11 </xml>

解析XML 获取 code_url:

 1 String resXml = HtmlUtil.postData("https://api.mch.weixin.qq.com/pay/unifiedorder", xmlData); 2 Document dd = null; 3 String code_url=null; 4 try { 5     dd = DocumentHelper.parseText(resXml); 6   } catch (DocumentException e) { 7        return "";  8 } 9 if (dd != null) {10     Element root = dd.getRootElement();11     if (root == null) {12     return "";13     }14     Element codeUrl = root.element("code_url");15     if (piEle == null) {16     return "";17     }  18     code_url = codeUrl.getText();  //解析 xml 获得 code_url
19 }

 

3.动态生成二维码图片

使用的是google ZXing库。 提供一个 jar 地址 直接引入到自己项目即可。http://download.csdn.net/detail/gonwy/7658135  

 页面代码:

<img src="qr_code.img?code_url= <#if code_url??>${code_url}</#if>" style="width:300px;height:300px;"/>

java 代码:

1
2
3
4
5
6
7
8
9
10
/**
   * 生成二维码图片并直接以流的形式输出到页面
   * @param code_url
   * @param response
   */
  @RequestMapping("qr_code.img")
  @ResponseBody
  publicvoid getQRCode(String code_url,HttpServletResponse response){
    GenerateQrCodeUtil.encodeQrcode(code_url, response);
  }
 1 /** 2      * 生成二维码图片 不存储 直接以流的形式输出到页面 3      * @param content 4      * @param response 5      */ 6     @SuppressWarnings({ "unchecked", "rawtypes" }) 7     public static void encodeQrcode(String content,HttpServletResponse response){ 8         if(StringUtils.isBlank(content)) 9             return;10         MultiFormatWriter multiFormatWriter = new MultiFormatWriter();11         Map hints = new HashMap();12         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型13         BitMatrix bitMatrix = null;14         try {15             bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300,hints);16             BufferedImage image = toBufferedImage(bitMatrix);17             //输出二维码图片流18             try {19                 ImageIO.write(image, "png", response.getOutputStream());20             } catch (IOException e) {21                 // TODO Auto-generated catch block22                 e.printStackTrace();23             }24         } catch (WriterException e1) {25             // TODO Auto-generated catch block26             e1.printStackTrace();27         }         28     }

然后生成的图片,通过微信扫码就可以发起支付了。

支付成功后,微信会调用,你之前设置的回调函数地址。并且会把你之前传给微信的商户自定义参数带给你,以帮助商户完成余下业务流程。



转载自:http://www.w2bc.com/article/49216

0 0
原创粉丝点击