java调用微信v3版本支付接口

最近做了个小项目,需要调用微信支付接口,看了官方文档,整个流程介绍的并不太好,在这里自己总结一下,以便以后使用

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.可以在页面中写一个调用授权的页面

<%@ 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 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

裂魂人1214

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值