微信支付快速生成签名sign

以微信支付接口为例,生成签名sign的方法(c#):

 protected string CreateSign(string appid, string body, string device_info, string mch_id, string nonce_str, string notify_url, string out_trade_no, string spbill_create_ip, string total_fee, string trade_type, string attach, string openID)
        {
            Dictionary sPara = new Dictionary();
            sPara.Add("appid", appid);
            sPara.Add("attach", attach);
            sPara.Add("body", body);
            sPara.Add("device_info", device_info);
            sPara.Add("mch_id", mch_id);
            sPara.Add("nonce_str", nonce_str);
            sPara.Add("notify_url", notify_url);
            sPara.Add("out_trade_no", out_trade_no);
            sPara.Add("spbill_create_ip", spbill_create_ip);
            sPara.Add("total_fee", total_fee);
            sPara.Add("trade_type", trade_type);
            if (!string.IsNullOrEmpty(openID))
            {
                sPara.Add("openid", openID);
            }
            StringBuilder sb = new StringBuilder();
            sPara = sPara.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
            foreach (KeyValuePair kvp in sPara)
            {
                sb.Append(string.Format("{0}={1}&", kvp.Key, kvp.Value));
            }
            string stringA = sb.ToString().TrimEnd('&');
            string stringSignTemp = string.Format("{0}&key={1}", stringA, ConfigurationManager.AppSettings["WEIXIN_APIKey"].ToString());
            string sign = Utilities.CommonHelper.GetMD5(stringSignTemp.ToString(), "utf-8").ToUpper();
            return sign;
        }

只需要将参数传入方法,此方法会自动按照参数名ASCII码从小到大排序(字典序)

你可能感兴趣的:(c#,签名sign,微信支付)