微信支付快速生成签名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<string, string> sPara = new Dictionary<string, string>();
            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<string, string> 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码从小到大排序(字典序)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值