Exemplo n.º 1
0
 public RefundQueryRequest(WeChatPayConfig config) : base(config)
 {
     logger = WeChatLogger.GetLogger();
     if (config == null)
     {
         throw new WeChatException("Wechatconfig cannot be empty");
     }
     url = config.RefundQueryUrl;
 }
Exemplo n.º 2
0
 public RefundApplyRequest(WeChatPayConfig config) : base(config)
 {
     logger = WeChatLogger.GetLogger();
     if (config == null)
     {
         throw new WeChatException("Wechatconfig cannot be empty");
     }
     op_user_id      = config.ShopID;
     url             = config.RefundUrl;
     refund_fee_type = "CNY";
     needCert        = true;
 }
Exemplo n.º 3
0
 public BaseRequest(WeChatPayConfig config)
 {
     logger           = WeChatLogger.GetLogger();
     this.appid       = config.APPID;
     this.secret      = config.Secret;
     this.shop_secret = config.ShopSecret;
     this.mch_id      = config.ShopID;
     this.signType    = config.SignType.ToLower();
     this.nonce_str   = Guid.NewGuid().ToString().Replace("-", "");
     if (nonce_str.Length > 32)
     {
         nonce_str = nonce_str.Substring(0, 32);
     }
     ConfigVerification();
 }
Exemplo n.º 4
0
        public static string PostHttpRequest(string url, NameValueCollection col, RequestType type, string contentType)
        {
            WeChatLogger.GetLogger().Info("PostHttpRequest.........");
            string       output = null;
            StreamReader rs     = null;

            try
            {
                string json_str = string.Empty;

                var client = new HttpClient();
                if (!string.IsNullOrEmpty(contentType))
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
                }

                if (type == RequestType.POST)
                {
                    HttpContent content  = null;
                    var         postData = new List <KeyValuePair <string, string> >();
                    if (col != null && col.Count > 0)
                    {
                        IEnumerator myEnumerator = col.GetEnumerator();
                        if (contentType != null && contentType.ToLower() == "text/xml")
                        {
                            WeChatLogger.GetLogger().Info("post xml data...");
                            StringBuilder cBuilder = new StringBuilder();
                            cBuilder.Append("<xml>");
                            foreach (String s in col.AllKeys)
                            {
                                cBuilder.Append("<" + s + ">");
                                cBuilder.Append(col[s]);
                                cBuilder.Append("</" + s + ">");
                            }
                            cBuilder.Append("</xml>");
                            WeChatLogger.GetLogger().Info(cBuilder.ToString());
                            content = new StringContent(cBuilder.ToString());
                        }
                        else
                        {
                            foreach (String s in col.AllKeys)
                            {
                                if (s != null && col[s] != null)
                                {
                                    postData.Add(new KeyValuePair <string, string>(s, col[s]));
                                }
                            }

                            content = new FormUrlEncodedContent(postData);
                        }
                    }

                    var response = client.PostAsync(url, content).Result;

                    if (!response.IsSuccessStatusCode)
                    {
                        return(null);
                    }

                    Stream res = response.Content.ReadAsStreamAsync().Result;
                    //res = response.Content.ReadAsStreamAsync().Result;
                    if (contentType != "multipart/form-data")
                    {
                        rs     = new StreamReader(res);
                        output = rs.ReadToEnd();
                    }
                }
                else if (type == RequestType.GET)
                {
                    StringBuilder urlParms = new StringBuilder();
                    if (col != null)
                    {
                        IEnumerator myEnumerator = col.GetEnumerator();
                        int         count        = 1;
                        foreach (String s in col.AllKeys)
                        {
                            urlParms.Append(s);
                            urlParms.Append("=");
                            urlParms.Append(col[s]);
                            if (count < (col.Count))
                            {
                                urlParms.Append("&");
                            }

                            count++;
                        }
                    }
                    string getUrl = url;
                    if (!string.IsNullOrEmpty(urlParms.ToString()))
                    {
                        getUrl += "?" + urlParms.ToString();
                    }
                    var response = client.GetAsync(getUrl).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        Stream res = response.Content.ReadAsStreamAsync().Result;
                        rs     = new StreamReader(res);
                        output = rs.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                WeChatLogger.GetLogger().Error(ex);
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                if (rs != null)
                {
                    rs.Close();
                }
            }
            WeChatLogger.GetLogger().Info("Post request done.");
            return(output);
        }
Exemplo n.º 5
0
        public static string PostHttpRequest(string url, NameValueCollection col, RequestType type, string contentType, bool needCret = false, WeChatPayConfig config = null)
        {
            WeChatLogger.GetLogger().Info("PostHttpRequest.........");
            string       output = null;
            StreamReader rs     = null;

            try
            {
                if (needCret && config == null)
                {
                    throw new Exception("Please provides wechatpayconfig instance when need cert");
                }
                string json_str = string.Empty;
                var    client   = new HttpClient();
                if (needCret)
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    WebRequestHandler handler = new WebRequestHandler();
                    System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                    X509Certificate cer = new X509Certificate(config.CertFilePath, config.ShopID);
                    WeChatLogger.GetLogger().Info("cert path:" + config.CertFilePath);
                    handler.ClientCertificates.Add(cer);
                    client = new HttpClient(handler);
                }

                if (!string.IsNullOrEmpty(contentType))
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
                }
                WeChatLogger.GetLogger().Info("url:" + url);
                if (type == RequestType.POST)
                {
                    HttpContent content  = null;
                    var         postData = new List <KeyValuePair <string, string> >();
                    if (col != null && col.Count > 0)
                    {
                        IEnumerator myEnumerator = col.GetEnumerator();
                        if (contentType != null && contentType.ToLower() == "text/xml")
                        {
                            WeChatLogger.GetLogger().Info("post xml data...");
                            StringBuilder cBuilder = new StringBuilder();
                            cBuilder.Append("<xml>");
                            foreach (String s in col.AllKeys)
                            {
                                cBuilder.Append("<" + s + ">");
                                cBuilder.Append(col[s]);
                                cBuilder.Append("</" + s + ">");
                            }
                            cBuilder.Append("</xml>");
                            WeChatLogger.GetLogger().Info(cBuilder.ToString());
                            content = new StringContent(cBuilder.ToString());
                        }
                        else
                        {
                            foreach (String s in col.AllKeys)
                            {
                                if (s != null && col[s] != null)
                                {
                                    postData.Add(new KeyValuePair <string, string>(s, col[s]));
                                }
                            }

                            content = new FormUrlEncodedContent(postData);
                        }
                    }

                    var response = client.PostAsync(url, content).Result;

                    if (!response.IsSuccessStatusCode)
                    {
                        return(null);
                    }

                    Stream res = response.Content.ReadAsStreamAsync().Result;
                    //res = response.Content.ReadAsStreamAsync().Result;
                    if (contentType != "multipart/form-data")
                    {
                        rs     = new StreamReader(res);
                        output = rs.ReadToEnd();
                    }
                }
                else if (type == RequestType.GET)
                {
                    StringBuilder urlParms = new StringBuilder();
                    if (col != null)
                    {
                        IEnumerator myEnumerator = col.GetEnumerator();
                        int         count        = 1;
                        foreach (String s in col.AllKeys)
                        {
                            urlParms.Append(s);
                            urlParms.Append("=");
                            urlParms.Append(col[s]);
                            if (count < (col.Count))
                            {
                                urlParms.Append("&");
                            }

                            count++;
                        }
                    }
                    string getUrl = url;
                    if (!string.IsNullOrEmpty(urlParms.ToString()))
                    {
                        getUrl += "?" + urlParms.ToString();
                    }
                    var response = client.GetAsync(getUrl).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        Stream res = response.Content.ReadAsStreamAsync().Result;
                        rs     = new StreamReader(res);
                        output = rs.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                WeChatLogger.GetLogger().Error(ex);
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                if (rs != null)
                {
                    rs.Close();
                }
            }
            WeChatLogger.GetLogger().Info("Post request done.");
            return(output);
        }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data">Data could be xml string, or json xml string, or could be key value pairs string</param>
        /// <param name="contentType"></param>
        /// <param name="type"></param>
        /// <param name="needCret"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static string PostHttpRequest(string url, string data, string contentType, RequestType type, bool needCret = false, WeChatPayConfig config = null)
        {
            WeChatLogger.GetLogger().Info("PostHttpRequest.........");
            string       output = null;
            StreamReader rs     = null;

            try
            {
                if (needCret && config == null)
                {
                    throw new Exception("Please provides wechatpayconfig instance when need cert");
                }
                string json_str = string.Empty;
                var    client   = new HttpClient();
                if (needCret)
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    WebRequestHandler handler = new WebRequestHandler();
                    System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                    X509Certificate cer = new X509Certificate(config.CertFilePath, config.ShopID);
                    WeChatLogger.GetLogger().Info("cert path:" + config.CertFilePath);
                    handler.ClientCertificates.Add(cer);
                    client = new HttpClient(handler);
                }
                if (!string.IsNullOrEmpty(contentType))
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
                }

                WeChatLogger.GetLogger().Info("url:" + url);
                if (type == RequestType.POST)
                {
                    HttpContent content = null;
                    content = new StringContent(data);
                    if (!string.IsNullOrEmpty(data))
                    {
                        WeChatLogger.GetLogger().Info("data:" + data);
                    }
                    var response = client.PostAsync(url, content).Result;
                    if (!response.IsSuccessStatusCode)
                    {
                        return(null);
                    }
                    Stream res = response.Content.ReadAsStreamAsync().Result;
                    //res = response.Content.ReadAsStreamAsync().Result;
                    if (contentType != "multipart/form-data")
                    {
                        rs     = new StreamReader(res);
                        output = rs.ReadToEnd();
                    }
                }
                else if (type == RequestType.GET)
                {
                    string getUrl = url;
                    if (!string.IsNullOrEmpty(data))
                    {
                        getUrl += "?" + data;
                    }
                    var response = client.GetAsync(getUrl).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        Stream res = response.Content.ReadAsStreamAsync().Result;
                        rs     = new StreamReader(res);
                        output = rs.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                WeChatLogger.GetLogger().Error(ex);
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                if (rs != null)
                {
                    rs.Close();
                }
            }
            WeChatLogger.GetLogger().Info("Post request done.");
            return(output);
        }