Inheritance: StripeEntity
        /// <summary>
        /// For Calling The API Directly
        /// </summary>
        /// <param name="webRequest"></param>
        /// <returns></returns>
        private static string ExecuteWebRequest(WebRequest webRequest)
        {
            try
            {
                using (var response = webRequest.GetResponse())
                {
                    return ReadStream(response.GetResponseStream().ToString());
                }
            }
            catch (WebException webException)
            {
                if (webException.Response != null)
                {
                    var statusCode = ((HttpWebResponse)webException.Response).StatusCode;

                    var stripeError = new StripeError();

                    if (webRequest.RequestUri.ToString().Contains("oauth"))
                        stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream().ToString()));
                    else
                        stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream().ToString()), "error");

                    throw new StripeException(statusCode, stripeError, stripeError.Message);
                }

                throw;
            }
        }
Beispiel #2
0
        private static string ExecuteWebRequest(WebRequest webRequest)
        {
            try
            {
                using (var response = webRequest.GetResponse())
                {
                    return(ReadStream(response.GetResponseStream()));
                }
            }
            catch (WebException webException)
            {
                if (webException.Response != null)
                {
                    var statusCode = ((HttpWebResponse)webException.Response).StatusCode;

                    var stripeError = new StripeError();

                    if (webRequest.RequestUri.ToString().Contains("oauth"))
                    {
                        stripeError = Mapper <StripeError> .MapFromJson(ReadStream(webException.Response.GetResponseStream()));
                    }
                    else
                    {
                        stripeError = Mapper <StripeError> .MapFromJson(ReadStream(webException.Response.GetResponseStream()), "error");
                    }

                    throw new StripeException(statusCode, stripeError, stripeError.Message);
                }

                throw;
            }
        }
Beispiel #3
0
        protected StripeException BuildStripeException(HttpStatusCode statusCode, string requestUri, string responseContent)
        {
            var stripeError = new StripeError();

            if (requestUri.Contains("oauth"))
                stripeError = Mapper<StripeError>.MapFromJson(responseContent);
            else
                stripeError = Mapper<StripeError>.MapFromJson(responseContent);

            return new StripeException(statusCode, stripeError, stripeError.Message);
        }
Beispiel #4
0
        internal static StripeException BuildStripeException(HttpStatusCode statusCode, string requestUri, string responseContent)
        {
            var stripeError = new StripeError();

            if (requestUri.Contains("oauth"))
            {
                stripeError = Mapper <StripeError> .MapFromJson(responseContent);
            }
            else
            {
                stripeError = Mapper <StripeError> .MapFromJson(responseContent, "error");
            }

            return(new StripeException(statusCode, stripeError, stripeError.Message));
        }
Beispiel #5
0
        private static string ExecuteWebRequest(WebRequest webRequest)
        {
            var verificationCallback = new RemoteCertificateValidationCallback(StripeCertificateVerificationCallback);

            try
            {
                ServicePointManager.ServerCertificateValidationCallback += verificationCallback;

                using (var response = webRequest.GetResponse())
                {
                    return(ReadStream(response.GetResponseStream()));
                }
            }
            catch (WebException webException)
            {
                if (webException.Response != null)
                {
                    var statusCode = ((HttpWebResponse)webException.Response).StatusCode;

                    var stripeError = new StripeError();

                    if (webRequest.RequestUri.ToString().Contains("oauth"))
                    {
                        stripeError = Mapper <StripeError> .MapFromJson(ReadStream(webException.Response.GetResponseStream()));
                    }
                    else
                    {
                        stripeError = Mapper <StripeError> .MapFromJson(ReadStream(webException.Response.GetResponseStream()), "error");
                    }

                    throw new StripeException(statusCode, stripeError, stripeError.Message);
                }

                throw;
            }
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback -= verificationCallback;
            }
        }
Beispiel #6
0
        private static StripeException BuildStripeException(StripeResponse response)
        {
            JObject jObject = null;

            try
            {
                jObject = JObject.Parse(response.Content);
            }
            catch (Newtonsoft.Json.JsonException)
            {
                return(BuildInvalidResponseException(response));
            }

            // If the value of the `error` key is a string, then the error is an OAuth error
            // and we instantiate the StripeError object with the entire JSON.
            // Otherwise, it's a regular API error and we instantiate the StripeError object
            // with just the nested hash contained in the `error` key.
            var errorToken = jObject["error"];

            if (errorToken == null)
            {
                return(BuildInvalidResponseException(response));
            }

            var stripeError = errorToken.Type == JTokenType.String
                ? StripeError.FromJson(response.Content)
                : StripeError.FromJson(errorToken.ToString());

            stripeError.StripeResponse = response;

            return(new StripeException(
                       response.StatusCode,
                       stripeError,
                       stripeError.Message ?? stripeError.ErrorDescription)
            {
                StripeResponse = response,
            });
        }
 public StripeException(HttpStatusCode httpStatusCode, StripeError stripeError, string message) : base(message)
 {
     HttpStatusCode = httpStatusCode;
     StripeError = stripeError;
 }
Beispiel #8
0
		private static string ExecuteWebRequest(WebRequest webRequest)
		{
			var verificationCallback = new RemoteCertificateValidationCallback(StripeCertificateVerificationCallback);

			try
			{
				ServicePointManager.ServerCertificateValidationCallback += verificationCallback;

				using (var response = webRequest.GetResponse())
				{
					return ReadStream(response.GetResponseStream());
				}
			}
			catch (WebException webException)
			{
				if (webException.Response != null)
				{
					var statusCode = ((HttpWebResponse)webException.Response).StatusCode;
					
					var stripeError = new StripeError();

					if(webRequest.RequestUri.ToString().Contains("oauth"))
						stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()));
					else
						stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()), "error");

					throw new StripeException(statusCode, stripeError, stripeError.Message);
				}

				throw;
			}
			finally
			{
				ServicePointManager.ServerCertificateValidationCallback -= verificationCallback;
			}
		}
 internal StripeException(HttpStatusCode httpStatusCode, StripeError stripeError, string message) : base(message)
 {
     HttpStatusCode = httpStatusCode;
     StripeError    = stripeError;
 }
Beispiel #10
0
        internal static string ExecuteMultipartFormPostRequest(
          HttpWebRequest webRequest,
          Dictionary<string, string> postData,
          Stream fileToUpload,
          string fileNameToUpload,
          string fileMimeType,
          string fileFormKey)
        {
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.KeepAlive = true;

            //Creates a multipart/form-data boundary.
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            Stream requestStream = webRequest.GetRequestStream();
            postData.WriteMultipartFormData(requestStream, boundary);
            if (fileToUpload != null)
            {
                fileToUpload.WriteMultipartFormData(fileNameToUpload, requestStream, boundary, fileMimeType, fileFormKey);
            }
            byte[] endBytes = Encoding.UTF8.GetBytes("--" + boundary + "--");
            requestStream.Write(endBytes, 0, endBytes.Length);
            requestStream.Close();

            try
            {
                using (WebResponse response = webRequest.GetResponse())
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                };
            }
            catch (WebException webException)
            {
                if (webException.Response != null)
                {
                    var statusCode = ((HttpWebResponse)webException.Response).StatusCode;

                    var stripeError = new StripeError();

                    if (webRequest.RequestUri.ToString().Contains("oauth"))
                        stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()));
                    else
                        stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()), "error");

                    throw new StripeException(statusCode, stripeError, stripeError.Message);
                }

                throw;
            }
        }
 public StripeException(HttpStatusCode httpStatusCode, StripeError stripeError, string message)
     : base(message)
 {
     this.HttpStatusCode = httpStatusCode;
     this.StripeError    = stripeError;
 }
Beispiel #12
0
        private static string ExecuteWebRequest(WebRequest webRequest)
        {
            string result = null;

            // Attempt communication up to 3 times, in case of temporary Stripe outtage or other reason
            // Idempotency string on charges will prevent multiple transactions
            const int maxAttempts = 3;

            for (var attempt = 1; attempt <= maxAttempts; attempt++)
            {
                try
                {
                    using (var response = webRequest.GetResponse())
                    {
                        result = ReadStream(response.GetResponseStream());
                    }
                }
                catch (WebException webException)
                {
                    if (webException.Response != null)
                    {
                        var statusCode = ((HttpWebResponse)webException.Response).StatusCode;

                        var stripeError = new StripeError();

                        if (webRequest.RequestUri.ToString().Contains("oauth"))
                            stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()));
                        else
                            stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()), "error");

                        if (attempt == maxAttempts)
                            throw new StripeException(statusCode, stripeError, stripeError.Message);
                    }

                    if (attempt == maxAttempts)
                        // NOTE: Running on localhost sometimes takes us here.
                        //       Doesn't seem to happen in production environment.
                        throw;
                }

                if (!string.IsNullOrEmpty(result))
                    break;
            }

            return result;
        }
Beispiel #13
0
        internal static string ExecuteMultipartFormPostRequest(
          HttpWebRequest webRequest,
          Dictionary<string, string> postData,
          Stream fileToUpload,
          string fileNameToUpload,
          string fileMimeType,
          string fileFormKey, StripeRequestOptions requestOptions = null)
        {
            string result = null;

            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.KeepAlive = true;

            //Creates a multipart/form-data boundary.
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            Stream requestStream = webRequest.GetRequestStream();
            postData.WriteMultipartFormData(requestStream, boundary);
            if (fileToUpload != null)
            {
                fileToUpload.WriteMultipartFormData(fileNameToUpload, requestStream, boundary, fileMimeType, fileFormKey);
            }
            byte[] endBytes = Encoding.UTF8.GetBytes("--" + boundary + "--");
            requestStream.Write(endBytes, 0, endBytes.Length);
            requestStream.Close();

            // Attempt communication up to 3 times, in case of temporary Stripe outtage or other reason
            // Idempotency string on charges will prevent multiple transactions
            const int maxAttempts = 3;

            for (var attempt = 1; attempt <= maxAttempts; attempt++)
            {
                try
                {
                    using (WebResponse response = webRequest.GetResponse())
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                    };
                }
                catch (WebException webException)
                {
                    if (webException.Response != null)
                    {
                        var statusCode = ((HttpWebResponse)webException.Response).StatusCode;

                        var stripeError = new StripeError();

                        if (webRequest.RequestUri.ToString().Contains("oauth"))
                            stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()));
                        else
                            stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()), "error");

                        if (attempt == maxAttempts)
                            throw new StripeException(statusCode, stripeError, stripeError.Message);
                    }

                    if (attempt == maxAttempts)
                        throw;
                }

                if (!string.IsNullOrEmpty(result))
                    break;
            }

            return result;
        }