Skip to content

Commit

Permalink
Properly deserialize source on BT during expand
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Apr 23, 2018
1 parent ca00165 commit adb8321
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public when_creating_charges_with_multiple_expand()
Currency = "usd",
Destination = account.Id,
};
chargeOptions.AddExpand("balance_transaction");
chargeOptions.AddExpand("balance_transaction.source");
chargeOptions.AddExpand("transfer.balance_transaction.source");
chargeOptions.AddExpand("destination");

Expand All @@ -34,13 +34,19 @@ public when_creating_charges_with_multiple_expand()
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);
}

[Fact]
public void should_properly_deserialize_balance_transaction_source()
{
Charge.BalanceTransaction.Source.Type.Should().Be(BalanceTransactionSourceType.Charge);
Charge.BalanceTransaction.SourceId.Should().Be(Charge.BalanceTransaction.Source.Charge.Id);
}
}
}
25 changes: 25 additions & 0 deletions src/Stripe.net/Entities/BalanceTransactionSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Newtonsoft.Json;
using Stripe.Infrastructure;

namespace Stripe
{
public enum BalanceTransactionSourceType
{
Charge,
Payout,
Refund,
Transfer,
Unknown,
}

[JsonConverter(typeof(BalanceTransactionSourceConverter))]
public class BalanceTransactionSource : StripeEntityWithId
{
public BalanceTransactionSourceType Type { get; set; }

public StripeCharge Charge { get; set; }
public StripePayout Payout { get; set; }
public StripeRefund Refund { get; set; }
public StripeTransfer Transfer { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Stripe.net/Entities/StripeBalanceTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ public class StripeBalanceTransaction : StripeEntityWithId
[JsonProperty("net")]
public int Net { get; set; }

#region Expandable Source
#region Expandable BalanceTransactionSource
public string SourceId { get; set; }

[JsonIgnore]
public StripeSource Source { get; set; }
public BalanceTransactionSource Source { get; set; }

[JsonProperty("source")]
internal object InternalSource
{
set
{
StringOrObject<StripeSource>.Map(value, s => SourceId = s, o => Source = o);
StringOrObject<BalanceTransactionSource>.Map(value, s => SourceId = s, o => Source = o);
}
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Stripe.Infrastructure
{
internal class BalanceTransactionSourceConverter : JsonConverter
{
public override bool CanWrite => false;

public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var incoming = JObject.FromObject(value);

incoming.WriteTo(writer);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var incoming = JObject.Load(reader);

var source = new BalanceTransactionSource
{
Id = incoming.SelectToken("id").ToString()
};

if (incoming.SelectToken("object")?.ToString() == "charge")
{
source.Type = BalanceTransactionSourceType.Charge;
source.Charge = Mapper<StripeCharge>.MapFromJson(incoming.ToString());
}
else if (incoming.SelectToken("object")?.ToString() == "payout")
{
source.Type = BalanceTransactionSourceType.Payout;
source.Payout = Mapper<StripePayout>.MapFromJson(incoming.ToString());
}
else if (incoming.SelectToken("object")?.ToString() == "refund")
{
source.Type = BalanceTransactionSourceType.Refund;
source.Refund = Mapper<StripeRefund>.MapFromJson(incoming.ToString());
}
else if (incoming.SelectToken("object")?.ToString() == "transfer")
{
source.Type = BalanceTransactionSourceType.Transfer;
source.Transfer = Mapper<StripeTransfer>.MapFromJson(incoming.ToString());
}
else
{
source.Type = BalanceTransactionSourceType.Unknown;
}

return source;
}
}
}

0 comments on commit adb8321

Please sign in to comment.