Android微信支付获取二次签名Sign的方法

2022-01-10 14:41:45 浏览数 (2)

代码语言:javascript复制
 /** 
   * 获取sign签名 
   *  
   * @return 
   */ 
   private String genPayReq() { 
   // 获取参数的值 
   PayReq request =  new PayReq(); 
   request.appId = ConstantsMember.APP_ID; 
   request.partnerId = ConstantsMember.MCHID; 
   request.prepayId = mPrepayId; 
   request.packageValue =  "Sign=WXPay" ; 
   request.nonceStr = DataUtil.getGuid(); 
   request.timeStamp = DataUtil.getTimeStamp(); 
   // 把参数的值传进去SortedMap集合里面 
   SortedMap<Object, Object> parameters =  new TreeMap<Object, Object>(); 
 //  {appid=wx34df375d7dae8c90, noncestr=3BF34EF2CA4A462DB8D4EA48E785CDC3,  
 //    package=Sign=WXPay, partnerid=1349967601,  
 //    prepayid=wx2016070910354542c7155d4e0846850809, timestamp=1468031760} 
   parameters.put( "appid" , request.appId); 
   parameters.put( "noncestr" , request.nonceStr); 
   parameters.put( "package" , request.packageValue); 
   parameters.put( "partnerid" , request.partnerId); 
   parameters.put( "prepayid" , request.prepayId); 
   parameters.put( "timestamp" , request.timeStamp); 
   String characterEncoding =  "UTF-8" ; 
   String mySign = createSign(characterEncoding, parameters); 
   System.out.println( "我的签名是:"   mySign); 
   return mySign; 
   } 
   /** 
   * 微信支付签名算法sign 
   *  
   * @param characterEncoding 
   * @param parameters 
   * @return 
   */ 
   public static String createSign(String characterEncoding, 
   SortedMap<Object, Object> parameters) { 
   StringBuffer sb =  new StringBuffer(); 
   Set es = parameters.entrySet(); // 所有参与传参的参数按照accsii排序(升序) 
   Iterator it = es.iterator(); 
   while (it.hasNext()) { 
   @SuppressWarnings ( "rawtypes" ) 
   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="   ConstantsMember.KEY);  //KEY是商户秘钥 
   String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding) 
   .toUpperCase(); 
   return sign;  // D3A5D13E7838E1D453F4F2EA526C4766 
   // D3A5D13E7838E1D453F4F2EA526C4766 
   } 

0 人点赞