最近做了个小项目,需要调用微信支付接口,看了官方文档,整个流程介绍的并不太好,在这里自己总结一下,以便以后使用
1.调用微信接口授权
授权这里有两种方式,一种是snsapi_bases,另一种是snsapi_userinfo,这里官方文档介绍的比较清楚,可以参考官网
我这里使用的是第一种,这里有几个需要传的参数说明一下
appid 登录微信公众号-基本配置-开发者ID-APPID
redirect_uri 就是一个回调地址,当你调用完微信的这个链接后,他会自动调用你定义的这个地址,返回code参数
注意,redirect_uri 也需要在公众号里面配置,在接口权限-网页帐号里修改,如我的是:www.xmaixle.com
2.可以在页面中写一个调用授权的页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<title>用户管理</title>
<script type="text/javascript" src="js/jquery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
function oauth() {
var url = "<%=basePath%>getOauth2Url";
$.ajax({
url: url,
success : function(result){
//var result = eval('('+result+')');
if (result.success){
location.href = result.obj; //https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx91af774aae000dcb&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2Fatoz_2014%2Fauthorize.htm&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
} else {
}
}
});
}
</script>
</head>
<body>
<p οnclick="oauth()">非用户认证鉴权</p>
</body>
</html>
</pre><pre class="html" name="code" snippet_file_name="blog_20160608_3_8825275" code_snippet_id="1711669">java中的方法
<pre class="html" name="code"> /**
* 获取授权地址
* @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.成功后,回到你当时写的回调地址
<pre class="html" name="code">/**
* 微信授权后的回调地址的方法
* @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.youkuaiyun.com/detail/b422761838/9544619