• net版本 微信现金红包接口调用的时候,老是出现签名错误


    创建签名  所使用到的几个方法

        

                 string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
                    //随机字符串
                    string nonce_str = CreateRandomCode(32);
                    //商户号
                    string mch_id = "111111111111";
                    //商户订单号
                    string mch_billno = mch_id + DateTime.Now.ToString("yyyyMMdd") + Rand();//自己生成把
                    //公众账号appid
                    string wxappid = "222222222222222222222";
                    //提供方名称
                    string nick_name = "提供方名称";
                    //商户名称
                    string send_name = "商户名称";
                    //用户openid
                    string re_openid = s.open_id;
                    //付款金额         这里要注意 提现是 1 - 200元 ,但是这里的单位是  “分”
                    string total_amount = (money * 100).ToString();
                    //最小红包金额
                    string min_value = (money * 100).ToString();
                    //最大红包金额
                    string max_value = (money * 100).ToString();
                    //红包发放总人数
                    string total_num = "1";
                    //红包祝福语
                    string wishing = "活动名称";
                    string client_ip = "192.168.10.0";
                    //活动名称
                    string act_name = "活动名称";
                    //备注
                    string remark = "提现问题,请联系客服人员";
    
     
    
      Dictionary<string, string> parameters = new Dictionary<string, string>();
                    parameters.Add("act_name", act_name);
                    parameters.Add("client_ip", client_ip);
                    parameters.Add("min_value", min_value);
                    parameters.Add("max_value", max_value);
                    parameters.Add("mch_billno", mch_billno);
                    parameters.Add("mch_id", mch_id);
                    parameters.Add("nonce_str", nonce_str);
                    parameters.Add("nick_name", nick_name);
                    parameters.Add("re_openid", re_openid);
                    parameters.Add("remark", remark);
                    parameters.Add("send_name", send_name);
                    parameters.Add("total_num", total_num);
                    parameters.Add("total_amount", total_amount);
                    parameters.Add("wishing", wishing);
                    parameters.Add("wxappid", wxappid);
                    //生成签名
                    string sign = CreateSignString(parameters, "自己的 API密钥 ");

    提供一个 加密的方法 CreateSignString

     static string CreateSignString(Dictionary<string, string> parameters, string paySecret)
            {
                foreach (var key in parameters.Keys.Where(key => string.IsNullOrEmpty(parameters[key])))
                {
                    parameters.Remove(key);
                }
    
                //第一步,设所有収送戒者接收到的数据为集合 M,将集合 M 内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),
                //使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串 stringA。
                //特别注意以下重要规则:
                //1.参数名 ASCII 码从小到大排序(字典序) ;
                //2.如果参数的值为空不参与签名;
                //3.参数名区分大小写;
                //4.验证调用返回戒微信主劢通知签名时,传送的 sign 参数不参与签名,将生成的签名与该 sign 值作校验。
                var keyArray = parameters.Keys.ToArray();
                Array.Sort(keyArray);
                var keyString = keyArray.Aggregate("", (current, t) => current + (t + "=" + parameters[t] + "&"));
    
                //第二步,在 stringA 最后拼接上 key=商户支付密钥得到 stringSignTemp 字符串,
                //并对 stringSignTemp 进行 MD5 运算,再将得到的字符串所有字符转换为大写,得到 sign的值 signValue
                keyString += "key=" + paySecret;
    
                return toMD5(keyString).ToUpper();
            }
            public static string toMD5(string str)
            {
                byte[] md5Bytes = Encoding.UTF8.GetBytes(str);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] cryptString = md5.ComputeHash(md5Bytes);
    
                int len;
                string temp = String.Empty;
    
                len = cryptString.Length;
    
                for (int i = 0; i < len; i++)
                {
                    temp += cryptString[i].ToString("X2");
                }
                return temp;
            }


    随机数 

     public string CreateRandomCode(int codeCount, int flag = 0)
            {
                string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
                if (flag == 1)
                {
                    allChar = "0,1,2,3,4,5,6,7,8,9";
                }
    
                //里面的字符你可以自己加啦      
                string[] allCharArray = allChar.Split(',');
                string randomCode = "";
                int temp = -1;
                Random random = new Random();
                for (int i = 0; i < codeCount; i++)
                {
                    if (temp != -1)
                    {
                        random = new Random(i * temp * (int)DateTime.Now.Ticks);
                    }
                    int t = random.Next(35);
                    if (temp == t)
                    {
                        return CreateRandomCode(codeCount);
                    }
                    temp = t;
                    randomCode += allCharArray[temp];
                }
                return randomCode;
            }
            public string Rand()
            {
                string all = "0,1,2,3,4,5,6,7,8,9";
                string[] allChar = all.Split(',');
                string result = "";
                Random rand = new Random();
                for (int i = 0; i < 10; i++)
                { result += allChar[rand.Next(9)]; }
                return result;
            }

    请求api 带证书的方法

      public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding)
            {
                try
                {
                    Encoding encoding = Encoding.UTF8;
    
                    string cert = @"D:\apiclient_cert.p12";
                    string password = "  这里是填写的是 微信支付商户号   ";
    
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    X509Certificate cer = new X509Certificate(cert, password);
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                    request.ClientCertificates.Add(cer);
    
    
                    request.Method = "post";
    
                    request.Accept = "text/html, application/xhtml+xml, */*";
    
                    request.ContentType = "application/x-www-form-urlencoded";
    
    
                    byte[] buffer = encoding.GetBytes(strPostdata);
    
                    request.ContentLength = buffer.Length;
    
                    request.GetRequestStream().Write(buffer, 0, buffer.Length);
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
                    {
                        return reader.ReadToEnd();
                    }
                }
                catch (Exception e)
                {
                     
                    return "";
                }
    
            }
    
  • 相关阅读:
    鸣人和佐助(OJ 6044)
    Papa的坦克(原BZOJ 1603 打谷机)
    linux基础学习之用户和用户组
    出现log4j:WARN No appenders could be found for logger 解决方法
    myeclipse当中,如何导入或者导出项目
    linux 基础学习之目录与文件处理命令
    linux 基础学习之权限管理命令
    hadoop环境搭建之伪分布集群环境搭建(单节点)
    linux基础学习之各目录的作用
    linux 基础学习之 磁盘,分区,MBR与GPT
  • 原文地址:https://www.cnblogs.com/Ofsoul/p/4492375.html
Copyright © 2020-2023  润新知