接着上次的博客<<JSAPI-------微信公衆號支付 java 之 第一篇>>講起,我們還差sign和openId這兩個參數就能得到prepay_id啦,加油哦,勝利在忘啦
sign
sign的工具類雖然我們在<<JSAPI-------微信公衆號支付 java 之 第一篇>>已經提供過了,但是爲了方便大家,在這篇文章中,小編在提供一次
sign工具類
/**
* 獲取簽名 md5加密(微信支付必須用MD5加密)
* 獲取支付簽名
* @param characterEncoding
* @param parameters
* @return
*/
public static String getSign(String characterEncoding,SortedMap<Object,Object> parameters){
StringBuffer sb = new StringBuffer();
Set es = parameters.entrySet();//所有參與傳參的參數按照accsii排序(升序)
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
Object v = entry.getValue();
if(null != v && !"".equals(v)
&& !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + Key);
String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
return sign;
}
有了工具類,那麼我們如果通過工具類來生成sign呢?預備向下看,集合中參數的具體含義就不多說了,因爲在<<JSAPI-------微信公衆號支付 java 之 第一篇>>中說過了,並且在統一下單開發文檔中有詳細的說明,地址再次留給大家https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
SortedMap<Object,Object> mapsign = new TreeMap<Object,Object>();
mapsign.put("appid", appid);
mapsign.put("mch_id", mch_id);
mapsign.put("out_trade_no", out_trade_no);
mapsign.put("total_fee", total_fee);
mapsign.put("trade_type", trade_type);
mapsign.put("notify_url", notify_url);
mapsign.put("body", body);
mapsign.put("nonce_str", nonce_str);
mapsign.put("spbill_create_ip", spbill_create_ip);
mapsign.put("openid", openid);
mapsign.put("attach", attach);
mapsign.put("time_start", time_start);
String sign = PayUtil.getSign("UTF-8", mapsign);//簽名 生成啦
openid的獲取
查看文檔網頁授權獲取用戶基本信息 http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
授權方式有兩種
以snsapi_base爲scope發起的網頁授權,用戶無感知
以snsapi_userinfo爲scope發起的網頁授權,用戶有感知
頁面<a href="
"></a>
重要的參數是以上兩個紅色參數 appid不多說 redirect_url爲獲得code的地址 我是用的springmvc框架,直接按照正常的參數接收寫就行了
String code = request.getParameter("code");
然後請求這個地址
下文中,我會把請求的工具類送給大家,
//get
public static String sendGet(String url, String param) throws Exception {
String result = "";
BufferedReader in = null;
try {
String urlName = "";
if (param != null) {
if (!url.contains("?")) {
// URLEncoder.encode(param, "utf-8");
urlName = url + "?" + param;
} else {
urlName = url + "&" + param;
}
} else {
urlName = url;
}
URL realUrl = new URL(urlName);
URLConnection conn = null;
try {
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)");
// 設置超時
conn.setConnectTimeout(15 * 1000);
// 設置讀超時
conn.setReadTimeout(15 * 1000);
conn.connect();
} catch (Exception e1) {
}
try {
in = new BufferedReader(new InputStreamReader(conn
.getInputStream(), "UTF-8"));
String line = null;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
} catch (Exception e2) {
}
} catch (Exception e) {
throw e;
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
}
}
return result;
}
然後就得到了opendi,然後在按照統一下單文檔的規範請求https://api.mch.weixin.qq.com/pay/unifiedorder,參數爲xml格式
就得到了prepay_id啦
然後就按照h5調起支付api進行傳參數就ok啦
艾瑪,累壞啦,去喝口水