微信支付之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支付權限” 這麼個錯誤。百了很久都沒人遇到這個坑。於是,發送郵件給微信支付([email protected][email protected])這兩個郵件我都發了,結果還是漫無迴應啊。於是,打通了商戶平臺的客服(0755-86018333),客服是MM,估計不懂技術問題,叫我去提問平臺提交問題(http://kf.qq.com//bills/150821samab01c976f2a.html),說是技術人員看到會回覆的,我問是不是 馬上回復,MM不說,就說會回覆的,唉,畢竟人家客服不懂,就沒繼續問下去了。打開客服給的網址,填寫的時候,發現沒有WAP端,也沒有統一下單這說法,那我只好填寫了  網頁(JSAPI)支付 ,下面在詳細說明,提交後,出現了個提示,說是七天內給個迴應。我去,那還不是白忙活,要7天 業務緊急啊。。

3.代碼編寫

(1).獲取統一下單參數

	public String CreateWapUrl(String outTradeNo, String ip) throws SDKRuntimeException {
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("appid", WxPayConfig.APPID);
		param.put("mch_id", WxPayConfig.MCHID);
		param.put("nonce_str", CommonUtil.CreateNoncestr());
		param.put("body", "產品測試");
		param.put("out_trade_no", outTradeNo);
		param.put("total_fee", 1);
		param.put("spbill_create_ip", ip);
		param.put("notify_url", WxPayConfig.NOTIFYURL);
		param.put("trade_type", "WAP");
		param.put("sign", getSign(param));
		return CommonUtil.MapToXml(param);
	}

(2).獲取簽名值

	public String getSign(HashMap<String, Object> param) throws SDKRuntimeException {
		String sign="";
		String content = CommonUtil.FormatParamMap(param);
		sign =  Sign(content, WxPayConfig.KEY);
		return sign;
	}

	public static String Sign(String content, String key) throws SDKRuntimeException {
		String signStr = "";
		if ("" == key) {
			throw new SDKRuntimeException("財付通簽名key不能爲空!");
		}
		if ("" == content) {
			throw new SDKRuntimeException("財付通簽名內容不能爲空");
		}
		signStr = content + "&key=" + key;
		return MD5Util.MD5(signStr).toUpperCase();
	}

(3).工具類方法

	public static boolean IsNumeric(String str) {
		if (str.matches("\\d *")) {
			return true;
		} else {
			return false;
		}
	}

	//map轉成xml
	public static String MapToXml(HashMap<String, Object> arr) {
		String xml = "<xml>";
		
		Iterator<Entry<String, Object>> iter = arr.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, Object> entry = iter.next();
			String key = entry.getKey();
			String val = entry.getValue()+"";
			if (IsNumeric(val)) {
				xml += "<" + key + ">" + val + "</" + key + ">";

			} else
				xml += "<" + key + "><![CDATA[" + val + "]]></" + key + ">";
		}

		xml += "</xml>";
		return xml;
	}

	//xml轉成map
	@SuppressWarnings("unchecked")
	public static Map<String, String> parseXml(String xml) throws Exception {
		 Map<String, String> map = new HashMap<String, String>();
		 Document document = DocumentHelper.parseText(xml);
		 Element root = document.getRootElement();
		 List<Element> elementList = root.elements();
		 for (Element e : elementList) {
			 map.put(e.getName(), e.getText());
		 }
		 return map;
	}
	

	public static String FormatParamMap(HashMap<String, Object> parameters) throws SDKRuntimeException {
		String buff = "";
		try {
			List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(
					parameters.entrySet());
			Collections.sort(infoIds,
					new Comparator<Map.Entry<String, Object>>() {
						public int compare(Map.Entry<String, Object> o1,
								Map.Entry<String, Object> o2) {
							return (o1.getKey()).toString().compareTo(
									o2.getKey());
						}
					});

			for (int i = 0; i < infoIds.size(); i++) {
				Map.Entry<String, Object> item = infoIds.get(i);
				if (item.getKey() != "") {
					buff += item.getKey() + "="
							+ URLEncoder.encode(item.getValue()+"", "utf-8") + "&";
				}
			}
			if (buff.isEmpty() == false) {
				buff = buff.substring(0, buff.length() - 1);
			}
		} catch (Exception e) {
			throw new SDKRuntimeException(e.getMessage());
		}
		return buff;
	}

	public static String CreateNoncestr() {
		String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		String res = "";
		for (int i = 0; i < 16; i++) {
			Random rd = new Random();
			res += chars.charAt(rd.nextInt(chars.length() - 1));
		}
		return res;
	}

(4).發送請求方法

public static String sendPost(String url, String param,String charset) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打開和URL之間的連接
            URLConnection conn = realUrl.openConnection();
            // 設置通用的請求屬性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
          
            // 發送POST請求必須設置如下兩行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 獲取URLConnection對象對應的輸出流
            out = new PrintWriter(conn.getOutputStream());
            // 發送請求參數
            out.print(new String(param.getBytes(),charset));
            // flush輸出流的緩衝
            out.flush();
            // 定義BufferedReader輸入流來讀取URL的響應
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //使用finally塊來關閉輸出流、輸入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }    

(5).執行接口

	//網頁版微信支付接口
	public String wxWapPay() throws Exception {
		String result = SUCCESS;
		String message = "";
		int code = 0;
		try {
			String ip = getIpAddr(request);
			String outTradeNo = new SimpleDateFormat("YYYYMMDDHHmmssSSS").format(new Date())+"-wap";
			String param = new WxPayHelper().CreateWapUrl(outTradeNo, ip);
			String resp = HttpRequest.sendPost(WxPayConfig.UNIFIEDORDER_INTERFACE, param, "utf-8");
			Map<String, String> res = CommonUtil.parseXml(resp);
			
			if(res.get("return_code") == "SUCCESS") {
				if(res.get("result_code") == "SUCCESS") {
					message = res.get("code_url");
				}else {
					code = -1;
					message = res.get("err_code_des");
					logger.error("wxWapPay error code"+res.get("err_code")+", reason is "+res.get("err_code_des"));
				}
			}else {
				code = -1;
				message = res.get("return_msg");
				logger.error("wxWapPay error reason is "+res.get("return_msg"));
			}
		} catch (Exception e) {
			code = -1;
			logger.error("wxWapPay Exception reason is "+ e);
			e.printStackTrace();
		}
		dataMap = new HashMap<String, Object>();
		dataMap.put("code", code);
		dataMap.put("message", message);
		
		return result;
	}


更多參考

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









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章