package com.cirle.scientific.utils.weixin;

//import com.alipay.config.MD5;
import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

public class SignUtil {
	
	 
 
	/**
	 * 生成签名
	 * @param weiXinPay
	 * @return
	 */
	public static String getSignString(WeiXinPay weiXinPay){
		
		String stringSignTemp="";
		
		String sign="";
		
		
		StringBuffer stringA=new StringBuffer();
		             //公众账号ID
		             stringA.append("appid="+WeixinpayConfig.appId+"&");
		             //商品描述
			         if(weiXinPay.getBody()!=null)
			        	  stringA.append("body="+weiXinPay.getBody()+"&");
		             //商户号
		             stringA.append("mch_id="+WeixinpayConfig.mchId+"&");
		             //随机字符串
			         if(weiXinPay.getNonceStr()!=null)
			        	  stringA.append("nonce_str="+weiXinPay.getNonceStr()+"&");
		             //通知地址
		        	 stringA.append("notify_url="+WeixinpayConfig.notifyUrl+"&");
		        	 //OpenId
		        	 if(weiXinPay.getOpenId()!=null && !"".equals(weiXinPay.getOpenId()))
		        		  stringA.append("openid="+weiXinPay.openId+"&");
	        	     //商户订单号
		        	 if(weiXinPay.getOutTradeNo()!=null)
		        		  stringA.append("out_trade_no="+weiXinPay.getOutTradeNo()+"&");
	                 //商品ID
		             if(weiXinPay.getProductId()!=null)
		            	 stringA.append("product_id="+weiXinPay.getProductId()+"&");
		             //终端IP
		             if(weiXinPay.getSpbillCreateIp()!=null)
		            	 stringA.append("spbill_create_ip="+weiXinPay.getSpbillCreateIp()+"&");
		             //总金额
		             if(weiXinPay.getTotalFee()!=0)
		            	 stringA.append("total_fee="+weiXinPay.getTotalFee()+"&");
		             //交易类型
		             if(weiXinPay.getTradeType()!=null)
		            	 stringA.append("trade_type="+weiXinPay.getTradeType());
 
		           
		           stringSignTemp=stringA.toString()+"&key="+WeixinpayConfig.apiKey;
		           
		           System.out.println("---------stringSignTemp------>"+stringSignTemp);
		 
		           sign= MD5(stringSignTemp);
		           
		           System.out.println("---------stringSignTemp-md5----->"+sign);
		return sign;
	}


    /**
     * 微信支付签名算法sign
     * @param characterEncoding
     * @param parameters
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String createSign(String characterEncoding,SortedMap<String,Object> parameters){
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v) 
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + WeixinpayConfig.apiKey);
        String sign= MD5(sb.toString());
        sign = sign.toUpperCase();
        
        return sign;
    }
	
	
	/**
	 * 生成签名-jsapi
	 * @param weiXinPay
	 * @return
	 */
	public static String getJsSignString(WeiXinPay weiXinPay){
		
		String stringSignTemp="";
		
		String sign="";
		
		
		StringBuffer stringA=new StringBuffer();
	             	//公众账号ID
	             	stringA.append("appId="+weiXinPay.getAppId()+"&");
		             
                 if(weiXinPay.getNonceStr()!=null&&!"".equals(weiXinPay.getNonceStr()))
                	 stringA.append("nonceStr="+weiXinPay.getNonceStr()+"&");
                 
                 if(weiXinPay.getPackages()!=null&&!"".equals(weiXinPay.getPackages()))
                	 stringA.append("package="+weiXinPay.getPackages()+"&");
                 
                 if(weiXinPay.getSignType()!=null&&!"".equals(weiXinPay.getSignType()))
                	 stringA.append("signType="+weiXinPay.getSignType()+"&");
                 
                 if(weiXinPay.getTimeStamp()!=null&&!"".equals(weiXinPay.getTimeStamp()))
                	 stringA.append("timeStamp="+weiXinPay.getTimeStamp());
		           
		           stringSignTemp=stringA.toString()+"&key="+weiXinPay.getApiKey();
		           
		           System.out.println("---------stringSignTemp------>"+stringSignTemp);
		 
		           sign= MD5(stringSignTemp);
		           
		           System.out.println("---------stringSignTemp-md5----->"+sign);
		           sign= sign.toUpperCase();
		           System.out.println("---------stringSignTemp-md5-toUpperCase----->"+sign);
		return sign;
	}
	/**
	 * 生成签名-app
	 * @param weiXinPay
	 * @return
	 */
	public static String getAppSignString(WeiXinPay weiXinPay){
		
		String stringSignTemp="";
		
		String sign="";
		
		
		StringBuffer stringA=new StringBuffer();
		             //公众账号ID
		             stringA.append("appid="+weiXinPay.appId+"&");
		             
		             stringA.append("noncestr="+weiXinPay.getNonceStr()+"&");
		             
		             stringA.append("package="+weiXinPay.getPackages()+"&");
		             
                	 stringA.append("partnerid="+weiXinPay.getMchId()+"&");
                 
                	 stringA.append("prepayid="+weiXinPay.getPrepayid()+"&");
                	 
                	 stringA.append("timestamp="+weiXinPay.getTimeStamp());
		           
		           stringSignTemp=stringA.toString()+"&key="+weiXinPay.getApiKey();
		           
		           System.out.println("---------stringSignTemp------>"+stringSignTemp);
		 
		           sign= MD5(stringSignTemp);
		           
		           System.out.println("---------stringSignTemp-md5----->"+sign);
		           sign= sign.toUpperCase();
		           System.out.println("---------stringSignTemp-md5-toUpperCase----->"+sign);
		return sign;
	}
	
	/**
	 * MD5加密算法
	 * @param str
	 * @return
	 */
	public static String MD5(String str) {
	        return MD5.getStringToMD5(str).toUpperCase();
	    }
	public static String getSha1(String str) {
        if (null == str || 0 == str.length()) {
            return null;
        }
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));
            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] buf = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            return null;
        }
    }
	public static void main(String[] args) {
		String ss = "appId=wx83f0ec421ed54703&nonceStr=f72cd7e0340f4b0e8c1d96ea6dd02dfe&package=prepay_id=wx0610232486966301b2f9f0dd1078404800&signType=MD5&timeStamp=1565058304&key=Mevos2017PaySecretMevosPaySecret";
	   String ss1 = "appid=wx83f0ec421ed54703&nonceStr=a8cabf874c8f4ff7b9d0ddc810b3fb75&package=prepay_id=wx05183209602855df2107206c1810887400&signType=MD5&timeStamp=1565001088&key=Mevos2017PaySecretMevosPaySecret";
		System.out.println(MD5.getStringToMD5(ss).toUpperCase());
		System.out.println(MD5.getStringToMD5(ss1).toUpperCase());
	}
}
