[QRTR-77] Updated plaid client to properly authenticate with given token. (repeat b/c I forgot to add the plaid_client.py file)
This commit is contained in:
parent
24148bbaf4
commit
c39b9cd24a
84
connection/connections/plaid_client.py
Executable file
84
connection/connections/plaid_client.py
Executable file
@ -0,0 +1,84 @@
|
||||
from .abstract import AbstractConnectionClient
|
||||
from django.conf import settings
|
||||
import plaid
|
||||
import os
|
||||
import datetime
|
||||
|
||||
|
||||
def format_error(e):
|
||||
return {
|
||||
'error': {
|
||||
'display_message': e.display_message,
|
||||
'error_code': e.code,
|
||||
'error_type': e.type,
|
||||
'error_message': e.message}}
|
||||
|
||||
|
||||
class Connection(AbstractConnectionClient):
|
||||
|
||||
def __init__(self, credentials):
|
||||
print("Plaid Connection Creation Initiated")
|
||||
self.credentials = credentials
|
||||
|
||||
# Fill in your Plaid API keys -
|
||||
# https://dashboard.plaid.com/account/keys
|
||||
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,
|
||||
# password: pass_good)
|
||||
# Use `development` to test with live users and credentials and `production`
|
||||
# to go live
|
||||
self.PLAID_ENV = settings.PLAID_ENV
|
||||
# PLAID_PRODUCTS is a comma-separated list of products to use when initializing
|
||||
# Link. Note that this list must contain 'assets' in order for the app to be
|
||||
# able to create and retrieve asset reports.
|
||||
self.PLAID_PRODUCTS = settings.PLAID_PRODUCTS
|
||||
|
||||
# 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,
|
||||
public_key=self.PLAID_PUBLIC_KEY,
|
||||
environment=self.PLAID_ENV,
|
||||
api_version='2019-05-29')
|
||||
print("Getting Public Key")
|
||||
public_key = self.credentials.get('public_key')
|
||||
print("Retrieving auth token")
|
||||
if not self.credentials.get('auth_token') and public_key:
|
||||
self.credentials['auth_token'] = self.get_auth_token(public_key)
|
||||
print("Plaid Connection successful")
|
||||
|
||||
def get_auth_token(self, public_token):
|
||||
try:
|
||||
exchange_response = self.client.Item.public_token.exchange(
|
||||
public_token)
|
||||
except Exception as e:
|
||||
print("Error Occurred")
|
||||
print(e)
|
||||
return format_error(e)
|
||||
access_token = exchange_response['access_token']
|
||||
return access_token
|
||||
|
||||
def get_transactions(
|
||||
self,
|
||||
start_date=None,
|
||||
end_date=None,
|
||||
auth_token=None):
|
||||
if not auth_token:
|
||||
auth_token = self.credentials.get('auth_token')
|
||||
if not auth_token:
|
||||
raise Exception("Missing Auth Token")
|
||||
if not start_date:
|
||||
start_date = '{:%Y-%m-%d}'.format(
|
||||
datetime.datetime.now() + datetime.timedelta(-30))
|
||||
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)
|
||||
except plaid.errors.PlaidError as e:
|
||||
return format_error(e)
|
||||
return transactions_resp
|
||||
Loading…
Reference in New Issue
Block a user