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 update_status_transaction(self): from finance.services.dwolla_api import DwollaAPI dw = DwollaAPI() transfers = self.model.objects.filter(status='pending') for transfer in transfers: transfer_url = dw.get_transfer_url(transfer.transfer_id) fees = dw.app_token.get(transfer_url) transfer.status = fees.body['status'] transfer.save()
def delete(self, *args, **kwargs): super().delete(*args, **kwargs) from finance.services.dwolla_api import DwollaAPI d = DwollaAPI() d.app_token.post(self.funding_items.first().funding_sources_url, {"removed": True})
def create_or_update_account(self, access_token, data, user, context): """ data is dictionary with response result. If account is deleted in Donkies (is_active=False), do not process. """ from finance.services.dwolla_api import DwollaAPI m_fields = self.model._meta.get_fields() m_fields = [f.name for f in m_fields] d = {k: v for (k, v) in data.items() if k in m_fields} d['plaid_id'] = data['account_id'] try: acc = self.model.objects.get(plaid_id=d['plaid_id']) if not acc.is_active: return None acc.__dict__.update(d) except self.model.DoesNotExist: acc = self.model(**d) if user is not None: try: funding_data = self.create_account_funding_source( data, context['access_token'], user) item = Item.objects.create_item(user, context) acc.item = item except Exception as e: raise e if funding_data is not None: dw = DwollaAPI() dw.save_funding_source(item, user, funding_data['funding_source'], funding_data['dwolla_balance_id']) acc.is_funding_source_for_transfer = True if context.get('account_id') == data.get('account_id'): acc.is_primary = True acc.save() return acc
def post(self, request, **kwargs): dw = DwollaAPI() try: transfer_response = dw.transfer_from_balance_to_check_acc( request.user ) except Exception as e: return Response(e, status=400) result_response = {} result_response.update(transfer_response['message']['clearing']) result_response.update( {"amount": transfer_response['message']['amount']}) result_response.update( {"status": transfer_response['message']['status']}) return Response(result_response, 200)
def delete(self, request, **kwargs): source = FundingSource.objects\ .get(item__plaid_id=request.data.get("item_id")) dwa = DwollaAPI() request_body = { "removed": true } dwa.app_token.post(source.funding_sources_url, request_body) return Response(status=204)
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 charge_application(amount, user): """ Try to get funding source make tranfer via DwollaApi """ from finance.services.dwolla_api import DwollaAPI try: funding_info = get_funding_source(user, amount) except Exception as error: raise Exception(error) dw = DwollaAPI() transfer_url = dw.transfer_to_customer_dwolla_balance( funding_info['funding_soure'], funding_info['dwolla_balance'], amount) TransferBalance = apps.get_model('finance', 'TransferBalance') fees = dw.app_token.get(transfer_url) TransferBalance.objects.create_transfer_balance( funding_info['funding_instance'], funding_info['account'], fees.body, 'account --> dwolla_balance')
def handle(self, *args, **kwargs): dwa = DwollaAPI() webhook_subscription = dwa.app_token.get('webhook-subscriptions') print(webhook_subscription.body) if webhook_subscription.body['total'] > 0: print("Webhook already exists, exiting") else: request_body = { 'url': 'http://api.donkies.co/v1/dwolla_webhook', 'secret': 'secret' } subscript = dwa.app_token.post('webhook-subscriptions', request_body) print(subscript) print("Created webhook on url")
def create_webhook_context(data): dwa = DwollaAPI() value = {} Customer = apps.get_model("bank", "Customer") topic = data.get('topic') if (topic in [ 'customer_created', 'customer_verified', 'customer_suspended', 'customer_activated', 'customer_deactivated' ]): customer_url = data['_links']['customer']['href']\ .replace('sandbox', 'uat') customer_id = dwa.app_token.get(customer_url).body['id'] customer = Customer.objects.get(dwolla_id=customer_id) email = customer.email value.update({ "email": email, "customer": customer, "name": customer.user.get_full_name() }) elif (topic in [ "customer_funding_source_added", "customer_funding_source_removed", "customer_funding_source_verified" ]): source_url = dwa.get_balance_funding_source(data['resourceId']) source = dwa.app_token.get(source_url).body customer_url = data['_links']['customer']['href']\ .replace('sandbox', 'uat') customer_id = dwa.app_token.get(customer_url).body['id'] customer = Customer.objects.get(dwolla_id=customer_id) email = customer.email value.update({ "bankName": source['bankName'], "bankAccountId": data['resourceId'], "account_identifier": source['name'], "_date": source['created'], "email": email, "customer": customer, "name": customer.user.get_full_name() }) elif (topic in [ "customer_bank_transfer_created", "customer_bank_transfer_cancelled", "customer_bank_transfer_failed", "customer_bank_transfer_completed" ]): transfer_url = dwa.get_transfer_url(data['resourceId']) transfer = dwa.app_token.get(transfer_url).body customer_url = (data['_links']['customer']['href'].replace( 'sandbox', 'uat')) customer_id = dwa.app_token.get(customer_url).body['id'] email = Customer.objects.get(dwolla_id=customer_id).email value.update({ 'transfer_type': transfer['status'], "bankAccountId": data['resourceId'], 'amount_currency': transfer['amount']['currency'], 'amount_value': transfer['amount']['value'], 'transfer_date': transfer['created'], 'destination': (transfer['_links']['destination']['href'].replace( 'sandbox', 'uat')), "email": email, "customer": customer, "name": customer.user.get_full_name() }) elif (topic in [ "customer_transfer_created", "customer_transfer_cancelled", "customer_transfer_failed", "customer_transfer_completed" ]): transfer_url = dwa.get_transfer_url(data['resourceId']) transfer = dwa.app_token.get(transfer_url).body customer_url = (data['_links']['customer']['href'].replace( 'sandbox', 'uat')) customer_id = dwa.app_token.get(customer_url).body['id'] customer = Customer.objects.get(dwolla_id=customer_id) email = customer.email value.update({ 'transfer_type': transfer['status'], 'amount_currency': transfer['amount']['currency'], "bankAccountId": data['resourceId'], 'amount_value': transfer['amount']['value'], 'transfer_date': transfer['created'], 'destination': transfer['_links']['destination']['href'], "email": email, "customer": customer, "name": customer.user.get_full_name() }) return value