Skip to content

Commit

Permalink
metadata fix on SourceCard (thanks @rmcohen!)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymedavis committed Apr 12, 2016
1 parent f727833 commit d0c354b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Stripe.Tests/Stripe.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="balance\when_listing_balancetransactions.cs" />
<Compile Include="balance\when_listing_balancetransactions_for_charge.cs" />
<Compile Include="balance\when_retrieving_a_balance.cs" />
<Compile Include="card\when_creating_a_card.cs" />
<Compile Include="charges\when_refunding_and_setting_fraud_details.cs" />
<Compile Include="Setup.cs" />
<Compile Include="charges\test_data\stripe_charge_update_options.cs" />
Expand Down
55 changes: 55 additions & 0 deletions src/Stripe.Tests/card/when_creating_a_card.cs
Original file line number Diff line number Diff line change
@@ -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<string, string>
{
{ "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");
};
}
}
34 changes: 21 additions & 13 deletions src/Stripe/Infrastructure/ParameterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>)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<string, string>)value;
Expand Down Expand Up @@ -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<JsonPropertyAttribute>())
foreach (var attribute in property.GetCustomAttributes(typeof(JsonPropertyAttribute), false).Cast<JsonPropertyAttribute>())
{
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<string, string>)value;

foreach (string key in metadata.Keys)
{
newUrl = ApplyParameterToUrl(newUrl, $"metadata[{key}]", metadata[key]);
}

return newUrl;
}
}
}

0 comments on commit d0c354b

Please sign in to comment.