/* 
 * Copyrights 尚果科技平台研发项目组 @ 2015，尚果科技 SEOCOO Computer Co., Ltd.<br>
 * 项目名称：尚果科技-平台开发组 <br>
 * 文件名称：MicroPayCore.java <br>
 * 创建时间：2015-6-15-下午04:38:18 <br>
 * Description： 
 * History：<br> [ Author Date Version Content ]<br>
 *  
 */
package cn.pluss.platform.wechat;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MicroPayCore {

	 /** 
     * 除去数组中的空值和签名参数
     * @param sArray 签名参数组
     * @return 去掉空值与签名参数后的新签名参数组
     */
    public static Map<String, String> paraFilter(Map<String, String> sArray) {

        Map<String, String> result = new HashMap<String, String>();

        if (sArray == null || sArray.size() <= 0) {
            return result;
        }

        for (String key : sArray.keySet()) {
            String value = sArray.get(key);
            if (value == null || value.equals("") || key.equalsIgnoreCase("sign")
                || key.equalsIgnoreCase("sign_type")) {
                continue;
            }
            result.put(key, value);
        }

        return result;
    }
    

    /**
     * 生成签名结果
     * @param sArray 要签名的数组
     * @return 签名结果字符串
     */
    public static String buildMysign(Map<String, String> sArray,String paternerKey) {
        String prestr = createLinkString(sArray); //把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
        prestr = prestr + "&key="+paternerKey; //把拼接后的字符串再与安全校验码直接连接起来
        String mysign = AlipayMd5Encrypt.md5(prestr);
        return mysign;
    }
    
    /** 
     * 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String createLinkString(Map<String, String> params) {

        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);

        String prestr = "";

        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);

            if (i == keys.size() - 1) {//拼接时，不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }

        return prestr;
    }
    
    
    /** 
     * 对所有的value进行urlencode编码
     * @param sArray 签名参数组
     * @return 去掉空值与签名参数后的新签名参数组
     */
    public static String urlEncode(Map<String, String> params) {

    	  List<String> keys = new ArrayList<String>(params.keySet());

          String prestr = "";

          for (int i = 0; i < keys.size(); i++) {
              String key = keys.get(i);
              String value = params.get(key);

              //urlencode编码
              try {
				value=URLEncoder.encode(value, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
              if (i == keys.size() - 1) {//拼接时，不包括最后一个&字符
                  prestr = prestr + key + "=" + value;
              } else {
                  prestr = prestr + key + "=" + value + "&";
              }
          }

        return prestr;
    }
    
    public static void main(String[] args) {
		
    	Map<String,String> param=new HashMap<String, String>();
		param.put("appId","wxe7dc6c765a568c76");
		param.put("timeStamp","1472480747126");
		param.put("nonceStr","112016082922254733027");
		param.put("package","prepay_id=wx20160829222547a02f157b8d0833160254");
		param.put("signType","MD5");
		System.out.println(buildMysign(param,"d1af79f0f3991dce45f246b8e1a6ebef"));
	}
}
