微信支付V2账单查询接口orderquery示例代码

         关于微信的支付功能,相信不少人用过。谈谈关于微信支付接口的web开发经验吧。

        首先聊一下:

                  服务端签名支付。


        服务端签名支付,官方给出了server端的实例代码。可以在微信的开发平台下载到。代码结构图如下:

        

          导入项目以后只需要修改的是config.jsp下的参数。

          

//收款方

String spname = "xxxxx";                                           

//商户号
String partner = "xxxxxx";

//密钥
String partner_key = "xxxxx";

//appi
String app_id="xxxxx";

String app_secret = "xxxx";

//appkey
String app_key="xxxxxx";

//支付完成后的回调处理页面
String notify_url ="http://[ip]:[port]/app-java/payNotifyUrl.jsp";
//调试模式
boolean DEBUG_ = true;

        注意一点。notify_url是外网地址,因为该地址是微信服务器在支付成功以后回调给商户服务器的地址,因此该地址必须是外网可以访问到的地址,其他参数均可以在申请微信支付的时候通过微信获取到。

         将该项目运行起来以后,手机就可以通过服务端签名的方式调用支付接口了。并且微信支付成功以后,微信服务器可以将成功支付的通知反馈给server端。

关于官网给出的流程图,我们可以看到还需要一个环节:查询账单环节。

          

    为什么需要查询账单功能呢?由于官方指明说未必保证微信支付成功回调一定能通知到咱们的server服务器。为此我们在没有收到微信支付成功通知的时候得需要自己调用微信的账单查询接口查询支付状态。

   但是对于账单的支付接口,没有明确的实例代码,只有接口文档说明。那么代码就有我来写一下吧。

   账单查询首先需要获取access_token

  

public static String getAccessToken() throws Exception {
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
				+ Constants.APP_ID + "&secret=" + Constants.APP_SECRET;
		String json = HttpUtils.get(url);
		JSONObject jsonObject = JSONObject.fromObject(json);
		String accessToken = jsonObject.get("access_token").toString();
		return accessToken;
	}

获取账单状态:

	public static boolean orderquery(String order) throws Exception {
		String access_token = getAccessToken();
		String url = "https://api.weixin.qq.com/pay/orderquery?access_token="
				+ access_token;

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("appid", Constants.APP_ID);

		String timestamp = Long.toString(System.currentTimeMillis() / 1000);
		jsonObject.put("timestamp", timestamp);
		jsonObject.put("sign_method", "sha1");
		String packageStr = "";

		String out_trade_no = order;
		String sign = "out_trade_no=" + out_trade_no + "&partner="
				+ Constants.PARTNER + "&key=" + Constants.PARTNER_KEY;
		sign = MD5Util.md5(sign);
		sign = sign.toUpperCase();

		packageStr = "out_trade_no=" + out_trade_no + "&partner="
				+ Constants.PARTNER + "&sign=" + sign;

		jsonObject.put("package", packageStr);

		SortedMap<String, String> prePayParams = new TreeMap<String, String>();
		prePayParams.put("appid", Constants.APP_ID);
		prePayParams.put("appkey", Constants.APP_KEY);
		prePayParams.put("package", packageStr);
		prePayParams.put("timestamp", timestamp);
		// 生成签名
		String app_signature = Sha1Util.createSHA1Sign(prePayParams);
		jsonObject.put("app_signature", app_signature);
		String json = HttpUtils.postString(url, jsonObject.toString());
		JSONObject mdata = JSONObject.fromObject(json);
		String errcode = mdata.get("errcode").toString();
		if (errcode.equals("0")) {
			JSONObject order_info = JSONObject.fromObject(mdata.get(
					"order_info").toString());
			String ret_code = order_info.get("ret_code").toString();
			if (ret_code.equals("0")) {
				return true;
			}
		}
		return false;
	}

补充说明:

代码中的Constans中的变量是定义的微信支付需要的变量,该信息均可在微信开放平台上找到。Sha1Utils类在服务器签名代码中有提供。


QQ群:

       255825960

关于v3支付接口,我也整理了一下供大家参考:http://pan.baidu.com/s/1pJGDqoN

猜你喜欢

转载自blog.csdn.net/woaixinxin123/article/details/44044445