From ba3d0889eaee6046f66432b7cb3cc9ba46040218 Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Thu, 14 Dec 2017 17:48:27 -0500 Subject: [PATCH 1/2] Add support for account debits * A Charge can be created with an Account id as the source. * Deserialization of the Account object will work even though the API only returns an id and an object. * Account debit as a transfer already works as `destination` is a string and can not be expanded. --- ...creating_a_charge_with_a_stripe_account.cs | 43 +++++++++++++++++++ src/Stripe.net/Entities/Source.cs | 2 + .../JsonConverters/SourceConverter.cs | 6 +++ 3 files changed, 51 insertions(+) create mode 100644 src/Stripe.net.Tests/charges/when_creating_a_charge_with_a_stripe_account.cs diff --git a/src/Stripe.net.Tests/charges/when_creating_a_charge_with_a_stripe_account.cs b/src/Stripe.net.Tests/charges/when_creating_a_charge_with_a_stripe_account.cs new file mode 100644 index 0000000000..c16b14ad89 --- /dev/null +++ b/src/Stripe.net.Tests/charges/when_creating_a_charge_with_a_stripe_account.cs @@ -0,0 +1,43 @@ +using Machine.Specifications; + +namespace Stripe.Tests +{ + public class when_creating_a_charge_with_a_stripe_account + { + private static StripeAccount _stripeAccount; + private static StripeCharge _stripeCharge; + + Establish context = () => + { + // create a new custom account + _stripeAccount = new StripeAccountService().Create(new StripeAccountCreateOptions() + { + Country = "US", + Type = StripeAccountType.Custom + }); + }; + + Because of = () => + { + // charge the customer something + _stripeCharge = new StripeChargeService().Create(new StripeChargeCreateOptions() + { + Amount = 100, + Currency = "usd", + SourceTokenOrExistingSourceId = _stripeAccount.Id + }); + }; + + It should_have_the_right_source = () => + _stripeCharge.Source.Account.ShouldNotBeNull(); + + It should_not_have_the_wrong_source = () => + _stripeCharge.Source.Card.ShouldBeNull(); + + It should_have_the_right_source_type = () => + _stripeCharge.Source.Type.ShouldEqual(SourceType.Account); + + It should_have_the_right_id = () => + _stripeCharge.Source.Id.ShouldEqual(_stripeAccount.Id); + } +} \ No newline at end of file diff --git a/src/Stripe.net/Entities/Source.cs b/src/Stripe.net/Entities/Source.cs index c275aed83f..d75b45330a 100755 --- a/src/Stripe.net/Entities/Source.cs +++ b/src/Stripe.net/Entities/Source.cs @@ -5,6 +5,7 @@ namespace Stripe { public enum SourceType { + Account, Card, BankAccount, Deleted @@ -15,6 +16,7 @@ public class Source : StripeEntityWithId { public SourceType Type { get; set; } + public StripeAccount Account { get; set; } public StripeDeleted Deleted { get; set; } public StripeCard Card { get; set; } public StripeBankAccount BankAccount { get; set; } diff --git a/src/Stripe.net/Infrastructure/JsonConverters/SourceConverter.cs b/src/Stripe.net/Infrastructure/JsonConverters/SourceConverter.cs index 881fb84f8d..f97084d802 100755 --- a/src/Stripe.net/Infrastructure/JsonConverters/SourceConverter.cs +++ b/src/Stripe.net/Infrastructure/JsonConverters/SourceConverter.cs @@ -29,6 +29,12 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist Id = incoming.SelectToken("id").ToString() }; + if (incoming.SelectToken("object")?.ToString() == "account") + { + source.Type = SourceType.Account; + source.Account = Mapper.MapFromJson(incoming.ToString()); + } + if (incoming.SelectToken("object")?.ToString() == "bank_account") { source.Type = SourceType.BankAccount; From a323a9efed44b58c24f52a65e51193ecd7616c98 Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Fri, 15 Dec 2017 08:06:47 -0500 Subject: [PATCH 2/2] Add extra test to ensure Account deserialization --- .../charges/when_creating_a_charge_with_a_stripe_account.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Stripe.net.Tests/charges/when_creating_a_charge_with_a_stripe_account.cs b/src/Stripe.net.Tests/charges/when_creating_a_charge_with_a_stripe_account.cs index c16b14ad89..5a7a505245 100644 --- a/src/Stripe.net.Tests/charges/when_creating_a_charge_with_a_stripe_account.cs +++ b/src/Stripe.net.Tests/charges/when_creating_a_charge_with_a_stripe_account.cs @@ -39,5 +39,8 @@ public class when_creating_a_charge_with_a_stripe_account It should_have_the_right_id = () => _stripeCharge.Source.Id.ShouldEqual(_stripeAccount.Id); + + It should_deserialize_the_account_properly = () => + _stripeCharge.Source.Account.Id.ShouldEqual(_stripeAccount.Id); } } \ No newline at end of file