Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove bank account action #570

Merged
merged 2 commits into from
Nov 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pinax/stripe/actions/externalaccounts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stripe

from .. import models


Expand Down Expand Up @@ -62,3 +64,35 @@ def sync_bank_account_from_stripe_data(data):
setattr(obj, a, data.get(a))
obj.save()
return obj


def delete_bank_account(account, bank_account):
"""
Deletes an external bank account from Stripe and Updates DB

Important: The user must have another bank account with default_for_currency set to True

Args:
account: stripe.models.Account object to delete the bank_account from
bank_account: stripe.models.BankAccount object

Returns:
True if Bank Account was deleted
"""

# Get Stripe Account
account = stripe.Account.retrieve(account.stripe_id)

# Retrieve the associated Bank Account and Delete it
try:
r = account.external_accounts.retrieve(bank_account.stripe_id).delete()

if r['deleted']: # if Stripe returns that deleted is True
# delete the account
bank_account.delete()
return True

except stripe.error.InvalidRequestError as E:
print(E)

return False