媒介
最近一个项目顶用到了微信开拓,之前没有做过付出相关的对象,算是拿这个来练练手,刚开始打仗付出时候很懵逼,加上微信付出开拓文档原来就讲得不清楚,我是彻底蒙圈了,参考了许多代码之后,算是有一点思路了。
用户认证获取openId
假如你常识存眷付出流程,这块可以跳过,因为我知道这些你已经做过了,在开始所有的流程之前,我以为你应该把所有微信相关的设置放到一个properties文件中去,这样不只显得更类型,并且会制止犯许多错误,真是一个完美的选择!
######## 设置文件 ######## 公家号开拓设置中的token(自界说) wechat.token= ######## 应用id wechat.appId= ######## 密钥(同token查察地点) wechat.appSecret= ######## 静默授权微信回调url wechat.callBackSlientUrl= ######## 商户Id(付出相关) wechat.MCHID= ######## 微信下单地点 wechat.wxorder=https://api.mch.weixin.qq.com/pay/unifiedorder ######## 付出api密钥 wechat.KEY= ######## 付出功效回调地点 wechat.NOTIFYURL=
接着你可以思量把这个properties注入到一个bean中,利用更利便,虽然你还可以选择利用java来读取properties的设置,比拟这两个要领,我更喜欢第一个,我就利用第一种要领来演示一下(这里利用spring boot框架,spring mvc雷同)
/** * <p>Created on 2017/3/13.</p> * * @author StormMma * * @Description: 微信相关常量 */ @Component @ConfigurationProperties(locations = {"classpath:config/wechat.properties"}, prefix = "wechat") public class WeChatConfigBean { /** * token */ private String token; /** * app id */ private String appId; /** * app secret */ private String appSecret; /** * 静默授权回调地点 */ private String callBackSlientUrl; /** * 商户id */ private String MCHID; /** * 异步回调地点 */ private String NOTIFYURL; /** * 微信统一下单地点 */ private String wxorder; /** * key */ private String KEY; public String getToken() { return token; } public void setToken(String token) { this.token = token; } public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getAppSecret() { return appSecret; } public void setAppSecret(String appSecret) { this.appSecret = appSecret; } public String getCallBackSlientUrl() { return callBackSlientUrl; } public void setCallBackSlientUrl(String callBackSlientUrl) { this.callBackSlientUrl = callBackSlientUrl; } public String getMCHID() { return MCHID; } public void setMCHID(String MCHID) { this.MCHID = MCHID; } public String getNOTIFYURL() { return NOTIFYURL; } public void setNOTIFYURL(String NOTIFYURL) { this.NOTIFYURL = NOTIFYURL; } public String getWxorder() { return wxorder; } public void setWxorder(String wxorder) { this.wxorder = wxorder; } public String getKEY() { return KEY; } public void setKEY(String KEY) { this.KEY = KEY; }
封装请求东西(这次我选择利用HttpClient, 此处的json东西我选择了ali的fastjson)
RequestUtil.java
/** * 发送Get请求到url,得到response的json实体 * @param url * @return * @throws IOException */ private JSONObject doGetUrl(String url) throws WechatException, ServerSystemException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response; String result; try { response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity, "UTF-8"); httpclient.close(); } catch (IOException e) { logger.error("执行GET请求产生错误!", e); throw new ServerSystemException("执行GET请求产生错误!{}", e); } return JSONObject.parseObject(result); } /** * 发送post请求 * @param url * @param param * @return * @throws ServerSystemException */ private JSONObject doPostUrl(String url, String param) throws ServerSystemException { final String CONTENT_TYPE_TEXT_JSON = "application/json"; DefaultHttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager()); HttpPost httpPost = new HttpPost(url); HttpResponse response; String result; try { StringEntity stringEntity = new StringEntity(param); stringEntity.setContentType(CONTENT_TYPE_TEXT_JSON); stringEntity.setContentEncoding("UTF-8"); httpPost.setEntity(stringEntity); response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity, "UTF-8"); httpClient.close(); } catch (IOException e) { logger.error("执行POST请求产生错误!", e); throw new ServerSystemException("执行POST请求产生错误!{}", e); } return JSONObject.parseObject(result); }
获取code
在此之前,我想我们应该抽出一个微信东西类,专门来封装各类请求和RequestUtil来团结利用,是的,这是一个很好的选择。