最近做了个小项目,需要调用微信支付接口,看了官方文档,整个流程介绍的并不太好,在这里自己总结一下,以便以后使用
1.调用微信接口授权
授权这里有两种方式,一种是snsapi_bases,另一种是snsapi_userinfo,这里官方文档介绍的比较清楚,可以参考官网
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN
我这里使用的是第一种,这里有几个需要传的参数说明一下
appid 登录微信公众号-基本配置-开发者ID-APPID
redirect_uri 就是一个回调地址,当你调用完微信的这个链接后,他会自动调用你定义的这个地址,返回code参数
注意,redirect_uri 也需要在公众号里面配置,在接口权限-网页帐号里修改,如我的是:www.xmaixle.com
2.可以在页面中写一个调用授权的页面
用户管理
非用户认证鉴权
java中的方法
/**
* 获取授权地址
* @return
*/
@RequestMapping(value = "/getOauth2Url",method = RequestMethod.GET)
public void getOauth2Url(Model model, HttpServletResponse response) {
Json j = new Json();
try {
String authorize_url = PayConfig.AUTHORIZE_URL;
String appId = PayConfig.APPID;
String redirect_uri = PayConfig.REDIRECT_URI;
String url = authorize_url + "?appid=" + appId + "&redirect_uri=" + urlEncodeUTF8(redirect_uri) + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
System.out.println(url);
j.setSuccess(true);
j.setMsg("保存成功!");
j.setObj(url);
write(response, JSON.toJSONString(j));
} catch (Exception e) {
j.setMsg(e.getMessage());
}
}
/**
* 对回调地址进行encode
* @param source
* @return
*/
public static String urlEncodeUTF8(String source){
String result = source;
try {
result = java.net.URLEncoder.encode(source,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
package com.crm.common;
public class PayConfig {
public static String APPID = "wx91af774aae000dcb"; //APPID
public static String MCHID = "1347742701"; //商户编号
public static String PAYKEY = "kong6a9829e4b49a0d2347b4162da6b7"; //支付密钥
public static String APPSECRET = "f8e1ff34070a94332b3b128232acb5abf "; //公众号-基本配置-APPSECRET密钥
public static String REDIRECT_URI = "http://www.xmaixle.com/authorize.htm"; //回调地址
public static String AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize"; //微信授权地址
public static String ACCESS_TOKEN_URI = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
public static String SCOPE = "snsapi_base"; //snsapi_base和snsapi_userinfo
public static String GRANT_TYPE = "grant_type"; //获取openid时用,不用改
}
3.成功后,回到你当时写的回调地址
/**
* 微信授权后的回调地址的方法
* @param code
* @param response
*/
@RequestMapping(value = "/authorize",method = RequestMethod.GET)
public void authorize(String code, HttpServletResponse response, String openid) {
code = "test";
String access_token_uri = PayConfig.ACCESS_TOKEN_URI;
String appid = PayConfig.APPID;
String secret = PayConfig.APPSECRET;
access_token_uri = access_token_uri.replace("APPID", appid).replace("SECRET", secret).replace("CODE", code);
System.out.println(access_token_uri);
if(openid != null){
UniteOrder order = getUniteOrder(openid);
String reqXML = PayImpl.generateXML(order, PayConfig.PAYKEY);
String respXML = PayImpl.requestWechat(ORDER_URL, reqXML);
System.out.println("respXML=" + respXML);
UniteOrderResult result = (UniteOrderResult) PayImpl.turnObject(UniteOrderResult.class, respXML);
getWechatPay(result);
}
}
源码可以去我的资源里下载
http://download.csdn.net/detail/b422761838/9544619