Merge branch 'dev-master'

This commit is contained in:
DJ Gillespie 2022-04-07 17:22:40 -06:00
commit 8638a607dd
4 changed files with 38 additions and 18 deletions

View File

@ -1,6 +1,10 @@
from .abstract import AbstractConnectionClient from .abstract import AbstractConnectionClient
from django.conf import settings from django.conf import settings
import plaid import plaid
from plaid.api import plaid_api
from plaid.model.item_public_token_exchange_request import ItemPublicTokenExchangeRequest
from plaid.model.transactions_get_request import TransactionsGetRequest
from plaid.model.accounts_get_request import AccountsGetRequest
import os import os
import datetime import datetime
@ -17,8 +21,7 @@ def format_error(e):
class Connection(AbstractConnectionClient): class Connection(AbstractConnectionClient):
def __init__(self, credentials): def __init__(self, credentials):
print("Plaid Connection Creation Initiated") self.credentials = credentials.dict()
self.credentials = credentials
# Fill in your Plaid API keys - # Fill in your Plaid API keys -
# https://dashboard.plaid.com/account/keys # https://dashboard.plaid.com/account/keys
@ -38,14 +41,16 @@ class Connection(AbstractConnectionClient):
# PLAID_COUNTRY_CODES is a comma-separated list of countries for which users # PLAID_COUNTRY_CODES is a comma-separated list of countries for which users
# will be able to select institutions from. # will be able to select institutions from.
self.PLAID_COUNTRY_CODES = settings.PLAID_COUNTRY_CODES self.PLAID_COUNTRY_CODES = settings.PLAID_COUNTRY_CODES
self.client = plaid.Client(
client_id=self.PLAID_CLIENT_ID, configuration = plaid.Configuration(
secret=self.PLAID_SECRET, host=self.PLAID_ENV,
environment=self.PLAID_ENV, api_key={
# api_version='2019-05-29', 'clientId': self.PLAID_CLIENT_ID,
api_version='2020-09-14', 'secret': self.PLAID_SECRET,
#webhook='https://qrtr-services.herokuapp.com/connection/plaid-webhook/' }
) )
api_client = plaid.ApiClient(configuration)
self.client = plaid_api.PlaidApi(api_client)
public_key = self.credentials.get('public_token') public_key = self.credentials.get('public_token')
auth_token = self.credentials.get('auth_token') auth_token = self.credentials.get('auth_token')
if not auth_token and public_key: if not auth_token and public_key:
@ -64,8 +69,11 @@ class Connection(AbstractConnectionClient):
def get_auth_token(self, public_token): def get_auth_token(self, public_token):
try: try:
exchange_response = self.client.Item.public_token.exchange( exchange_request = ItemPublicTokenExchangeRequest(
public_token) public_token=public_token
)
exchange_response = self.client.item_public_token_exchange(
exchange_request)
except Exception as e: except Exception as e:
print("Error Occurred") print("Error Occurred")
print(e) print(e)
@ -80,7 +88,8 @@ class Connection(AbstractConnectionClient):
if not auth_token: if not auth_token:
raise Exception("Missing Auth Token") raise Exception("Missing Auth Token")
try: try:
accounts = self.client.Accounts.get(auth_token) acc_request = AccountsGetRequest(access_token=auth_token)
accounts = self.client.accounts_get(acc_request).to_dict()
except Exception as e: except Exception as e:
print(e) print(e)
accounts = None accounts = None
@ -101,8 +110,13 @@ class Connection(AbstractConnectionClient):
if not end_date: if not end_date:
end_date = '{:%Y-%m-%d}'.format(datetime.datetime.now()) end_date = '{:%Y-%m-%d}'.format(datetime.datetime.now())
try: try:
transactions_resp = self.client.Transactions.get( transactions_req = TransactionsGetRequest(
auth_token, start_date, end_date) access_token=auth_token,
start_date=start_date,
end_date=end_date
)
transactions_resp = self.client.transactions_get(
transactions_req)
except plaid.errors.PlaidError as e: except plaid.errors.PlaidError as e:
return format_error(e) return format_error(e)
return transactions_resp.get("transactions") return transactions_resp.get("transactions")

View File

@ -63,10 +63,10 @@ class ConnectionViewSet(viewsets.ModelViewSet):
print(f"Account Found: {accounts[0]}") print(f"Account Found: {accounts[0]}")
account = accounts[0] account = accounts[0]
print(request) print(request)
plaid = importlib.import_module(f"connection.connections.plaid_client") plaid_conn = importlib.import_module(f"connection.connections.plaid_client")
conn_type = ConnectionType.objects.get(name="Plaid") conn_type = ConnectionType.objects.get(name="Plaid")
try: try:
plaid_client = plaid.Connection(request.data) plaid_client = plaid_conn.Connection(request.data)
except ValueError: except ValueError:
return Response(status=status.HTTP_503, return Response(status=status.HTTP_503,
data="ERROR: Invalid public_token") data="ERROR: Invalid public_token")

View File

@ -1,5 +1,6 @@
import os import os
import dj_database_url import dj_database_url
import plaid
DEFAULT_CONNECTION = dj_database_url.parse(os.environ.get("DATABASE_URL")) DEFAULT_CONNECTION = dj_database_url.parse(os.environ.get("DATABASE_URL"))
@ -17,8 +18,12 @@ DEBUG = True
ALLOWED_HOSTS = ['*'] ALLOWED_HOSTS = ['*']
plaid_envs = {'sandbox': plaid.Environment.Sandbox,
'development': plaid.Environment.Development,
'production': plaid.Environment.Production
}
PLAID_ENV = plaid_envs.get(os.environ.get("PLAID_ENV"), plaid_envs['sandbox'])
PLAID_PRODUCTS = os.environ.get("PLAID_PRODUCTS") PLAID_PRODUCTS = os.environ.get("PLAID_PRODUCTS")
PLAID_ENV = os.environ.get("PLAID_ENV")
PLAID_PUBLIC_KEY = os.environ.get("PLAID_PUBLIC_KEY") PLAID_PUBLIC_KEY = os.environ.get("PLAID_PUBLIC_KEY")
PLAID_SECRET = os.environ.get("PLAID_SECRET") PLAID_SECRET = os.environ.get("PLAID_SECRET")
PLAID_CLIENT_ID = os.environ.get("PLAID_CLIENT_ID") PLAID_CLIENT_ID = os.environ.get("PLAID_CLIENT_ID")

View File

@ -16,6 +16,7 @@ djangorestframework-simplejwt==4.6.0
drf-yasg==1.20.0 drf-yasg==1.20.0
idna==2.10 idna==2.10
oauthlib==3.1.0 oauthlib==3.1.0
plaid-python==9.2.0
psycopg2==2.8.6 psycopg2==2.8.6
pycparser==2.20 pycparser==2.20
PyJWT==2.1.0 PyJWT==2.1.0