def get_or_create_funding_source(customer, name='My Bank'): """ Returns funding source (bank) or creates verified funding source (bank) in Dwolla for test customer. name is used to simulate different test cases. name = "R01" Insufficient Funds: failing from the source bank account. name = "R01-late" Simulate funds failing from the source bank account post-settlement. Must click "Process bank transfers" twice. """ dw = DwollaApi() for fs in dw.get_funding_sources(customer['id']): if fs['status'] == 'verified' and fs['type'] == 'bank': # Set funding source's name. dw.edit_funding_source_name(fs['id'], name) return fs data = { 'routingNumber': '222222226', 'accountNumber': '123456789', 'type': 'savings', 'name': name } print('Creating funding source --------') id = dw.create_funding_source(customer['id'], data) fs = dw.get_funding_source(id) print(fs) print('Initiating micro-deposits -------') res = dw.initiate_micro_deposits(fs['id']) print(res) print('Get status of micro-deposits -------') res = dw.get_micro_deposits(fs['id']) print('Verifying funding source -------') res = dw.verify_micro_deposits( fs['id'], '0.05', '0.05') print(res) print('Checking the status of funding source -------') fs = dw.get_funding_source(id) print(fs['status']) return fs
def take_monthly_fee(): dwa = DwollaApi() root = dwa.token.get(url='/') dwolla_account = root.body['_links']['account']['href'] Customer = apps.get_model('bank', 'Customer') account_token = dwa.client.Token(access_token=dwa.token.access_token, refresh_token=dwa.token.access_token) for customer in Customer.objects.exclude(user__is_paused=True).exclude( user__is_closed_account=True).exclude(user__active_days=30): customer.user.active_days += 1 customer.user.save() for customer in Customer.objects.exclude(user__is_paused=True).exclude( user__is_closed_account=True).filter(user__active_days=30): result = dwa.get_funding_sources(customer.dwolla_id)[0] customer_source_url = result['_links']['self']['href'] customer_balance_id = result[0]['id'] customer_balance = dwa.get_funding_source_balance( customer_balance_id)['balance']['value'] if float(customer_balance) < 1.99: request_body = { '_links': { 'source': { 'href': customer_source_url }, 'destination': { 'href': dwolla_account } }, 'amount': { 'currency': 'USD', 'value': '1.99' } } transfer = account_token.post('transfers', request_body) customer.user.active_days = 0 customer.user.save() else: logger.info("Not enough money to fee") logger.info("Got fee from customer{}".format(customer.dwolla_id))