· 9 years ago · Sep 16, 2016, 05:14 PM
1diff --git a/app/controllers/braintree_controller.rb b/app/controllers/braintree_controller.rb
2index 14f4adf..17ca5a7 100644
3--- a/app/controllers/braintree_controller.rb
4+++ b/app/controllers/braintree_controller.rb
5@@ -31,11 +31,12 @@ class BraintreeController < ApplicationController
6 account_id: account.id})
7 end
8
9- account.add_connection({:scope => token_params.credentials.instance_variable_get(:@scope),
10- :client_id => params[:merchantId],
11- :refresh_token => token_params.credentials.refresh_token,
12- :secret_key => token_params.credentials.access_token,
13- :publishable_key => ''})
14+ account.add_connection({ scope: token_params.credentials.instance_variable_get(:@scope),
15+ client_id: params[:merchantId],
16+ refresh_token: token_params.credentials.refresh_token,
17+ secret_key: token_params.credentials.access_token,
18+ publishable_key: '',
19+ type: 'BraintreeConnection' })
20
21 AccountSyncJob.perform_later(account)
22 redirect_to team_switch_path(public_key: account.public_key, mode: params[:mode], path: 'dashboard')
23diff --git a/app/controllers/stripe_controller.rb b/app/controllers/stripe_controller.rb
24index 3c59b30..930484d 100644
25--- a/app/controllers/stripe_controller.rb
26+++ b/app/controllers/stripe_controller.rb
27@@ -48,13 +48,12 @@ class StripeController < ApplicationController
28 end
29
30 # Add connections
31- account.add_connection({
32- scope: token_params[:scope],
33- client_id: token_params[:stripe_client_id],
34- refresh_token: token_params[:refresh_token],
35- secret_key: token_params[:access_token],
36- publishable_key: token_params[:stripe_publishable_key]
37- })
38+ account.add_connection({ scope: token_params[:scope],
39+ client_id: token_params[:stripe_client_id],
40+ refresh_token: token_params[:refresh_token],
41+ secret_key: token_params[:access_token],
42+ publishable_key: token_params[:stripe_publishable_key],
43+ type: 'StripeConnection' })
44
45 account.update({stripe_account_raw: stripe_account.to_json})
46
47diff --git a/app/models/account.rb b/app/models/account.rb
48index 7803ba8..36e7216 100644
49--- a/app/models/account.rb
50+++ b/app/models/account.rb
51@@ -7,7 +7,6 @@
52 # handle :string(255) not null
53 # installed :boolean default(FALSE), not null
54 # cnames :text
55-# stripe_account_id :string
56 # stripe_account_raw :text
57 # is_pro :boolean default(FALSE), not null
58 # transactions_viewed :integer default(0), not null
59@@ -36,14 +35,12 @@
60 # slack_bot_access_token :string
61 # synced :boolean default(FALSE)
62 # subscriptions_count :integer default(0), not null
63-# braintree_account_id :string
64 #
65 # Indexes
66 #
67-# index_accounts_on_handle (handle)
68-# index_accounts_on_id (id) UNIQUE
69-# index_accounts_on_public_key (public_key) UNIQUE
70-# index_accounts_on_stripe_account_id (stripe_account_id)
71+# index_accounts_on_handle (handle)
72+# index_accounts_on_id (id) UNIQUE
73+# index_accounts_on_public_key (public_key) UNIQUE
74 #
75
76 # I don't believe handle is used at the moment
77@@ -62,7 +59,7 @@ class Account < ActiveRecord::Base
78 has_many :cards, through: :customers # Don't know if this is necessary
79 has_many :charges, dependent: :destroy
80 has_many :refunds, through: :charges
81- has_one :connection, dependent: :destroy
82+ has_many :connections, dependent: :destroy
83 has_many :customers, dependent: :destroy
84 has_many :invoices, dependent: :destroy
85 has_many :disputes, dependent: :destroy
86@@ -430,31 +427,7 @@ class Account < ActiveRecord::Base
87 # Pull historic Stripe data into local DB (this is usually called by ActiveSyncJob.perform_later(self))
88 def sync
89 if self.connection.present? && !self.disconnected_at
90- if self.stripe?
91- begin
92- puts "Syncing Customers for account #{self.id}"
93- sync_customers
94- puts "Syncing Invoices for account #{self.id}"
95- sync_invoices
96- puts "Syncing Charges for account #{self.id}"
97- sync_charges
98- puts "Syncing Disputes for account #{self.id}"
99- sync_disputes
100- puts "Syncing Subscriptions for account #{self.id}"
101- sync_subscriptions
102- self.update_attribute(:synced, true)
103- # # rescue Exception => ex
104- # # self.update_attribute(:disconnected_reason, ex.to_s)
105- # self.touch(:disconnected_at)
106- # # raise ex
107- end
108- else
109- begin
110- puts "Syncing Customers for account #{self.id}"
111- BraintreeClient.sync_customers(self)
112- self.update_attribute(:synced, true)
113- end
114- end
115+ self.connections.map(&:sync)
116 end
117 end
118 # These are helpers to see sync progress...
119@@ -549,22 +522,6 @@ class Account < ActiveRecord::Base
120 self.add_or_update_charge(stripe_charge)
121 end
122
123- def provider
124- if self.braintree_account_id.present?
125- 'braintree'
126- elsif self.stripe_account_id.present?
127- 'stripe'
128- end
129- end
130-
131- def stripe?
132- provider == 'stripe'
133- end
134-
135- def braintree?
136- provider == 'braintree'
137- end
138-
139 protected
140 def slack
141 @slack ||= Slack.new(self.slack_bot_access_token)
142diff --git a/app/models/braintree_connection.rb b/app/models/braintree_connection.rb
143new file mode 100644
144index 0000000..af05be1
145--- /dev/null
146+++ b/app/models/braintree_connection.rb
147@@ -0,0 +1,39 @@
148+# == Schema Information
149+#
150+# Table name: connections
151+#
152+# id :integer not null, primary key
153+# status :string(8) not null
154+# public_key :string(255) not null
155+# scope :string(255) not null
156+# account_id :integer not null
157+# client_id :string(255) not null
158+# refresh_token :string(255) not null
159+# secret_key :string(255) not null
160+# publishable_key :string(255) not null
161+# created_at :datetime not null
162+# updated_at :datetime not null
163+# type :string
164+# active :string
165+# synced :string
166+# provider_account_id :string
167+#
168+# Indexes
169+#
170+# index_connections_on_account_id (account_id)
171+# index_connections_on_id_and_status (id,status)
172+# index_connections_on_status_and_account_id (status,account_id)
173+# index_connections_on_status_and_public_key (status,public_key)
174+#
175+# Foreign Keys
176+#
177+# fk_rails_b202e9a6e9 (account_id => accounts.id)
178+#
179+
180+class BraintreeConnection < Connection
181+ def sync
182+ puts "Syncing Customers for account #{current_account.id}"
183+ BraintreeClient.sync_customers(current_account)
184+ self.update_attribute(:synced, true)
185+ end
186+end
187diff --git a/app/models/connection.rb b/app/models/connection.rb
188index db6f97d..a791bd7 100644
189--- a/app/models/connection.rb
190+++ b/app/models/connection.rb
191@@ -2,17 +2,21 @@
192 #
193 # Table name: connections
194 #
195-# id :integer not null, primary key
196-# status :string(8) not null
197-# public_key :string(255) not null
198-# scope :string(255) not null
199-# account_id :integer not null
200-# client_id :string(255) not null
201-# refresh_token :string(255) not null
202-# secret_key :string(255) not null
203-# publishable_key :string(255) not null
204-# created_at :datetime not null
205-# updated_at :datetime not null
206+# id :integer not null, primary key
207+# status :string(8) not null
208+# public_key :string(255) not null
209+# scope :string(255) not null
210+# account_id :integer not null
211+# client_id :string(255) not null
212+# refresh_token :string(255) not null
213+# secret_key :string(255) not null
214+# publishable_key :string(255) not null
215+# created_at :datetime not null
216+# updated_at :datetime not null
217+# type :string
218+# active :string
219+# synced :string
220+# provider_account_id :string
221 #
222 # Indexes
223 #
224@@ -49,4 +53,12 @@ class Connection < ActiveRecord::Base
225 "Read/Write"
226 end
227 end
228+
229+ def provider
230+ if self.braintree_account_id.present?
231+ 'braintree'
232+ elsif self.stripe_account_id.present?
233+ 'stripe'
234+ end
235+ end
236 end
237diff --git a/app/models/customer.rb b/app/models/customer.rb
238index 48ce53d..2bf0651 100644
239--- a/app/models/customer.rb
240+++ b/app/models/customer.rb
241@@ -4,7 +4,6 @@
242 #
243 # id :integer not null, primary key
244 # public_key :string(255) not null
245-# stripe_customer_id :string
246 # email :string default("")
247 # account_id :integer not null
248 # deleted :boolean default(FALSE), not null
249@@ -22,14 +21,13 @@
250 # additional_billing_info :string
251 # receive_email_notifications :boolean default(FALSE)
252 # timezone :string
253-# braintree_customer_id :string
254+# type :string
255+# vendor_customer_id :string
256 #
257 # Indexes
258 #
259-# index_customers_on_account_id (account_id)
260-# index_customers_on_account_id_and_stripe_customer_id (account_id,stripe_customer_id) UNIQUE
261-# index_customers_on_public_key (public_key) UNIQUE
262-# index_customers_on_stripe_customer_id (stripe_customer_id)
263+# index_customers_on_account_id (account_id)
264+# index_customers_on_public_key (public_key) UNIQUE
265 #
266 # Foreign Keys
267 #
268@@ -48,6 +46,9 @@ class Customer < ActiveRecord::Base
269 dependent: :destroy
270 has_many :activities, dependent: :destroy
271
272+ # Uniqueness is still a requirement, but we enforce in the db.
273+ validates :vendor_customer_id, presence: true #, uniqueness: true
274+
275 scope :deleted, -> { where(deleted: true) }
276 scope :active, -> { where.not(deleted: true) }
277
278diff --git a/app/models/stripe_connection.rb b/app/models/stripe_connection.rb
279new file mode 100644
280index 0000000..932649d
281--- /dev/null
282+++ b/app/models/stripe_connection.rb
283@@ -0,0 +1,47 @@
284+# == Schema Information
285+#
286+# Table name: connections
287+#
288+# id :integer not null, primary key
289+# status :string(8) not null
290+# public_key :string(255) not null
291+# scope :string(255) not null
292+# account_id :integer not null
293+# client_id :string(255) not null
294+# refresh_token :string(255) not null
295+# secret_key :string(255) not null
296+# publishable_key :string(255) not null
297+# created_at :datetime not null
298+# updated_at :datetime not null
299+# type :string
300+# active :string
301+# synced :string
302+# provider_account_id :string
303+#
304+# Indexes
305+#
306+# index_connections_on_account_id (account_id)
307+# index_connections_on_id_and_status (id,status)
308+# index_connections_on_status_and_account_id (status,account_id)
309+# index_connections_on_status_and_public_key (status,public_key)
310+#
311+# Foreign Keys
312+#
313+# fk_rails_b202e9a6e9 (account_id => accounts.id)
314+#
315+
316+class StripeConnection < Connection
317+ def sync
318+ puts "Syncing Customers for account #{current_account.id}"
319+ sync_customers
320+ puts "Syncing Invoices for account #{current_account.id}"
321+ sync_invoices
322+ puts "Syncing Charges for account #{current_account.id}"
323+ sync_charges
324+ puts "Syncing Disputes for account #{current_account.id}"
325+ sync_disputes
326+ puts "Syncing Subscriptions for account #{current_account.id}"
327+ sync_subscriptions
328+ self.update_attribute(:synced, true)
329+ end
330+end
331diff --git a/db/migrate/20160914103835_add_braintree_customer_id_to_customers.rb b/db/migrate/20160914103835_add_braintree_customer_id_to_customers.rb
332deleted file mode 100644
333index 1bccc48..0000000
334--- a/db/migrate/20160914103835_add_braintree_customer_id_to_customers.rb
335+++ /dev/null
336@@ -1,5 +0,0 @@
337-class AddBraintreeCustomerIdToCustomers < ActiveRecord::Migration
338- def change
339- add_column :customers, :braintree_customer_id, :string
340- end
341-end
342diff --git a/db/migrate/20160914130511_remove_null_validation_from_customers.rb b/db/migrate/20160914130511_remove_null_validation_from_customers.rb
343deleted file mode 100644
344index 000823b..0000000
345--- a/db/migrate/20160914130511_remove_null_validation_from_customers.rb
346+++ /dev/null
347@@ -1,5 +0,0 @@
348-class RemoveNullValidationFromCustomers < ActiveRecord::Migration
349- def change
350- change_column :customers, :stripe_customer_id, :string, null: true
351- end
352-end
353diff --git a/db/migrate/20160916094926_add_vendor_customer_id_to_customers.rb b/db/migrate/20160916094926_add_vendor_customer_id_to_customers.rb
354new file mode 100644
355index 0000000..ba92c85
356--- /dev/null
357+++ b/db/migrate/20160916094926_add_vendor_customer_id_to_customers.rb
358@@ -0,0 +1,6 @@
359+class AddVendorCustomerIdToCustomers < ActiveRecord::Migration
360+ def change
361+ add_column :customers, :vendor_customer_id, :string
362+ remove_column :customers, :stripe_customer_id, :string
363+ end
364+end
365diff --git a/db/migrate/20160916103818_add_type_to_customers.rb b/db/migrate/20160916103818_add_type_to_customers.rb
366new file mode 100644
367index 0000000..0defb16
368--- /dev/null
369+++ b/db/migrate/20160916103818_add_type_to_customers.rb
370@@ -0,0 +1,5 @@
371+class AddTypeToCustomers < ActiveRecord::Migration
372+ def change
373+ add_column :customers, :type, :string
374+ end
375+end
376diff --git a/db/migrate/20160916105308_remove_account_ids_from_accounts.rb b/db/migrate/20160916105308_remove_account_ids_from_accounts.rb
377new file mode 100644
378index 0000000..4c27356
379--- /dev/null
380+++ b/db/migrate/20160916105308_remove_account_ids_from_accounts.rb
381@@ -0,0 +1,6 @@
382+class RemoveAccountIdsFromAccounts < ActiveRecord::Migration
383+ def change
384+ remove_column :accounts, :stripe_account_id, :string
385+ remove_column :accounts, :braintree_account_id, :string
386+ end
387+end
388diff --git a/db/migrate/20160916105450_add_provider_account_id_to_connections.rb b/db/migrate/20160916105450_add_provider_account_id_to_connections.rb
389new file mode 100644
390index 0000000..921b98e
391--- /dev/null
392+++ b/db/migrate/20160916105450_add_provider_account_id_to_connections.rb
393@@ -0,0 +1,5 @@
394+class AddProviderAccountIdToConnections < ActiveRecord::Migration
395+ def change
396+ add_column :connections, :provider_account_id, :string
397+ end
398+end
399diff --git a/db/migrate/20160916122225_add_type_to_connections.rb b/db/migrate/20160916122225_add_type_to_connections.rb
400new file mode 100644
401index 0000000..7ce6b9a
402--- /dev/null
403+++ b/db/migrate/20160916122225_add_type_to_connections.rb
404@@ -0,0 +1,7 @@
405+class AddTypeToConnections < ActiveRecord::Migration
406+ def change
407+ add_column :connections, :type, :string
408+ add_column :connections, :active, :string
409+ add_column :connections, :synced, :string
410+ end
411+end
412diff --git a/db/schema.rb b/db/schema.rb
413index f04a328..23a06ee 100644
414--- a/db/schema.rb
415+++ b/db/schema.rb
416@@ -11,7 +11,7 @@
417 #
418 # It's strongly recommended that you check this file into your version control system.
419
420-ActiveRecord::Schema.define(version: 20160914133617) do
421+ActiveRecord::Schema.define(version: 20160916122225) do
422
423 # These are extensions that must be enabled in order to support this database
424 enable_extension "plpgsql"
425@@ -34,7 +34,6 @@ ActiveRecord::Schema.define(version: 20160914133617) do
426 t.string "handle", :limit=>255, :null=>false, :index=>{:name=>"index_accounts_on_handle"}
427 t.boolean "installed", :default=>false, :null=>false
428 t.text "cnames"
429- t.string "stripe_account_id", :index=>{:name=>"index_accounts_on_stripe_account_id"}
430 t.text "stripe_account_raw"
431 t.boolean "is_pro", :default=>false, :null=>false
432 t.integer "transactions_viewed", :default=>0, :null=>false
433@@ -63,7 +62,6 @@ ActiveRecord::Schema.define(version: 20160914133617) do
434 t.string "slack_bot_access_token"
435 t.boolean "synced", :default=>false
436 t.integer "subscriptions_count", :default=>0, :null=>false
437- t.string "braintree_account_id"
438 end
439 add_index "accounts", ["id"], :name=>"index_accounts_on_id", :unique=>true
440
441@@ -128,23 +126,26 @@ ActiveRecord::Schema.define(version: 20160914133617) do
442 end
443
444 create_table "connections", force: :cascade do |t|
445- t.string "status", :limit=>8, :null=>false, :index=>{:name=>"index_connections_on_status_and_account_id", :with=>["account_id"]}
446- t.string "public_key", :limit=>255, :null=>false
447- t.string "scope", :limit=>255, :null=>false
448- t.integer "account_id", :null=>false, :index=>{:name=>"index_connections_on_account_id"}
449- t.string "client_id", :limit=>255, :null=>false
450- t.string "refresh_token", :limit=>255, :null=>false
451- t.string "secret_key", :limit=>255, :null=>false
452- t.string "publishable_key", :limit=>255, :null=>false
453- t.datetime "created_at", :null=>false
454- t.datetime "updated_at", :null=>false
455+ t.string "status", :limit=>8, :null=>false, :index=>{:name=>"index_connections_on_status_and_account_id", :with=>["account_id"]}
456+ t.string "public_key", :limit=>255, :null=>false
457+ t.string "scope", :limit=>255, :null=>false
458+ t.integer "account_id", :null=>false, :index=>{:name=>"index_connections_on_account_id"}
459+ t.string "client_id", :limit=>255, :null=>false
460+ t.string "refresh_token", :limit=>255, :null=>false
461+ t.string "secret_key", :limit=>255, :null=>false
462+ t.string "publishable_key", :limit=>255, :null=>false
463+ t.datetime "created_at", :null=>false
464+ t.datetime "updated_at", :null=>false
465+ t.string "type"
466+ t.string "active"
467+ t.string "synced"
468+ t.string "provider_account_id"
469 end
470 add_index "connections", ["id", "status"], :name=>"index_connections_on_id_and_status"
471 add_index "connections", ["status", "public_key"], :name=>"index_connections_on_status_and_public_key"
472
473 create_table "customers", force: :cascade do |t|
474 t.string "public_key", :limit=>255, :null=>false, :index=>{:name=>"index_customers_on_public_key", :unique=>true}
475- t.string "stripe_customer_id", :index=>{:name=>"index_customers_on_stripe_customer_id"}
476 t.string "email", :default=>""
477 t.integer "account_id", :null=>false, :index=>{:name=>"index_customers_on_account_id"}
478 t.boolean "deleted", :default=>false, :null=>false
479@@ -162,9 +163,9 @@ ActiveRecord::Schema.define(version: 20160914133617) do
480 t.string "additional_billing_info"
481 t.boolean "receive_email_notifications", :default=>false
482 t.string "timezone"
483- t.string "braintree_customer_id"
484+ t.string "type"
485+ t.string "vendor_customer_id"
486 end
487- add_index "customers", ["account_id", "stripe_customer_id"], :name=>"index_customers_on_account_id_and_stripe_customer_id", :unique=>true
488
489 create_table "disputes", force: :cascade do |t|
490 t.string "status"