1.通过用户授权获取access_token
1.1 通过后端http请求重定向让用户授权
public final static String AUTHURL ="https://open.weixin.qq.com/connect/oauth2/authorize";
public final static String SERVERURL ="https://域名/getOpenId";
/**
*微信授权,获取access_token
* @param response
* @return
*/
@GetMapping("/wxAuth")
public void wxAuth(HttpServletResponse response) throws IOException , ServletException {
response.sendRedirect(AUTHURL+"?appid="+weixinConfig.getAppid()+"&redirect_uri=" +
SERVERURL+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
}
2.通过access_token获取用户的openid
@Override
public String getUserOpenId(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String code = req.getParameter("code");
String state = req.getParameter("state");
String res = HttpReqUtils.httpToGet("https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid="+weixinConfig.getAppid()+"&secret="+weixinConfig.getAppSecret()+"&code="+code+"&grant_type=authorization_code");
JSONObject jsonObject = (JSONObject) JSONObject.parse(res);
Map<String,Object>dataMap = new HashMap<>();
//获取微信的accessToken
dataMap.put("access_token",jsonObject.get("access_token"));
dataMap.put("refresh_token",jsonObject.get("refresh_token"));
dataMap.put("openid",jsonObject.get("openid"));
System.out.println("openid==="+jsonObject.get("openid").toString());
String openId =jsonObject.get("openid").toString();
if (null !=openId){
return openId;
}
return null;
}
3.调用微信统一下单接口,如:JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付
public MVCResultMsg<Object> jsapi(String orderNo, String openid) {
Map<String, Object> restMap = new HashMap<String, Object>();
MVCResultMsg<Object>resMsg = new MVCResultMsg<>();
WxPay weChatPay = new WxPay();
String nonce_str =WXPayUtil.generateNonceStr();
weChatPay.setAppid("")//appid
.setMch_id("")//商户id
.setNonce_str(nonce_str)
.setAttach("附加数据NO.1")
.setBody("XXX支付")
.setTotal_fee("0.01")
.setOut_trade_no("") 商户订单号 唯一
.setSpbill_create_ip("XXX.XXX.XXX.XXX")
.setCode_url("https://xxxxxxx")
.setOpenid("")//获取到的openid
.setNotify_url("https://xxxx/payBack")
.setTrade_type("JSAPI");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
try {
restMap = WxPayService.Unifiedorder(weChatPay);
Map<String,Object>dataMap = new HashMap<>();
dataMap.put("appid",weChatPay.getAppid());
dataMap.put("nonce_str",nonce_str);
dataMap.put("timeStamp",String.valueOf( System.currentTimeMillis() / 1000) );
dataMap.put("package","prepay_id="+restMap.get("prepay_id").toString());
dataMap.put("signType","MD5");
SortedMap<String, String> req = new TreeMap<String, String>();
req.put("appid",weChatPay.getAppid());
req.put("partnerid",weChatPay.getAppid());
req.put("nonceStr",nonce_str);
req.put("prepayid",nonce_str);
req.put("package",nonce_str);
req.put("noncestr",nonce_str);
req.put("timeStamp",dataMap.get("timeStamp").toString());
req.put("package","prepay_id="+restMap.get("prepay_id").toString());
dataMap.put("paySign",WXPayUtil.MD5("appId="+weChatPay.getAppid()+"&nonceStr="+nonce_str+"&package=prepay_id="+restMap.get("prepay_id").toString()+"&signType=MD5&timeStamp="+dataMap.get("timeStamp").toString()+"&key="+"apiKey"));
System.out.println("返回给前端的参数"+dataMap);
resMsg.setCode(ResultCode.SUCCESS);
resMsg.setData(dataMap);
} catch (Exception e) {
e.printStackTrace();
resMsg.setCode(ResultCode.FAIL);
resMsg.setData(null);
}
return resMsg;
}