-
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.
Add support for managing External Accounts
- Loading branch information
1 parent
5257021
commit 3c7a82d
Showing
10 changed files
with
254 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace Stripe.Tests.Xunit | ||
{ | ||
public class account_fixture : IDisposable | ||
{ | ||
public StripeExternalAccountCreateOptions ExernalAccountCreateOptionsBankAccount { get; } | ||
public StripeExternalAccountUpdateOptions ExernalAccountUpdateOptions { get; } | ||
|
||
public StripeAccount Account { get; } | ||
public StripeExternalAccount ExternalAccountCard { get; } | ||
public StripeExternalAccount ExternalAccountBankAccount { get; } | ||
public StripeExternalAccount ExternalAccountUpdated { get; } | ||
public StripeExternalAccount ExternalAccountRetrieved { get; } | ||
public StripeList<StripeExternalAccount> ExternalAccountList { get; } | ||
|
||
public account_fixture() | ||
{ | ||
Account = new StripeAccountService(Cache.ApiKey).Create(new StripeAccountCreateOptions | ||
{ | ||
Country = "US", | ||
Type = StripeAccountType.Custom | ||
}); | ||
|
||
ExernalAccountCreateOptionsBankAccount = new StripeExternalAccountCreateOptions | ||
{ | ||
ExternalAccountBankAccount = new StripeAccountBankAccountOptions | ||
{ | ||
AccountNumber = "000123456789", | ||
Country = "US", | ||
Currency = "usd", | ||
RoutingNumber = "110000000" | ||
} | ||
} | ||
|
||
ExernalAccountUpdateOptions = new StripeExternalAccountCreateOptions | ||
{ | ||
Metadata = new Dictionary<string, string>() | ||
{ | ||
{ "some-key", "some-value" } | ||
} | ||
}; | ||
|
||
var service = new StripeExternalAccountService(Cache.ApiKey); | ||
|
||
ExternalAccountCard = service.Create(Account.Id, new StripeExternalAccountCreateOptions | ||
{ | ||
ExternalAccountTokenId = "tok_visa_debit" | ||
}); | ||
ExternalAccountBankAccount = service.Create(Account.Id, ExernalAccountCreateOptionsBankAccount); | ||
ExternalAccountUpdated = service.Update(Account.Id, ExternalAccountCard.Id, ExernalAccountUpdateOptions); | ||
ExternalAccountRetrieved = service.Get(Account.Id, ExternalAccountCard.Id); | ||
ExternalAccountList = service.List(Account.Id); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
new StripeAccountService(Cache.ApiKey).Delete(Account.Id); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Stripe.Tests.XUnit/external_accounts/creating_and_updating_external_accounts.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,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Stripe.Tests.Xunit | ||
{ | ||
public class creating_and_updating_external_accounts : IClassFixture<external_account_fixture> | ||
{ | ||
private readonly external_account_fixture fixture; | ||
|
||
public creating_and_updating_external_accounts(external_account_fixture fixture) | ||
{ | ||
this.fixture = fixture; | ||
} | ||
|
||
[Fact] | ||
public void created_card_has_the_right_info() | ||
{ | ||
fixture.ExternalAccountCard.Type.Should().Be(SourceType.Card); | ||
} | ||
|
||
[Fact] | ||
public void created_bank_account_has_the_right_info() | ||
{ | ||
fixture.ExternalAccountBankAccount.Type.Should().Be(SourceType.BankAccount); | ||
fixture.ExternalAccountBankAccount.AccountId.Should().Be(fixture.Account.Id); | ||
fixture.ExternalAccountBankAccount.RoutingNumber.Should().Be(fixture.ExernalAccountCreateOptionsBankAccount.RoutingNumber); | ||
} | ||
} | ||
} | ||
|
File renamed without changes.
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,22 @@ | ||
using Newtonsoft.Json; | ||
using Stripe.Infrastructure; | ||
|
||
namespace Stripe | ||
{ | ||
public enum ExternalAccountType | ||
{ | ||
Card, | ||
BankAccount, | ||
Deleted | ||
} | ||
|
||
[JsonConverter(typeof(ExternalAccountConverter))] | ||
public class StripeExternalAccount : StripeEntityWithId | ||
{ | ||
public ExternalAccountType Type { get; set; } | ||
|
||
public StripeDeleted Deleted { get; set; } | ||
public StripeCard Card { get; set; } | ||
public StripeBankAccount BankAccount { get; set; } | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Stripe.net/Infrastructure/JsonConverters/ExternalAccountConverter.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,53 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Stripe.Infrastructure | ||
{ | ||
internal class ExternalAccountConverter : 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 externalAccount = new StripeExternalAccount | ||
{ | ||
Id = incoming.SelectToken("id").ToString() | ||
}; | ||
|
||
if (incoming.SelectToken("object")?.ToString() == "bank_account") | ||
{ | ||
externalAccount.Type = ExternalAccountType.BankAccount; | ||
externalAccount.BankAccount = Mapper<StripeBankAccount>.MapFromJson(incoming.ToString()); | ||
} | ||
|
||
if (incoming.SelectToken("object")?.ToString() == "card") | ||
{ | ||
externalAccount.Type = ExternalAccountType.Card; | ||
externalAccount.Card = Mapper<StripeCard>.MapFromJson(incoming.ToString()); | ||
} | ||
|
||
if (incoming.SelectToken("deleted")?.ToString() == "true") | ||
{ | ||
externalAccount.Type = ExternalAccountType.Deleted; | ||
externalAccount.Deleted = Mapper<StripeDeleted>.MapFromJson(incoming.ToString()); | ||
} | ||
|
||
return externalAccount; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Stripe.net/Services/ExternalAccounts/StripeExternalAccountCreateOptions.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,20 @@ | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
|
||
namespace Stripe | ||
{ | ||
public class StripeExternalAccountCreateOptions : StripeBaseOptions | ||
{ | ||
[JsonProperty("external_account")] | ||
public string ExternalAccountTokenId { get; set; } | ||
|
||
[JsonProperty("external_account")] | ||
public StripeAccountBankAccountOptions ExternalAccountBankAccount { get; set; } | ||
|
||
[JsonProperty("metadata")] | ||
public Dictionary<string, string> Metadata { get; set; } | ||
|
||
[JsonProperty("default_for_currency")] | ||
public bool? DefaultForCurrency { get; set; } | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Stripe.net/Services/ExternalAccounts/StripeExternalAccountListOptions.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,8 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Stripe | ||
{ | ||
public class StripeExternalAccountListOptions : StripeListOptions | ||
{ | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/Stripe.net/Services/ExternalAccounts/StripeExternalAccountUpdateOptions.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,14 @@ | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
|
||
namespace Stripe | ||
{ | ||
public class StripeExternalAccountUpdateOptions : StripeBaseOptions | ||
{ | ||
[JsonProperty("metadata")] | ||
public Dictionary<string, string> Metadata { get; set; } | ||
|
||
[JsonProperty("default_for_currency")] | ||
public bool? DefaultForCurrency { get; set; } | ||
} | ||
} |