package com.winsun.tenpay;


import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.winsun.tenpay.util.ConstantUtil;
import com.winsun.tenpay.util.MD5Util;

public class TenpayNotify
{
  private static Logger log = Logger.getLogger(TenpayNotify.class);
  
  public static void buildSign(SortedMap<String, String> paramMap)
  {
    try
    {
      StringBuffer sb = new StringBuffer();
      Set set = paramMap.entrySet();
      Iterator it = set.iterator();
      while (it.hasNext())
      {
        Map.Entry entry = (Map.Entry)it.next();
        String k = (String)entry.getKey();
        String v = (String)entry.getValue();
        if ((v != null) && (!"".equals(v)) && (!"sign".equals(k)) && (!"key".equals(k))) {
          sb.append(k + "=" + v + "&");
        }
      }
      sb.append("key=" + ConstantUtil.APP_KEY);
      String sign = MD5Util.MD5Encode(sb.toString(), "utf-8").toUpperCase();
      paramMap.put("sign", sign);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  
  public static String buildTradeSign(SortedMap<String, String> paramMap)
  {
    String sign = "";
    try
    {
      StringBuffer sb = new StringBuffer();
      Set set = paramMap.entrySet();
      Iterator it = set.iterator();
      while (it.hasNext())
      {
        Map.Entry entry = (Map.Entry)it.next();
        String k = (String)entry.getKey();
        String v = (String)entry.getValue();
        if ((v != null) && (!"".equals(v)) && (!"sign".equals(k)) && (!"key".equals(k))) {
          sb.append(k + "=" + v + "&");
        }
      }
      sb.append("key=" + ConstantUtil.APP_KEY);
      sign = MD5Util.MD5Encode(sb.toString(), "utf-8").toUpperCase();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return sign;
  }
  
  public static boolean verifySign(SortedMap<String, String> paramMap)
  {
    try
    {
      StringBuffer sb = new StringBuffer();
      String remoteSign = (String)paramMap.remove("sign");
      Set set = paramMap.entrySet();
      Iterator it = set.iterator();
      while (it.hasNext())
      {
        Map.Entry entry = (Map.Entry)it.next();
        String k = (String)entry.getKey();
        String v = (String)entry.getValue();
        if ((v != null) && (!"".equals(v)) && (!"sign".equals(k)) && (!"key".equals(k))) {
          sb.append(k + "=" + v + "&");
        }
      }
      sb.append("key=" + ConstantUtil.APP_KEY);
      
      String sign = MD5Util.MD5Encode(sb.toString(), "utf-8").toUpperCase(Locale.ENGLISH);
      if (remoteSign.equals(sign)) {
        return true;
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return false;
  }
  
  public static SortedMap<String, String> analyzeXmlToMap(String xml)
  {
    SortedMap<String, String> rtnMap = new TreeMap();
    try
    {
      Document doc = DocumentHelper.parseText(xml);
      Element root = doc.getRootElement();
      String code = root.element("return_code").getText();
      if ("SUCCESS".equals(code))
      {
        List<Element> elist = root.elements();
        for (Element ele : elist)
        {
          String name = ele.getName();
          String value = ele.getText();
          rtnMap.put(name, value);
        }
      }
      else
      {
        String msg = root.element("return_msg").getText();
        log.error("微信原生支付通知失败，建议校园奖励发起主动查询" + msg);
      }
    }
    catch (Exception e)
    {
      log.error("解析微信原生支付xml异常，" + e.toString());
      e.printStackTrace();
    }
    return rtnMap;
  }
}
