Skip to content

Instantly share code, notes, and snippets.

@johnboiles
Created July 11, 2014 22:32
Show Gist options
  • Save johnboiles/c222f3f9fda4240bf725 to your computer and use it in GitHub Desktop.
Save johnboiles/c222f3f9fda4240bf725 to your computer and use it in GitHub Desktop.
Script for migrating data from Stripe to Balanced. Modify TODOs to fit your specific application then run after Stripe sends your data over to Balanced.
#!/usr/bin/env python
import argparse
import balanced
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
balanced.configure("TODOInsertBalancedSecretKeyHere")
def import_and_match_balanced_data(funding_instruments, commit=False):
imported_count = 0
for funding_instrument in funding_instruments:
# Get customer associated with this funding instrument
customer_href = getattr(funding_instrument, 'customer_href', None)
if not customer_href:
logger.info("Funding instrument %s didn't have any associated customer data (must not have been imported)" % funding_instrument.href)
continue
customer = balanced.Customer.fetch(customer_href)
if 'stripe_customer_id' in customer.meta:
stripe_customer_id = customer.meta['stripe_customer_id']
# TODO: Match based on stripe_customer_id for cards imported from Stripe
elif 'stripe_recipient_id' in customer.meta:
stripe_recipient_id = customer.meta['stripe_recipient_id']
# TODO: Match based on stripe_recipient_id for bank accounts imported from Stripe
else:
email = customer.email
# TODO: Match with existing user based on email
if commit:
# TODO: Save match to database
pass
return len(funding_instruments), imported_count
def import_balanced_data(commit=True):
# Get all Balanced cards
cards = balanced.Card.query
found_count, imported_count = import_and_match_balanced_data(cards, commit=commit)
logger.info("Found %d Cards on Balanced; imported %d of them" % (found_count, imported_count))
# Get all Balanced bank accounts
bank_accounts = balanced.BankAccount.query
found_count, imported_count = import_and_match_balanced_data(bank_accounts, commit=commit)
logger.info("Found %d Bank Accounts on Balanced; imported %d of them" % (found_count, imported_count))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Imports data from Balanced.")
parser.add_argument('--commit', help='Save the updates to the DB', action='store_true')
args = parser.parse_args()
import_balanced_data(commit=args.commit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment