应用场景
该接口主要用于扫码原生支付静态支付中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),减小二维码数据量,提升扫描速度和精确度。
官方在线文档:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=9_9
接口地址
https://api.mch.weixin.qq.com/tools/shorturl
应用此接口前请保证你的扫码原生支付静态支付可以正常支付。
参考代码如下:
package com.wingo.action.config;
importjava.util.Date;
importjava.util.Map;
importjava.util.SortedMap;
importjava.util.TreeMap;
importcom.wingo.util.CommonUtil;
importcom.wingo.util.ConfigUtil;
importcom.wingo.util.PayCommonUtil;
importcom.wingo.util.XMLUtil;
/**
*
* @author 李欣桦
* @date 2015-1-6下午5:14:01
*
* 将静态native支付链接转为短链接
*/
public classShortAction {
public static void main(String[] args)throws Exception{
SortedMap
paras.put("appid",ConfigUtil.APPID);
paras.put("mch_id",ConfigUtil.MCH_ID);
paras.put("time_stamp",Long.toString(new Date().getTime()));
paras.put("nonce_str",PayCommonUtil.CreateNoncestr());
paras.put("product_id","No.201401051607001");//商品号要唯一
String sign =PayCommonUtil.createSign("UTF-8", paras);
paras.put("sign", sign);
String url ="weixin://wxpay/bizpayurl?sign=SIGN&appid=APPID&mch_id=MCHID&product_id=PRODUCTID&time_stamp=TIMESTAMP&nonce_str=NOCESTR";
String nativeUrl =url.replace("SIGN", sign).
replace("APPID", ConfigUtil.APPID).
replace("MCHID", ConfigUtil.MCH_ID).
replace("PRODUCTID",(String)paras.get("product_id")).
replace("TIMESTAMP",(String)paras.get("time_stamp")).
replace("NOCESTR", (String)paras.get("nonce_str"));
System.out.println("nativeUrl="+nativeUrl);
SortedMap
parameters.put("appid",ConfigUtil.APPID);
parameters.put("mch_id",ConfigUtil.MCH_ID);
parameters.put("nonce_str",PayCommonUtil.CreateNoncestr());
parameters.put("long_url",CommonUtil.urlEncodeUTF8(nativeUrl));
String sign2 =PayCommonUtil.createSign("UTF-8", parameters);
parameters.put("sign", sign2);
String requestXML = PayCommonUtil.getRequestXml(parameters);
String result=CommonUtil.httpsRequest(ConfigUtil.SHORT_URL, "POST", requestXML);
System.out.println(result);
Map
String returnCode =map.get("return_code");
String resultCode =map.get("result_code");
if(returnCode.equalsIgnoreCase("SUCCESS")&&resultCode.equalsIgnoreCase("SUCCESS")){
String shortUrl =map.get("short_url");
//TODO 拿到shortUrl,写代码生成二维码
System.out.println("shortUrl="+shortUrl);
}
}
}