Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi-level Expand feature #1140

Merged
merged 1 commit into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/Stripe.Tests.XUnit/_infrastructure/expanding_resources.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using FluentAssertions;
using Xunit;
using Stripe.Infrastructure;

namespace Stripe.Tests.Xunit
{
public class expanding_resources
{
public class TestOptions : StripeBaseOptions
{
[JsonProperty("a_string")]
public string AString { get; set; }
}

public class TestService : StripeService
{
public TestService() : base(null)
{
}
}

public TestService Service { get; }

public expanding_resources()
{
Service = new TestService();
}

[Fact]
public void can_expand_resources()
{
var obj = new TestOptions
{
AString = "foo",
};
obj.AddExpand("example1.subexample1");
obj.AddExpand("example2");
obj.AddExpand("example3.subexample3");

var url = Service.ApplyAllParameters(obj, "", false);
url.Should().Be("?a_string=foo&expand[]=example1.subexample1&expand[]=example2&expand[]=example3.subexample3");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using Xunit;

namespace Stripe.Tests.Xunit
{
public class when_creating_charges_with_multiple_expand
{
private static StripeCharge Charge;

public when_creating_charges_with_multiple_expand()
{
var accountService = new StripeAccountService(Cache.ApiKey);
var account = accountService.Create(new StripeAccountCreateOptions
{
Type = StripeAccountType.Custom
});

var chargeOptions = new StripeChargeCreateOptions
{
SourceTokenOrExistingSourceId = "tok_visa",
ApplicationFee = 10,
Amount = 100,
Currency = "usd",
Destination = account.Id,
};
chargeOptions.AddExpand("balance_transaction");
chargeOptions.AddExpand("transfer.balance_transaction.source");
chargeOptions.AddExpand("destination");

Charge = new StripeChargeService(Cache.ApiKey).Create(chargeOptions);

accountService.Delete(account.Id);
}


[Fact]
public void should_have_all_objects_expanded()
{
Charge.BalanceTransactionId.Should().Be(Charge.BalanceTransaction.Id);
Charge.DestinationId.Should().Be(Charge.Destination.Id);
Charge.Transfer.BalanceTransaction.Source.Id.Should().Be(Charge.Transfer.Id);
}
}
}
3 changes: 3 additions & 0 deletions src/Stripe.net/Infrastructure/ParameterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public static string ApplyAllParameters(this StripeService service, StripeBaseOp
var key = WebUtility.UrlEncode(pair.Key);
RequestStringBuilder.ApplyParameterToRequestString(ref requestString, key, pair.Value);
}
foreach (var value in obj.Expand) {
RequestStringBuilder.ApplyParameterToRequestString(ref requestString, "expand[]", value);
}
}

if (service != null)
Expand Down
7 changes: 7 additions & 0 deletions src/Stripe.net/Services/StripeBaseOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ public void AddExtraParam(string key, string value) {
}

public Dictionary<string, string> ExtraParams = new Dictionary<string, string>();


public void AddExpand(string value) {
Expand.Add(value);
}

public List<string> Expand = new List<string>();
}
}