-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly deserialize source on BT during expand
- Loading branch information
1 parent
ca00165
commit 851a0cc
Showing
4 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Stripe.net/Infrastructure/JsonConverters/BalanceTransactionSourceConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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() | ||
}; | ||
|
||
Console.WriteLine(incoming.SelectToken("id").ToString()); | ||
Console.WriteLine(incoming.SelectToken("balance_transaction").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; | ||
} | ||
} | ||
} |