diff --git a/src/Stripe.Tests/Stripe.Tests.csproj b/src/Stripe.Tests/Stripe.Tests.csproj index 786963b053..21f959a795 100644 --- a/src/Stripe.Tests/Stripe.Tests.csproj +++ b/src/Stripe.Tests/Stripe.Tests.csproj @@ -78,6 +78,7 @@ + diff --git a/src/Stripe.Tests/card/when_creating_a_card.cs b/src/Stripe.Tests/card/when_creating_a_card.cs new file mode 100644 index 0000000000..29e180a818 --- /dev/null +++ b/src/Stripe.Tests/card/when_creating_a_card.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using Machine.Specifications; + +namespace Stripe.Tests +{ + public class when_creating_a_card + { + private static StripeCustomer _stripeCustomer; + private static StripeCardService _stripeCardService; + private static StripeCardCreateOptions _stripeCardCreateOptions; + private static StripeCard _stripeCard; + + Establish context = () => + { + _stripeCustomer = new StripeCustomerService().Create(new StripeCustomerCreateOptions() + { + Email = "bob@" + Guid.NewGuid() + "" + }); + + _stripeCardService = new StripeCardService(); + _stripeCardCreateOptions = new StripeCardCreateOptions() + { + SourceCard = new SourceCard() + { + Name = "TestCard " + Guid.NewGuid(), + ReceiptEmail = "testcard@" + Guid.NewGuid() + ".com", + ExpirationMonth = "10", + ExpirationYear = "2021", + Number = "4000000000000077", + Metadata = new Dictionary + { + { "OwnerId", "1234" }, + { "EventId", "1414141" } + } + } + }; + }; + + Because of = () => + _stripeCard = _stripeCardService.Create(_stripeCustomer.Id, _stripeCardCreateOptions); + + It should_have_metadata = () => + _stripeCard.Metadata.ShouldNotBeNull(); + + It should_have_the_correct_metadata = () => + { + _stripeCard.Metadata.Keys.ShouldContain("OwnerId"); + _stripeCard.Metadata["OwnerId"].ShouldEqual("1234"); + + _stripeCard.Metadata.Keys.ShouldContain("EventId"); + _stripeCard.Metadata["EventId"].ShouldEqual("1414141"); + }; + } +} \ No newline at end of file diff --git a/src/Stripe/Infrastructure/ParameterBuilder.cs b/src/Stripe/Infrastructure/ParameterBuilder.cs index 19a9368fb4..fab44bea1f 100644 --- a/src/Stripe/Infrastructure/ParameterBuilder.cs +++ b/src/Stripe/Infrastructure/ParameterBuilder.cs @@ -26,14 +26,7 @@ public static string ApplyAllParameters(this StripeService service, object obj, { // simplify this crap if (attribute.PropertyName.ToLower().Contains("metadata")) - { - var metadata = (Dictionary)value; - - foreach (string key in metadata.Keys) - { - newUrl = ApplyParameterToUrl(newUrl, string.Format("metadata[{0}]", key), metadata[key]); - } - } + newUrl = ApplyMetadataParameters(newUrl, value); else if (attribute.PropertyName.ToLower().Contains("fraud_details")) { var fraudDetails = (Dictionary)value; @@ -143,18 +136,33 @@ public static string ApplyParameterToUrl(string url, string argument, string val private static string ApplyNestedObjectProperties(string newUrl, object nestedObject) { - foreach (var prop in nestedObject.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) + foreach (var property in nestedObject.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { - var val = prop.GetValue(nestedObject, null); - if (val == null) continue; + var value = property.GetValue(nestedObject, null); + if (value == null) continue; - foreach (var attr in prop.GetCustomAttributes(typeof(JsonPropertyAttribute), false).Cast()) + foreach (var attribute in property.GetCustomAttributes(typeof(JsonPropertyAttribute), false).Cast()) { - newUrl = ApplyParameterToUrl(newUrl, attr.PropertyName, val.ToString()); + if (attribute.PropertyName.ToLower().Contains("metadata")) + newUrl = ApplyMetadataParameters(newUrl, value); + else + newUrl = ApplyParameterToUrl(newUrl, attribute.PropertyName, value.ToString()); } } return newUrl; } + + private static string ApplyMetadataParameters(string newUrl, object value) + { + var metadata = (Dictionary)value; + + foreach (string key in metadata.Keys) + { + newUrl = ApplyParameterToUrl(newUrl, $"metadata[{key}]", metadata[key]); + } + + return newUrl; + } } }