stripe / stripe-dotnet

Stripe.net is a sync/async .NET 4.6.1+ client, and a portable class library for stripe.com.
Apache License 2.0
1.35k stars 566 forks source link

SourceType - where is BankAccount const property ? #1359

Closed pmahend1 closed 5 years ago

pmahend1 commented 5 years ago

After recent major upgrade where you folks changed IPaymentSource and Source I am using SourceType class for type of IPaymentSource object's type by

var list = customerService.GetAllSources(customerId);
 foreach (var item in list){

if(item.Object == SourceType.Card){
...
}
else if(item.Object == "bank_account"){// here
}

SourceType class does not have property for bank account, it however has for AchCreditTransfer AchDebit and Bancontact (??)

Is this intentional?

Earlier I just used to iterate through this list which was Source type and has either Card and BankAccount set.

using hard_coded "bank_account" seems dumb to me.

I searched for "bank_account" in this repo, couldnt find it in any Enum or class other than Json converters or Test classes.

Rationale behind this? I think SourceType should have constant property.

public const string BankAccount= "bank_account";
remi-stripe commented 5 years ago

@pmahend1 SourceType is used to represent the type of Source object that you are using. This only works if you use a Source and not a BankAccount.

Your example with SourceType.Card works by chance because Card is also a valid Type on Source while Bank Account is not.

Unfortunately, this is currently quite confusing in the API because Source is a generic name that makes you think it works for Card and Bank Account objects but it does not, those are entirely separate resources:

The idea here when you get an IPaymentSource is to check its real type by using GetType to know if it's a BankAccount, a Source, a Card or an Account for example.

ob-stripe commented 5 years ago

To add to Remi's answer, with C# 7.0 you can use pattern matching. You could update your code like this:

var list = customerService.GetAllSources(customerId);

foreach (var item in list)
{
  switch (item)
  {
    case Account account:
      // do something with account
      break;
    case BankAccount bankAccount:
      // do something with bankAccount
      break;
    case Card card:
      // do something with card
      break;
    case Source source:
      // do something with source
      break;
  }
}

Closing the issue, but feel free to reply if you have more questions.