一.流程步骤
本实例是基于springmvc框架编写
1.执行流程
当手机端app(就是你公司开发的app)在支付页面时,调起服务端(后台第1个创建订单接口)接口,后台把需要调起微信支付的参数返回给手机端,手机端拿到
这些参数后,拉起微信支付环境完成支付,完成支付后会调异步通知(第2个接口),此时需要给微信返回成功或者失败信息,成功后,由app端调用同步通知(第3个接口)
返回支付成功页面,完成整个支付流程。
2.需要说明的事项
因为微信支付都是用自己工具类生成加密、解析xml、验签等方法,需要写的类比较多,因此大家在参考此文档时,直接复制就行了
二.配置文件及通用类
1.配置文件类
ConstantUtil(各应应用id的配置,把自己应用对应的找到即可)
package com.qtkj.app.weixinpay.util; public class ConstantUtil { // 微信开发平台应用ID*
public static final String APP_ID=""; // 应用对应的凭证 appsecret
public static final String APP_SECRET=""; // 应用对应的密钥 appkey
public static final String APP_KEY=""; //微信支付商户号
public static final String MCH_ID=""; //商户id
public static final String PARTNER_ID=""; //商品描述
public static final String BODY="游戏币-账户充值"; // 获取预支付id的接口访问路径
public static String GATEURL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; // 微信服务器回调通知url
public static String NOTIFY_URL="";
}
2.工具类(一共8个,直接复制使用即可)
TenpayUtil
package com.qtkj.app.weixinpay.util; import java.text.SimpleDateFormat;
import java.util.Date; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class TenpayUtil { /**
* 把对象转换成字符串
* @param obj
* @return String 转换成字符串,若对象为null,则返回空字符串.
*/
public static String toString(Object obj) {
if(obj == null)
return ""; return obj.toString();
} /**
* 把对象转换为int数值.
*
* @param obj
* 包含数字的对象.
* @return int 转换后的数值,对不能转换的对象返回0。
*/
public static int toInt(Object obj) {
int a = 0;
try {
if (obj != null)
a = Integer.parseInt(obj.toString());
} catch (Exception e) { }
return a;
} /**
* 获取当前时间 yyyyMMddHHmmss
* @return String
*/
public static String getCurrTime() {
Date now = new Date();
SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String s = outFormat.format(now);
return s;
} /**
* 获取当前日期 yyyyMMdd
* @param date
* @return String
*/
public static String formatDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String strDate = formatter.format(date);
return strDate;
} /**
* 取出一个指定长度大小的随机正整数.
*
* @param length
* int 设定所取出随机数的长度。length小于11
* @return int 返回生成的随机数。
*/
public static int buildRandom(int length) {
int num = 1;
double random = Math.random();
if (random < 0.1) {
random = random + 0.1;
}
for (int i = 0; i < length; i++) {
num = num * 10;
}
return (int) ((random * num));
} /**
* 获取编码字符集
* @param request
* @param response
* @return String
*/
public static String getCharacterEncoding(HttpServletRequest request,
HttpServletResponse response) { if(null == request || null == response) {
return "gbk";
} String enc = request.getCharacterEncoding();
if(null == enc || "".equals(enc)) {
enc = response.getCharacterEncoding();
} if(null == enc || "".equals(enc)) {
enc = "gbk";
} return enc;
} /**
* 获取unix时间,从1970-01-01 00:00:00开始的秒数
* @param date
* @return long
*/
public static long getUnixTime(Date date) {
if( null == date ) {
return 0;
} return date.getTime()/1000;
} /**
* 时间转换成字符串
* @param date 时间
* @param formatType 格式化类型
* @return String
*/
public static String date2String(Date date, String formatType) {
SimpleDateFormat sdf = new SimpleDateFormat(formatType);
return sdf.format(date);
}
}
PrepayIdRequestHandler
package com.qtkj.app.weixinpay.handler; import java.util.Iterator;
import java.util.Map;
import java.util.Set; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.qtkj.app.weixinpay.util.ConstantUtil;
import com.qtkj.app.weixinpay.util.MD5Util;
import com.qtkj.app.weixinpay.util.XMLUtil; public class PrepayIdRequestHandler extends RequestHandler { public PrepayIdRequestHandler(HttpServletRequest request,
HttpServletResponse response) {
super(request, response);
} public String createMD5Sign() {
StringBuffer sb = new StringBuffer();
Set es = super.getAllParameters().entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
sb.append(k + "=" + v + "&");
}
String params=sb.append("key="+ConstantUtil.APP_KEY).substring(0); String sign = MD5Util.MD5Encode(params, "utf8");
return sign.toUpperCase();
} // 提交预支付
public String sendPrepay() throws Exception {
String prepayid = "";
Set es=super.getAllParameters().entrySet();
Iterator it=es.iterator();
StringBuffer sb = new StringBuffer("<xml>");
while(it.hasNext()){
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
sb.append("<"+k+">"+v+"</"+k+">");
}
sb.append("</xml>");
String params=sb.substring(0);
System.out.println("请求参数:"+params);
String requestUrl = super.getGateUrl();
System.out.println("请求url:"+requestUrl);
TenpayHttpClient httpClient = new TenpayHttpClient();
httpClient.setReqContent(requestUrl);
String resContent = "";
if (httpClient.callHttpPost(requestUrl, params)) {
resContent = httpClient.getResContent();
System.out.println("获取prepayid的返回值:"+resContent);
Map<String,String> map=XMLUtil.doXMLParse(resContent);
if(map.containsKey("prepay_id"))
prepayid=map.get("prepay_id");
}
return prepayid;
}
}
RequestHandler
package com.qtkj.app.weixinpay.handler; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.qtkj.app.weixinpay.util.MD5Util;
import com.qtkj.app.weixinpay.util.TenpayUtil; /**
* 请求处理类
* 请求处理类继承此类,重写createSign方法即可。
*
*/
public class RequestHandler { /** 网关url地址 */
private String gateUrl; /** 密钥 */
private String key; /** 请求的参数 */
private SortedMap parameters; protected HttpServletRequest request; protected HttpServletResponse response; /**
* 构造函数
* @param request
* @param response
*/
public RequestHandler(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response; this.gateUrl = "https://gw.tenpay.com/gateway/pay.htm";
this.key = "";
this.parameters = new TreeMap();
} /**
*初始化函数。
*/
public void init() {
//nothing to do
} /**
*获取入口地址,不包含参数值
*/
public String getGateUrl() {
return gateUrl;
} /**
*设置入口地址,不包含参数值
*/
public void setGateUrl(String gateUrl) {
this.gateUrl = gateUrl;
} /**
*获取密钥
*/
public String getKey() {
return key;
} /**
*设置密钥
*/
public void setKey(String key) {
this.key = key;
} /**
* 获取参数值
* @param parameter 参数名称
* @return String
*/
public String getParameter(String parameter) {
String s = (String)this.parameters.get(parameter);
return (null == s) ? "" : s;
} /**
* 设置参数值
* @param parameter 参数名称
* @param parameterValue 参数值
*/
public void setParameter(String parameter, Object parameterValue) {
String v = "";
if(null != parameterValue) {
if(parameterValue instanceof String)
v = ((String) parameterValue).trim();
}
this.parameters.put(parameter, v);
} /**
* 返回所有的参数
* @return SortedMap
*/
public SortedMap getAllParameters() {
return this.parameters;
} /**
* 获取带参数的请求URL
* @return String
* @throws UnsupportedEncodingException
*/
public String getRequestURL() throws UnsupportedEncodingException { this.createSign(); StringBuffer sb = new StringBuffer();
String enc = TenpayUtil.getCharacterEncoding(this.request, this.response);
Set es = this.parameters.entrySet();
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
String v = (String)entry.getValue(); if(!"spbill_create_ip".equals(k)) {
sb.append(k + "=" + URLEncoder.encode(v, enc) + "&");
} else {
sb.append(k + "=" + v.replace("\\.", "%2E") + "&");
}
} //去掉最后一个&
String reqPars = sb.substring(0, sb.lastIndexOf("&")); return this.getGateUrl() + "?" + reqPars; } public void doSend() throws UnsupportedEncodingException, IOException {
this.response.sendRedirect(this.getRequestURL());
} /**
* 创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
*/
protected void createSign() {
StringBuffer sb = new StringBuffer();
Set es = this.parameters.entrySet();
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
String v = (String)entry.getValue();
if(null != v && !"".equals(v)
&& !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + this.getKey());
String enc = TenpayUtil.getCharacterEncoding(this.request, this.response);
String sign = MD5Util.MD5Encode(sb.toString(), enc).toUpperCase(); this.setParameter("sign", sign); } protected HttpServletRequest getHttpServletRequest() {
return this.request;
} protected HttpServletResponse getHttpServletResponse() {
return this.response;
}
}
MD5Util
/**
*
*/
package com.qtkj.app.weixinpay.util; import java.security.MessageDigest; /**
* @author Zhao
* @version 创建时间:2017年10月22日 下午3:24:07
*
*/
/**
*<p>Title:MD5Util </p>
*<p>Description:</p>
*<p>Company:</p>
*@author ZHAO
*@date 2017年10月22日
*/
public class MD5Util {
/**
* MD5加密
* @param b
* @return
*/
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");//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" };
}
TenpayHttpClient
package com.qtkj.app.weixinpay.handler; import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory; import com.qtkj.app.weixinpay.util.HttpClientUtil; public class TenpayHttpClient { /** 请求内容,无论post和get,都用get方式提供 */
private String reqContent; /** 应答内容 */
private String resContent; /** 请求方法 */
private String method; /** 错误信息 */
private String errInfo; /** 超时时间,以秒为单位 */
private int timeOut; /** http应答编码 */
private int responseCode; /** 字符编码 */
private String charset; private InputStream inputStream; public TenpayHttpClient() {
this.reqContent = "";
this.resContent = "";
this.method = "POST";
this.errInfo = "";
this.timeOut = 30;//30秒 this.responseCode = 0;
this.charset = "utf8"; this.inputStream = null;
} /**
* 设置请求内容
* @param reqContent 表求内容
*/
public void setReqContent(String reqContent) {
this.reqContent = reqContent;
} /**
* 获取结果内容
* @return String
* @throws IOException
*/
public String getResContent() {
try {
this.doResponse();
} catch (IOException e) {
this.errInfo = e.getMessage();
//return "";
} return this.resContent;
} /**
* 设置请求方法post或者get
* @param method 请求方法post/get
*/
public void setMethod(String method) {
this.method = method;
} /**
* 获取错误信息
* @return String
*/
public String getErrInfo() {
return this.errInfo;
} /**
* 设置超时时间,以秒为单位
* @param timeOut 超时时间,以秒为单位
*/
public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
} /**
* 获取http状态码
* @return int
*/
public int getResponseCode() {
return this.responseCode;
} protected void callHttp() throws IOException { if("POST".equals(this.method.toUpperCase())) {
String url = HttpClientUtil.getURL(this.reqContent);
String queryString = HttpClientUtil.getQueryString(this.reqContent);
byte[] postData = queryString.getBytes(this.charset);
this.httpPostMethod(url, postData); return ;
} this.httpGetMethod(this.reqContent); } public boolean callHttpPost(String url, String postdata) {
boolean flag = false;
byte[] postData;
try {
postData = postdata.getBytes(this.charset);
this.httpPostMethod(url, postData);
flag = true;
} catch (IOException e1) {
e1.printStackTrace();
}
return flag;
} /**
* 以http post方式通信
* @param url
* @param postData
* @throws IOException
*/
protected void httpPostMethod(String url, byte[] postData)
throws IOException { HttpURLConnection conn = HttpClientUtil.getHttpURLConnection(url); this.doPost(conn, postData);
} /**
* 以http get方式通信
*
* @param url
* @throws IOException
*/
protected void httpGetMethod(String url) throws IOException { HttpURLConnection httpConnection =
HttpClientUtil.getHttpURLConnection(url); this.setHttpRequest(httpConnection); httpConnection.setRequestMethod("GET"); this.responseCode = httpConnection.getResponseCode(); this.inputStream = httpConnection.getInputStream(); } /**
* 以https get方式通信
* @param url
* @param sslContext
* @throws IOException
*/
protected void httpsGetMethod(String url, SSLContext sslContext)
throws IOException { SSLSocketFactory sf = sslContext.getSocketFactory(); HttpsURLConnection conn = HttpClientUtil.getHttpsURLConnection(url); conn.setSSLSocketFactory(sf); this.doGet(conn); } protected void httpsPostMethod(String url, byte[] postData,
SSLContext sslContext) throws IOException { SSLSocketFactory sf = sslContext.getSocketFactory(); HttpsURLConnection conn = HttpClientUtil.getHttpsURLConnection(url); conn.setSSLSocketFactory(sf); this.doPost(conn, postData); } /**
* 设置http请求默认属性
* @param httpConnection
*/
protected void setHttpRequest(HttpURLConnection httpConnection) { //设置连接超时时间
httpConnection.setConnectTimeout(this.timeOut * 1000); //不使用缓存
httpConnection.setUseCaches(false); //允许输入输出
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true); } /**
* 处理应答
* @throws IOException
*/
protected void doResponse() throws IOException { if(null == this.inputStream) {
return;
} //获取应答内容
this.resContent=HttpClientUtil.InputStreamTOString(this.inputStream,this.charset); //关闭输入流
this.inputStream.close(); } /**
* post方式处理
* @param conn
* @param postData
* @throws IOException
*/
protected void doPost(HttpURLConnection conn, byte[] postData)
throws IOException { // 以post方式通信
conn.setRequestMethod("POST"); // 设置请求默认属性
this.setHttpRequest(conn); // Content-Type
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded"); BufferedOutputStream out = new BufferedOutputStream(conn
.getOutputStream()); final int len = 1024; // 1KB
HttpClientUtil.doOutput(out, postData, len); // 关闭流
out.close(); // 获取响应返回状态码
this.responseCode = conn.getResponseCode(); // 获取应答输入流
this.inputStream = conn.getInputStream(); } /**
* get方式处理
* @param conn
* @throws IOException
*/
protected void doGet(HttpURLConnection conn) throws IOException { //以GET方式通信
conn.setRequestMethod("GET"); //设置请求默认属性
this.setHttpRequest(conn); //获取响应返回状态码
this.responseCode = conn.getResponseCode(); //获取应答输入流
this.inputStream = conn.getInputStream();
} }
XMLUtil
package com.qtkj.app.weixinpay.util;
/**
*<p>Title:XMLUtil </p>
*<p>Description:</p>
*<p>Company:</p>
*@author ZHAO
*@date 2017年10月22日
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import java.io.ByteArrayInputStream;
public class XMLUtil {
/**
* 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
* @param strxml
* @return
* @throws JDOMException
* @throws IOException
*/
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;
} /**
* 获取子结点的xml
* @param children
* @return String
*/
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();
} /**
* 获取xml编码字符集
* @param strxml
* @return
* @throws IOException
* @throws JDOMException
*/
public static String getXMLEncoding(String strxml) throws JDOMException, IOException {
InputStream in = HttpClientUtil.String2Inputstream(strxml);
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(in);
in.close();
return (String)doc.getProperty("encoding");
} /**
* 支付成功,返回微信那服务器
* @param return_code
* @param return_msg
* @return
*/
public static String setXML(String return_code, String return_msg) {
return "<xml><return_code><![CDATA[" + return_code + "]]></return_code><return_msg><![CDATA[" + return_msg + "]]></return_msg></xml>";
} public static String createXML(Map<String,Object> map){
Set<Entry<String,Object>> set=map.entrySet();
set.iterator();
return null;
} }
WXUtil
package com.qtkj.app.weixinpay.util; import java.util.Random; public class WXUtil {
/**
* 生成随机字符串
* @return
*/
public static String getNonceStr() {
Random random = new Random();
return MD5Util.MD5Encode(String.valueOf(random.nextInt(10000)), "utf8");
}
/**
* 获取时间戳
* @return
*/
public static String getTimeStamp() {
return String.valueOf(System.currentTimeMillis() / 1000);
}
}
HttpClientUtil
package com.qtkj.app.weixinpay.util;
import java.io.ByteArrayOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.Map; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory; public class HttpClientUtil {
/**
* http客户端工具类
*
*/
public static final String SunX509 = "SunX509";
public static final String JKS = "JKS";
public static final String PKCS12 = "PKCS12";
public static final String TLS = "TLS"; /**
* get HttpURLConnection
* @param strUrl url地址
* @return HttpURLConnection
* @throws IOException
*/
public static HttpURLConnection getHttpURLConnection(String strUrl)
throws IOException {
URL url = new URL(strUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
return httpURLConnection;
} /**
* get HttpsURLConnection
* @param strUrl url地址ַ
* @return HttpsURLConnection
* @throws IOException
*/
public static HttpsURLConnection getHttpsURLConnection(String strUrl)
throws IOException {
URL url = new URL(strUrl);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url
.openConnection();
return httpsURLConnection;
} /**
* 获取不带查询串的url
* @param strUrl
* @return String
*/
public static String getURL(String strUrl) { if(null != strUrl) {
int indexOf = strUrl.indexOf("?");
if(-1 != indexOf) {
return strUrl.substring(0, indexOf);
} return strUrl;
} return strUrl; } /**
* 获取查询串
* @param strUrl
* @return String
*/
public static String getQueryString(String strUrl) { if(null != strUrl) {
int indexOf = strUrl.indexOf("?");
if(-1 != indexOf) {
return strUrl.substring(indexOf+1, strUrl.length());
} return "";
} return strUrl;
} /**
* 查询字符串转化为map
* name1=key1&name2=key2&...
* @param queryString
* @return
*/
public static Map queryString2Map(String queryString) {
if(null == queryString || "".equals(queryString)) {
return null;
} Map m = new HashMap();
String[] strArray = queryString.split("&");
for(int index = 0; index < strArray.length; index++) {
String pair = strArray[index];
HttpClientUtil.putMapByPair(pair, m);
} return m; } /**
* 把键值添加到map
* pair:name=value
* @param pair name=value
* @param m
*/
public static void putMapByPair(String pair, Map m) { if(null == pair || "".equals(pair)) {
return;
} int indexOf = pair.indexOf("=");
if(-1 != indexOf) {
String k = pair.substring(0, indexOf);
String v = pair.substring(indexOf+1, pair.length());
if(null != k && !"".equals(k)) {
m.put(k, v);
}
} else {
m.put(pair, "");
}
}
/**
* BufferedReader转换成String<br/>
* 注意:流关闭需要自行处理
* @param reader
* @return
* @throws IOException
*/
public static String bufferedReader2String(BufferedReader reader) throws IOException {
StringBuffer buf = new StringBuffer();
String line = null;
while( (line = reader.readLine()) != null) {
buf.append(line);
buf.append("\r\n");
} return buf.toString();
}
/**
* 处理输出<br/>
* 注意:流关闭需要自行处理
* @param out
* @param data
* @param len
* @throws IOException
*/
public static void doOutput(OutputStream out, byte[] data, int len)
throws IOException {
int dataLen = data.length;
int off = 0;
while (off < data.length) {
if (len >= dataLen) {
out.write(data, off, dataLen);
off += dataLen;
} else {
out.write(data, off, len);
off += len;
dataLen -= len;
} // ˢ�»�����
out.flush();
} }
/**
* 获取SSLContext
* @param trustFile
* @param trustPasswd
* @param keyFile
* @param keyPasswd
* @return
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws IOException
* @throws CertificateException
* @throws UnrecoverableKeyException
* @throws KeyManagementException
*/
public static SSLContext getSSLContext(
FileInputStream trustFileInputStream, String trustPasswd,
FileInputStream keyFileInputStream, String keyPasswd)
throws NoSuchAlgorithmException, KeyStoreException,
CertificateException, IOException, UnrecoverableKeyException,
KeyManagementException { // ca
TrustManagerFactory tmf = TrustManagerFactory.getInstance(HttpClientUtil.SunX509);
KeyStore trustKeyStore = KeyStore.getInstance(HttpClientUtil.JKS);
trustKeyStore.load(trustFileInputStream, HttpClientUtil
.str2CharArray(trustPasswd));
tmf.init(trustKeyStore); final char[] kp = HttpClientUtil.str2CharArray(keyPasswd);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(HttpClientUtil.SunX509);
KeyStore ks = KeyStore.getInstance(HttpClientUtil.PKCS12);
ks.load(keyFileInputStream, kp);
kmf.init(ks, kp); SecureRandom rand = new SecureRandom();
SSLContext ctx = SSLContext.getInstance(HttpClientUtil.TLS);
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), rand); return ctx;
} /**
* 字符串转换成char数组
* @param str
* @return char[]
*/
public static char[] str2CharArray(String str) {
if(null == str) return null; return str.toCharArray();
} public static InputStream String2Inputstream(String str) {
return new ByteArrayInputStream(str.getBytes());
} /**
* InputStream转换成Byte
* 注意:流关闭需要自行处理
* @param in
* @return byte
* @throws Exception
*/
public static byte[] InputStreamTOByte(InputStream in) throws IOException{ int BUFFER_SIZE = 4096;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1; while((count = in.read(data,0,BUFFER_SIZE)) != -1)
outStream.write(data, 0, count); data = null;
byte[] outByte = outStream.toByteArray();
outStream.close(); return outByte;
} /**
* InputStream转换成String
* 注意:流关闭需要自行处理
* @param in
* @param encoding 编码
* @return String
* @throws Exception
*/
public static String InputStreamTOString(InputStream in,String encoding) throws IOException{ return new String(InputStreamTOByte(in),encoding); } }
三.控制层的接入(Controller)
1.Controller层的代码实现过程
package com.qtkj.app.weixinpay.controller; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.jdom2.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import com.qtkj.admin.common.CacheXmlConfig;
import com.qtkj.admin.settings.entity.SiteConfig;
import com.qtkj.admin.user.entity.User;
import com.qtkj.admin.user.service.UserService;
import com.qtkj.app.weixinpay.handler.ClientRequestHandler;
import com.qtkj.app.weixinpay.handler.PrepayIdRequestHandler;
import com.qtkj.app.weixinpay.util.ConstantUtil;
import com.qtkj.app.weixinpay.util.MD5Util;
import com.qtkj.app.weixinpay.util.TenpayUtil;
import com.qtkj.app.weixinpay.util.UUID;
import com.qtkj.app.weixinpay.util.WXUtil;
import com.qtkj.app.weixinpay.util.XMLUtil;
import com.qtkj.user.entity.Trade;
import com.qtkj.user.service.TradeService;
import com.qtkj.util.NumberUtils;
import com.qtkj.util.PageHelper; /**
*<p>Title:WeiXinPayController </p>
*<p>Description:</p>
*<p>Company:</p>
*@author ZHAO
*@date 2017年10月27日
*/
@Controller
public class WeiXinPayController { @Autowired
private TradeService tradeservice; @Autowired
private TradeService tradeService; @Autowired
private UserService userService; //------------------------------------------------------------------------------------------------------------------------------------------------------------------ /**
* 9.1.生成订单
*@author Zhao
*@date 2017年10月31日
*@param request
*@param response
*@param model
*@param legalMoney 充值法币金额(legalMoney)
*@param totalPrice 充值金额(RMB)
*@param userId 用户id
*@return
*@throws Exception
*/
@RequestMapping("api/weixin/createOrder")
@ResponseBody
public Model doWeinXinRequest(HttpServletRequest request,HttpServletResponse response,Model model,
@RequestParam("totalPrice") String totalPrice,
@RequestParam("legalMoney") String legalMoney,
@RequestParam("userId") String userId
) throws Exception {
Map<String,Object> resultMap = new LinkedHashMap<>();
try { //---------------2 生成订单号 开始------------------------
//2.1.当前时间 yyyyMMddHHmmss
String currTime = TenpayUtil.getCurrTime();
//2.2 8位日期
String strTime = currTime.substring(8, currTime.length());
//2.3四位随机数
String strRandom = TenpayUtil.buildRandom(4) + "";
//2.4 10位序列号,可以自行调整。
String strReq = strTime + strRandom;
//2.5 订单号,此处用时间加随机数生成,商户根据自己情况调整,只要保持全局唯一就行
String out_trade_no = strReq;
//---------------生成订单号 结束 ------------------------ //3.获取生成预支付订单的请求类
PrepayIdRequestHandler prepayReqHandler = new PrepayIdRequestHandler(request, response); //3.1封装数据
String nonce_str = WXUtil.getNonceStr(); //订单号
out_trade_no = String.valueOf(UUID.next());
String timestamp = WXUtil.getTimeStamp(); //超时时间 //3.2---------------------------------------------- ***** 统一下单开始 ***** -----------------------------------------------------------
prepayReqHandler.setParameter("appid", ConstantUtil.APP_ID); //平台应用appId
prepayReqHandler.setParameter("mch_id", ConstantUtil.MCH_ID); //商户号
prepayReqHandler.setParameter("nonce_str", nonce_str); //随机字符串
prepayReqHandler.setParameter("body", ConstantUtil.BODY); //商品描述
prepayReqHandler.setParameter("out_trade_no", out_trade_no); //订单号
prepayReqHandler.setParameter("total_fee",String.valueOf(totalPrice)); //订单价格
prepayReqHandler.setParameter("spbill_create_ip", request.getRemoteAddr()); //获取客户端ip
prepayReqHandler.setParameter("notify_url", ConstantUtil.NOTIFY_URL); //回调通知
prepayReqHandler.setParameter("trade_type", "APP"); //支付类型
prepayReqHandler.setParameter("time_start", timestamp); //时间戳
prepayReqHandler.setGateUrl(ConstantUtil.GATEURL); //设置预支付id的接口url //3.3 注意签名(sign)的生成方式,具体见官方文档(传参都要参与生成签名,且参数名按照字典序排序,最后接上APP_KEY,转化成大写)
prepayReqHandler.setParameter("sign", prepayReqHandler.createMD5Sign()); //sign 签名 //3.4 提交预支付,获取prepayid
String prepayid = prepayReqHandler.sendPrepay();
//---------------------------------------------- ***** 统一下单 结束 ***** -------------------------------------------------------------- //3.5 若获取prepayid成功,将相关信息返回客户端
if (prepayid != null && !prepayid.equals("")) { //---------------4.封装订单数据开始 ------------------------
此处用于封装你自己实体类的订单信息 例:Trade trade = new Trade(); //---------------4.封装订单数据开始 ------------------------ String signs =
"appid=" + ConstantUtil.APP_ID +
"&noncestr=" + nonce_str +
"&package=Sign=WXPay"+
"&partnerid="+ ConstantUtil.PARTNER_ID +
"&prepayid=" + prepayid +
"×tamp=" + timestamp+
"&key="+ ConstantUtil.APP_KEY; resultMap.put("appid", ConstantUtil.APP_ID);
resultMap.put("partnerid", ConstantUtil.PARTNER_ID); //商家id
resultMap.put("prepayid", prepayid); //预支付id
resultMap.put("package", "Sign=WXPay"); //固定常量
resultMap.put("noncestr", nonce_str); //与请求prepayId时值一致
resultMap.put("timestamp", timestamp); //等于请求prepayId时的time_start
resultMap.put("sign", MD5Util.MD5Encode(signs, "utf8").toUpperCase());//签名方式与上面类似
model.addAttribute("orderNum", out_trade_no);
model.addAttribute("resultMap", resultMap);
model.addAttribute("msg", "获取prepayid成功,生成订单成功");
model.addAttribute("status",0);
}else {
model.addAttribute("msg", "获取prepayid失败");
model.addAttribute("status",1);
}
} catch (Exception e) {
model.addAttribute("msg", "订单生成失败");
model.addAttribute("status",2);
}
return model;
} /**
* 9.2 接收微信支付成功通知
* @param request
* @param response
* @throws IOException
* @throws java.io.IOException
* @throws ParseException
*/
@RequestMapping(value = "api/weixin/notify")
public void getnotify(HttpServletRequest request, HttpServletResponse response)
throws IOException, ParseException {
System.err.println("微信支付回调");
System.err.println("微信支付回调");
//1.创建输入输出流
PrintWriter writer = response.getWriter();
InputStream inStream = request.getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
//2.将结果转换
String result = new String(outSteam.toByteArray(), "utf-8");
System.out.println("微信支付通知结果:" + result);
Map<String, String> map = null;
try {
//3.解析微信通知返回的信息
map = XMLUtil.doXMLParse(result);
System.err.println(map);
} catch (JDOMException e) {
e.printStackTrace();
}
// 4.若支付成功,则告知微信服务器收到通知
if (map.get("return_code").equals("SUCCESS")) {
if (map.get("result_code").equals("SUCCESS")) {
System.out.println("充值成功!");
//4.1 修改当前订单状态为:付款成功 2
System.err.println("交易号:"+Long.valueOf(map.get("out_trade_no")));
// Trade trade = tradeservice.selectByOrderNumber((String)(map.get("out_trade_no")));
Trade trade = tradeservice.selectByTradeNumber((String)(map.get("out_trade_no")));
if(trade !=null){
trade.setTradeType((byte)2); //设置状态
trade.setPaymentTime(new Date());
trade.setPayTime(new Date());
if(tradeservice.updateByPrimaryKeySelective(trade) > 0){
//更新成功
System.err.println("通知微信后台");
String notifyStr = XMLUtil.setXML("SUCCESS", "");
writer.write(notifyStr);
writer.flush();
}
}else{
String notifyStr = XMLUtil.setXML("ERROR", "");
writer.write(notifyStr);
writer.flush();
}
}
}
} /**
*微信支付成功后.通知页面
*@author Zhao
*@date 2017年11月2日
*@param request
*@return
*@throws UnsupportedEncodingException
*/
@RequestMapping(value="api/weixin/return",method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public Model returnUrl(@RequestParam("id") String id,HttpServletRequest request,Model model) throws UnsupportedEncodingException {
System.err.println("。。。。。。 微信同步通知 。。。。。。");
System.err.println("。。。。。。 微信同步通知 。。。。。。");
System.err.println("。。。。。。 微信同步通知 。。。。。。");
Map returnMap = new HashMap();
try { Trade trade = tradeservice.selectByTradeNumber(id);
// 返回值Map
if(trade !=null && trade.getTradeStatus() == 2){
User user = userService.selectByPrimaryKey(trade.gettUserId());
returnMap.put("tradeType", trade.getTradeType()); //支付方式
returnMap.put("phoneNum", user.getPhoneNumber()); //支付帐号
returnMap.put("tradeMoney", trade.getTradeMoney()+""); //订单金额
}else{
model.addAttribute("msg", "查询失败");
model.addAttribute("status", 0);
}
model.addAttribute("returnMap", returnMap);
System.err.println(returnMap);
model.addAttribute("msg", "查询成功");
model.addAttribute("status", 0);
} catch (Exception e) {
model.addAttribute("msg", "查询失败");
model.addAttribute("status", 1);
} return model;
}
}
2.微信的支付注意事项
1.在第1个接口中,生成订单时,是签名2次,将数据返回给app端的,手机端可以直接用来向微信发起支付
2.第2个接口是app端支付成功后,微信的服务器需要回调你的这个接口,所以这个接口的地址要公网可以测,具体见上一篇的支付宝支付中的(nei wang chuan tou),这里
还可能出现验签失败等情况,微信特别坑,就返回 -1,然后就没有提示信息了,所以希望看官不要急,先去查找一下自己的参数有没有问题,必要时可以一个一个对一遍,避免因
为不认真造成失误。
3.第3个接口是在第2个接口返回给微信服务器信息后(这个信息是确认我确实收到钱了),手机端支付成功后跳转页面时所需要的数据,这个接口根据自己的业务需要把
数据返回给手机端即可。
四.测试
微信支付在app端支付成功后,要调用异步通知,微信官方并未提供沙箱环境,因此需要访问的url必须是外网可以访问的,在这里推荐一款内网穿透工具natapp,需要用身份证号验证,测试个支付是没问题的
附:
1. natapp官网: https://natapp.cn
2.natapp 1分钟新手图文教程: https://natapp.cn/article/natapp_newbie
由于本人能力水平有限,理解能为一般,有不当之处,请名位看官批评指正!!!