接着上次的博客<<>>讲起,我们还差sign和openId这两个参数就能得到prepay_id啦,加油哦,胜利在忘啦

    sign

    sign的工具类虽然我们在<<>>已经提供过了,但是为了方便大家,在这篇文章中,小编在提供一次

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呢?预备向下看,集合中参数的具体含义就不多说了,因为在<<>>中说过了,并且在统一下单开发文档中有详细的说明,地址再次留给大家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的获取

     查看文档网页授权获取用户基本信息 

    授权方式有两种

    以snsapi_base为scope发起的网页授权,用户无感知

    以snsapi_userinfo为scope发起的网页授权,用户有感知

    页面<a href="

https://open.weixin.qq.com/connect/oauth2/authorize?appid= wx520c15f417810387&redirect_uri= https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect

"></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,然后在按照统一下单文档的规范请求

就得到了prepay_id啦

然后就按照h5调起支付api进行传参数就ok啦

艾玛,累坏啦,去喝口水