Skip to content

Commit

Permalink
Add a BankAccount object
Browse files Browse the repository at this point in the history
Currently, bank accounts that were added as a source to Customers cannot be deleted in the same way as Cards. This patch takes care of that.
  • Loading branch information
trenton42 committed May 20, 2015
1 parent 9712d2f commit 46be7e0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
5 changes: 3 additions & 2 deletions stripe/__init__.py
Expand Up @@ -15,8 +15,9 @@

# Resource
from stripe.resource import ( # noqa
Account, Balance, BalanceTransaction, Card, Charge, Customer, Invoice,
InvoiceItem, Plan, Token, Coupon, Event, Transfer, Recipient, FileUpload,
Account, Balance, BalanceTransaction, BankAccount, Card,
Charge, Customer, Invoice, InvoiceItem, Plan, Token, Coupon,
Event, Transfer, Recipient, FileUpload,
ApplicationFee, Subscription, BitcoinReceiver, BitcoinTransaction)

# Error imports. Note that we may want to move these out of the root
Expand Down
37 changes: 37 additions & 0 deletions stripe/resource.py
Expand Up @@ -10,6 +10,7 @@ def convert_to_stripe_object(resp, api_key, account):
'invoice': Invoice, 'invoiceitem': InvoiceItem,
'plan': Plan, 'coupon': Coupon, 'token': Token, 'event': Event,
'transfer': Transfer, 'list': ListObject, 'recipient': Recipient,
'bank_account': BankAccount,
'card': Card, 'application_fee': ApplicationFee,
'subscription': Subscription, 'refund': Refund,
'file_upload': FileUpload,
Expand Down Expand Up @@ -422,6 +423,42 @@ def retrieve(cls, id, api_key=None, stripe_account=None, **params):
"recipient.cards.retrieve('card_id') instead.")


class BankAccount(UpdateableAPIResource, DeletableAPIResource):

def instance_url(self):
self.id = util.utf8(self.id)
extn = urllib.quote_plus(self.id)
if (hasattr(self, 'customer')):
self.customer = util.utf8(self.customer)

base = Customer.class_url()
owner_extn = urllib.quote_plus(self.customer)
class_base = "sources"

elif (hasattr(self, 'account')):
self.account = util.utf8(self.account)

base = Account.class_url()
owner_extn = urllib.quote_plus(self.account)
class_base = "bank_accounts"

else:
raise error.InvalidRequestError(
"Could not determine whether bank_account_id %s is "
"attached to a customer "
"or an account." % self.id, 'id')

return "%s/%s/%s/%s" % (base, owner_extn, class_base, extn)

@classmethod
def retrieve(cls, id, api_key=None, stripe_account=None, **params):
raise NotImplementedError(
"Can't retrieve a bank account without a customer or "
"account ID. Use "
"customer.sources.retrieve('bank_account_id') or "
"recipient.sources.retrieve('bank_account_id') instead.")


class Charge(CreateableAPIResource, ListableAPIResource,
UpdateableAPIResource):

Expand Down
16 changes: 15 additions & 1 deletion stripe/test/test_resources.py
Expand Up @@ -1066,6 +1066,20 @@ def test_customer_delete_source(self):
None
)

def test_customer_delete_bank_account(self):
source = stripe.BankAccount.construct_from({
'customer': 'cus_delete_source',
'id': 'ba_delete_source',
}, 'api_key')
source.delete()

self.requestor_mock.request.assert_called_with(
'delete',
'/v1/customers/cus_delete_source/sources/ba_delete_source',
{},
None
)


class TransferTest(StripeResourceTest):

Expand Down Expand Up @@ -1504,7 +1518,7 @@ def test_create_file_upload(self):
stripe.FileUpload.create(
purpose='dispute_evidence',
file=test_file
)
)
self.requestor_mock.request.assert_called_with(
'post',
'/v1/files',
Expand Down

0 comments on commit 46be7e0

Please sign in to comment.