Class: PlaidRails::LinkController

Inherits:
ApplicationController show all
Defined in:
app/controllers/plaid_rails/link_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticateObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/plaid_rails/link_controller.rb', line 6

def authenticate
  begin
    @exchange_token = Plaid::User.exchange_token(link_params[:public_token])

    @params = link_params.merge!(token: link_params[:public_token])
    
  rescue => e
    Rails.logger.error "Error: #{e}"
    Rails.logger.error e.backtrace.join("\n")
    render text: e.message, status: 500
  end
end

#updateObject

updates the access token after changing password with institution



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/plaid_rails/link_controller.rb', line 20

def update
  begin
    exchange_token = Plaid::User.exchange_token(link_params[:public_token])
    
    # find old access_token
    old_access_token = PlaidRails::.find_by(owner_type: link_params[:owner_type],
    owner_id: link_params[:owner_id],number: link_params[:number]).access_token
    
    # find all plaid_accounts with old access_token 
    accounts = PlaidRails::.where(owner_type: link_params[:owner_type],
    owner_id: link_params[:owner_id], access_token: old_access_token)
  
    # update found accounts with new token.
    accounts.update_all(access_token: exchange_token.access_token, 
        transactions_start_date: Date.today)

    # get all accounts to display back to user.
    @plaid_accounts = PlaidRails::.where(owner_type: link_params[:owner_type],
    owner_id: link_params[:owner_id])
  
    flash[:success]="You have successfully updated your account(s)"
  rescue => e
    Rails.logger.error "Error: #{e}"
    render text: e.message, status: 500
  end
end