C# 调用LAKALA接口

 /// <summary>
        /// 获取PostData数据
        /// </summary>
        /// <returns></returns>
        public PostLakalaData GetPostLakalaData()
        {
            string secretKey = "f6cc7030b8c0ba07a6da488362f2748e";
            PostLakalaData postData = new PostLakalaData();
            postData.ver = "1.0.0";
            postData.appid = "qm1624980136"; //拉卡拉授权提供
            postData.productType = "LKL_APP_QR";//拉卡拉授权提供
            postData.token = "43e99b4a38a748d3932fca9382404b41";//拉卡拉授权提供
            postData.cmd = "QR_ORDER_BIND_CODE_CREATE";//指令
            postData.channel = "LAKALA";//渠道
            postData.rnd = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 32);//随机数
            postData.timestamp = DateTimeUtil.Timestamp(DateTime.Now);//时间戳
            postData.reqId = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 32);//请求序列(具体传值待确认)
            reqDataData reqDataDataref = new reqDataData();
            reqDataDataref.termId = txttermId.Text;// 拉卡拉授权提供;
            reqDataDataref.shopNo = txtshopNo.Text;//拉卡拉授权提供;
            postData.reqData= reqDataDataref;
            postData.sign = GetSign(reqDataDataref, secretKey);//对reqData,进行签名,注意不能有空格
            orderExtInfoData oeinfodata = new orderExtInfoData();
            postData.orderExtInfo=oeinfodata;
            termExtInfoData termExtInfodata = new termExtInfoData();
            postData.termExtInfo=termExtInfodata;

            return postData;
        }
        /// <summary>
        /// 转换Sign数据
        /// </summary>
        /// <param name="reqDataDataref"></param>
        /// <param name="secretKey"></param>
        /// <returns></returns>
        private string GetSign(reqDataData reqDataDataref,string secretKey)
        {
            if (reqDataDataref == null)
                return "";
            string sign = "shopNo="+ reqDataDataref.shopNo+ "" +  "|termId=" + reqDataDataref.termId+ "|"+ secretKey;
            return GetMD5(sign).ToUpper();
        }
        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string GetMD5(string s)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] t = md5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(s));
            StringBuilder sb = new StringBuilder(32);
            for (int i = 0; i < t.Length; i++)
            {
                sb.Append(t[i].ToString("x").PadLeft(2, '0'));
            }
            return sb.ToString();
        }
  public static string PostResponse(string url, string postData, out string statusCode)
        {
            string result = string.Empty;
            //设置Http的正文
            HttpContent httpContent = new StringContent(postData);
            //设置Http的内容标头
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            //设置Http的内容标头的字符
            httpContent.Headers.ContentType.CharSet = "utf-8";
            using (HttpClient httpClient = new HttpClient())
            {
                //异步Post
                HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
                //输出Http响应状态码
                statusCode = response.StatusCode.ToString();
                //确保Http响应成功
                if (response.IsSuccessStatusCode)
                {
                    //异步读取json
                    result = response.Content.ReadAsStringAsync().Result;
                }
            }
            return result;
        }
 /// <summary>
        /// 获取当前的时间戳
        /// </summary>
        /// <returns></returns>
        public static string Timestamp(DateTime dt )
        {
            long ts = ConvertDateTimeToInt(dt);
            return ts.ToString();
        }

        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public static long ConvertDateTimeToInt(System.DateTime time)
        {
            //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            //long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            long t = (time.Ticks - 621356256000000000) / 10000;
            return t;
        } 

猜你喜欢

转载自www.cnblogs.com/sbjl/p/11326160.html