e2

滴滴侠,fai抖

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
  1. package com.wxpay.util;  
  2.   
  3.   
  4. import java.io.BufferedReader;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7. import java.io.OutputStreamWriter;  
  8. import java.net.URL;  
  9. import java.net.URLConnection;  
  10.   
  11.   
  12. import org.apache.log4j.Logger;  
  13.   
  14.   
  15. /** 
  16.  * http请求工具类 
  17.  * @author chenp 
  18.  * 
  19.  */  
  20. public class HttpUtil {  
  21.   
  22.     private final static int CONNECT_TIMEOUT = 5000// in milliseconds  连接超时的时间  
  23.     private final static String DEFAULT_ENCODING = "UTF-8";  //字符串编码  
  24.     private static Logger lg=Logger.getLogger(HttpUtil.class);  
  25.       
  26.     public static String postData(String urlStr, String data){    
  27.         return postData(urlStr, data, null);    
  28.     }    
  29.     /** 
  30.      * post数据请求 
  31.      * @param urlStr 
  32.      * @param data 
  33.      * @param contentType 
  34.      * @return 
  35.      */  
  36.     public static String postData(String urlStr, String data, String contentType){    
  37.         BufferedReader reader = null;    
  38.         try {    
  39.             URL url = new URL(urlStr);    
  40.             URLConnection conn = url.openConnection();    
  41.             conn.setDoOutput(true);    
  42.             conn.setConnectTimeout(CONNECT_TIMEOUT);    
  43.             conn.setReadTimeout(CONNECT_TIMEOUT);    
  44.             if(contentType != null)    
  45.                 conn.setRequestProperty("content-type", contentType);    
  46.             OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);    
  47.             if(data == null)    
  48.                 data = "";    
  49.             writer.write(data);     
  50.             writer.flush();    
  51.             writer.close();      
  52.     
  53.             reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));    
  54.             StringBuilder sb = new StringBuilder();    
  55.             String line = null;    
  56.             while ((line = reader.readLine()) != null) {    
  57.                 sb.append(line);    
  58.                 sb.append("\r\n");    
  59.             }    
  60.             return sb.toString();    
  61.         } catch (IOException e) {    
  62.         lg.info("Error connecting to " + urlStr + ": " + e.getMessage());    
  63.         } finally {    
  64.             try {    
  65.                 if (reader != null)    
  66.                     reader.close();    
  67.             } catch (IOException e) {    
  68.             }    
  69.         }    
  70.         return null;    
  71.     }    
  72. }    
  73.   
  74. package com.wxpay.util;  
  75.   
  76.   
  77. import java.security.MessageDigest;  
  78. /** 
  79.  * Md5加密类 
  80.  * @author chenp 
  81.  * 
  82.  */  
  83. public class MD5Util {  
  84.   
  85. private static String byteArrayToHexString(byte b[]) {    
  86.         StringBuffer resultSb = new StringBuffer();    
  87.         for (int i = 0; i < b.length; i++)    
  88.             resultSb.append(byteToHexString(b[i]));    
  89.     
  90.         return resultSb.toString();    
  91.     }    
  92.     
  93.     private static String byteToHexString(byte b) {    
  94.         int n = b;    
  95.         if (n < 0)    
  96.             n += 256;    
  97.         int d1 = n / 16;    
  98.         int d2 = n % 16;    
  99.         return hexDigits[d1] + hexDigits[d2];    
  100.     }    
  101.     
  102.     public static String MD5Encode(String origin, String charsetname) {    
  103.         String resultString = null;    
  104.         try {    
  105.             resultString = new String(origin);    
  106.             MessageDigest md = MessageDigest.getInstance("MD5");    
  107.             if (charsetname == null || "".equals(charsetname))    
  108.                 resultString = byteArrayToHexString(md.digest(resultString    
  109.                         .getBytes()));    
  110.             else    
  111.                 resultString = byteArrayToHexString(md.digest(resultString    
  112.                         .getBytes(charsetname)));    
  113.         } catch (Exception exception) {    
  114.         }    
  115.         return resultString;    
  116.     }    
  117.     
  118.     private static final String hexDigits[] = { "0""1""2""3""4""5",    
  119.             "6""7""8""9""a""b""c""d""e""f" };    
  120.     
  121. }  
  122.   
  123.   
  124. package com.wxpay.util;  
  125.   
  126.   
  127. import java.net.Inet4Address;  
  128. import java.net.InetAddress;  
  129. import java.net.InterfaceAddress;  
  130. import java.net.NetworkInterface;  
  131. import java.net.SocketException;  
  132. import java.text.SimpleDateFormat;  
  133. import java.util.Date;  
  134. import java.util.Enumeration;  
  135. import java.util.Iterator;  
  136. import java.util.List;  
  137. import java.util.Map;  
  138. import java.util.Set;  
  139. import java.util.SortedMap;  
  140.   
  141.   
  142. import org.apache.log4j.Logger;  
  143.   
  144.   
  145. public class PayForUtil {  
  146.   
  147. private static Logger lg=Logger.getLogger(PayForUtil.class);  
  148.   
  149. /**  
  150.      * 是否签名正确,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。  
  151.      * @return boolean  
  152.      */    
  153.     public static boolean isTenpaySign(String characterEncoding, SortedMap<Object, Object> packageParams, String API_KEY) {    
  154.         StringBuffer sb = new StringBuffer();    
  155.         Set es = packageParams.entrySet();    
  156.         Iterator it = es.iterator();    
  157.         while(it.hasNext()) {    
  158.             Map.Entry entry = (Map.Entry)it.next();    
  159.             String k = (String)entry.getKey();    
  160.             String v = (String)entry.getValue();    
  161.             if(!"sign".equals(k) && null != v && !"".equals(v)) {    
  162.                 sb.append(k + "=" + v + "&");    
  163.             }    
  164.         }    
  165.         sb.append("key=" + API_KEY);    
  166.             
  167.         //算出摘要    
  168.         String mysign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toLowerCase();    
  169.         String tenpaySign = ((String)packageParams.get("sign")).toLowerCase();    
  170.             
  171.         return tenpaySign.equals(mysign);    
  172.     }    
  173.     
  174.     /**  
  175.      * @author chenp 
  176.      * @Description:sign签名  
  177.      * @param characterEncoding  
  178.      *            编码格式  
  179.      * @param parameters  
  180.      *            请求参数  
  181.      * @return  
  182.      */    
  183.     public static String createSign(String characterEncoding, SortedMap<Object, Object> packageParams, String API_KEY) {    
  184.         StringBuffer sb = new StringBuffer();    
  185.         Set es = packageParams.entrySet();    
  186.         Iterator it = es.iterator();    
  187.         while (it.hasNext()) {    
  188.             Map.Entry entry = (Map.Entry) it.next();    
  189.             String k = (String) entry.getKey();    
  190.             String v = (String) entry.getValue();    
  191.             if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {    
  192.                 sb.append(k + "=" + v + "&");    
  193.             }    
  194.         }    
  195.         sb.append("key=" + API_KEY);    
  196.         String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();    
  197.         return sign;    
  198.     }    
  199.     
  200.     /**  
  201.      * @author chenp 
  202.      * @Description:将请求参数转换为xml格式的string  
  203.      * @param parameters  
  204.      *            请求参数  
  205.      * @return  
  206.      */    
  207.     public static String getRequestXml(SortedMap<Object, Object> parameters) {    
  208.         StringBuffer sb = new StringBuffer();    
  209.         sb.append("<xml>");    
  210.         Set es = parameters.entrySet();    
  211.         Iterator it = es.iterator();    
  212.         while (it.hasNext()) {    
  213.             Map.Entry entry = (Map.Entry) it.next();    
  214.             String k = (String) entry.getKey();    
  215.             String v = (String) entry.getValue();    
  216.             if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k)) {    
  217.                 sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");    
  218.             } else {    
  219.                 sb.append("<" + k + ">" + v + "</" + k + ">");    
  220.             }    
  221.         }    
  222.         sb.append("</xml>");    
  223.         return sb.toString();    
  224.     }    
  225.     
  226.     /**  
  227.      * 取出一个指定长度大小的随机正整数.  
  228.      *   
  229.      * @param length  
  230.      *            int 设定所取出随机数的长度。length小于11  
  231.      * @return int 返回生成的随机数。  
  232.      */    
  233.     public static int buildRandom(int length) {    
  234.         int num = 1;    
  235.         double random = Math.random();    
  236.         if (random < 0.1) {    
  237.             random = random + 0.1;    
  238.         }    
  239.         for (int i = 0; i < length; i++) {    
  240.             num = num * 10;    
  241.         }    
  242.         return (int) ((random * num));    
  243.     }    
  244.     
  245.     /**  
  246.      * 获取当前时间 yyyyMMddHHmmss  
  247.      *  @author chenp 
  248.      * @return String  
  249.      */    
  250.     public static String getCurrTime() {    
  251.         Date now = new Date();    
  252.         SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");    
  253.         String s = outFormat.format(now);    
  254.         return s;    
  255.     }  
  256.     /** 
  257.      * 获取本机IP地址 
  258.      * @author chenp 
  259.      * @return 
  260.      */  
  261.     public static String localIp(){  
  262.         String ip = null;  
  263.         Enumeration allNetInterfaces;  
  264.         try {  
  265.             allNetInterfaces = NetworkInterface.getNetworkInterfaces();              
  266.             while (allNetInterfaces.hasMoreElements()) {  
  267.                 NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();  
  268.                 List<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses();  
  269.                 for (InterfaceAddress add : InterfaceAddress) {  
  270.                     InetAddress Ip = add.getAddress();  
  271.                     if (Ip != null && Ip instanceof Inet4Address) {  
  272.                         ip = Ip.getHostAddress();  
  273.                     }  
  274.                 }  
  275.             }  
  276.         } catch (SocketException e) {  
  277.         lg.warn("获取本机Ip失败:异常信息:"+e.getMessage());  
  278.         }  
  279.         return ip;  
  280.     }  
  281.     
  282. }  
  283.   
  284. package com.wxpay.util;  
  285.   
  286.   
  287. import com.iptop.service.base.ConfigProperties;  
  288.   
  289.   
  290. /** 
  291.  * 微信支付配置文件 
  292.  * @author chenp 
  293.  * 
  294.  */  
  295. public class WeChatConfig {  
  296.   
  297. /** 
  298. * 微信服务号APPID 
  299. */  
  300. public static String APPID=ConfigProperties.get("APPID");  
  301. public static String APPID_APP=ConfigProperties.get("APPID_APP");  
  302. /** 
  303. * 微信支付的商户号 
  304. */  
  305. public static String MCHID=ConfigProperties.get("MCHID");  
  306. public static String MCHID_APP=ConfigProperties.get("MCHID_APP");  
  307. /** 
  308. * 微信支付的API密钥 
  309. */  
  310. public static String APIKEY=ConfigProperties.get("APIKEY");  
  311. public static String APIKEY_APP=ConfigProperties.get("APIKEY_APP");  
  312. /** 
  313. * 微信支付成功之后的回调地址【注意:当前回调地址必须是公网能够访问的地址】 
  314. */  
  315. public static String WECHAT_NOTIFY_URL_PC=ConfigProperties.get("WECHAT_NOTIFY_URL_PC");  
  316. public static String WECHAT_NOTIFY_URL_APP=ConfigProperties.get("WECHAT_NOTIFY_URL_APP");  
  317. /** 
  318. * 微信下单API地址 
  319. */  
  320. public static String UFDODER_URL=ConfigProperties.get("UFDODER_URL");  
  321. /** 
  322. * true为使用真实金额支付,false为使用测试金额支付(1分) 
  323. */  
  324. public static String WXPAY=ConfigProperties.get("WXPAY");  
  325.   
  326. }  
  327.   
  328. package com.wxpay.util;  
  329. /** 
  330.  * 微信支付需要的一些参数 
  331.  * @author chenp 
  332.  * 
  333.  */  
  334. public class WeChatParams {  
  335.   
  336. public String total_fee;//订单金额【备注:以分为单位】  
  337. public String body;//商品名称  
  338. public String out_trade_no;//商户订单号  
  339. public String attach;//附加參數  
  340. public String memberid;//会员ID  
  341.   
  342. }  
  343.   
  344. package com.wxpay.util;  
  345.   
  346.   
  347. import java.io.ByteArrayInputStream;  
  348. import java.io.IOException;  
  349. import java.io.InputStream;  
  350. import java.util.HashMap;  
  351. import java.util.Iterator;  
  352. import java.util.List;  
  353. import java.util.Map;  
  354.   
  355.   
  356. import org.jdom.Document;  
  357. import org.jdom.Element;  
  358. import org.jdom.JDOMException;  
  359. import org.jdom.input.SAXBuilder;  
  360.   
  361.   
  362. public class XMLUtil {  
  363.   
  364. /**  
  365.      * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。  
  366.      * @param strxml  
  367.      * @return  
  368.      * @throws JDOMException  
  369.      * @throws IOException  
  370.      */    
  371.     public static Map doXMLParse(String strxml) throws JDOMException, IOException {    
  372.         strxml = strxml.replaceFirst("encoding=\".*\"""encoding=\"UTF-8\"");    
  373.     
  374.         if(null == strxml || "".equals(strxml)) {    
  375.             return null;    
  376.         }    
  377.             
  378.         Map m = new HashMap();    
  379.             
  380.         InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));    
  381.         SAXBuilder builder = new SAXBuilder();    
  382.         Document doc = builder.build(in);    
  383.         Element root = doc.getRootElement();    
  384.         List list = root.getChildren();    
  385.         Iterator it = list.iterator();    
  386.         while(it.hasNext()) {    
  387.             Element e = (Element) it.next();    
  388.             String k = e.getName();    
  389.             String v = "";    
  390.             List children = e.getChildren();    
  391.             if(children.isEmpty()) {    
  392.                 v = e.getTextNormalize();    
  393.             } else {    
  394.                 v = XMLUtil.getChildrenText(children);    
  395.             }    
  396.                 
  397.             m.put(k, v);    
  398.         }    
  399.             
  400.         //关闭流    
  401.         in.close();    
  402.             
  403.         return m;    
  404.     }    
  405.         
  406.     /**  
  407.      * 获取子结点的xml  
  408.      * @param children  
  409.      * @return String  
  410.      */    
  411.     public static String getChildrenText(List children) {    
  412.         StringBuffer sb = new StringBuffer();    
  413.         if(!children.isEmpty()) {    
  414.             Iterator it = children.iterator();    
  415.             while(it.hasNext()) {    
  416.                 Element e = (Element) it.next();    
  417.                 String name = e.getName();    
  418.                 String value = e.getTextNormalize();    
  419.                 List list = e.getChildren();    
  420.                 sb.append("<" + name + ">");    
  421.                 if(!list.isEmpty()) {    
  422.                     sb.append(XMLUtil.getChildrenText(list));    
  423.                 }    
  424.                 sb.append(value);    
  425.                 sb.append("</" + name + ">");    
  426.             }    
  427.         }    
  428.             
  429.         return sb.toString();    
  430.     }    
  431.         
  432. }  
  433.   
  434.   
  435.   
  436. package com.wxpay.util;  
  437.   
  438.   
  439. import java.awt.image.BufferedImage;  
  440. import java.io.IOException;  
  441. import java.io.UnsupportedEncodingException;  
  442. import java.net.URLEncoder;  
  443. import java.util.HashMap;  
  444. import java.util.Map;  
  445. import java.util.SortedMap;  
  446. import java.util.TreeMap;  
  447. import javax.imageio.ImageIO;  
  448. import javax.servlet.http.HttpServletResponse;  
  449. import org.apache.commons.lang.StringUtils;  
  450. import org.apache.log4j.Logger;  
  451. import com.google.zxing.BarcodeFormat;  
  452. import com.google.zxing.MultiFormatWriter;  
  453. import com.google.zxing.WriterException;  
  454. import com.google.zxing.common.BitMatrix;  
  455.   
  456.   
  457. public class WeixinPay {  
  458.   
  459. public static Logger lg=Logger.getLogger(WeixinPay.class);  
  460. private static final int BLACK = 0xff000000;  
  461. private static final int WHITE = 0xFFFFFFFF;  
  462.   
  463. /** 
  464. * 获取微信支付的二维码地址 
  465. * @return 
  466. * @author chenp 
  467. * @throws Exception 
  468. */  
  469. public static String getCodeUrl(WeChatParams ps) throws Exception {    
  470.         /** 
  471.          * 账号信息   
  472.          */  
  473.         String appid = WeChatConfig.APPID;//微信服务号的appid    
  474.         String mch_id = WeChatConfig.MCHID; //微信支付商户号    
  475.         String key = WeChatConfig.APIKEY; // 微信支付的API密钥    
  476.         String notify_url = WeChatConfig.WECHAT_NOTIFY_URL_PC;//回调地址【注意,这里必须要使用外网的地址】       
  477.         String ufdoder_url=WeChatConfig.UFDODER_URL;//微信下单API地址  
  478.         String trade_type = "NATIVE"//类型【网页扫码支付】  
  479.           
  480.         /** 
  481.          * 时间字符串 
  482.          */  
  483.         String currTime = PayForUtil.getCurrTime();  
  484.         String strTime = currTime.substring(8, currTime.length());    
  485.         String strRandom = PayForUtil.buildRandom(4) + "";    
  486.         String nonce_str = strTime + strRandom;    
  487.             
  488.         /** 
  489.          * 参数封装 
  490.          */  
  491.         SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();    
  492.         packageParams.put("appid", appid);    
  493.         packageParams.put("mch_id", mch_id);  
  494.         packageParams.put("nonce_str", nonce_str);//随机字符串  
  495.         packageParams.put("body", ps.body);//支付的商品名称    
  496.         packageParams.put("out_trade_no", ps.out_trade_no+nonce_str);//商户订单号【备注:每次发起请求都需要随机的字符串,否则失败。】  
  497.         packageParams.put("total_fee", ps.total_fee);//支付金额  
  498.         packageParams.put("spbill_create_ip", PayForUtil.localIp());//客户端主机  
  499.         packageParams.put("notify_url", notify_url);  
  500.         packageParams.put("trade_type", trade_type);  
  501.         packageParams.put("attach", ps.attach);//额外的参数【业务类型+会员ID+支付类型】  
  502.           
  503.     
  504.         String sign = PayForUtil.createSign("UTF-8", packageParams,key);  //获取签名  
  505.         packageParams.put("sign", sign);    
  506.             
  507.         String requestXML = PayForUtil.getRequestXml(packageParams);//将请求参数转换成String类型    
  508.         lg.info("微信支付请求参数的报文"+requestXML);    
  509.         String resXml = HttpUtil.postData(ufdoder_url,requestXML);  //解析请求之后的xml参数并且转换成String类型  
  510.         Map map = XMLUtil.doXMLParse(resXml);    
  511.         lg.info("微信支付响应参数的报文"+resXml);   
  512.         String urlCode = (String) map.get("code_url");    
  513.           
  514.         return urlCode;    
  515. }    
  516.   
  517.   /** 
  518.    * 将路径生成二维码图片 
  519.    * @author chenp 
  520.    * @param content 
  521.    * @param response 
  522.    */  
  523.   @SuppressWarnings({ "unchecked""rawtypes" })  
  524.   public static void encodeQrcode(String content,HttpServletResponse response){  
  525.    
  526.       if(StringUtils.isBlank(content))  
  527.           return;  
  528.      MultiFormatWriter multiFormatWriter = new MultiFormatWriter();  
  529.      Map hints = new HashMap();  
  530.      BitMatrix bitMatrix = null;  
  531.      try {  
  532.          bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 250250,hints);  
  533.          BufferedImage image = toBufferedImage(bitMatrix);  
  534.          //输出二维码图片流  
  535.          try {  
  536.              ImageIO.write(image, "png", response.getOutputStream());  
  537.          } catch (IOException e) {  
  538.              e.printStackTrace();  
  539.          }  
  540.      } catch (WriterException e1) {  
  541.          e1.printStackTrace();  
  542.      }           
  543.  }  
  544.   /** 
  545.   * 类型转换 
  546.   * @author chenp 
  547.   * @param matrix 
  548.   * @return 
  549.   */  
  550. public static BufferedImage toBufferedImage(BitMatrix matrix) {  
  551.          int width = matrix.getWidth();  
  552.          int height = matrix.getHeight();  
  553.          BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);  
  554.          for (int x = 0; x < width; x++) {  
  555.              for (int y = 0; y < height; y++) {  
  556.                  image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);  
  557.              }  
  558.          }  
  559.          return image;  
  560. }  
  561. // 特殊字符处理    
  562. public static String UrlEncode(String src)  throws UnsupportedEncodingException {    
  563.    return URLEncoder.encode(src, "UTF-8").replace("+""%20");    
  564. }  
  565.   
  566. }  
  567.   
  568. /** 
  569. * pc端微信支付之后的回调方法 
  570. * @param request 
  571. * @param response 
  572. * @throws Exception 
  573. */  
  574.     @RequestMapping(value="wechat_notify_url_pc",method=RequestMethod.POST)  
  575. public void wechat_notify_url_pc(HttpServletRequest request,HttpServletResponse response) throws Exception{    
  576.           
  577.         //读取参数    
  578.         InputStream inputStream ;    
  579.         StringBuffer sb = new StringBuffer();    
  580.         inputStream = request.getInputStream();    
  581.         String s ;    
  582.         BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));    
  583.         while ((s = in.readLine()) != null){    
  584.             sb.append(s);    
  585.         }    
  586.         in.close();    
  587.         inputStream.close();    
  588.     
  589.         //解析xml成map    
  590.         Map<String, String> m = new HashMap<String, String>();    
  591.         m = XMLUtil.doXMLParse(sb.toString());    
  592.             
  593.         //过滤空 设置 TreeMap    
  594.         SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();          
  595.         Iterator<String> it = m.keySet().iterator();    
  596.         while (it.hasNext()) {    
  597.             String parameter = it.next();    
  598.             String parameterValue = m.get(parameter);    
  599.                 
  600.             String v = "";    
  601.             if(null != parameterValue) {    
  602.                 v = parameterValue.trim();    
  603.             }    
  604.             packageParams.put(parameter, v);    
  605.         }    
  606.         // 微信支付的API密钥    
  607.         String key = WeChatConfig.APIKEY; // key    
  608.     
  609.         lg.info("微信支付返回回来的参数:"+packageParams);    
  610.         //判断签名是否正确    
  611.         if(PayForUtil.isTenpaySign("UTF-8", packageParams,key)) {    
  612.             //------------------------------    
  613.             //处理业务开始    
  614.             //------------------------------    
  615.             String resXml = "";    
  616.             if("SUCCESS".equals((String)packageParams.get("result_code"))){    
  617.                 // 这里是支付成功    
  618.             //执行自己的业务逻辑开始  
  619.             String app_id = (String)packageParams.get("appid");  
  620.                 String mch_id = (String)packageParams.get("mch_id");    
  621.                 String openid = (String)packageParams.get("openid");   
  622.                 String is_subscribe = (String)packageParams.get("is_subscribe");//是否关注公众号  
  623.                   
  624.                 //附加参数【商标申请_0bda32824db44d6f9611f1047829fa3b_15460】--【业务类型_会员ID_订单号】  
  625.                 String attach = (String)packageParams.get("attach");  
  626.                 //商户订单号  
  627.                 String out_trade_no = (String)packageParams.get("out_trade_no");    
  628.                 //付款金额【以分为单位】  
  629.                 String total_fee = (String)packageParams.get("total_fee");    
  630.                 //微信生成的交易订单号  
  631.                 String transaction_id = (String)packageParams.get("transaction_id");//微信支付订单号  
  632.                 //支付完成时间  
  633.                 String time_end=(String)packageParams.get("time_end");  
  634.                   
  635.                 lg.info("app_id:"+app_id);  
  636.                 lg.info("mch_id:"+mch_id);    
  637.                 lg.info("openid:"+openid);    
  638.                 lg.info("is_subscribe:"+is_subscribe);    
  639.                 lg.info("out_trade_no:"+out_trade_no);    
  640.                 lg.info("total_fee:"+total_fee);    
  641.                 lg.info("额外参数_attach:"+attach);   
  642.                 lg.info("time_end:"+time_end);   
  643.                   
  644.                 //执行自己的业务逻辑结束  
  645.                 lg.info("支付成功");    
  646.                 //通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了.    
  647.                 resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"    
  648.                         + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";    
  649.                     
  650.             } else {    
  651.                 lg.info("支付失败,错误信息:" + packageParams.get("err_code"));    
  652.                 resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"    
  653.                         + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";    
  654.             }    
  655.             //------------------------------    
  656.             //处理业务完毕    
  657.             //------------------------------    
  658.             BufferedOutputStream out = new BufferedOutputStream(    
  659.                     response.getOutputStream());    
  660.             out.write(resXml.getBytes());    
  661.             out.flush();    
  662.             out.close();  
  663.         } else{    
  664.             lg.info("通知签名验证失败");    
  665.         }    
  666.             
  667.     }  
posted on 2017-05-19 19:40  纯黑Se丶  阅读(376)  评论(0)    收藏  举报