Ejemplo n.º 1
0
 private static StripeException BuildInvalidResponseException(StripeResponse response)
 {
     return(new StripeException(
                response.StatusCode,
                null,
                $"Invalid response object from API: \"{response.Content}\"")
     {
         StripeResponse = response,
     });
 }
Ejemplo n.º 2
0
        // the ResponseJson on a list method is the entire list (as json) returned from stripe.
        // the ObjectJson is so we can store only the json for a single object in the list on that entity for
        // logging and/or debugging
        public static T MapFromJson(string json, string parentToken = null, StripeResponse stripeResponse = null)
        {
            var jsonToParse = string.IsNullOrEmpty(parentToken) ? json : JObject.Parse(json).SelectToken(parentToken).ToString();

            var result = JsonConvert.DeserializeObject <T>(jsonToParse);

            applyStripeResponse(json, stripeResponse, result);

            return(result);
        }
Ejemplo n.º 3
0
        // the ResponseJson on a list method is the entire list (as json) returned from stripe.
        // the ObjectJson is so we can store only the json for a single object in the list on that entity for
        // logging and/or debugging
        public static T MapFromJson(string json, string parentToken = null, StripeResponse stripeResponse = null)
        {
            var jsonToParse = string.IsNullOrEmpty(parentToken) ? json : JObject.Parse(json).SelectToken(parentToken).ToString();

            var result = JsonConvert.DeserializeObject <T>(jsonToParse, StripeConfiguration.SerializerSettings);

            // if necessary, we might need to apply the stripe response to nested properties for StripeList<T>
            ApplyStripeResponse(json, stripeResponse, result);

            return(result);
        }
Ejemplo n.º 4
0
        private static void ApplyStripeResponse(string json, StripeResponse stripeResponse, object obj)
        {
            if (stripeResponse == null)
            {
                return;
            }

            foreach (var property in obj.GetType().GetRuntimeProperties())
            {
                if (property.Name == nameof(StripeResponse))
                {
                    property.SetValue(obj, stripeResponse);
                }
            }

            stripeResponse.ObjectJson = json;
        }
Ejemplo n.º 5
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,
            });
        }
Ejemplo n.º 6
0
        private static T ProcessResponse <T>(StripeResponse response)
            where T : IStripeEntity
        {
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw BuildStripeException(response);
            }

            T obj;

            try
            {
                obj = StripeEntity.FromJson <T>(response.Content);
            }
            catch (Newtonsoft.Json.JsonException)
            {
                throw BuildInvalidResponseException(response);
            }

            obj.StripeResponse = response;

            return(obj);
        }
Ejemplo n.º 7
0
 public static T MapFromJson(StripeResponse stripeResponse, string parentToken = null)
 {
     return(MapFromJson(stripeResponse.ResponseJson, parentToken, stripeResponse));
 }
Ejemplo n.º 8
0
 public static List <T> MapCollectionFromJson(StripeResponse stripeResponse, string token = "data")
 {
     return(MapCollectionFromJson(stripeResponse.ResponseJson, token, stripeResponse));
 }
Ejemplo n.º 9
0
        public static List <T> MapCollectionFromJson(string json, string token = "data", StripeResponse stripeResponse = null)
        {
            var jObject = JObject.Parse(json);

            var allTokens = jObject.SelectToken(token);

            return(allTokens.Select(tkn => MapFromJson(tkn.ToString(), null, stripeResponse)).ToList());
        }