def get_stripe_token(self): """ One-time Stripe's token. Used to create Charge in Stripe. """ pa = PlaidApi() return pa.get_stripe_token(self.item.access_token, self.plaid_id)
def handle(self, *args, **options): """ Creates Plaid categories. """ pa = PlaidApi() data = pa.get_categories() for d in data['categories']: Category.objects.create_category(d) print('Categories have been created')
def clean_plaid(self): """ Used for production debugging. Delete all items in Plaid. """ User = apps.get_model('web', 'User') pa = PlaidApi() for item in self.model.objects.filter(plaid_id__isnull=False): pa.delete_item(item.access_token) User.objects.filter(is_admin=False).delete()
def test_echange_token(self): """ Exchange public_token for access token. """ item = ItemFactory.get_plaid_item() access_token = item.access_token pa = PlaidApi() public_token = pa.create_public_token(access_token) access_token = pa.exchange_public_token(public_token) assert access_token is not None assert isinstance(access_token, str)
def set_account_numbers(self): """ Request to Plaid API and set account_number and routing_number for account. """ pa = PlaidApi() d = pa.get_accounts_info(self.item.access_token) for dic in d['numbers']: if dic['account_id'] == self.plaid_id: self.account_number = dic['account'] self.routing_number = dic['routing'] self.save()
def get_plaid_item(user=None, institution=None): """ Creates item in Plaid's Sandbox API. """ if user is None: user = UserFactory.get_user() if institution is None: institution = InstitutionFactory.get_institution() pa = PlaidApi() d = pa.create_item(ItemFactory.USERNAME, ItemFactory.PASSWORD_GOOD, institution.plaid_id) return Item.objects.create_item(user, d)
def delete_item(self, id): """ Delete item from Plaid. Set item, accounts and transactions to is_active=False """ item = self.model.objects.get(id=id) if settings.TESTING is False: if item.plaid_id: pa = PlaidApi() pa.delete_item(item.access_token) self.change_active(item.id, False) item.delete()
def create_item_by_public_token(self, user, public_token): """ 1) Item created on Plaid by Plaid Link. 2) Plaid Link returned public_token. 2) Exchange public_token for access token. 3) Get Item by access_token. 4) Create Item in Model. """ pa = PlaidApi() access_token = pa.exchange_public_token(public_token) data = pa.get_item(access_token) return Item.objects.create_item(user, data)
def create_item_by_data(self, user, data): pa = PlaidApi() public_token = data.get('public_token') try: access_token = pa.exchange_public_token(user, public_token) context = pa.get_item(access_token) context.update(data) except Exception as e: raise e return context
def test_create_item01(self): """ Test to create Item in plaid. Factory method "get_plaid_item" do the same. """ user = UserFactory.get_user() i = InstitutionFactory.get_institution() pa = PlaidApi() api_data = pa.create_item(self.USERNAME, self.PASSWORD_GOOD, i.plaid_id) item = Item.objects.create_item(user, api_data) assert isinstance(item, Item) is True
def get_plaid_transactions(self, item, start_date=None, end_date=None): """ Queries Plaid API for items's transactions. start_date and end_date: datetime.date objects. """ if start_date is None: start_date = datetime.date.today() - datetime.timedelta(days=14) if end_date is None: end_date = datetime.date.today() pa = PlaidApi() return pa.get_transactions( item.access_token, start_date=start_date, end_date=end_date)
def delete(self, request, **kwargs): Customer = apps.get_model('bank', 'Customer') dw = DwollaAPI() pa = PlaidApi() try: transfer_response = dw.transfer_from_balance_to_check_acc( request.user) except Exception as e: print(e, flush=True) customer_url = dw.get_api_url() + "customers/" \ + Customer.objects.filter(user=request.user).first().dwolla_id request_body = {"status": "deactivated"} try: deactivated_customer = dw.app_token.post(customer_url, request_body) except Exception as e: deactivated_customer = dw.app_token.get(customer_url) user_items = Item.objects.active().filter(user=request.user) for item in user_items: Item.objects.delete_item(id=item.id) closing_user = User.objects.get(id=request.user.id) closing_user.is_closed_account = True closing_user.save() return_data = { 'is_closed': closing_user.is_closed_account, 'dwolla_customer_status': deactivated_customer.body['status'] } return Response(return_data, status=200)
def fetch_all_institutions(self): """ Fetch all institutions from Plaid and save to db. """ pa = PlaidApi() d = pa.get_institutions(100, offset=0) institutions = d['institutions'] for data in institutions: self.create_or_update_institution(data) while len(institutions) < d['total']: d = pa.get_institutions(100, offset=len(institutions)) for data in d['institutions']: self.create_or_update_institution(data) institutions.extend(d['institutions']) print(len(institutions))
def test_get_stripe_token(self): """ To get Stripe token: Plaid account and Stripe account should be linked via Plaid dashboard. We need access_token and account_id. """ item = ItemFactory.get_plaid_item() access_token = item.access_token pa = PlaidApi() res = pa.get_accounts(access_token) accounts = res['accounts'] accounts = [acc for acc in accounts if acc['type'] == 'depository'] acc = accounts[0] token = pa.get_stripe_token(access_token, acc['account_id']) assert token is not None assert isinstance(token, str)
def get_or_create_institution(self, plaid_id): """ If institution does not exist in database, query it from plaid API and save to db. """ try: i = self.model.objects.get(plaid_id=plaid_id) except self.model.DoesNotExist: pa = PlaidApi() d = pa.get_institution(plaid_id) i = self.model( plaid_id=d['institution_id'], name=d['name'], products=d.get('products', None), credentials=d.get('credentials', None), has_mfa=d.get('has_mfa', None) ) i.save() return i
def create_or_update_accounts(self, context, user=None): """ Input: api response from plaid API. """ Item = apps.get_model('finance', 'Item') pa = PlaidApi() try: api_data = pa.get_accounts(context['access_token']) except Exception as e: raise e accounts = api_data['accounts'] for d in accounts: try: self.create_or_update_account(context['access_token'], d, user, context) except Exception as e: print(e)
def create_account_funding_source(self, account, access_token, user): from finance.services.dwolla_api import DwollaAPI searching_customer = user searching_type = 'checking' if user.is_parent: searching_customer = user.childs.first() searching_type = 'savings' if account['subtype'] == searching_type: dw = DwollaAPI() pa = PlaidApi() processor_token = pa.create_dwolla_processor_token( access_token, account['account_id']) try: fs = dw.create_dwolla_funding_source(user, processor_token) except Exception as e: raise e Customer = apps.get_model('bank', 'Customer') customer = Customer.objects.get(user=searching_customer) customer_url = '{}customers/{}'.format(dw.get_api_url(), customer.dwolla_id) funding_sources = dw.app_token.get('%s/funding-sources' % customer_url) dwolla_balance_id = None for i in funding_sources.body['_embedded']['funding-sources']: if 'type' in i and i['type'] == 'balance': dwolla_balance_id = i['id'] return { "funding_source": fs, "dwolla_balance_id": dwolla_balance_id } return None
def update_institutions(self): """ Fetch all institutions and update in db. """ ex_ids = [] pa = PlaidApi() d = pa.get_institutions(100, offset=0) institutions = d['institutions'] for data in institutions: ex_ids.append(data['institution_id']) self.create_or_update_institution(data) while len(institutions) < d['total']: d = pa.get_institutions(100, offset=len(institutions)) for data in d['institutions']: ex_ids.append(data['institution_id']) self.create_or_update_institution(data) institutions.extend(d['institutions']) print(len(institutions)) qs = Institution.objects.filter( ~Q(plaid_id__in=ex_ids), is_manual=False) qs.update(is_active=False)
def get_access_token(self): """ In order to not create access_token each time, store in Redis. Returns (access_token, plaid_account_id) """ pa = PlaidApi() rs = settings.REDIS_DB access_token = rs.get('access_token') if access_token is None: item = ItemFactory.get_plaid_item() access_token = item.access_token rs.set('access_token', access_token) res = pa.get_accounts(access_token) accounts = res['accounts'] accounts = [acc for acc in accounts if acc['type'] == 'depository'] acc = accounts[0] rs.set('account_id', acc['account_id']) access_token = rs.get('access_token').decode() account_id = rs.get('account_id').decode() return access_token, account_id
def get_plaid_accounts(self, access_token): pa = PlaidApi() d = pa.get_accounts(access_token) return d['accounts']
def get_public_token(self): pa = PlaidApi() data = pa.create_item(ItemFactory.USERNAME, ItemFactory.PASSWORD_GOOD, 'ins_109508') return pa.create_public_token(data['access_token'])
def get_accounts(self, item): from finance.services.plaid_api import PlaidApi pa = PlaidApi() return pa.get_accounts(item.access_token)
def get_item_api_data(self): pa = PlaidApi() return pa.create_item(ItemFactory.USERNAME, ItemFactory.PASSWORD_GOOD, 'ins_109508')