微信公众号支付,获取openId
1.获取code
1.1关注公众号后采用https://open.weixin.qq.com/connect/qrconnect
https://open.weixin.qq.com/connect/qrconnect?appid=wxd01b****c55a&redirect_uri=my-***.com%2fcheckout%2fcart&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
1.2h5网页版采用
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx87a****09713&redirect_uri=http%3A%2F%2F10.88.75.139%3A8082%2Fpay%2Fwechat.htm&response_type=code&scope=snsapi_base&state=1#wechat_redirect
2.根据appid,secret,code获取openId
/* * (non-Javadoc) * @see com.baozun.store.manager.wechat.WechatAccessTokenService#getOpenIdByAccessTokenUrl(java.lang.String, java.lang.String) */ @Override public String getOpenIdByAccessTokenUrl(String code,String authType){ // 用来存储用户的openid String openid = ""; // 过滤无效数据 if (StringUtils.isBlank(code) || StringUtils.isBlank(authType)){ return null; } // 构建accessTokenUrl String accessTokenUrl = this.buildGetAccessTokenUrl(authType, code); LOGGER.debug("wechat get access token url is {}", accessTokenUrl); // 从接口中取出accessToken数据 String json = HttpUtils.doGetReq(accessTokenUrl); JSONObject jsonobject = JSONObject.fromObject(json); // 如果调用接口成功,不会出现Errcode值 if (Validator.isNullOrEmpty(jsonobject.get("errcode"))){ openid = jsonobject.get("openid").toString(); LOGGER.info("## Get access token success,open_id is [{}]", openid); }else{ LOGGER.error( "## Get access token failed, status code is:[{}],errmsg is:[{}]", jsonobject.get("errcode"), jsonobject.get("errmsg")); } return openid; } /* * (non-Javadoc) * @see com.baozun.store.manager.wechat.WechatAccessTokenService#buildGetAccessTokenUrl(java.lang.String) */ @Override public String buildGetAccessTokenUrl(String oAuthType,String code){ //顺序链接获取微信code的url配置参数,转换于url地址 Map<String,String> connectionParam=new LinkedHashMap<String, String>(); connectionParam.put("appid", appId); connectionParam.put("secret", appSecret); String getAccessTokenUrl; // 根据授权方式选择不同url地址链接 (链接参数地址的顺序不能改变) if (WechatConstants.AUTH_TYPE_2.equalsIgnoreCase(oAuthType) || WechatConstants.AUTH_TYPE_3.equalsIgnoreCase(oAuthType)){ connectionParam.put("code", code); connectionParam.put("grant_type", WechatConstants.GRANT_TYPE_AUTHORIZATION_CODE); getAccessTokenUrl=ParamUtil.addParameterSingleValueMap(oAuthAccessTokenUrl, connectionParam, null); }else{ connectionParam.put("grant_type", WechatConstants.GRANT_TYPE_CLIENT_CREDENTIAL); getAccessTokenUrl=ParamUtil.addParameterSingleValueMap(globalAccessTokenUrl, connectionParam, null); } LOGGER.info("-------buildGetAccessTokenUrl of [{}]",getAccessTokenUrl); return getAccessTokenUrl; }