微信公众号开发---微信支付之H5页面WAP端接入

更新:2015年12月3日微信提供 Wap 支付, 开发者文档:【微信支付】开发者文档

1.前言

公司是通过支付宝和微信支付那块内容获取收入,app端已经接入成功,现在要做WAP端。需要页面和后台接口一起来实现。

2.接口接入

因为微信支付版本更新了,网上下的demo是V2.5版的,用不了了。所以去网上找资料,看到最新版的V3。

这里我找到了一个统一下单接口,文档入口.

他的接口地址为:https://api.mch.weixin.qq.com/pay/unifiedorder

因此,开始接入我所需要的wap端参数。

这里需要的参数关键有Appid,mch_id,key。

appid和mch_id是在公众平台那边获取。key值是在商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置 这边自己设置的。

坑一:若key值设置不对,会出现错误“支付权限查询失败” 。这时候请检查 appid,mch_id所在的公众号 对应 商户号的key值是否正确。

坑二:我在开发中还遇到“您没有WAP支付权限” 这么个错误。百了很久都没人遇到这个坑。于是,发送邮件给微信支付(weixinpay@tencent.comwepayTS@tencent.com)这两个邮件我都发了,结果还是漫无回应啊。于是,打通了商户平台的客服(0755-86018333),客服是MM,估计不懂技术问题,叫我去提问平台提交问题(http://kf.qq.com//bills/150821samab01c976f2a.html),说是技术人员看到会回复的,我问是不是 马上回复,MM不说,就说会回复的,唉,毕竟人家客服不懂,就没继续问下去了。打开客服给的网址,填写的时候,发现没有WAP端,也没有统一下单这说法,那我只好填写了  网页(JSAPI)支付 ,下面在详细说明,提交后,出现了个提示,说是七天内给个回应。我去,那还不是白忙活,要7天 业务紧急啊。。

3.代码编写

(1).获取统一下单参数

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public String CreateWapUrl(String outTradeNo, String ip) throws SDKRuntimeException {  
  2.     HashMap<String, Object> param = new HashMap<String, Object>();  
  3.     param.put("appid", WxPayConfig.APPID);  
  4.     param.put("mch_id", WxPayConfig.MCHID);  
  5.     param.put("nonce_str", CommonUtil.CreateNoncestr());  
  6.     param.put("body""产品测试");  
  7.     param.put("out_trade_no", outTradeNo);  
  8.     param.put("total_fee"1);  
  9.     param.put("spbill_create_ip", ip);  
  10.     param.put("notify_url", WxPayConfig.NOTIFYURL);  
  11.     param.put("trade_type""WAP");  
  12.     param.put("sign", getSign(param));  
  13.     return CommonUtil.MapToXml(param);  
  14. }  

(2).获取签名值

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public String getSign(HashMap<String, Object> param) throws SDKRuntimeException {  
  2.     String sign="";  
  3.     String content = CommonUtil.FormatParamMap(param);  
  4.     sign =  Sign(content, WxPayConfig.KEY);  
  5.     return sign;  
  6. }  
  7.   
  8. public static String Sign(String content, String key) throws SDKRuntimeException {  
  9.     String signStr = "";  
  10.     if ("" == key) {  
  11.         throw new SDKRuntimeException("财付通签名key不能为空!");  
  12.     }  
  13.     if ("" == content) {  
  14.         throw new SDKRuntimeException("财付通签名内容不能为空");  
  15.     }  
  16.     signStr = content + "&key=" + key;  
  17.     return MD5Util.MD5(signStr).toUpperCase();  
  18. }  

(3).工具类方法

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static boolean IsNumeric(String str) {  
  2.     if (str.matches("\\d *")) {  
  3.         return true;  
  4.     } else {  
  5.         return false;  
  6.     }  
  7. }  
  8.   
  9. //map转成xml  
  10. public static String MapToXml(HashMap<String, Object> arr) {  
  11.     String xml = "<xml>";  
  12.       
  13.     Iterator<Entry<String, Object>> iter = arr.entrySet().iterator();  
  14.     while (iter.hasNext()) {  
  15.         Entry<String, Object> entry = iter.next();  
  16.         String key = entry.getKey();  
  17.         String val = entry.getValue()+"";  
  18.         if (IsNumeric(val)) {  
  19.             xml += "<" + key + ">" + val + "</" + key + ">";  
  20.   
  21.         } else  
  22.             xml += "<" + key + "><![CDATA[" + val + "]]></" + key + ">";  
  23.     }  
  24.   
  25.     xml += "</xml>";  
  26.     return xml;  
  27. }  
  28.   
  29. //xml转成map  
  30. @SuppressWarnings("unchecked")  
  31. public static Map<String, String> parseXml(String xml) throws Exception {  
  32.      Map<String, String> map = new HashMap<String, String>();  
  33.      Document document = DocumentHelper.parseText(xml);  
  34.      Element root = document.getRootElement();  
  35.      List<Element> elementList = root.elements();  
  36.      for (Element e : elementList) {  
  37.          map.put(e.getName(), e.getText());  
  38.      }  
  39.      return map;  
  40. }  
  41.   
  42.   
  43. public static String FormatParamMap(HashMap<String, Object> parameters) throws SDKRuntimeException {  
  44.     String buff = "";  
  45.     try {  
  46.         List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(  
  47.                 parameters.entrySet());  
  48.         Collections.sort(infoIds,  
  49.                 new Comparator<Map.Entry<String, Object>>() {  
  50.                     public int compare(Map.Entry<String, Object> o1,  
  51.                             Map.Entry<String, Object> o2) {  
  52.                         return (o1.getKey()).toString().compareTo(  
  53.                                 o2.getKey());  
  54.                     }  
  55.                 });  
  56.   
  57.         for (int i = 0; i < infoIds.size(); i++) {  
  58.             Map.Entry<String, Object> item = infoIds.get(i);  
  59.             if (item.getKey() != "") {  
  60.                 buff += item.getKey() + "="  
  61.                         + URLEncoder.encode(item.getValue()+"""utf-8") + "&";  
  62.             }  
  63.         }  
  64.         if (buff.isEmpty() == false) {  
  65.             buff = buff.substring(0, buff.length() - 1);  
  66.         }  
  67.     } catch (Exception e) {  
  68.         throw new SDKRuntimeException(e.getMessage());  
  69.     }  
  70.     return buff;  
  71. }  
  72.   
  73. public static String CreateNoncestr() {  
  74.     String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
  75.     String res = "";  
  76.     for (int i = 0; i < 16; i++) {  
  77.         Random rd = new Random();  
  78.         res += chars.charAt(rd.nextInt(chars.length() - 1));  
  79.     }  
  80.     return res;  
  81. }  

(4).发送请求方法

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static String sendPost(String url, String param,String charset) {  
  2.         PrintWriter out = null;  
  3.         BufferedReader in = null;  
  4.         String result = "";  
  5.         try {  
  6.             URL realUrl = new URL(url);  
  7.             // 打开和URL之间的连接  
  8.             URLConnection conn = realUrl.openConnection();  
  9.             // 设置通用的请求属性  
  10.             conn.setRequestProperty("accept""*/*");  
  11.             conn.setRequestProperty("connection""Keep-Alive");  
  12.             conn.setRequestProperty("user-agent""Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
  13.             
  14.             // 发送POST请求必须设置如下两行  
  15.             conn.setDoOutput(true);  
  16.             conn.setDoInput(true);  
  17.             // 获取URLConnection对象对应的输出流  
  18.             out = new PrintWriter(conn.getOutputStream());  
  19.             // 发送请求参数  
  20.             out.print(new String(param.getBytes(),charset));  
  21.             // flush输出流的缓冲  
  22.             out.flush();  
  23.             // 定义BufferedReader输入流来读取URL的响应  
  24.             in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));  
  25.             String line;  
  26.             while ((line = in.readLine()) != null) {  
  27.                 result += line;  
  28.             }  
  29.         } catch (Exception e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.         //使用finally块来关闭输出流、输入流  
  33.         finally{  
  34.             try{  
  35.                 if(out!=null){  
  36.                     out.close();  
  37.                 }  
  38.                 if(in!=null){  
  39.                     in.close();  
  40.                 }  
  41.             }  
  42.             catch(IOException ex){  
  43.                 ex.printStackTrace();  
  44.             }  
  45.         }  
  46.         return result;  
  47.     }      

(5).执行接口

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //网页版微信支付接口  
  2. public String wxWapPay() throws Exception {  
  3.     String result = SUCCESS;  
  4.     String message = "";  
  5.     int code = 0;  
  6.     try {  
  7.         String ip = getIpAddr(request);  
  8.         String outTradeNo = new SimpleDateFormat("YYYYMMDDHHmmssSSS").format(new Date())+"-wap";  
  9.         String param = new WxPayHelper().CreateWapUrl(outTradeNo, ip);  
  10.         String resp = HttpRequest.sendPost(WxPayConfig.UNIFIEDORDER_INTERFACE, param, "utf-8");  
  11.         Map<String, String> res = CommonUtil.parseXml(resp);  
  12.           
  13.         if(res.get("return_code") == "SUCCESS") {  
  14.             if(res.get("result_code") == "SUCCESS") {  
  15.                 message = res.get("code_url");  
  16.             }else {  
  17.                 code = -1;  
  18.                 message = res.get("err_code_des");  
  19.                 logger.error("wxWapPay error code"+res.get("err_code")+", reason is "+res.get("err_code_des"));  
  20.             }  
  21.         }else {  
  22.             code = -1;  
  23.             message = res.get("return_msg");  
  24.             logger.error("wxWapPay error reason is "+res.get("return_msg"));  
  25.         }  
  26.     } catch (Exception e) {  
  27.         code = -1;  
  28.         logger.error("wxWapPay Exception reason is "+ e);  
  29.         e.printStackTrace();  
  30.     }  
  31.     dataMap = new HashMap<String, Object>();  
  32.     dataMap.put("code", code);  
  33.     dataMap.put("message", message);  
  34.       
  35.     return result;  
  36. }  


更多参考

https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值