java微信公眾號支付示例詳解
本文實例為大傢分享瞭java微信公眾號支付示例代碼,供大傢參考,具體內容如下
開始之前,先準備好:appid、商傢號、商戶密匙。
工具類:
MD5Util.java
package com.yiexpress.core.utils.wechat; import java.security.MessageDigest; /** * MD5工具類 */ public class MD5Util { public final static String MD5(String s) { char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; try { byte[] btInput = s.getBytes(); MessageDigest mdInst = MessageDigest.getInstance("MD5"); mdInst.update(btInput); byte[] md = mdInst.digest(); int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } String md5Str = new String(str); return md5Str; } catch (Exception e) { e.printStackTrace(); return null; } } }
SapUtils.java
package com.yiexpress.core.utils; import java.lang.reflect.*; import java.util.List; import java.io.IOException; import java.io.StringWriter; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; public class SapUtils { private static final Logger logger = LoggerFactory.getLogger(SapUtils.class); /** 根據反射對javabean轉成xml文件的格式 * 以類名為第一標簽,所有屬性作為第二節點,並放入對應的值,如果屬性為空 就不放入該熟悉 * @param dto 傳入的對象 * @param operationName 操作名稱 * @return */ public static String formatToXml(Object dto,String operationName){ logger.info("解析當前類{}為指定的xml文檔格式的數據",dto.getClass().getName()); logger.info("當前的同步方法是,{}",operationName); String result = null; Field fields[]=dto.getClass().getDeclaredFields();//dto 是實體類名稱 //DocumentHelper提供瞭創建Document對象的方法 Document document = DocumentHelper.createDocument(); //添加節點信息 String className=dto.getClass().getName(); // 操作的名稱 Element rootElement = document.addElement(operationName); try { Field.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { //添加節點信息 if(!StringUtils.isEmpty(fields[i].get(dto))){ Class<?> type = fields[i].getType(); // 如果是list if(type == List.class){ String listName = fields[i].getName(); createElement(rootElement, fields[i].get(dto),listName); } else{ Element element = rootElement.addElement(fields[i].getName()); element.setText((String) fields[i].get(dto)); } } } // 設置XML文檔格式 OutputFormat outputFormat = OutputFormat.createPrettyPrint(); // 設置XML編碼方式,即是用指定的編碼方式保存XML文檔到字符串(String),這裡也可以指定為GBK或是ISO8859-1 outputFormat.setEncoding("UTF-8"); // outputFormat.setSuppressDeclaration(true); //是否生產xml頭 outputFormat.setIndent(true); //設置是否縮進 outputFormat.setIndent(" "); //以四個空格方式實現縮進 outputFormat.setNewlines(true); //設置是否換行 StringWriter stringWriter =null; // Writer fileWriter =null; // xmlWriter是用來把XML文檔寫入字符串的(工具) XMLWriter xmlWriter = null; try { // stringWriter字符串是用來保存XML文檔的 stringWriter = new StringWriter(); // fileWriter = new FileWriter("D:\\modu11le.xml"); // xmlWriter是用來把XML文檔寫入字符串的(工具) xmlWriter = new XMLWriter(stringWriter, outputFormat); // 把創建好的XML文檔寫入字符串 xmlWriter.write(document); //fileWriter.write(stringWriter.toString()); result=stringWriter.toString(); } catch (IOException e) { logger.error("寫入數據失敗"); throw new RuntimeException("寫入數據失敗"+e); }finally{ try { if(xmlWriter!=null){ xmlWriter.flush(); xmlWriter.close(); } /* if(fileWriter!=null){ fileWriter.flush(); fileWriter.close(); }*/ } catch (IOException e) { logger.error("關閉輸出流出錯"); throw new RuntimeException("關閉輸出流出錯"+e); } } } catch (Exception e) { logger.error("添加xml的節點失敗"+e); } logger.error("轉換xml結束"); return result; } /** * 添加類中的list * @param element * @param object * @param name * @return * @throws IllegalArgumentException * @throws IllegalAccessException */ public static Element createElement(Element element ,Object object,String name ) throws IllegalArgumentException, IllegalAccessException{ Element nameElement = element.addElement(name); List info = (List)object; for(int j= 0;j<info.size();j++){ // 添加row的標簽 Element rowElement = nameElement.addElement("row"); // 添加 對象的熟悉 Field fields[]=info.get(j).getClass().getDeclaredFields();//dto 是實體類名稱 Field.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { //添加節點信息 if(!StringUtils.isEmpty(fields[i].get(info.get(j)))){ Element childElement = rowElement.addElement(fields[i].getName()); childElement.setText((String) fields[i].get(info.get(j))); } } } return element; } }
UnifiedOrderRequest.java
package com.yiexpress.core.utils.wechat; public class UnifiedOrderRequest { private String appid;// 公眾賬號ID private String mch_id;//商戶號 private String device_info; //設備號 否 private String nonce_str;//隨機字符串 private String sign;//簽名 private String sign_type;//簽名類型 private String body;//商品描述 private String detail;//商品詳情 private String attach;//附加數據 private String out_trade_no;//商戶訂單號 private String fee_type;//標價幣種 private String total_fee;//標價金額 private String spbill_create_ip;//終端IP private String time_start;//交易起始時間 private String time_expire;//交易結束時間 private String goods_tag;//訂單優惠標記 private String notify_url;//通知地址 private String trade_type;//交易類型 private String product_id;//商品ID private String limit_pay;//指定支付方式 private String openid;//用戶標識 public String getAppid() { return appid; } public void setAppid(String appid) { this.appid = appid; } public String getMch_id() { return mch_id; } public void setMch_id(String mch_id) { this.mch_id = mch_id; } public String getDevice_info() { return device_info; } public void setDevice_info(String device_info) { this.device_info = device_info; } public String getNonce_str() { return nonce_str; } public void setNonce_str(String nonce_str) { this.nonce_str = nonce_str; } public String getSign() { return sign; } public void setSign(String sign) { this.sign = sign; } public String getSign_type() { return sign_type; } public void setSign_type(String sign_type) { this.sign_type = sign_type; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public String getAttach() { return attach; } public void setAttach(String attach) { this.attach = attach; } public String getOut_trade_no() { return out_trade_no; } public void setOut_trade_no(String out_trade_no) { this.out_trade_no = out_trade_no; } public String getFee_type() { return fee_type; } public void setFee_type(String fee_type) { this.fee_type = fee_type; } public String getTotal_fee() { return total_fee; } public void setTotal_fee(String total_fee) { this.total_fee = total_fee; } public String getSpbill_create_ip() { return spbill_create_ip; } public void setSpbill_create_ip(String spbill_create_ip) { this.spbill_create_ip = spbill_create_ip; } public String getTime_start() { return time_start; } public void setTime_start(String time_start) { this.time_start = time_start; } public String getTime_expire() { return time_expire; } public void setTime_expire(String time_expire) { this.time_expire = time_expire; } public String getGoods_tag() { return goods_tag; } public void setGoods_tag(String goods_tag) { this.goods_tag = goods_tag; } public String getNotify_url() { return notify_url; } public void setNotify_url(String notify_url) { this.notify_url = notify_url; } public String getTrade_type() { return trade_type; } public void setTrade_type(String trade_type) { this.trade_type = trade_type; } public String getProduct_id() { return product_id; } public void setProduct_id(String product_id) { this.product_id = product_id; } public String getLimit_pay() { return limit_pay; } public void setLimit_pay(String limit_pay) { this.limit_pay = limit_pay; } public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid; } }
UnifiedOrderRespose.java
package com.yiexpress.core.utils.wechat; public class UnifiedOrderRespose { private String return_code; //返回狀態碼 private String return_msg; //返回信息 private String appid; //公眾賬號ID private String mch_id; //商戶號 private String device_info; //設備號 private String nonce_str; //隨機字符串 private String sign; //簽名 private String result_code; //業務結果 private String err_code; //錯誤代碼 private String err_code_des; //錯誤代碼描述 private String trade_type; //交易類型 private String prepay_id; //預支付交易會話標識 private String code_url; //二維碼鏈接 public String getReturn_code() { return return_code; } public void setReturn_code(String return_code) { this.return_code = return_code; } public String getReturn_msg() { return return_msg; } public void setReturn_msg(String return_msg) { this.return_msg = return_msg; } public String getAppid() { return appid; } public void setAppid(String appid) { this.appid = appid; } public String getMch_id() { return mch_id; } public void setMch_id(String mch_id) { this.mch_id = mch_id; } public String getDevice_info() { return device_info; } public void setDevice_info(String device_info) { this.device_info = device_info; } public String getNonce_str() { return nonce_str; } public void setNonce_str(String nonce_str) { this.nonce_str = nonce_str; } public String getSign() { return sign; } public void setSign(String sign) { this.sign = sign; } public String getResult_code() { return result_code; } public void setResult_code(String result_code) { this.result_code = result_code; } public String getErr_code() { return err_code; } public void setErr_code(String err_code) { this.err_code = err_code; } public String getErr_code_des() { return err_code_des; } public void setErr_code_des(String err_code_des) { this.err_code_des = err_code_des; } public String getTrade_type() { return trade_type; } public void setTrade_type(String trade_type) { this.trade_type = trade_type; } public String getPrepay_id() { return prepay_id; } public void setPrepay_id(String prepay_id) { this.prepay_id = prepay_id; } public String getCode_url() { return code_url; } public void setCode_url(String code_url) { this.code_url = code_url; } }
WXPayConstants.java
package com.yiexpress.core.utils.wechat; public class WXPayConstants { public enum SignType { MD5, HMACSHA256 } public static final String FAIL = "FAIL"; public static final String SUCCESS = "SUCCESS"; public static final String HMACSHA256 = "HMAC-SHA256"; public static final String MD5 = "MD5"; public static final String FIELD_SIGN = "sign"; public static final String FIELD_SIGN_TYPE = "sign_type"; }
WXPayUtil.java
package com.yiexpress.core.utils.wechat; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; import java.io.Writer; import java.net.HttpURLConnection; import java.net.URL; import java.util.*; import java.security.MessageDigest; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.yiexpress.core.utils.SapUtils; import com.yiexpress.core.utils.XmlUtil; import com.yiexpress.core.utils.wechat.WXPayConstants.SignType; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 支付工具類 */ public class WXPayUtil { private static Logger log = LoggerFactory.getLogger(WXPayUtil.class); /** * 生成訂單對象信息 * @param orderId 訂單號 * @param appId 微信appId * @param mch_id 微信分配的商戶ID * @param body 支付介紹主體 * @param price 支付價格(放大100倍) * @param spbill_create_ip 終端IP * @param notify_url 異步直接結果通知接口地址 * @param noncestr * @return */ public static Map<String,Object> createOrderInfo(Map<String, String> requestMap,String shopKey) { //生成訂單對象 UnifiedOrderRequest unifiedOrderRequest = new UnifiedOrderRequest(); unifiedOrderRequest.setAppid(requestMap.get("appId"));//公眾賬號ID unifiedOrderRequest.setBody(requestMap.get("body"));//商品描述 unifiedOrderRequest.setMch_id(requestMap.get("mch_id"));//商戶號 unifiedOrderRequest.setNonce_str(requestMap.get("noncestr"));//隨機字符串 unifiedOrderRequest.setNotify_url(requestMap.get("notify_url"));//通知地址 unifiedOrderRequest.setOpenid(requestMap.get("userWeixinOpenId")); unifiedOrderRequest.setDetail(requestMap.get("detail"));//詳情 unifiedOrderRequest.setOut_trade_no(requestMap.get("out_trade_no"));//商戶訂單號 unifiedOrderRequest.setSpbill_create_ip(requestMap.get("spbill_create_ip"));//終端IP unifiedOrderRequest.setTotal_fee(requestMap.get("payMoney")); //金額需要擴大100倍:1代表支付時是0.01 unifiedOrderRequest.setTrade_type("JSAPI");//JSAPI--公眾號支付、NATIVE--原生掃碼支付、APP--app支付 SortedMap<String, String> packageParams = new TreeMap<String, String>(); packageParams.put("appid", unifiedOrderRequest.getAppid()); packageParams.put("body", unifiedOrderRequest.getBody()); packageParams.put("mch_id", unifiedOrderRequest.getMch_id()); packageParams.put("nonce_str", unifiedOrderRequest.getNonce_str()); packageParams.put("notify_url", unifiedOrderRequest.getNotify_url()); packageParams.put("openid", unifiedOrderRequest.getOpenid()); packageParams.put("detail", unifiedOrderRequest.getDetail()); packageParams.put("out_trade_no", unifiedOrderRequest.getOut_trade_no()); packageParams.put("spbill_create_ip", unifiedOrderRequest.getSpbill_create_ip()); packageParams.put("total_fee", unifiedOrderRequest.getTotal_fee()); packageParams.put("trade_type", unifiedOrderRequest.getTrade_type()); try { unifiedOrderRequest.setSign(generateSignature(packageParams,shopKey));//簽名 } catch (Exception e) { e.printStackTrace(); } //將訂單對象轉為xml格式 String orderstr=SapUtils.formatToXml(unifiedOrderRequest,"xml").replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>",""); log.debug("封裝好的統一下單請求數據:"+orderstr.replace("__", "_")); Map<String,Object> responseMap = new HashMap<String,Object>(); responseMap.put("orderInfo_toString", orderstr.replace("__", "_")); responseMap.put("unifiedOrderRequest",unifiedOrderRequest); return responseMap; } public static void main(String[] args) { // UnifiedOrderRequest ut=new UnifiedOrderRequest(); // ut.setAppid("wx1234156789"); // ut.setBody("內容body"); // ut.setMch_id("商戶號"); // ut.setNonce_str("隨機字符串"); // ut.setNotify_url("回調地址"); // ut.setOpenid("openid"); // ut.setDetail("詳情"); // ut.setOut_trade_no("訂單號"); // ut.setSpbill_create_ip("終端IP"); // ut.setTotal_fee("金額"); // ut.setTrade_type("調用類型JSAPI"); // System.out.println("---"+SapUtils.formatToXml(ut,"xml")+"---"); // UnifiedOrderRequest unifiedOrderRequest = new UnifiedOrderRequest(); // unifiedOrderRequest.setAppid("dsfsdf");//公眾賬號ID // unifiedOrderRequest.setBody("sdfsdf");//商品描述 // unifiedOrderRequest.setMch_id("sdfsd");//商戶號 // unifiedOrderRequest.setNonce_str("dfsd");//隨機字符串 // unifiedOrderRequest.setNotify_url("sdfdsf");//通知地址 // unifiedOrderRequest.setOpenid("sdfsdf"); // // unifiedOrderRequest.setTrade_type("JSAPI");//JSAPI--公眾號支付、NATIVE--原生掃碼支付、APP--app支付 // // System.out.println("---"+SapUtils.formatToXml(unifiedOrderRequest,"xml").replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","")+"---"); // String str="<xml><appid>dsfsdf</appid><mch_id>sdfsd</mch_id><nonce_str>dfsd</nonce_str><body>sdfsdf</body><notify_url>sdfdsf</notify_url><trade_type>JSAPI</trade_type><openid>sdfsdf</openid></xml>"; // UnifiedOrderRequest s=SapUtils.getBeanByxml(str,UnifiedOrderRequest.class); // System.out.println(s.getAppid()+"---"+s.getMch_id()+"--"+s.getFee_type()); } /** * 生成簽名 * @param appid_value * @param mch_id_value * @param productId * @param nonce_str_value * @param trade_type * @param notify_url * @param spbill_create_ip * @param total_fee * @param out_trade_no * @return */ private static String createSign(UnifiedOrderRequest unifiedOrderRequest,String shopKey) { //根據規則創建可排序的map集合 SortedMap<String, String> packageParams = new TreeMap<String, String>(); packageParams.put("appid", unifiedOrderRequest.getAppid()); packageParams.put("body", unifiedOrderRequest.getBody()); packageParams.put("mch_id", unifiedOrderRequest.getMch_id()); packageParams.put("nonce_str", unifiedOrderRequest.getNonce_str()); packageParams.put("notify_url", unifiedOrderRequest.getNotify_url()); packageParams.put("out_trade_no", unifiedOrderRequest.getOut_trade_no()); packageParams.put("spbill_create_ip", unifiedOrderRequest.getSpbill_create_ip()); packageParams.put("trade_type", unifiedOrderRequest.getTrade_type()); packageParams.put("total_fee", unifiedOrderRequest.getTotal_fee()); 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 + "&"); } } //第二步拼接key,key設置路徑:微信商戶平臺(pay.weixin.qq.com)-->賬戶設置-->API安全-->密鑰設置 sb.append("key="+shopKey); String sign = MD5Util.MD5(sb.toString()).toUpperCase();//MD5加密 log.error("方式一生成的簽名="+sign); return sign; } //xml解析 public static SortedMap<String, String> doXMLParseWithSorted(String strxml) throws Exception { strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\""); if(null == strxml || "".equals(strxml)) { return null; } SortedMap<String,String> m = new TreeMap<String,String>(); 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 = 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(getChildrenText(list)); } sb.append(value); sb.append("</" + name + ">"); } } return sb.toString(); } /** * 調統一下單API * @param orderInfo * @return */ public static UnifiedOrderRespose httpOrder(String orderInfo,int index) { //統一下單接口地址 自動適應 1中國境內 2東南亞 3其他 String[] urlList={"https://api.mch.weixin.qq.com/pay/unifiedorder","https://apihk.mch.weixin.qq.com/pay/unifiedorder" ,"https://apius.mch.weixin.qq.com/pay/unifiedorder "}; //String url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; try { HttpURLConnection conn = (HttpURLConnection) new URL(urlList[index]).openConnection(); //加入數據 conn.setRequestMethod("POST"); conn.setDoOutput(true); BufferedOutputStream buffOutStr = new BufferedOutputStream(conn.getOutputStream()); buffOutStr.write(orderInfo.getBytes("UTF-8")); buffOutStr.flush(); buffOutStr.close(); //獲取輸入流 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line = null; StringBuffer sb = new StringBuffer(); while((line = reader.readLine())!= null){ sb.append(line); } //xml轉對象 UnifiedOrderRespose unifiedOrderRespose =XmlUtil.getBeanByxml(sb.toString(),UnifiedOrderRespose.class); return unifiedOrderRespose; } catch (Exception e) { e.printStackTrace(); } return null; } /** * XML格式字符串轉換為Map * * @param strXML XML字符串 * @return XML數據轉換後的Map * @throws Exception */ public static Map<String, String> xmlToMap(String strXML) throws Exception { try { Map<String, String> data = new HashMap<String, String>(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8")); org.w3c.dom.Document doc = documentBuilder.parse(stream); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getDocumentElement().getChildNodes(); for (int idx = 0; idx < nodeList.getLength(); ++idx) { Node node = nodeList.item(idx); if (node.getNodeType() == Node.ELEMENT_NODE) { org.w3c.dom.Element element = (org.w3c.dom.Element) node; data.put(element.getNodeName(), element.getTextContent()); } } try { stream.close(); } catch (Exception ex) { // do nothing } return data; } catch (Exception ex) { WXPayUtil.getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML); throw ex; } } /** * 將Map轉換為XML格式的字符串 * * @param data Map類型數據 * @return XML格式的字符串 * @throws Exception */ public static String mapToXml(Map<String, String> data) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder(); org.w3c.dom.Document document = documentBuilder.newDocument(); org.w3c.dom.Element root = document.createElement("xml"); document.appendChild(root); for (String key: data.keySet()) { String value = data.get(key); if (value == null) { value = ""; } value = value.trim(); org.w3c.dom.Element filed = document.createElement(key); filed.appendChild(document.createTextNode(value)); root.appendChild(filed); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(source, result); String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", ""); try { writer.close(); } catch (Exception ex) { } return output; } /** * 生成帶有 sign 的 XML 格式字符串 * * @param data Map類型數據 * @param key API密鑰 * @return 含有sign字段的XML */ public static String generateSignedXml(final Map<String, String> data, String key) throws Exception { return generateSignedXml(data, key, SignType.MD5); } /** * 生成帶有 sign 的 XML 格式字符串 * * @param data Map類型數據 * @param key API密鑰 * @param signType 簽名類型 * @return 含有sign字段的XML */ public static String generateSignedXml(final Map<String, String> data, String key, SignType signType) throws Exception { String sign = generateSignature(data, key, signType); data.put(WXPayConstants.FIELD_SIGN, sign); return mapToXml(data); } /** * 判斷簽名是否正確 * * @param xmlStr XML格式數據 * @param key API密鑰 * @return 簽名是否正確 * @throws Exception */ public static boolean isSignatureValid(String xmlStr, String key) throws Exception { Map<String, String> data = xmlToMap(xmlStr); if (!data.containsKey(WXPayConstants.FIELD_SIGN) ) { return false; } String sign = data.get(WXPayConstants.FIELD_SIGN); return generateSignature(data, key).equals(sign); } /** * 判斷簽名是否正確,必須包含sign字段,否則返回false。使用MD5簽名。 * * @param data Map類型數據 * @param key API密鑰 * @return 簽名是否正確 * @throws Exception */ public static boolean isSignatureValid(Map<String, String> data, String key) throws Exception { return isSignatureValid(data, key, SignType.MD5); } /** * 判斷簽名是否正確,必須包含sign字段,否則返回false。 * * @param data Map類型數據 * @param key API密鑰 * @param signType 簽名方式 * @return 簽名是否正確 * @throws Exception */ public static boolean isSignatureValid(Map<String, String> data, String key, SignType signType) throws Exception { if (!data.containsKey(WXPayConstants.FIELD_SIGN) ) { return false; } String sign = data.get(WXPayConstants.FIELD_SIGN); return generateSignature(data, key, signType).equals(sign); } /** * 生成簽名 * * @param data 待簽名數據 * @param key API密鑰 * @return 簽名 */ public static String generateSignature(final Map<String, String> data, String key) throws Exception { return generateSignature(data, key, SignType.MD5); } /** * 生成簽名. 註意,若含有sign_type字段,必須和signType參數保持一致。 * * @param data 待簽名數據 * @param key API密鑰 * @param signType 簽名方式 * @return 簽名 */ public static String generateSignature(final Map<String, String> data, String key, SignType signType) throws Exception { Set<String> keySet = data.keySet(); String[] keyArray = keySet.toArray(new String[keySet.size()]); Arrays.sort(keyArray); StringBuilder sb = new StringBuilder(); for (String k : keyArray) { if (k.equals(WXPayConstants.FIELD_SIGN)) { continue; } if (data.get(k).trim().length() > 0) // 參數值為空,則不參與簽名 sb.append(k).append("=").append(data.get(k).trim()).append("&"); } sb.append("key=").append(key); if (SignType.MD5.equals(signType)) { return MD5(sb.toString()).toUpperCase(); } else if (SignType.HMACSHA256.equals(signType)) { return HMACSHA256(sb.toString(), key); } else { log.error("獲取簽名失敗,失敗原因:"+String.format("Invalid sign_type: %s", signType)); throw new Exception(String.format("Invalid sign_type: %s", signType)); } } /** * 獲取隨機字符串 Nonce Str * @return String 隨機字符串 */ public static String generateNonceStr() { return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32); } /** * Map轉xml數據 */ public static String GetMapToXML(Map<String,String> param){ StringBuffer sb = new StringBuffer(); sb.append("<xml>"); for (Map.Entry<String,String> entry : param.entrySet()) { sb.append("<"+ entry.getKey() +">"); sb.append(entry.getValue()); sb.append("</"+ entry.getKey() +">"); } sb.append("</xml>"); return sb.toString(); } /** * 生成 MD5 * @param data 待處理數據 * @return MD5結果 */ public static String MD5(String data) throws Exception { java.security.MessageDigest md = MessageDigest.getInstance("MD5"); byte[] array = md.digest(data.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte item : array) { sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); } return sb.toString().toUpperCase(); } /** * 生成 HMACSHA256 * @param data 待處理數據 * @param key 密鑰 * @return 加密結果 * @throws Exception */ public static String HMACSHA256(String data, String key) throws Exception { Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"); sha256_HMAC.init(secret_key); byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte item : array) { sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); } return sb.toString().toUpperCase(); } /** * 日志 * @return */ public static Logger getLogger() { Logger logger = LoggerFactory.getLogger("wxpay java sdk"); return logger; } /** * 獲取當前時間戳,單位秒 * @return */ public static long getCurrentTimestamp() { return System.currentTimeMillis()/1000; } /** * 獲取當前時間戳,單位毫秒 * @return */ public static long getCurrentTimestampMs() { return System.currentTimeMillis(); } /** * 生成 uuid, 即用來標識一筆單,也用做 nonce_str * @return */ public static String generateUUID() { return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32); } /** * 支付簽名 * @param timestamp * @param noncestr * @param packages * @return * @throws UnsupportedEncodingException */ public static String paySign(String timestamp, String noncestr,String packages,String appId){ Map<String, String> paras = new HashMap<String, String>(); paras.put("appid", appId); paras.put("timestamp", timestamp); paras.put("noncestr", noncestr); paras.put("package", packages); paras.put("signType", "MD5"); StringBuffer sb = new StringBuffer(); Set es = paras.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 + "&"); } } String sign = MD5Util.MD5(sb.toString()).toUpperCase();//MD5加密 return sign; } }
XmlUtil.java
package com.yiexpress.core.utils; import java.io.StringReader; import java.lang.reflect.Field; import java.util.Date; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.xml.sax.InputSource; public class XmlUtil{ /** * json 數據轉換對象 * * @param Element * 要轉換的Element數據 * @param pojo * 要轉換的目標對象類型 * @return 轉換的目標對象 * @throws Exception * 轉換失敗 */ @SuppressWarnings("rawtypes") public static Object fromXmlToBean(Element rootElt, Class pojo) throws Exception{ // 首先得到pojo所定義的字段 Field[] fields = pojo.getDeclaredFields(); // 根據傳入的Class動態生成pojo對象 Object obj = pojo.newInstance(); for (Field field : fields) { // 設置字段可訪問(必須,否則報錯) field.setAccessible(true); // 得到字段的屬性名 String name = field.getName(); // 這一段的作用是如果字段在Element中不存在會拋出異常,如果出異常,則跳過。 try { rootElt.elementTextTrim(name); } catch (Exception ex) { continue; } if (rootElt.elementTextTrim(name) != null && !"".equals(rootElt.elementTextTrim(name))) { // 根據字段的類型將值轉化為相應的類型,並設置到生成的對象中。 if (field.getType().equals(Long.class) || field.getType().equals(long.class)) { field.set(obj, Long.parseLong(rootElt.elementTextTrim(name))); } else if (field.getType().equals(String.class)) { field.set(obj, rootElt.elementTextTrim(name)); } else if (field.getType().equals(Double.class) || field.getType().equals(double.class)) { field.set(obj, Double.parseDouble(rootElt.elementTextTrim(name))); } else if (field.getType().equals(Integer.class) || field.getType().equals(int.class)) { field.set(obj, Integer.parseInt(rootElt.elementTextTrim(name))); } else if (field.getType().equals(java.util.Date.class)) { field.set(obj, Date.parse(rootElt.elementTextTrim(name))); } else { continue; } } } return obj; } /** * 把xml格式轉化為指定對象 * * @param xml * @return */ @SuppressWarnings("unchecked") public static <T> T getBeanByxml(String xml, Class<T> valueType) { T person = null; InputSource in = new InputSource(new StringReader(xml)); in.setEncoding("UTF-8"); SAXReader reader = new SAXReader(); Document document; try { document = reader.read(in); Element root = document.getRootElement(); person = (T) XmlUtil.fromXmlToBean(root, valueType); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("數據解析錯誤"); } return person; } }
獲取預支付ID和簽名的controller
package com.yiexpress.jerry.controller.ewe.wechat; import java.util.HashMap; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.yiexpress.core.utils.wechat.UnifiedOrderRequest; import com.yiexpress.core.utils.wechat.UnifiedOrderRespose; import com.yiexpress.core.utils.wechat.WXPayUtil; /** * 微信支付controller */ @Controller @RequestMapping(value = "/wxpay") public class WXPayController{ private static final Logger LOGGER = LoggerFactory.getLogger(WXPayController.class); private String appId="公總號 appid";//公總號 appid private String mchId="商傢號";//商傢號 private String apiKey="商戶密匙";//商戶密匙 /** * 獲取終端IP * @param request * @return */ public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader( " x-forwarded-for " ); if (ip == null || ip.length() == 0 || " unknown " .equalsIgnoreCase(ip)) { ip = request.getHeader( " Proxy-Client-IP " ); } if (ip == null || ip.length() == 0 || " unknown " .equalsIgnoreCase(ip)) { ip = request.getHeader( " WL-Proxy-Client-IP " ); } if (ip == null || ip.length() == 0 || " unknown " .equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } /** * 支付初始化 返回預支付ID、簽名等信息 * @param payMoney * @return map * result -1 */ @RequestMapping("/toPayInit") @ResponseBody public Map<String,Object> toPay(HttpServletRequest request,@RequestParam(value="payMoney",required=true)float payMoney,@RequestParam(value="openId",required=true) String openId,@RequestParam(value="orderId",required=true)String orderId){ Map<String,Object> map = new HashMap<>(); //訂單號 目前生產的隨機數 後面放入指定系統唯一的單號 //判斷單號是否存在 String noncestr = WXPayUtil.generateNonceStr(); Map<String,String> requestMap = new HashMap<String, String>(); requestMap.put("appId",appId); requestMap.put("userWeixinOpenId",openId); //之前使用ETCA單號作為商戶訂單號,現在改為自動生成的賬單號 2018-10-25 Peter //requestMap.put("out_trade_no",auShipmentBrief.getShipmentReference()); requestMap.put("out_trade_no","訂單號"); requestMap.put("mch_id",mchId); //計算金額 微信支付的金額的單位是分,例如:實際支付1.23元,傳入參數就是123 int money=0; try { money=(int)(payMoney*100); } catch (Exception e) { map.put("result",-1); map.put("msg","金額格式不正確"); return map; } requestMap.put("payMoney",money+""); requestMap.put("spbill_create_ip", getIpAddr(request)); requestMap.put("notify_url","回調地址"); requestMap.put("noncestr", noncestr); requestMap.put("body","微信下單賬單支付"); requestMap.put("detail","散客下單賬單支付"); Map<String,Object> requestInfo = WXPayUtil.createOrderInfo(requestMap,apiKey); String orderInfo_toString = (String) requestInfo.get("orderInfo_toString"); LOGGER.debug("request 請求字符串:"+orderInfo_toString); //判斷返回碼 UnifiedOrderRespose orderResponse = WXPayUtil.httpOrder(orderInfo_toString,0);// 調用統一下單接口 //判斷超時的情況 if(orderResponse==null || orderResponse.getReturn_code()==null || ("SUCCESS".equals(orderResponse.getReturn_code()) && (orderResponse.getErr_code()==null || "SYSTEMERROR".equals(orderResponse.getErr_code())))){ orderResponse = WXPayUtil.httpOrder(orderInfo_toString,1); if(orderResponse==null || orderResponse.getReturn_code()==null || ("SUCCESS".equals(orderResponse.getReturn_code()) && (orderResponse.getErr_code()==null || "SYSTEMERROR".equals(orderResponse.getErr_code())))){ orderResponse = WXPayUtil.httpOrder(orderInfo_toString,2); } } LOGGER.debug("response 返回字段:==》{}",orderResponse); //根據微信文檔return_code 和result_code都為SUCCESS的時候才會返回code_url if(null!=orderResponse && "SUCCESS".equals(orderResponse.getReturn_code()) && "SUCCESS".equals(orderResponse.getResult_code())){ String timestamp = String.valueOf(WXPayUtil.getCurrentTimestamp()); map.put("timestamp",timestamp); map.put("noncestr",noncestr); UnifiedOrderRequest unifiedOrderRequest = (UnifiedOrderRequest) requestInfo.get("unifiedOrderRequest"); map.put("unifiedOrderRequest",unifiedOrderRequest); SortedMap<String, String> packageParams = new TreeMap<String, String>(); packageParams.put("appId",appId); packageParams.put("signType","MD5"); packageParams.put("nonceStr", noncestr); packageParams.put("timeStamp", timestamp); String packages = "prepay_id="+orderResponse.getPrepay_id(); packageParams.put("package",packages); String sign = null; try { //生成簽名 sign = WXPayUtil.generateSignature(packageParams,apiKey); } catch (Exception e) { map.put("result",-1); map.put("msg","支付簽名信息異常"); e.printStackTrace(); } if(sign!=null && !"".equals(sign)){ LOGGER.debug("------------支付簽名:"+sign+"-------------------"); map.put("paySign",sign); map.put("result",1); map.put("appId",appId); }else{ map.put("result",-1); map.put("msg","支付簽名信息異常"); } map.put("prepay_id",orderResponse.getPrepay_id()); return map; }else{ //不成功 if(orderResponse!=null){ String text = "調用微信支付出錯,返回狀態碼:"+orderResponse.getReturn_code()+",返回信息:"+orderResponse.getReturn_msg(); if(orderResponse.getErr_code()!=null && !"".equals(orderResponse.getErr_code())){ text = text +",錯誤碼:"+orderResponse.getErr_code()+",錯誤描述:"+orderResponse.getErr_code_des(); } LOGGER.error(text); }else{ LOGGER.error("返回值 orderResponse對象為空"); } map.put("result",-1); map.put("msg","支付環境異常,請稍後再試"); return map; } } }
jsp代碼
<script type="text/javascript"> //點擊支付按鈕 開始支付 function toPay(){ //初步判斷數據 var openId=$("#openId").val(); var payMoney=$("#payMoney").val(); $.ajax({ url : "${pageContext.request.contextPath}/toPayInit", type:"POST", dataType : 'json', data:{ payMoney:payMoney, openId:openId, orderId:"訂單號" }, success : function(result) { if(result.result==1){ var paySign = result.paySign; var prepay_id = result.prepay_id; var nonceStr = result.noncestr; var timestamp = result.timestamp; var unifiedOrderRequest = result.unifiedOrderRequest; var spbill_create_ip = unifiedOrderRequest.spbill_create_ip; var detail = unifiedOrderRequest.detail; var out_trade_no = unifiedOrderRequest.out_trade_no; var appId=result.appId; onBridgeReady(paySign,prepay_id,nonceStr,timestamp,appId); }else{ alert("失敗"); } }, error : function(data, status, e) { // 服務器響應失敗時的處理函數 alert("數據異常,支付失敗", 'error'); } }); } //調起公眾號支付 function onBridgeReady(paySign,prepay_id,nonceStr,timestamp,appId){ WeixinJSBridge.invoke( 'getBrandWCPayRequest', { "appId":appId, //appid "timeStamp":timestamp, "nonceStr":nonceStr, //隨機串 "package":"prepay_id="+prepay_id, "signType":"MD5", "paySign":paySign //微信簽名 }, function(res){ // 使用以上方式判斷前端返回,微信團隊鄭重提示:res.err_msg將在用戶支付成功後返回 ok,但並不保證它絕對可靠。 if(res.err_msg == "get_brand_wcpay_request:ok" ) { alert("支付完成", 'success'); }else if(res.err_msg == "get_brand_wcpay_request:cancel" ) { alert("取消支付", 'success'); }else if(res.err_msg == "get_brand_wcpay_request:fail"){ alert("支付失敗", 'success'); } } ); } </script>
定義微信支付成功回調接口APIAupostController.java
package com.yiexpress.api.controller.ewe.aupost; import java.util.HashMap; import java.util.Map; import javax.annotation.Resource; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections.MapUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.yiexpress.core.utils.wechat.WXPayUtil; @Controller @RequestMapping("/api") public class APIAupostController { private static final Logger LOGGER=LoggerFactory.getLogger(APIAupostController.class); @Resource(name = "jacksonBean") private MappingJackson2HttpMessageConverter jackson; private String apiKey="商戶密匙";//商戶密匙 /** * 異步回調接口 * @param request * @param response * @throws Exception */ @RequestMapping(value="/paymentNotice",produces="text/html;charset=utf-8") @ResponseBody public String WeixinParentNotifyPage(HttpServletRequest request,HttpServletResponse response) throws Exception{ ServletInputStream instream = request.getInputStream(); StringBuffer sb = new StringBuffer(); int len = -1; byte[] buffer = new byte[1024]; while((len = instream.read(buffer)) != -1){ sb.append(new String(buffer,0,len)); } instream.close(); Map<String,String> map = WXPayUtil.xmlToMap(sb.toString());//接受微信的回調的通知參數 Map<String,String> return_data = new HashMap<String,String>(); //判斷簽名是否正確 if(WXPayUtil.isSignatureValid(map,apiKey)){ if(map.get("return_code").toString().equals("FAIL")){ return_data.put("return_code", "FAIL"); return_data.put("return_msg", map.get("return_msg")); }else { String return_code=MapUtils.getString(map,"return_code"); String result_code=MapUtils.getString(map,"result_code"); if(return_code!=null && "SUCCESS".equals(return_code) && result_code!=null && "SUCCESS".equals(result_code)){ String out_trade_no =MapUtils.getString(map,"out_trade_no");//系統訂單號 //支付成功,可以自定義新邏輯 } } }else{ return_data.put("return_code", "FAIL"); return_data.put("return_msg", "簽名錯誤"); } String xml = WXPayUtil.GetMapToXML(return_data); LOGGER.error("支付通知回調結果:"+xml); return xml; } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- java實現支付寶支付接口的調用
- 三分鐘python搭建支付寶三方支付
- 如何設計一個安全的API接口詳解
- SpringBoot實現簡易支付寶網頁支付功能
- springboot如何使用@ConfigurationProperties封裝配置文件