Merge branch 'dev-master'
This commit is contained in:
commit
8638a607dd
@ -1,6 +1,10 @@
|
||||
from .abstract import AbstractConnectionClient
|
||||
from django.conf import settings
|
||||
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 datetime
|
||||
|
||||
@ -17,8 +21,7 @@ def format_error(e):
|
||||
class Connection(AbstractConnectionClient):
|
||||
|
||||
def __init__(self, credentials):
|
||||
print("Plaid Connection Creation Initiated")
|
||||
self.credentials = credentials
|
||||
self.credentials = credentials.dict()
|
||||
|
||||
# Fill in your Plaid API 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
|
||||
# will be able to select institutions from.
|
||||
self.PLAID_COUNTRY_CODES = settings.PLAID_COUNTRY_CODES
|
||||
self.client = plaid.Client(
|
||||
client_id=self.PLAID_CLIENT_ID,
|
||||
secret=self.PLAID_SECRET,
|
||||
environment=self.PLAID_ENV,
|
||||
# api_version='2019-05-29',
|
||||
api_version='2020-09-14',
|
||||
#webhook='https://qrtr-services.herokuapp.com/connection/plaid-webhook/'
|
||||
)
|
||||
|
||||
configuration = plaid.Configuration(
|
||||
host=self.PLAID_ENV,
|
||||
api_key={
|
||||
'clientId': self.PLAID_CLIENT_ID,
|
||||
'secret': self.PLAID_SECRET,
|
||||
}
|
||||
)
|
||||
api_client = plaid.ApiClient(configuration)
|
||||
self.client = plaid_api.PlaidApi(api_client)
|
||||
public_key = self.credentials.get('public_token')
|
||||
auth_token = self.credentials.get('auth_token')
|
||||
if not auth_token and public_key:
|
||||
@ -64,8 +69,11 @@ class Connection(AbstractConnectionClient):
|
||||
|
||||
def get_auth_token(self, public_token):
|
||||
try:
|
||||
exchange_response = self.client.Item.public_token.exchange(
|
||||
public_token)
|
||||
exchange_request = ItemPublicTokenExchangeRequest(
|
||||
public_token=public_token
|
||||
)
|
||||
exchange_response = self.client.item_public_token_exchange(
|
||||
exchange_request)
|
||||
except Exception as e:
|
||||
print("Error Occurred")
|
||||
print(e)
|
||||
@ -80,7 +88,8 @@ class Connection(AbstractConnectionClient):
|
||||
if not auth_token:
|
||||
raise Exception("Missing Auth Token")
|
||||
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:
|
||||
print(e)
|
||||
accounts = None
|
||||
@ -101,8 +110,13 @@ class Connection(AbstractConnectionClient):
|
||||
if not end_date:
|
||||
end_date = '{:%Y-%m-%d}'.format(datetime.datetime.now())
|
||||
try:
|
||||
transactions_resp = self.client.Transactions.get(
|
||||
auth_token, start_date, end_date)
|
||||
transactions_req = TransactionsGetRequest(
|
||||
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:
|
||||
return format_error(e)
|
||||
return transactions_resp.get("transactions")
|
||||
|
||||
@ -63,10 +63,10 @@ class ConnectionViewSet(viewsets.ModelViewSet):
|
||||
print(f"Account Found: {accounts[0]}")
|
||||
account = accounts[0]
|
||||
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")
|
||||
try:
|
||||
plaid_client = plaid.Connection(request.data)
|
||||
plaid_client = plaid_conn.Connection(request.data)
|
||||
except ValueError:
|
||||
return Response(status=status.HTTP_503,
|
||||
data="ERROR: Invalid public_token")
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import os
|
||||
import dj_database_url
|
||||
import plaid
|
||||
|
||||
|
||||
DEFAULT_CONNECTION = dj_database_url.parse(os.environ.get("DATABASE_URL"))
|
||||
@ -17,8 +18,12 @@ DEBUG = True
|
||||
|
||||
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_ENV = os.environ.get("PLAID_ENV")
|
||||
PLAID_PUBLIC_KEY = os.environ.get("PLAID_PUBLIC_KEY")
|
||||
PLAID_SECRET = os.environ.get("PLAID_SECRET")
|
||||
PLAID_CLIENT_ID = os.environ.get("PLAID_CLIENT_ID")
|
||||
|
||||
@ -16,6 +16,7 @@ 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
|
||||
pycparser==2.20
|
||||
PyJWT==2.1.0
|
||||
|
||||
Loading…
Reference in New Issue
Block a user