微信支付业务类
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;
import java.security.MessageDigest;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 测试支付(为方便展示,公共方法也放在业务类中,可根据业务提取公共方法到工具类中)
*/
public class TestPay {
public static final String APPID = ""; //服务号APPID
public static final String AppSecret = ""; //服务号AppSecret
public static final String MCH_ID = ""; //支付商户号
public static final String API_KEY = ""; //API密钥
/**
* 获取微信支付签名
*/
@RequestMapping(value = "wap_pay_signature.htm", method = {RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public SortedMap<Object, Object> WapSignSignatureAction(HttpServletRequest request, HttpServletResponse response)throws Exception {
SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
String code = request.getParameter("code");
System.out.println("code-------------" + code);
// code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。
// 通过code换取网页授权access_token
Map<String, String> data = getAccess_tokenByCode(code, response);
String openid = data.get("openid");
String tName = "名称";
String orderno = "1234567890";
String total_fee = "8.88";
String nonce_str = create_nonce_str();
String timestamp = create_timestamp();
// 获取prepayId
try {
String result = getPrepayId(request,tName,orderno,total_fee,openid);
Map<String, String> map = doXMLParse(result);
// 重新生成签名
parameters.put("appId", APPID);
parameters.put("timeStamp", timestamp);
parameters.put("nonceStr", nonce_str);
parameters.put("package", "prepay_id=" + map.get("prepay_id"));
parameters.put("signType", "MD5");
String sign = createSign("UTF-8", parameters);
parameters.put("prepay_id", "prepay_id=" + map.get("prepay_id"));
parameters.put("paySign", sign);
} catch (Exception e) {
e.printStackTrace();
}
return parameters;
}
/**
* 根据用户授权code获取access_token
*/
private Map<String, String> getAccess_tokenByCode(String code,HttpServletResponse response) {
Map<String, String> data = new HashMap<String, String>();
String requestUrlMessageFormat = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code";
String requestUrl