public static string ApplyAllParameters(this StripeService service, object obj, string url, bool isListMethod = false) { // store the original url from the service call into requestString (e.g. https://api.stripe.com/v1/accounts/account_id) // before the stripe attributes get applied. all of the attributes that will get passed to stripe will be applied to this string, // don't worry - if the request is a post, the Requestor will take care of moving the attributes to the post body var requestString = url; // obj = the options object passed from the service if (obj != null) { foreach (var property in obj.GetType().GetRuntimeProperties()) { var value = property.GetValue(obj, null); if (value == null) { continue; } foreach (var attribute in property.GetCustomAttributes <JsonPropertyAttribute>()) { if (attribute.PropertyName.ToLower().Contains("metadata")) { requestString = ApplyMetadataParameters(requestString, value); } else if (attribute.PropertyName.ToLower().Contains("fraud_details")) { var fraudDetails = (Dictionary <string, string>)value; foreach (string key in fraudDetails.Keys) { requestString = ApplyParameterToUrl(requestString, $"fraud_details[{key}]", fraudDetails[key]); } } else if (property.PropertyType == typeof(StripeDateFilter)) { var filter = (StripeDateFilter)value; if (filter.EqualTo.HasValue) { requestString = ApplyParameterToUrl(requestString, attribute.PropertyName, filter.EqualTo.Value.ConvertDateTimeToEpoch().ToString()); } if (filter.LessThan.HasValue) { requestString = ApplyParameterToUrl(requestString, attribute.PropertyName + "[lt]", filter.LessThan.Value.ConvertDateTimeToEpoch().ToString()); } if (filter.LessThanOrEqual.HasValue) { requestString = ApplyParameterToUrl(requestString, attribute.PropertyName + "[lte]", filter.LessThanOrEqual.Value.ConvertDateTimeToEpoch().ToString()); } if (filter.GreaterThan.HasValue) { requestString = ApplyParameterToUrl(requestString, attribute.PropertyName + "[gt]", filter.GreaterThan.Value.ConvertDateTimeToEpoch().ToString()); } if (filter.GreaterThanOrEqual.HasValue) { requestString = ApplyParameterToUrl(requestString, attribute.PropertyName + "[gte]", filter.GreaterThanOrEqual.Value.ConvertDateTimeToEpoch().ToString()); } } else if (value as INestedOptions != null) { requestString = ApplyNestedObjectProperties(requestString, value); } else { requestString = ApplyParameterToUrl(requestString, attribute.PropertyName, value.ToString()); } } } } if (service != null) { // expandable properties var propertiesToExpand = service.GetType() .GetRuntimeProperties() .Where(p => p.Name.StartsWith("Expand") && p.PropertyType == typeof(bool)) .Where(p => (bool)p.GetValue(service, null)) .Select(p => p.Name); foreach (var propertyName in propertiesToExpand) { string expandPropertyName = propertyName.Substring("Expand".Length); expandPropertyName = Regex.Replace(expandPropertyName, "([a-z])([A-Z])", "$1_$2").ToLower(); if (isListMethod) { expandPropertyName = "data." + expandPropertyName; } requestString = ApplyParameterToUrl(requestString, "expand[]", expandPropertyName); } } return(requestString); }
public static string ApplyAllParameters(this StripeService service, object obj, string url, bool isListMethod) { string newUrl = url; if (obj != null) { foreach (var property in obj.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { var value = property.GetValue(obj, null); if (value == null) { continue; } foreach (var attribute in property.GetCustomAttributes(typeof(JsonPropertyAttribute), false).Cast <JsonPropertyAttribute>()) { if (string.Compare(attribute.PropertyName, "metadata", true) == 0) { var metadata = (Dictionary <string, string>)value; foreach (string key in metadata.Keys) { newUrl = ApplyParameterToUrl(newUrl, string.Format("metadata[{0}]", key), metadata[key]); } } else if (property.PropertyType == typeof(StripeDateFilter)) { var filter = (StripeDateFilter)value; if (filter.EqualTo.HasValue) { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName, filter.EqualTo.Value.ConvertDateTimeToEpoch().ToString()); } else if (filter.LessThan.HasValue) { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName + "[lt]", filter.LessThan.Value.ConvertDateTimeToEpoch().ToString()); } if (filter.LessThanOrEqual.HasValue) { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName + "[lte]", filter.LessThanOrEqual.Value.ConvertDateTimeToEpoch().ToString()); } if (filter.GreaterThan.HasValue) { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName + "[gt]", filter.GreaterThan.Value.ConvertDateTimeToEpoch().ToString()); } if (filter.GreaterThanOrEqual.HasValue) { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName + "[gte]", filter.GreaterThanOrEqual.Value.ConvertDateTimeToEpoch().ToString()); } } else if (property.PropertyType == typeof(StripeBankAccountOptions)) { var bankAccountOptions = (StripeBankAccountOptions)value; newUrl = ApplyNestedObjectProperties(newUrl, bankAccountOptions); } else if (property.PropertyType == typeof(StripeCreditCardOptions)) { var creditCardOptions = (StripeCreditCardOptions)value; newUrl = ApplyNestedObjectProperties(newUrl, creditCardOptions); } else { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName, value.ToString()); } } } } if (service != null) { var propertiesToExpand = service.GetType() .GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.Name.StartsWith("Expand") && p.PropertyType == typeof(bool)) .Where(p => (bool)p.GetValue(service, null)) .Select(p => p.Name); foreach (var propertyName in propertiesToExpand) { string expandPropertyName = propertyName.Substring("Expand".Length); expandPropertyName = Regex.Replace(expandPropertyName, "([a-z])([A-Z])", "$1_$2").ToLower(); if (isListMethod) { expandPropertyName = "data." + expandPropertyName; } newUrl = ApplyParameterToUrl(newUrl, "expand[]", expandPropertyName); } } return(newUrl); }
public static string ApplyAllParameters(this StripeService service, object obj, string url, bool isListMethod = false) { string newUrl = url; if (obj != null) { foreach (var property in obj.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { var value = property.GetValue(obj, null); if (value == null) { continue; } foreach (var attribute in property.GetCustomAttributes(typeof(JsonPropertyAttribute), false).Cast <JsonPropertyAttribute>()) { // simplify this crap if (attribute.PropertyName.ToLower().Contains("metadata")) { newUrl = ApplyMetadataParameters(newUrl, value); } else if (attribute.PropertyName.ToLower().Contains("fraud_details")) { var fraudDetails = (Dictionary <string, string>)value; foreach (string key in fraudDetails.Keys) { newUrl = ApplyParameterToUrl(newUrl, $"fraud_details[{key}]", fraudDetails[key]); } } else if (property.PropertyType == typeof(StripeDateFilter)) { var filter = (StripeDateFilter)value; if (filter.EqualTo.HasValue) { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName, filter.EqualTo.Value.ConvertDateTimeToEpoch().ToString()); } else if (filter.LessThan.HasValue) { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName + "[lt]", filter.LessThan.Value.ConvertDateTimeToEpoch().ToString()); } if (filter.LessThanOrEqual.HasValue) { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName + "[lte]", filter.LessThanOrEqual.Value.ConvertDateTimeToEpoch().ToString()); } if (filter.GreaterThan.HasValue) { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName + "[gt]", filter.GreaterThan.Value.ConvertDateTimeToEpoch().ToString()); } if (filter.GreaterThanOrEqual.HasValue) { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName + "[gte]", filter.GreaterThanOrEqual.Value.ConvertDateTimeToEpoch().ToString()); } } else if (property.PropertyType == typeof(StripeBankAccountOptions)) { var options = (StripeBankAccountOptions)value; newUrl = ApplyNestedObjectProperties(newUrl, options); } else if (property.PropertyType == typeof(StripeCreditCardOptions)) { var options = (StripeCreditCardOptions)value; newUrl = ApplyNestedObjectProperties(newUrl, options); } else if (property.PropertyType == typeof(StripeSourceOptions)) { var options = (StripeSourceOptions)value; newUrl = ApplyNestedObjectProperties(newUrl, options); } else if (property.PropertyType == typeof(SourceCard)) { var options = (SourceCard)value; newUrl = ApplyNestedObjectProperties(newUrl, options); } else if (property.PropertyType == typeof(SourceBankAccount)) { var options = (SourceBankAccount)value; newUrl = ApplyNestedObjectProperties(newUrl, options); } else if (property.PropertyType == typeof(StripeAccountCardOptions)) { var options = (StripeAccountCardOptions)value; newUrl = ApplyNestedObjectProperties(newUrl, options); } else if (property.PropertyType == typeof(StripeAccountBankAccountOptions)) { var options = (StripeAccountBankAccountOptions)value; newUrl = ApplyNestedObjectProperties(newUrl, options); } else if (property.PropertyType == typeof(StripeAccountLegalEntityOptions)) { var sripeAccountLegalEntityOptions = (StripeAccountLegalEntityOptions)value; newUrl = ApplyNestedObjectProperties(newUrl, sripeAccountLegalEntityOptions); } // end the crap else { newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName, value.ToString()); } } } } if (service != null) { var propertiesToExpand = service.GetType() .GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.Name.StartsWith("Expand") && p.PropertyType == typeof(bool)) .Where(p => (bool)p.GetValue(service, null)) .Select(p => p.Name); foreach (var propertyName in propertiesToExpand) { string expandPropertyName = propertyName.Substring("Expand".Length); expandPropertyName = Regex.Replace(expandPropertyName, "([a-z])([A-Z])", "$1_$2").ToLower(); if (isListMethod) { expandPropertyName = "data." + expandPropertyName; } newUrl = ApplyParameterToUrl(newUrl, "expand[]", expandPropertyName); } } return(newUrl); }