1.页面发请求:到action。
url 授权:https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=URLEncoder.encode(url)
&response_type=code&scope=snsapi_userinfo&state=xxx_state#wechat_redirect;
2.重定向到请求的URL。得到code,
授权:https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=appSecret
&code=code&grant_type=authorization_code
3.返回得到openid和access_token
4.验证access_token 是否失效,
请求:https://api.weixin.qq.com/sns/auth?access_token="
+ access_token + "&openid=" + openid
返回json,获取errcode,如果errcode 不等与0,再去获取access_token
5.获取微信名和用户头像。
请求:"https://api.weixin.qq.com/sns/userinfo?access_token="
+ access_token + "&openid=" + openid + "&lang=zh_CN"
6.也可以查询商品一起返回。授权成功
7.微信支付》
支付页面发请求都后台。转过来。openid
String tradeNo = String.valueOf(new Date().getTime());
String nonceStr1 = wechatUtils.generateNonceStr();
String notifyUrl = "http://mobile.knnqq.com/wechatPay/payAsk.html";
String openId = request.getParameter("openId");
String totalFee ="1";
TreeMap<String, Object> map = new TreeMap<String, Object>();
map.put("appid", appId);
map.put("mch_id", merchantId);
map.put("device_info", "WEB");
map.put("body", body);
map.put("trade_type", "JSAPI");
map.put("nonce_str", nonceStr1);
map.put("notify_url", notifyUrl);
map.put("out_trade_no", tradeNo);
map.put("total_fee", totalFee);
map.put("openid", openId);
String sign = wechatUtils.generateSignature(map,
"商务秘钥", SignType.MD5);
String xml = "<xml>" + "<appid>" + appId + "</appid>" + "<body>" + body
+ "</body>" + "<device_info>WEB</device_info>" + "<mch_id>"
+ merchantId + "</mch_id>" + "<nonce_str>" + nonceStr1
+ "</nonce_str>" + "<notify_url>" + notifyUrl + "</notify_url>"
+ "<openid>" + openId + "</openid>" + "<out_trade_no>"
+ tradeNo + "</out_trade_no>" + "<total_fee>" + totalFee
+ "</total_fee>" + "<trade_type>JSAPI</trade_type>" + "<sign>"
+ sign + "</sign>" + "</xml>";
//向下单接口发起请求
String url = "localhost:8080/pay/unifiedorder";
HttpClient httpclient = new HttpClient();//发起请求
PostMethod post = new PostMethod(url);
String info = null;
try {
RequestEntity entity = new StringRequestEntity(xml, "text/plain",
"utf-8");
post.setRequestEntity(entity);
httpclient.executeMethod(post);
int code = post.getStatusCode();
if (code == HttpStatus.SC_OK)
info = new String(post.getResponseBodyAsString()); // 接口返回的信息
} catch (Exception ex) {
ex.printStackTrace();
} finally {
post.releaseConnection();
}
System.out.println(info);
//利用正则表达式获取prepayid
String reg = "<prepay_id><!\\[CDATA\\[(\\w+)\\]\\]></prepay_id>";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(info);
String prepayId = "";
while (matcher.find()) {
prepayId = matcher.group(1);
System.out.println(prepayId);
}
//将数据appid、timeStamp、noncestr、prepayid、paySign传如前端页面
Date beijingDate = Calendar.getInstance(Locale.CHINA).getTime();
String nonceStr2 = wechatUtils.generateNonceStr();
JSONObject json = new JSONObject();
json.put("appId", appId);
json.put("timeStamp", beijingDate.getTime() / 1000);
json.put("nonceStr", nonceStr2);
json.put("package", "prepay_id=" + prepayId);
json.put("signType", "MD5");
TreeMap<String, Object> map2 = new TreeMap<String, Object>();
map2.put("appId", appId);
map2.put("timeStamp", String.valueOf(beijingDate.getTime() / 1000));
map2.put("nonceStr", nonceStr2);
map2.put("package", "prepay_id=" + prepayId);
map2.put("signType", "MD5");
String paySign = wechatUtils.generateSignature(map2,
"商务秘钥", SignType.MD5);
json.put("paySign", paySign);
String data = json.toJSONString();
System.out.println("data************"+data);