微信公众号支付 js api java版本

        提及来.微信支付真是一堆坑. 竟然官网都没有java版本的完整代码. 就算是php版本的.还都有错误.且先后各类版本.各类文档一大堆....不停的误导开发人员.javascript

        花了一天半时间.总算实现了微信公众号支付.和pc端的微信扫码支付.其余不说了.直接给思路
php

        本人作的是微信V3版本的微信支付.微信的官方文档中.提供的demo 只有一些工具类.这些类仍是颇有做用的.java

        https://mp.weixin.qq.com/paymch/readtemplate?t=mp/business/course3_tmpl&lang=zh_CN  能够在这个链接中找到相应的java类.jquery

        这里必定要注意.在官网填写的受权目录必定要写到三级目录.如:
git

个人回调地址是:http://111.111.111.111:1111/control/weixinPay_notify web

那么,官网填写都受权目录就是:http://111.111.111.111:1111/control/api

我试过.受权目录写到2级.是没用的.此处差评,第一个坑.数组


  首先,定义各类微信支付所须要的参数
缓存

GzhConfig.java服务器

public static String APPID = "XXXXXXXXXXXX";
//受理商ID,身份标识
public static String MCHID = "XXXXXXXXXXXXxx";
//商户支付密钥Key。审核经过后,在微信发送的邮件中查看
public static String KEY = "XXXXXXXXXXXXXXXXX";
//JSAPI接口中获取openid,审核后在公众平台开启开发模式后可查看
public static String APPSECRET = "xxxxxxxxxxxxxx";
//重定向地址
public static String REDIRECT_URL = "http://XXXXXXXXXXXXXXXXXXX/callWeiXinPay";
//异步回调地址
public static String NOTIFY_URL = "http://XXXXXXXXXXXXXXXXXXXXXX/weixinPay_notify";
//web回调地址
public static String WEB_NOTIFY_URL = "http://XXXXXXXXXXXXXXXXXXXXXXXXX/weixinPay_notify";

而后.就是正式的开始代码了:

1.使用Oauth2.0受权.进行页面跳转,获取code .(code关系着后面获取openid.)

https://open.weixin.qq.com/connect/oauth2/authorize?appid=123456789&redirect_uri=http://111.111.111.111:1111/control/orderPay&response_type=code&scope=snsapi_base&state=456123456#wechat_redirect

此处.appid 这个在微信官网能够获取. 重定向地址. 就是获取code 后.跳转指向你的地址.这里能够是你的订单结算页面.response_type=code和scope=snsapi_base 都是固定格式.   state 是传入传出.这个参数用户自定义为任何均可以,好比说订单id. 而后会和code 一块儿传递到你的重定向地址,如我上面写的重定向地址就是 orderPay连接.

2.在重定向到页面(http://111.111.111.111:1111/control/orderPay)的时候中间执行java方法(如获取openid 如执行微信统一下单接口,获取预支付ID.).处理各类参数.下面贴具体代码作说明.

GzhService.java


String code = request.getParameter("code");
String state = request.getParameter("state");
Debug.log("code-======"+code+"===========state======"+state);
String noncestr = Sha1Util.getNonceStr();//生成随机字符串
String timestamp = Sha1Util.getTimeStamp();//生成1970年到如今的秒数.
//state 能够传递你的订单号.而后根据订单号 查询付款金额.我就不详细写了.
                                
String out_trade_no = state;//订单号
GenericValue orderHeader = delegator.findOne("OrderHeader", UtilMisc.toMap("orderId", out_trade_no),false);
String total_fee = String.valueOf(orderHeader.getBigDecimal("grandTotal").doubleValue()*100);
String order_price = total_fee.substring(0, total_fee.indexOf("."));//订单金额
//微信金额 以分为单位.这是第二坑.若是不注意.页面的报错.你基本看不出来.由于他提示系统升级,正在维护.扯淡.....
String product_name=out_trade_no;//订单名称
//获取jsapi_ticket.此参数是为了生成 js api  加载时候的签名用.必须.jsapi_ticket只会存在7200秒.而且有时间限制,(好像一年还只能调用两万次.因此必定要缓存.)这是第三坑.
//能够在java中模拟url请求.就能获取access_token 而后根据access_token 取得 jsapi_ticket,但必定要缓存起来..这个代码.我只提供获取.缓存大家本身处理.
//SendGET方法我会在后面放出
String tokenParam = "grant_type=client_credential&appid="+GzhConfig.APPID+"&secret="+GzhConfig.APPSECRET;
String tokenJsonStr = SendGET("https://api.weixin.qq.com/cgi-bin/token", tokenParam);
Map tokenMap = JSON.parseObject(tokenJsonStr);
//获取access_token
String access_token = (String)tokenMap.get("access_token");
String ticketParam = "access_token="+access_token+"&type=jsapi";
String ticketJsonStr = SendGET("https://api.weixin.qq.com/cgi-bin/ticket/getticket", ticketParam);
Map ticketMap = JSON.parseObject(ticketJsonStr);
//获取jsapi_ticket
String ticket = (String)ticketMap.get("ticket");
 //下面就到了获取openid,这个表明用户id.
//获取openID
String openParam = "appid="+GzhConfig.APPID+"&secret="+GzhConfig.APPSECRET+"&code="+code+"&grant_type=authorization_code";
String openJsonStr = SendGET("https://api.weixin.qq.com/sns/oauth2/access_token", openParam);
Map openMap = JSON.parseObject(openJsonStr);
String openid = (String) openMap.get("openid");
RequestHandler reqHandler = new RequestHandler(request, response);
 //初始化     RequestHandler  类能够在微信的文档中找到.还有相关工具类    
reqHandler.init();
reqHandler.init(GzhConfig.APPID, GzhConfig.APPSECRET, GzhConfig.KEY, "");
//执行统一下单接口 得到预支付id
reqHandler.setParameter("appid",GzhConfig.APPID);
reqHandler.setParameter("mch_id", GzhConfig.MCHID);                //商户号
reqHandler.setParameter("nonce_str", noncestr);            //随机字符串
reqHandler.setParameter("body", product_name);                        //商品描述(必填.若是不填.也会提示系统升级.正在维护我艹.)
reqHandler.setParameter("out_trade_no", out_trade_no);        //商家订单号
reqHandler.setParameter("total_fee", order_price);                    //商品金额,以分为单位
reqHandler.setParameter("spbill_create_ip",request.getRemoteAddr());   //用户的公网ip  IpAddressUtil.getIpAddr(request)
//下面的notify_url是用户支付成功后为微信调用的action  异步回调.
reqHandler.setParameter("notify_url", GzhConfig.NOTIFY_URL);
reqHandler.setParameter("trade_type", "JSAPI");
//------------须要进行用户受权获取用户openid-------------
reqHandler.setParameter("openid", openid);   //这个必填.
//这里只是在组装数据.还没到执行到统一下单接口.由于统一下单接口的数据传递格式是xml的.因此才须要组装.
String requestUrl = reqHandler.getRequestURL();
                                requestUrl 例子:
 /*
<xml><appid>wx2421b1c4370ec43b</appid><attach>支付测试</attach><body>JSAPI支付测试</body><mch_id>10000100</mch_id><nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str><notify_url>http://wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php</notify_url><openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid><out_trade_no>1415659990</out_trade_no><spbill_create_ip>14.23.150.211</spbill_create_ip><total_fee>1</total_fee><trade_type>JSAPI</trade_type><sign>0CB01533B8C1EF103065174F50BCA001</sign></xml>
*/
                                
Debug.log("requestUrl==================="+requestUrl);
//统一下单接口提交  xml格式
URL orderUrl = new URL("https://api.mch.weixin.qq.com/pay/unifiedorder");
HttpURLConnection conn = (HttpURLConnection) orderUrl.openConnection();
conn.setConnectTimeout(30000); // 设置链接主机超时(单位:毫秒)
conn.setReadTimeout(30000); // 设置从主机读取数据超时(单位:毫秒)
conn.setDoOutput(true); // post请求参数要放在http正文内,顾设置成true,默认是false
conn.setDoInput(true); // 设置是否从httpUrlConnection读入,默认状况下是true
conn.setUseCaches(false); // Post 请求不能使用缓存
// 设定传送的内容类型是可序列化的java对象(若是不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestMethod("POST");// 设定请求的方法为"POST",默认是GET
conn.setRequestProperty("Content-Length",requestUrl.length()+"");
String encode = "utf-8";
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), encode);
out.write(requestUrl.toString());
out.flush();
out.close();
String result = getOut(conn);
Debug.log("result=========返回的xml============="+result);
Map<String, String> orderMap = XMLUtil.doXMLParse(result);
Debug.log("orderMap==========================="+orderMap);
//获得的预支付id
String prepay_id = orderMap.get("prepay_id");
SortedMap<String,String> params = new TreeMap<String,String>();
params.put("appId", GzhConfig.APPID);
params.put("timeStamp",timestamp);
params.put("nonceStr", noncestr);
params.put("package", "prepay_id="+prepay_id);
params.put("signType", "MD5");
        
//生成支付签名,这个签名 给 微信支付的调用使用
String paySign =  reqHandler.createSign(params);        
request.setAttribute("paySign", paySign);
request.setAttribute("appId", GzhConfig.APPID);
request.setAttribute("timeStamp", timestamp);        //时间戳
request.setAttribute("nonceStr", noncestr);            //随机字符串
request.setAttribute("signType", "MD5");        //加密格式
request.setAttribute("out_trade_no", out_trade_no);          //订单号
request.setAttribute("package", "prepay_id="+prepay_id);//预支付id ,就这样的格式.
String url = "http://xxxxxxxxxx/control/wxPayment";
String signValue = "jsapi_ticket="+ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+url;
Debug.log("url====="+signValue);
//这个签名.主要是给加载微信js使用.别和上面的搞混了.
String signature = Sha1Util.getSha1((signValue));
request.setAttribute("signature", signature);

                


//此页面的一些其余方法

public static String getOut(HttpURLConnection conn) throws IOException{
        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            return null;
        }
        // 获取响应内容体
        BufferedReader in = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), "UTF-8"));
        String line = "";
        StringBuffer strBuf = new StringBuffer();
        while ((line = in.readLine()) != null) {
            strBuf.append(line).append("\n");
        }
        in.close();
        return  strBuf.toString().trim();
}

public static String SendGET(String url,String param){
   String result="";//访问返回结果
   BufferedReader read=null;//读取访问结果
    
   try {
    //建立url
    URL realurl=new URL(url+"?"+param);
    //打开链接
    URLConnection connection=realurl.openConnection();
     // 设置通用的请求属性
             connection.setRequestProperty("accept", "*/*");
             connection.setRequestProperty("connection", "Keep-Alive");
             connection.setRequestProperty("user-agent",
                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
             //创建链接
             connection.connect();
          // 获取全部响应头字段
//             Map<String, List<String>> map = connection.getHeaderFields();
             // 遍历全部的响应头字段,获取到cookies等
//             for (String key : map.keySet()) {
//                 System.out.println(key + "--->" + map.get(key));
//             }
             // 定义 BufferedReader输入流来读取URL的响应
             read = new BufferedReader(new InputStreamReader(
                     connection.getInputStream(),"UTF-8"));
             String line;//循环读取
             while ((line = read.readLine()) != null) {
                 result += line;
             }
   } catch (IOException e) {
    e.printStackTrace();
   }finally{
    if(read!=null){//关闭流
     try {
      read.close();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }
     
   return result; 
 }

            

            其余相关类的方法:

            


/*

'============================================================================

'api说明:

'createSHA1Sign建立签名SHA1

'getSha1()Sha1签名

'============================================================================

'*/

public class Sha1Util {
public static String getNonceStr() {
Random random = new Random();
return MD5Util.MD5Encode(String.valueOf(random.nextInt(10000)), "UTF-8");
}
public static String getTimeStamp() {
return String.valueOf(System.currentTimeMillis() / 1000);
}
   //建立签名SHA1
public static String createSHA1Sign(SortedMap<String, String> signParams) throws Exception {
StringBuffer sb = new StringBuffer();
Set es = signParams.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 + "&");
//要采用URLENCODER的原始值!
}
String params = sb.substring(0, sb.lastIndexOf("&"));
System.out.println("sha1 sb:" + params);
return getSha1(params);
}
//Sha1签名
public static String getSha1(String str) {
if (str == null || str.length() == 0) {
return null;
}
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
mdTemp.update(str.getBytes("GBK"));
byte[] md = mdTemp.digest();
int j = md.length;
char buf[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (Exception e) {
return null;
}
}
}



/**

 * xml工具类

 * @author  miklchen

 *

 */

public class XMLUtil {
/**
 * 解析xml,返回第一级元素键值对。若是第一级元素有子节点,则此节点的值是子节点的xml数据。
 * @param  strxml
 * @return 
 * @throws  JDOMException
 * @throws  IOException
 */
public static Map<String,String> doXMLParse(String strxml) throws JDOMException, IOException {
if(null == strxml || "".equals(strxml)) {
return null;
}
Map<String,String> m = new HashMap<String,String>();
InputStream in = HttpClientUtil.String2Inputstream(strxml);
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");
}
}

  

   

/**
 * Http客户端工具类<br/>
 * 这是内部调用类,请不要在外部调用。
 * @author miklchen
 *
 */
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
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.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
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 {
	
	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<br/>
	 * 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<br/>
	 * 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 String
	 * @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 < dataLen) {
			if(len >= dataLen) {
				out.write(data, off, dataLen);
			} else {
				out.write(data, off, len);
			}
			
			//刷新缓冲区
			out.flush();
			
			off += len;
			
			dataLen -= len;
		}
		
	}
	
	/**
	 * 获取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;
	}
	
	/**
	 * 获取CA证书信息
	 * @param cafile CA证书文件
	 * @return Certificate
	 * @throws CertificateException
	 * @throws IOException
	 */
	public static Certificate getCertificate(File cafile)
			throws CertificateException, IOException {
		CertificateFactory cf = CertificateFactory.getInstance("X.509");
		FileInputStream in = new FileInputStream(cafile);
		Certificate cert = cf.generateCertificate(in);
		in.close();
		return cert;
	}
	
	/**
	 * 字符串转换成char数组
	 * @param str
	 * @return char[]
	 */
	public static char[] str2CharArray(String str) {
		if(null == str) return null;
		
		return str.toCharArray();
	}
	
	/**
	 * 存储ca证书成JKS格式
	 * @param cert
	 * @param alias
	 * @param password
	 * @param out
	 * @throws KeyStoreException
	 * @throws NoSuchAlgorithmException
	 * @throws CertificateException
	 * @throws IOException
	 */
	public static void storeCACert(Certificate cert, String alias,
			String password, OutputStream out) throws KeyStoreException,
			NoSuchAlgorithmException, CertificateException, IOException {
		KeyStore ks = KeyStore.getInstance("JKS");

		ks.load(null, null);

		ks.setCertificateEntry(alias, cert);

		// store keystore
		ks.store(out, HttpClientUtil.str2CharArray(password));

	}
	
	public static InputStream String2Inputstream(String str) {
		return new ByteArrayInputStream(str.getBytes());
	}

}

 

 RequestHandler.java

  

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;

/*
 '微信支付服务器签名支付请求请求类
 '============================================================================
 'api说明:
 'init(app_id, app_secret, partner_key, app_key);
 '初始化函数,默认给一些参数赋值,如cmdno,date等。
 'setKey(key_)'设置商户密钥
 'getLasterrCode(),获取最后错误号
 'GetToken();获取Token
 'getTokenReal();Token过时后实时获取Token
 'createMd5Sign(signParams);生成Md5签名
 'genPackage(packageParams);获取package包
 'createSHA1Sign(signParams);建立签名SHA1
 'sendPrepay(packageParams);提交预支付
 'getDebugInfo(),获取debug信息
 '============================================================================
 '*/
public class RequestHandler {
	/** Token获取网关地址地址 */
	private String tokenUrl;
	/** 预支付网关url地址 */
	private String gateUrl;
	/** 查询支付通知网关URL */
	private String notifyUrl;
	/** 商户参数 */
	private String appid;
	private String appkey;
	private String partnerkey;
	private String appsecret;
	private String key;
	/** 请求的参数 */
	private SortedMap parameters;
	/** Token */
	private String Token;
	private String charset;
	/** debug信息 */
	private String debugInfo;
	private String last_errcode;

	private HttpServletRequest request;

	private HttpServletResponse response;

	/**
	 * 初始构造函数。
	 * 
	 * @return
	 */
	public RequestHandler(HttpServletRequest request,
			HttpServletResponse response) {
		this.last_errcode = "0";
		this.request = request;
		this.response = response;
		this.charset = "GBK";
		this.parameters = new TreeMap();
		// 验证notify支付订单网关
		notifyUrl = "https://gw.tenpay.com/gateway/simpleverifynotifyid.xml";
		
	}

	/**
	 * 初始化函数。
	 */
	public void init(String app_id, String app_secret, String app_key,
			String partner_key) {
		this.last_errcode = "0";
		this.Token = "token_";
		this.debugInfo = "";
		this.appkey = app_key;
		this.appid = app_id;
		this.partnerkey = partner_key;
		this.appsecret = app_secret;
	}

	public void init() {
	}

	/**
	 * 获取最后错误号
	 */
	public String getLasterrCode() {
		return last_errcode;
	}

	/**
	 *获取入口地址,不包含参数值
	 */
	public String getGateUrl() {
		return gateUrl;
	}

	/**
	 * 获取参数值
	 * 
	 * @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, String parameterValue) {

        String v = "";

        if(null != parameterValue) {

            v = parameterValue.trim();

        }
        this.parameters.put(parameter, v);

    }
	
	 //设置密钥
	
	public void setKey(String key) {
		this.partnerkey = key;
	}
	//设置微信密钥
	public void  setAppKey(String key){
		this.appkey = key;
	}
	
	// 特殊字符处理
	public String UrlEncode(String src) throws UnsupportedEncodingException {
		return URLEncoder.encode(src, this.charset).replace("+", "%20");
	}

	// 获取package的签名包
	public String genPackage(SortedMap<String, String> packageParams)
			throws UnsupportedEncodingException {
		String sign = createSign(packageParams);

		StringBuffer sb = new StringBuffer();
		Set es = packageParams.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 + "=" + UrlEncode(v) + "&");
		}

		// 去掉最后一个&
		String packageValue = sb.append("sign=" + sign).toString();
		System.out.println("packageValue=" + packageValue);
		return packageValue;
	}

	/**
	 * 建立md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
	 */
	public String createSign(SortedMap<String, String> packageParams) {
		StringBuffer sb = new StringBuffer();
		Set es = packageParams.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=" + GzhConfig.KEY);
		System.out.println("md5 sb:" + sb);
		String sign = MD5Util.MD5Encode(sb.toString(), this.charset)
				.toUpperCase();

		return sign;

	}
	/**
	 * 建立package签名
	 */
	public boolean createMd5Sign(String signParams) {
		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 (!"sign".equals(k) && null != v && !"".equals(v)) {
				sb.append(k + "=" + v + "&");
			}
		}

		// 算出摘要
		String enc = TenpayUtil.getCharacterEncoding(this.request,
				this.response);
		String sign = MD5Util.MD5Encode(sb.toString(), enc).toLowerCase();

		String tenpaySign = this.getParameter("sign").toLowerCase();

		// debug信息
		this.setDebugInfo(sb.toString() + " => sign:" + sign + " tenpaySign:"
				+ tenpaySign);

		return tenpaySign.equals(sign);
	}

	public String getRequestURL() throws UnsupportedEncodingException {        

        this.createSign();        

        StringBuffer sb = new StringBuffer();

        sb.append("<xml>");

        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 ("attach".equalsIgnoreCase(k)||"body".equalsIgnoreCase(k)||"sign".equalsIgnoreCase(k)) {

                sb.append("<"+k+">"+"<![CDATA["+v+"]]></"+k+">");

            }else {

                sb.append("<"+k+">"+v+"</"+k+">");

            }

        }

        sb.append("</xml>");

        return sb.toString();

    }
	
	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=" + GzhConfig.KEY); //本身的API密钥      

        String enc = TenpayUtil.getCharacterEncoding(this.request, this.response);

        String sign = MD5Util.MD5Encode(sb.toString(), enc).toUpperCase();

        this.setParameter("sign", sign);              

    }
	
    //输出XML
	   public String parseXML() {
		   StringBuffer sb = new StringBuffer();
	       sb.append("<xml>");
	       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) && !"appkey".equals(k)) {
					
					sb.append("<" + k +">" + getParameter(k) + "</" + k + ">\n");
				}
			}
	       sb.append("</xml>");
			return sb.toString();
		}

	/**
	 * 设置debug信息
	 */
	protected void setDebugInfo(String debugInfo) {
		this.debugInfo = debugInfo;
	}
	public void setPartnerkey(String partnerkey) {
		this.partnerkey = partnerkey;
	}
	public String getDebugInfo() {
		return debugInfo;
	}
	public String getKey() {
		return key;
	}
	
	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>";
	}
}

  

TenpayUtil

   

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class TenpayUtil {
	
	private static Object Server;
	private static String QRfromGoogle;

	/**
	 * 把对象转换成字符串
	 * @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;
	}
	
	public  static String URLencode(String content){
		
		String URLencode;
		
		URLencode= replace(Server.equals(content), "+", "%20");
		
		return URLencode;
	}
	private static String replace(boolean equals, String string, String string2) {
		
		return null;
	}

	/**
	 * 获取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;
	}
	
	 public static String QRfromGoogle(String chl)
	    {
	        int widhtHeight = 300;
	        String EC_level = "L";
	        int margin = 0;
	        String QRfromGoogle;
	        chl = URLencode(chl);
	        
	        QRfromGoogle = "http://chart.apis.google.com/chart?chs=" + widhtHeight + "x" + widhtHeight + "&cht=qr&chld=" + EC_level + "|" + margin + "&chl=" + chl;
	       
	        return QRfromGoogle;
	    }

	/**
	 * 时间转换成字符串
	 * @param date 时间
	 * @param formatType 格式化类型
	 * @return String
	 */
	public static String date2String(Date date, String formatType) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatType);
		return sdf.format(date);
	}
	
}
MD5Util
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" };
}

下面才到了页面代码:jsp 页面

    

<script type="text/javascript" src="/xxxx/jquery-1.6.2.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script language="javascript">
//加载
wx.config({
    debug: true, // 开启调试模式,调用的全部api的返回值会在客户端alert出来,若要查看传入的参数,能够在pc端打开,参数信息会经过log打出,仅在pc端时才会打印。
    appId: '${StringUtil.wrapString(requestAttributes.appId)!}', // 必填,公众号的惟一标识
    timestamp: ${StringUtil.wrapString(requestAttributes.timeStamp)?default(0)!}, // 必填,生成签名的时间戳
    nonceStr: '${StringUtil.wrapString(requestAttributes.nonceStr)!}', // 必填,生成签名的随机串
    signature: '${StringUtil.wrapString(requestAttributes.signature)!}',// 必填,签名,见附录1
    jsApiList: [
    'checkJsApi',
            'chooseWXPay'] // 必填,须要使用的JS接口列表,全部JS接口列表见附录2
});
wx.ready(function(){
//支付
wx.chooseWXPay({
    timestamp: ${StringUtil.wrapString(requestAttributes.timeStamp)?default(0)!}, // 支付签名时间戳,注意微信jssdk中的全部使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
    nonceStr: '${StringUtil.wrapString(requestAttributes.nonceStr)!}', // 支付签名随机串,不长于 32 位
    package: '${StringUtil.wrapString(requestAttributes.package)!}', // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
    signType: '${StringUtil.wrapString(requestAttributes.signType)!}', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
    paySign: '${StringUtil.wrapString(requestAttributes.paySign)!}', // 支付签名
    success: function (res) {
        // 支付成功后的回调函数
        WeixinJSBridge.log(res.err_msg);
        //alert("支付接口:"+res.err_code + res.err_desc + res.err_msg);
        if(!res.err_msg){
                    //支付完后.跳转到成功页面.
        location.href="orderconfirm?orderId=${StringUtil.wrapString(requestAttributes.out_trade_no)!}";
        }
    }
});
    // config信息验证后会执行ready方法,全部接口调用都必须在config接口得到结果以后,config是一个客户端的异步操做,因此若是须要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则能够直接调用,不须要放在ready函数中。
});
wx.error(function(res){
    // config信息验证失败会执行error函数,如签名过时致使验证失败,具体错误信息能够打开config的debug模式查看,也能够在返回的res参数中查看,对于SPA能够在这里更新签名。
});
wx.checkJsApi({
    jsApiList: ['chooseWXPay'], // 须要检测的JS接口列表,全部JS接口列表见附录2,
    success: function(res) {
    //alert("检测接口:"+res.err_msg);
    }
    });
</script>





下面是后台异步回调代码:

/**
 * 异步返回
 */
@SuppressWarnings("deprecation")
public static String weixinNotify(HttpServletRequest request, HttpServletResponse response){
        try {
        
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();
String resultStr  = new String(outSteam.toByteArray(),"utf-8");
Map<String, String> resultMap = XMLUtil.doXMLParse(resultStr);
String result_code = resultMap.get("result_code");
String is_subscribe = resultMap.get("is_subscribe");
String out_trade_no = resultMap.get("out_trade_no");
String transaction_id = resultMap.get("transaction_id");
String sign = resultMap.get("sign");
String time_end = resultMap.get("time_end");
String bank_type = resultMap.get("bank_type");
String return_code = resultMap.get("return_code");
//签名验证
GenericValue userLogin =delegator.findOne("UserLogin", UtilMisc.toMap("userLoginId","admin"),false);
if(return_code.equals("SUCCESS")){
    //此处就是你的逻辑代码
        }
 request.setAttribute("out_trade_no", out_trade_no);
//通知微信.异步确认成功.必写.否则会一直通知后台.八次以后就认为交易失败了.
response.getWriter().write(RequestHandler.setXML("SUCCESS", ""));
} catch (UnsupportedEncodingException e) { 
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} 
return "success";
}


        代码中,删除了一些和公司相关的代码.因此若是直接复制进去.确定是要作大量修改的.见谅.

相关文章
相关标签/搜索