一、准备工作
首先吐槽一下微信关于支付这块,本身支持的支付模式就好几种,但是官方文档特别零散,连像样的Java相关的demo也没几个。本人之前没有搞过微信支付,一开始真是被它搞晕,折腾两天终于调通了,特此写下来,以享后人吧!
关于准备工作,就“微信扫码支付模式二”官方文档地址在这 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1 可以先看看,实际上需要准备的东西有以下几个:
其中APP_ID和APP_SECRET可以在公众平台找着,MCH_ID和API_KEY则在商户平台找到,特别是API_KEY要在商户平台设置好,对于“微信扫码支付模式二”(支付与回调)实际只会用到APP_ID、MCH_ID和API_KEY,其他的都不用。
关于开发环境,我就不罗嗦了,不管你是springMVC、struts2又或者直接serverlet,都差不多,只要你能保证对应的方法能调用起来就行。关于引用第三方的jar包,我这里只用到了一个xml操作的jdom,记住是1.*的版本,不是官网上最新的2.*,两者不兼容。具体是jdom-1.1.3.jar,依赖包jaxen-1.1.6.jar,就这两个包,我没用到有些例子中使用的httpclient,感觉没必要,而且依赖包特别繁杂,当然你是maven当我没说。
二、开发实战
1、首先是接入微信接口,获取微信支付二维码。
-
public String weixin_pay() throws Exception {
-
-
String appid = PayConfigUtil.APP_ID;
-
-
String mch_id = PayConfigUtil.MCH_ID;
-
String key = PayConfigUtil.API_KEY;
-
-
String currTime = PayCommonUtil.getCurrTime();
-
String strTime = currTime.substring(8, currTime.length());
-
String strRandom = PayCommonUtil.buildRandom(4) + "";
-
String nonce_str = strTime + strRandom;
-
-
String order_price = 1;
-
String body = "goodssssss";
-
String out_trade_no = "11338";
-
-
-
String spbill_create_ip = PayConfigUtil.CREATE_IP;
-
-
String notify_url = PayConfigUtil.NOTIFY_URL;
-
String trade_type = "NATIVE";
-
-
SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();
-
packageParams.put("appid", appid);
-
packageParams.put("mch_id", mch_id);
-
packageParams.put("nonce_str", nonce_str);
-
packageParams.put("body", body);
-
packageParams.put("out_trade_no", out_trade_no);
-
packageParams.put("total_fee", order_price);
-
packageParams.put("spbill_create_ip", spbill_create_ip);
-
packageParams.put("notify_url", notify_url);
-
packageParams.put("trade_type", trade_type);
-
-
String sign = PayCommonUtil.createSign("UTF-8", packageParams,key);
-
packageParams.put("sign", sign);
-
-
String requestXML = PayCommonUtil.getRequestXml(packageParams);
-
System.out.println(requestXML);
-
-
String resXml = HttpUtil.postData(PayConfigUtil.UFDODER_URL, requestXML);
-
-
-
Map map = XMLUtil.doXMLParse(resXml);
-
-
-
String urlCode = (String) map.get("code_url");
-
-
return urlCode;
-
}
如果不出意外的话,这里就从微信服务器获取了一个支付url,形如weixin://wxpay/bizpayurl?pr=pIxXXXX,之后我们就需要把这个url生成一个二维码,然后就可以使用自己手机微信端扫码支付了。关于二维码生成有很多种方法,各位各取所需吧,我这里提供一个google的二维码生成接口:
-
public static String QRfromGoogle(String chl) throws Exception {
-
int widhtHeight = 300;
-
String EC_level = "L";
-
int margin = 0;
-
chl = UrlEncode(chl);
-
String QRfromGoogle = "http://chart.apis.google.com/chart?chs=" + widhtHeight + "x" + widhtHeight
-
+ "&cht=qr&chld=" + EC_level + "|" + margin + "&chl=" + chl;
-
-
return QRfromGoogle;
-
}
-
-
public static String UrlEncode(String src) throws UnsupportedEncodingException {
-
return URLEncoder.encode(src, "UTF-8").replace("+", "%20");
-
}
上面代码中涉及到几个工具类:PayConfigUtil、PayCommonUtil、HttpUtil和XMLUtil,其中PayConfigUtil放的就是上面提到一些配置及路径,PayCommonUtil涉及到了获取当前事件、产生随机字符串、获取参数签名和拼接xml几个方法,代码如下:
HttpUtil和XMLUtil如下:
-
public class HttpUtil {
-
-
private static final Log logger = Logs.get();
-
private final static int CONNECT_TIMEOUT = 5000;
-
private final static String DEFAULT_ENCODING = "UTF-8";
-
-
public static String postData(String urlStr, String data){
-
return postData(urlStr, data, null);
-
}
-
-
public static String postData(String urlStr, String data, String contentType){
-
BufferedReader reader = null;
-
try {
-
URL url = new URL(urlStr);
-
URLConnection conn = url.openConnection();
-
conn.setDoOutput(true);
-
conn.setConnectTimeout(CONNECT_TIMEOUT);
-
conn.setReadTimeout(CONNECT_TIMEOUT);
-
if(contentType != null)
-
conn.setRequestProperty("content-type", contentType);
-
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);
-
if(data == null)
-
data = "";
-
writer.write(data);
-
writer.flush();
-
writer.close();
-
-
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
-
StringBuilder sb = new StringBuilder();
-
String line = null;
-
while ((line = reader.readLine()) != null) {
-
sb.append(line);
-
sb.append("\r\n");
-
}
-
return sb.toString();
-
} catch (IOException e) {
-
logger.error("Error connecting to " + urlStr + ": " + e.getMessage());
-
} finally {
-
try {
-
if (reader != null)
-
reader.close();
-
} catch (IOException e) {
-
}
-
}
-
return null;
-
}
-
}
-
public class XMLUtil {
-
-
-
-
-
-
-
-
public static Map doXMLParse(String strxml) throws JDOMException, IOException {
-
strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");
-
-
if(null == strxml || "".equals(strxml)) {
-
return null;
-
}
-
-
Map m = new HashMap();
-
-
InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
-
SAXBuilder builder = new SAXBuilder();
-
Document doc = builder.build(in);
-
Element root = doc.getRootElement();
-
List list = root.getChildren();
-
Iterator it = list.iterator();
-
while(it.hasNext()) {
-
Element e = (Element) it.next();
-
String k = e.getName();
-
String v = "";
-
List children = e.getChildren();
-
if(children.isEmpty()) {
-
v = e.getTextNormalize();
-
} else {
-
v = XMLUtil.getChildrenText(children);
-
}
-
-
m.put(k, v);
-
}
-
-
-
in.close();
-
-
return m;
-
}
-
-
-
-
-
-
-
public static String getChildrenText(List children) {
-
StringBuffer sb = new StringBuffer();
-
if(!children.isEmpty()) {
-
Iterator it = children.iterator();
-
while(it.hasNext()) {
-
Element e = (Element) it.next();
-
String name = e.getName();
-
String value = e.getTextNormalize();
-
List list = e.getChildren();
-
sb.append("<" + name + ">");
-
if(!list.isEmpty()) {
-
sb.append(XMLUtil.getChildrenText(list));
-
}
-
sb.append(value);
-
sb.append("</" + name + ">");
-
}
-
}
-
-
return sb.toString();
-
}
-
-
}
当然还有一个MD5计算工具类
-
public class MD5Util {
-
-
private static String byteArrayToHexString(byte b[]) {
-
StringBuffer resultSb = new StringBuffer();
-
for (int i = 0; i < b.length; i++)
-
resultSb.append(byteToHexString(b[i]));
-
-
return resultSb.toString();
-
}
-
-
private static String byteToHexString(byte b) {
-
int n = b;
-
if (n < 0)
-
n += 256;
-
int d1 = n / 16;
-
int d2 = n % 16;
-
return hexDigits[d1] + hexDigits[d2];
-
}
-
-
public static String MD5Encode(String origin, String charsetname) {
-
String resultString = null;
-
try {
-
resultString = new String(origin);
-
MessageDigest md = MessageDigest.getInstance("MD5");
-
if (charsetname == null || "".equals(charsetname))
-
resultString = byteArrayToHexString(md.digest(resultString
-
.getBytes()));
-
else
-
resultString = byteArrayToHexString(md.digest(resultString
-
.getBytes(charsetname)));
-
} catch (Exception exception) {
-
}
-
return resultString;
-
}
-
-
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
-
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
-
-
}
2、支付回调
支付完成后,微信会把相关支付结果和用户信息发送到我们上面指定的那个回调地址,我们需要接收处理,并返回应答。对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功。 (通知频率为15/15/30/180/1800/1800/1800/1800/3600,单位:秒)
关于支付回调接口,我们首先要对于支付结果通知的内容进行签名验证,然后根据支付结果进行相应的处理流程即可。
-
public void weixin_notify(HttpServletRequest request,HttpServletResponse response) throws Exception{
-
-
-
InputStream inputStream ;
-
StringBuffer sb = new StringBuffer();
-
inputStream = request.getInputStream();
-
String s ;
-
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
-
while ((s = in.readLine()) != null){
-
sb.append(s);
-
}
-
in.close();
-
inputStream.close();
-
-
-
Map<String, String> m = new HashMap<String, String>();
-
m = XMLUtil.doXMLParse(sb.toString());
-
-
-
SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();
-
Iterator it = m.keySet().iterator();
-
while (it.hasNext()) {
-
String parameter = (String) it.next();
-
String parameterValue = m.get(parameter);
-
-
String v = "";
-
if(null != parameterValue) {
-
v = parameterValue.trim();
-
}
-
packageParams.put(parameter, v);
-
}
-
-
-
String key = PayConfigUtil.API_KEY;
-
-
logger.info(packageParams);
-
-
if(PayCommonUtil.isTenpaySign("UTF-8", packageParams,key)) {
-
-
-
-
String resXml = "";
-
if("SUCCESS".equals((String)packageParams.get("result_code"))){
-
-
-
String mch_id = (String)packageParams.get("mch_id");
-
String openid = (String)packageParams.get("openid");
-
String is_subscribe = (String)packageParams.get("is_subscribe");
-
String out_trade_no = (String)packageParams.get("out_trade_no");
-
-
String total_fee = (String)packageParams.get("total_fee");
-
-
logger.info("mch_id:"+mch_id);
-
logger.info("openid:"+openid);
-
logger.info("is_subscribe:"+is_subscribe);
-
logger.info("out_trade_no:"+out_trade_no);
-
logger.info("total_fee:"+total_fee);
-
-
-
-
logger.info("支付成功");
-
-
resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
-
+ "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
-
-
} else {
-
logger.info("支付失败,错误信息:" + packageParams.get("err_code"));
-
resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
-
+ "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
-
}
-
-
-
-
BufferedOutputStream out = new BufferedOutputStream(
-
response.getOutputStream());
-
out.write(resXml.getBytes());
-
out.flush();
-
out.close();
-
} else{
-
logger.info("通知签名验证失败");
-
}
-
-
}
签名验证算法和签名生成的算法类似,在上面PayCommonUtil工具类中提供。
三、后话
感觉微信扫描支付体验效果还是挺好的,唯一缺点就是相关文档零散,官方的demo居然没有java编写的,希望之后微信官方能够逐步完善吧!