From e942f1a76e7211293ccf33729486a1565eabfbd1 Mon Sep 17 00:00:00 2001 From: DJ Gillespie Date: Wed, 6 Dec 2023 21:04:07 -0700 Subject: [PATCH] finished up new link flow updates. Still need to implement secondary get_auth_token flow. --- connection/connections/plaid_client_v2.py | 21 ++++++++++++++++----- connection/views.py | 16 ++++++++-------- requirements.txt | 4 ++-- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/connection/connections/plaid_client_v2.py b/connection/connections/plaid_client_v2.py index f692c39..1a037d7 100755 --- a/connection/connections/plaid_client_v2.py +++ b/connection/connections/plaid_client_v2.py @@ -3,6 +3,8 @@ from django.conf import settings import os import datetime +import plaid +from plaid.api import plaid_api from plaid.model.link_token_create_request import LinkTokenCreateRequest from plaid.model.link_token_create_request_user import LinkTokenCreateRequestUser from plaid.model.products import Products @@ -20,7 +22,7 @@ def format_error(e): class Connection(AbstractConnectionClient): - def __init__(self, credentials): + def __init__(self, credentials, account_id=None): self.credentials = credentials.dict() # Fill in your Plaid API keys - @@ -28,7 +30,7 @@ class Connection(AbstractConnectionClient): self.PLAID_CLIENT_ID = settings.PLAID_CLIENT_ID self.PLAID_SECRET = settings.PLAID_SECRET self.PLAID_PUBLIC_KEY = settings.PLAID_PUBLIC_KEY - # Use 'sandbox' to test with Plaid's Sandbox environment (username: user_good, + # Use 'sandbox' to test with Plaid's Sandbox environment (usplaid-python==9.2.0ername: user_good, # password: pass_good) # Use `development` to test with live users and credentials and `production` # to go live @@ -42,17 +44,26 @@ class Connection(AbstractConnectionClient): # will be able to select institutions from. self.PLAID_COUNTRY_CODES = settings.PLAID_COUNTRY_CODES - client_user_id = user.id + configuration = plaid.Configuration( + host=self.PLAID_ENV, + api_key={ + 'clientId': self.PLAID_CLIENT_ID, + 'secret': self.PLAID_SECRET, + } + ) + api_client = plaid.ApiClient(configuration) + client = plaid_api.PlaidApi(api_client) + # Create a link_token for the given user request = LinkTokenCreateRequest( products=[Products("auth")], client_name="Qrtr Plaid", country_codes=[CountryCode('US')], - redirect_uri='https://domainname.com/oauth-page.html', + #redirect_uri='https://domainname.com/oauth-page.html', language='en', webhook='https://webhook.example.com', user=LinkTokenCreateRequestUser( - client_user_id=client_user_id + client_user_id=account_id ) ) response = client.link_token_create(request) diff --git a/connection/views.py b/connection/views.py index 147e067..83a19d0 100644 --- a/connection/views.py +++ b/connection/views.py @@ -37,14 +37,14 @@ class ConnectionViewSet(viewsets.ModelViewSet): def authenticate(self, request): print(request.data) print(request.data.keys()) - public_token = request.data.get("public_token") + # public_token = request.data.get("public_token") name = request.data.get("name", "dummyName") account_id = request.data.get("account") print(f"Account ID Detected: {account_id}") - if public_token is None: - return Response( - status=status.HTTP_400_BAD_REQUEST, - data="ERROR: missing public_token") + # if public_token is None: + # return Response( + # status=status.HTTP_400_BAD_REQUEST, + # data="ERROR: missing public_token") if account_id is None: return Response( status=status.HTTP_400_BAD_REQUEST, @@ -63,10 +63,10 @@ class ConnectionViewSet(viewsets.ModelViewSet): print(f"Account Found: {accounts[0]}") account = accounts[0] print(request) - plaid_conn = importlib.import_module(f"connection.connections.plaid_client") + plaid_conn = importlib.import_module(f"connection.connections.plaid_client_v2") conn_type = ConnectionType.objects.get(name="Plaid") try: - plaid_client = plaid_conn.Connection(request.data) + plaid_client = plaid_conn.Connection(request.data, account_id=account_id) except ValueError: return Response(status=status.HTTP_503, data="ERROR: Invalid public_token") @@ -83,7 +83,7 @@ class ConnectionViewSet(viewsets.ModelViewSet): }) conn.credentials = plaid_client.credentials conn.save() - return Response(plaid_client.get_accounts()) + return Response(plaid_client.credentials) @action(detail=False, methods=['post'], url_path='plaid-webhook', permission_classes=[AllowAny]) diff --git a/requirements.txt b/requirements.txt index a438490..a7fea57 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,8 +16,8 @@ djangorestframework-simplejwt==4.6.0 drf-yasg==1.20.0 idna==2.10 oauthlib==3.1.0 -plaid-python==9.2.0 -psycopg2==2.8.6 +plaid_python==14.0.0 +psycopg2-binary==2.8.6 pycparser==2.20 PyJWT==2.1.0 python3-openid==3.2.0