plaid_rails 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +2 -2
- data/app/assets/javascripts/plaid_rails/link.js +56 -31
- data/app/controllers/plaid_rails/accounts_controller.rb +1 -1
- data/app/controllers/plaid_rails/link_controller.rb +17 -2
- data/app/services/plaid_rails/create_account_service.rb +1 -1
- data/app/views/plaid_rails/link/_auth.html.erb +1 -0
- data/app/views/plaid_rails/link/update.js.erb +5 -0
- data/config/routes.rb +1 -1
- data/db/migrate/20160215155024_create_plaid_rails_accounts.rb +2 -0
- data/lib/generators/plaid_rails/install_generator.rb +18 -0
- data/lib/generators/plaid_rails/templates/initializer.rb +29 -0
- data/lib/plaid_rails/version.rb +1 -1
- data/spec/controllers/plaid_rails/accounts_controller_spec.rb +2 -1
- data/spec/controllers/plaid_rails/link_controller_spec.rb +12 -1
- data/spec/dummy/db/schema.rb +1 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +2105 -0
- data/spec/services/plaid_rails/create_account_service_spec.rb +2 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d197c9a37166ed867afe4fcd976b52ca6eee6c1b
|
4
|
+
data.tar.gz: ef42e083ed29bb7cbc7b4e4a182c7a3b74c9b409
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa5f7468171b9af012523da2f10eaddacdbaf37b0c82ee71ffd9c7845cb9cb7becd5eaa667d9a648f64630a3c90b626a0699a409b59011d3aa692fe6cd6d0eac
|
7
|
+
data.tar.gz: 36bec95361228c31eee08cccfabde4fe1314073e0ee1b53e3cf3e305daf12fc74be143df9dca0f071e9763c0d4352bdd53f56d7a42de344684bf040d48f960ae
|
data/Rakefile
CHANGED
@@ -25,5 +25,5 @@ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
|
|
25
25
|
require 'rspec/core'
|
26
26
|
require 'rspec/core/rake_task'
|
27
27
|
desc "Run all specs in spec directory (excluding plugin specs)"
|
28
|
-
RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
|
29
|
-
task :default => :spec
|
28
|
+
RSpec::Core::RakeTask.new #(:spec => 'app:db:test:prepare')
|
29
|
+
task :default => :spec
|
@@ -1,36 +1,61 @@
|
|
1
|
-
|
2
|
-
var
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
},
|
11
|
-
onSuccess: function (public_token, metadata) {
|
12
|
-
// Send the public_token to your app server here.
|
13
|
-
// The metadata object contains info about the institution the
|
14
|
-
// user selected and the account ID, if selectAccount is enabled.
|
15
|
-
$.ajax({
|
16
|
-
type: 'POST',
|
17
|
-
dataType: 'script',
|
18
|
-
url: '/plaid/authenticate',
|
19
|
-
data: {
|
20
|
-
public_token: public_token,
|
21
|
-
name: metadata.institution.name,
|
22
|
-
type: metadata.institution.type,
|
23
|
-
owner_type: plaidData.data('owner-type'),
|
24
|
-
owner_id: plaidData.data('owner-id')
|
25
|
-
}
|
26
|
-
});
|
27
|
-
},
|
28
|
-
onExit: function () {
|
29
|
-
// The user exited the Link flow.
|
1
|
+
function getPlaid(plaidData) {
|
2
|
+
var url = null;
|
3
|
+
var token = plaidData.data('token');
|
4
|
+
var env = plaidData.data('env');
|
5
|
+
if (typeof token === 'undefined') {
|
6
|
+
url = '/plaid/authenticate';
|
7
|
+
token = null;
|
8
|
+
} else {
|
9
|
+
url = '/plaid/update';
|
30
10
|
}
|
31
|
-
|
11
|
+
// set token for test environment
|
12
|
+
if (env === 'tartan' && typeof plaidData.data('type') !== 'undefined') {
|
13
|
+
token = 'test,' + plaidData.data('type') + ',connected'
|
14
|
+
}
|
15
|
+
|
16
|
+
var linkHandler = Plaid.create({
|
17
|
+
env: env,
|
18
|
+
clientName: plaidData.data('client-name'),
|
19
|
+
key: plaidData.data('key'),
|
20
|
+
product: 'connect',
|
21
|
+
webhook: plaidData.data('webhook'),
|
22
|
+
onLoad: function () {
|
23
|
+
// The Link module finished loading.
|
24
|
+
},
|
25
|
+
onSuccess: function (public_token, metadata) {
|
26
|
+
// Send the public_token to your app server here.
|
27
|
+
// The metadata object contains info about the institution the
|
28
|
+
// user selected and the account ID, if selectAccount is enabled.
|
29
|
+
$.ajax({
|
30
|
+
type: 'POST',
|
31
|
+
dataType: 'script',
|
32
|
+
url: url,
|
33
|
+
data: {
|
34
|
+
public_token: public_token,
|
35
|
+
name: metadata.institution.name,
|
36
|
+
type: metadata.institution.type,
|
37
|
+
owner_type: plaidData.data('owner-type'),
|
38
|
+
owner_id: plaidData.data('owner-id')
|
39
|
+
}
|
40
|
+
});
|
41
|
+
},
|
42
|
+
onExit: function () {
|
43
|
+
// The user exited the Link flow.
|
44
|
+
}
|
45
|
+
});
|
46
|
+
return linkHandler;
|
47
|
+
}
|
32
48
|
// Trigger the authentication view
|
33
49
|
$(document).on("click", '#plaidLinkButton', function () {
|
34
|
-
|
50
|
+
var plaidData = $(this);
|
51
|
+
linkHandler = getPlaid(plaidData);
|
52
|
+
var plaidType = plaidData.data('type')
|
53
|
+
//open handler for the institution
|
54
|
+
if (typeof plaidType === 'undefined') {
|
55
|
+
linkHandler.open();
|
56
|
+
} else {
|
57
|
+
linkHandler.open(plaidType);
|
58
|
+
}
|
59
|
+
|
35
60
|
});
|
36
61
|
|
@@ -33,7 +33,7 @@ module PlaidRails
|
|
33
33
|
|
34
34
|
# Never trust parameters from the scary internet, only allow the white list through.
|
35
35
|
def account_params
|
36
|
-
params.require(:account).permit(:access_token, :type,:name,:owner_id,:owner_type,account_ids:[])
|
36
|
+
params.require(:account).permit(:token,:access_token, :type,:name,:owner_id,:owner_type,account_ids:[])
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -7,7 +7,7 @@ module PlaidRails
|
|
7
7
|
begin
|
8
8
|
@exchange_token = Plaid.exchange_token(link_params[:public_token])
|
9
9
|
|
10
|
-
@params = link_params
|
10
|
+
@params = link_params.merge!(token: link_params[:public_token])
|
11
11
|
|
12
12
|
rescue => e
|
13
13
|
Rails.logger.error "Error: #{e}"
|
@@ -16,10 +16,25 @@ module PlaidRails
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
+
def update
|
20
|
+
begin
|
21
|
+
exchange_token = Plaid.exchange_token(link_params[:public_token])
|
22
|
+
|
23
|
+
@accounts =PlaidRails::Account.where(owner_id: link_params[:owner_id])
|
24
|
+
@accounts.each do |account|
|
25
|
+
account.update(access_token: exchange_token.access_token)
|
26
|
+
end
|
27
|
+
flash[:success]="You successfully updated your account(s)"
|
28
|
+
rescue => e
|
29
|
+
Rails.logger.error "Error: #{e}"
|
30
|
+
Rails.logger.error e.backtrace.join("\n")
|
31
|
+
render text: e.message, status: 500
|
32
|
+
end
|
33
|
+
end
|
19
34
|
private
|
20
35
|
# Never trust parameters from the scary internet, only allow the white list through.
|
21
36
|
def link_params
|
22
|
-
params.permit(:public_token, :type,:name,:owner_id,:owner_type)
|
37
|
+
params.permit(:access_token, :public_token, :type,:name,:owner_id,:owner_type)
|
23
38
|
end
|
24
39
|
end
|
25
40
|
end
|
@@ -8,9 +8,9 @@ module PlaidRails
|
|
8
8
|
user = Plaid.set_user(account_params["access_token"],['auth'])
|
9
9
|
#find the account by account_id
|
10
10
|
account = user.accounts.find{|a| a.id==id}
|
11
|
-
|
12
11
|
PlaidRails::Account.create!(
|
13
12
|
access_token: account_params["access_token"],
|
13
|
+
token: account_params["token"],
|
14
14
|
plaid_type: account_params["type"],
|
15
15
|
name: account.name,
|
16
16
|
bank_name: account.meta["name"],
|
data/config/routes.rb
CHANGED
@@ -2,6 +2,7 @@ class CreatePlaidRailsAccounts < ActiveRecord::Migration
|
|
2
2
|
def change
|
3
3
|
create_table :plaid_rails_accounts do |t|
|
4
4
|
t.string :access_token
|
5
|
+
t.string :token
|
5
6
|
t.string :plaid_type
|
6
7
|
t.string :name
|
7
8
|
t.string :bank_name
|
@@ -13,6 +14,7 @@ class CreatePlaidRailsAccounts < ActiveRecord::Migration
|
|
13
14
|
t.decimal :current_balance, :precision => 10, :scale => 2
|
14
15
|
t.decimal :available_balance, :precision => 10, :scale => 2
|
15
16
|
t.string :error
|
17
|
+
|
16
18
|
t.timestamps
|
17
19
|
end
|
18
20
|
add_index :plaid_rails_accounts, :access_token
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module PlaidRails
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
def install_initializer
|
6
|
+
initializer 'plaid.rb', File.read(File.expand_path('../templates/initializer.rb', __FILE__))
|
7
|
+
end
|
8
|
+
def install_js
|
9
|
+
inject_into_file 'app/assets/javascripts/application.js', after: "//= require jquery\n" do <<-'JS'
|
10
|
+
//= require plaid_rails
|
11
|
+
JS
|
12
|
+
end
|
13
|
+
end
|
14
|
+
def install_route
|
15
|
+
route "mount PlaidRails::Engine => '/plaid', as: :plaid_rails"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
Plaid.config do |p|
|
2
|
+
Plaid.customer_id = Rails.application.secrets[:plaid][:customer_id]
|
3
|
+
Plaid.secret = Rails.application.secrets[:plaid][:secret]
|
4
|
+
Plaid.environment_location = Rails.env.production? ? 'https://api.plaid.com/' : 'https://tartan.plaid.com/'
|
5
|
+
end
|
6
|
+
PlaidRails.configure do |config|
|
7
|
+
config.public_key = Rails.application.secrets[:plaid][:public_key]
|
8
|
+
config.long_tail = true
|
9
|
+
config.webhook = 'https://my.app.com/plaid/webhooks'
|
10
|
+
|
11
|
+
# https://plaid.com/docs/#webhook
|
12
|
+
#subscribe to plaid webhooks
|
13
|
+
config.all do |event|
|
14
|
+
Rails.logger.debug "Plaid Webhook: #{event.inspect}"
|
15
|
+
end
|
16
|
+
|
17
|
+
config.subscribe "transactions.initial" do |event|
|
18
|
+
Rails.logger.debug "transactions.initial #{event.inspect}"
|
19
|
+
# do something with intial transactions
|
20
|
+
end
|
21
|
+
config.subscribe "transactions.new" do |event|
|
22
|
+
Rails.logger.debug "transactions.new #{event.inspect}"
|
23
|
+
# do something with the new transactions
|
24
|
+
end
|
25
|
+
config.subscribe "transactions.interval" do |event|
|
26
|
+
Rails.logger.debug "transactions.initial #{event.inspect}"
|
27
|
+
# do something with the new transactions
|
28
|
+
end
|
29
|
+
end
|
data/lib/plaid_rails/version.rb
CHANGED
@@ -24,7 +24,8 @@ module PlaidRails
|
|
24
24
|
it "can create" do
|
25
25
|
accounts = user.accounts.map{|a| a.id}
|
26
26
|
post :create, account: {access_token: 'test_wells', account_ids: accounts,
|
27
|
-
name:'Wells Fargo', type: 'wells', owner_id: "1", owner_type: "User"
|
27
|
+
name:'Wells Fargo', type: 'wells', owner_id: "1", owner_type: "User",
|
28
|
+
token: public_token}
|
28
29
|
expect(response).to be_success
|
29
30
|
expect(assigns(:accounts).size).to eq 4
|
30
31
|
end
|
@@ -11,7 +11,7 @@ module PlaidRails
|
|
11
11
|
expect(response).to render_template('plaid_rails/link/authenticate')
|
12
12
|
end
|
13
13
|
|
14
|
-
it "can't
|
14
|
+
it "can't authenticate with bad public token" do
|
15
15
|
xhr :post, :authenticate, public_token: 'badtoken', name:'Wells Fargo', type: 'wells',
|
16
16
|
owner_id: "1", owner_type: "User"
|
17
17
|
expect(response).to_not be_success
|
@@ -19,9 +19,20 @@ module PlaidRails
|
|
19
19
|
expect(response.body).to eq "unauthorized product"
|
20
20
|
end
|
21
21
|
|
22
|
+
it "update with public token" do
|
23
|
+
account = create(:account)
|
24
|
+
xhr :post, :update, public_token: 'test,wells,connected', name:'Wells Fargo', type: 'wells',
|
25
|
+
owner_id: "1", owner_type: "User"
|
26
|
+
expect(response).to be_success
|
27
|
+
expect(assigns(:accounts)).to_not be_nil
|
28
|
+
expect(response).to render_template('plaid_rails/link/update')
|
29
|
+
end
|
22
30
|
it {
|
23
31
|
should permit(:public_token, :type,:name,:owner_id,:owner_type).
|
24
32
|
for(:authenticate, verb: :post, format: :js)}
|
33
|
+
it {
|
34
|
+
should permit(:access_token,:public_token, :type,:name,:owner_id,:owner_type).
|
35
|
+
for(:update, verb: :post, format: :js)}
|
25
36
|
|
26
37
|
end
|
27
38
|
end
|
data/spec/dummy/db/schema.rb
CHANGED
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|