Dima_S

Untitled

Oct 11th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.22 KB | None | 0 0
  1. dima@dima-Aspire-ES1-512:~/rails_project/accountdock-rails$ git diff
  2. diff --cc app/controllers/braintree_controller.rb
  3. index 4eaa46a,02db7eb..0000000
  4. --- a/app/controllers/braintree_controller.rb
  5. +++ b/app/controllers/braintree_controller.rb
  6. @@@ -5,10 -5,48 +5,11 @@@ class BraintreeController < Application
  7. end
  8.  
  9. def callback
  10. -binding.pry
  11. token_params = BraintreeClient.get_token(params[:code])
  12. -
  13. - # TODO Create a new account with the adding payment service provider
  14. - if current_account
  15. - account_connection = current_account.connections.braintree.find_or_initialize_by(vendor_customer_id: params[:merchantId])
  16. - account = account_connection.account
  17. - else
  18. - account = Account.first_or_initialize
  19. - end
  20. -
  21. - # TODO Get merchant name, email from Braintree Auth when it will be provide this functionality.
  22. - # TODO Now it doesn't. https://github.com/braintree/braintree_ruby/issues/124
  23. - braintree_account_name = 'example_name'
  24. - braintree_account_email = '[email protected]'
  25. -
  26. - if account.new_record?
  27. - account.set_handle(braintree_account_name || braintree_account_email[/[^@]+/])
  28. - if account.valid?
  29. - account.save
  30. - else
  31. - raise "Can't create account object!"
  32. - end
  33. -
  34. - account.build_setting
  35. - account.setting.save!
  36. - end
  37. -
  38. - unless current_user.accounts.where(id: account.id).any?
  39. - UserAccount.create!({
  40. - role: account.user_accounts.count > 0 ? 'owner' : 'member',
  41. - user_id: current_user.id,
  42. - account_id: account.id}
  43. - )
  44. - end
  45. -
  46. token_params[:vendor_customer_id] = params[:merchantId]
  47. - connection = BraintreeConnection.find_or_initialize_by(account_id: account.id)
  48. - connection.update!(token_params)
  49.  
  50. - AccountSyncJob.perform_later(account) unless connection.synced
  51. + account = BraintreeServices::ConnectService.new(token_params, current_user, current_account).call
  52. +
  53. redirect_to team_switch_path(public_key: account.public_key, mode: params[:mode], path: 'dashboard')
  54. end
  55. end
  56. diff --cc app/controllers/stripe_controller.rb
  57. index b364145,893a355..0000000
  58. --- a/app/controllers/stripe_controller.rb
  59. +++ b/app/controllers/stripe_controller.rb
  60. diff --cc app/models/connections/braintree_connection.rb
  61. index 34f2226,acdab0f..0000000
  62. --- a/app/models/connections/braintree_connection.rb
  63. +++ b/app/models/connections/braintree_connection.rb
  64. diff --git a/app/controllers/direct/base_controller.rb b/app/controllers/direct/base_controller.rb
  65. index 4511afd..0691216 100644
  66. --- a/app/controllers/direct/base_controller.rb
  67. +++ b/app/controllers/direct/base_controller.rb
  68. @@ -10,7 +10,7 @@ class Direct::BaseController < ApplicationController
  69. format.html {
  70. fetch_account
  71. begin
  72. - @customer = @account.ensure_customer(params[:customer])
  73. + @customer = @account.connections.find_by(type: 'StripeConnection').ensure_customer(params[:customer])
  74. @handler = params[:handler]
  75. @container = params[:container]
  76. @container = 'window' if @container.nil?
  77. @@ -24,10 +24,11 @@ class Direct::BaseController < ApplicationController
  78. end
  79.  
  80. private
  81. +
  82. def fetch_account
  83. account_public_key = params[:direct_id] || params[:key] # Last is legacy
  84. @account = Account.find_by_public_key(account_public_key)
  85. - raise "Can't find the account" if @account.nil? or [email protected]?
  86. + raise "Can't find the account" if @account.nil? or @account.connections.find_by(type: 'StripeConnection').nil?
  87. @account.update_attribute(:installed, true) unless @account.installed?
  88. end
  89. # The main purpose of this modal HTML is to show in
  90. diff --git a/app/controllers/direct/settings_controller.rb b/app/controllers/direct/settings_controller.rb
  91. index 543440a..37aaf18 100644
  92. --- a/app/controllers/direct/settings_controller.rb
  93. +++ b/app/controllers/direct/settings_controller.rb
  94. @@ -1,6 +1,6 @@
  95. class Direct::SettingsController < Direct::BaseController
  96. def index
  97. - @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
  98. + @customer = @account.customers.find_by_vendor_customer_id(params[:customer_id])
  99. raise "Can't find the customer" if @customer.nil?
  100.  
  101. render layout: false
  102. @@ -8,7 +8,7 @@ class Direct::SettingsController < Direct::BaseController
  103.  
  104. def create
  105. unless @account.is_demo?
  106. - @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
  107. + @customer = @account.customers.find_by_vendor_customer_id(params[:customer_id])
  108. raise "Can't find the customer" if @customer.nil?
  109.  
  110. settings = settings_params
  111. diff --git a/app/controllers/direct/transactions_controller.rb b/app/controllers/direct/transactions_controller.rb
  112. index 4ba46e6..a3a79ca 100644
  113. --- a/app/controllers/direct/transactions_controller.rb
  114. +++ b/app/controllers/direct/transactions_controller.rb
  115. @@ -1,6 +1,6 @@
  116. class Direct::TransactionsController < Direct::BaseController
  117. def index
  118. - @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
  119. + @customer = @account.customers.find_by_vendor_customer_id(params[:customer_id])
  120. raise "Can't find the customer" if @customer.nil?
  121.  
  122. # TODO: This was in the PHP javascript code to pass in timezone.
  123. @@ -13,7 +13,7 @@ class Direct::TransactionsController < Direct::BaseController
  124. @collection.sort! {|a, b| b.created_at <=> a.created_at}
  125.  
  126. begin
  127. - @upcoming_invoice = @account.stripe.upcoming_invoice(@customer.stripe_customer_id)
  128. + @upcoming_invoice = @account.client.upcoming_invoice(@customer.vendor_customer_id)
  129. rescue => e
  130. end
  131.  
  132. diff --git a/app/controllers/team/home_controller.rb b/app/controllers/team/home_controller.rb
  133. index 9aa61a4..c8fad97 100644
  134. --- a/app/controllers/team/home_controller.rb
  135. +++ b/app/controllers/team/home_controller.rb
  136. @@ -18,7 +18,7 @@ class Team::HomeController < Team::BaseController
  137.  
  138. def view_payment_failed_email
  139. @customer = current_account.customers.new(email: current_user.email,
  140. - stripe_customer_id: 'cus_1234')
  141. + vendor_customer_id: 'cus_1234')
  142. @customer.cards.new(name: "Test Customer")
  143. render text: process_template(@customer, current_account.setting.first_failed_payment_body)
  144. end
  145. diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
  146. index 3a40af4..5b06291 100644
  147. --- a/app/controllers/users_controller.rb
  148. +++ b/app/controllers/users_controller.rb
  149. @@ -19,11 +19,11 @@ class UsersController < ApplicationController
  150. end
  151.  
  152. def save_creditcard
  153. - if current_user.stripe_customer_id.blank?
  154. + if current_user.vendor_customer_id.blank?
  155. customer = STRIPE.create_customer(current_user.email)
  156. - current_user.update({stripe_customer_id: customer.id})
  157. + current_user.update({vendor_customer_id: customer.id})
  158. end
  159. - card = STRIPE.create_card(current_user.stripe_customer_id, params[:stripe_token])
  160. + card = STRIPE.create_card(current_user.vendor_customer_id, params[:stripe_token])
  161.  
  162. redirect_to team_home_path, notice: 'Credit card is added successfully.' if current_account.is_pro
  163. redirect_to users_upgrade_path, notice: 'Credit card is added successfully.' unless current_account.is_pro
  164. @@ -37,7 +37,7 @@ class UsersController < ApplicationController
  165. plan = Plan.find_by_id(params[:plan_id])
  166. redirect_to users_upgrade_path if plan.nil?
  167.  
  168. - subscription = STRIPE.create_subscription_for_plan(current_user.stripe_customer_id, plan.stripe_id)
  169. + subscription = STRIPE.create_subscription_for_plan(current_user.vendor_customer_id, plan.stripe_id)
  170. if subscription.id
  171. current_account.update({is_pro: true})
  172. current_account.setting.update({show_branding: false})
  173. @@ -66,7 +66,7 @@ class UsersController < ApplicationController
  174. end
  175.  
  176. def has_stripe_customer
  177. - redirect_to users_creditcard_path if current_user.stripe_customer_id.blank?
  178. + redirect_to users_creditcard_path if current_user.vendor_customer_id.blank?
  179. end
  180.  
  181. end
  182. diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
  183. index ad0749b..6cb3664 100644
  184. --- a/app/helpers/application_helper.rb
  185. +++ b/app/helpers/application_helper.rb
  186. @@ -48,8 +48,8 @@ module ApplicationHelper
  187.  
  188. def process_template(customer, txt)
  189. txt.gsub!(/{account.displayName}/, customer.account.setting.display_name || '')
  190. - txt.gsub!(/{customer.id}/, customer.stripe_customer_id)
  191. - txt.gsub!(/{customer.urls.payment}/, edit_direct_customer_payment_url(customer.account.public_key, customer.stripe_customer_id))
  192. + txt.gsub!(/{customer.id}/, customer.vendor_customer_id)
  193. + txt.gsub!(/{customer.urls.payment}/, edit_direct_customer_payment_url(customer.account.public_key, customer.vendor_customer_id))
  194. txt.gsub!(/{customer.firstName( \|\| '([^']+)')?}/) { |q| customer.first_name || $2 }
  195. txt.gsub!(/{customer.name( \|\| '([^']+)')?}/) { |q| customer.raw_name || $2 }
  196. txt.gsub!(/{customer.email}/, customer.notifications_email_address)
  197. diff --git a/app/helpers/charges_helper.rb b/app/helpers/charges_helper.rb
  198. index 1e15ada..267ca70 100644
  199. --- a/app/helpers/charges_helper.rb
  200. +++ b/app/helpers/charges_helper.rb
  201. @@ -15,7 +15,7 @@ module ChargesHelper
  202. end
  203.  
  204. if charge.customer.present?
  205. - note.gsub!('{customer.id}', charge.customer.stripe_customer_id)
  206. + note.gsub!('{customer.id}', charge.customer.vendor_customer_id)
  207. note.gsub!('{customer.email}', charge.customer.email || '')
  208. note.gsub!('{customer.description}', charge.customer.description.blank? ? '' : charge.customer.description)
  209. charge.customer.metadata.each do |key, value|
  210. diff --git a/app/mailers/customer_mailer.rb b/app/mailers/customer_mailer.rb
  211. index 244ee2b..d7607ee 100644
  212. --- a/app/mailers/customer_mailer.rb
  213. +++ b/app/mailers/customer_mailer.rb
  214. @@ -29,7 +29,7 @@ class CustomerMailer < ApplicationMailer
  215. if microdata
  216. # This should only show on emails that are for expiring credit cards
  217. microdata_html = render partial: 'customer_mailer/microdata', # One-click action from GMail
  218. - locals: { url: edit_direct_customer_payment_url(customer.account.public_key, customer.stripe_customer_id) }
  219. + locals: { url: edit_direct_customer_payment_url(customer.account.public_key, customer.vendor_customer_id) }
  220. html_body += microdata_html
  221. end
  222.  
  223. diff --git a/app/models/account.rb b/app/models/account.rb
  224. index 2fa9f03..d22cff6 100644
  225. --- a/app/models/account.rb
  226. +++ b/app/models/account.rb
  227. @@ -110,7 +110,7 @@ class Account < ActiveRecord::Base
  228.  
  229. # Is there a Vendor connection?
  230. def has_connection?
  231. - self.connections.present?
  232. + self.connections.last.present?
  233. end
  234.  
  235. def vendor_customers_estimate
  236. diff --git a/app/models/connections/stripe_connection.rb b/app/models/connections/stripe_connection.rb
  237. index 297a77d..88c8cdf 100644
  238. --- a/app/models/connections/stripe_connection.rb
  239. +++ b/app/models/connections/stripe_connection.rb
  240. @@ -67,7 +67,7 @@ class StripeConnection < Connection
  241. Invoice.unscoped do
  242. # Get all the invoices in the db that match the stripe invoices:
  243. db_invoices = self.account.invoices.where(stripe_invoice_id: stripe_invoices.map { |s| s.id })
  244. - db_customers = self.account.customers.where(stripe_customer_id: stripe_invoices.map { |s| s.customer })
  245. + db_customers = self.account.customers.where(vendor_customer_id: stripe_invoices.map { |s| s.customer })
  246. # Then iterate over all the stripe invoices and add/update DB invoices as needed:
  247. stripe_invoices.collect do |stripe_invoice|
  248. # Use the invoice if we have it in the db:
  249. @@ -75,7 +75,7 @@ class StripeConnection < Connection
  250. # Or build a new one if we don't:
  251. db_invoice ||= self.account.invoices.build(stripe_invoice_id: stripe_invoice.id)
  252. # Make sure we have a customer and invoice is not orphaned:
  253. - db_customer = db_customers.select { |c| c.stripe_customer_id == stripe_invoice.customer }.first
  254. + db_customer = db_customers.select { |c| c.vendor_customer_id == stripe_invoice.customer }.first
  255. # Update the customer only if it wasn't already in the DB:
  256. db_customer ||= self.account.ensure_customer(stripe_invoice.customer)
  257. # Update the db / save the record:
  258. @@ -203,7 +203,7 @@ class StripeConnection < Connection
  259.  
  260. def remove_customer(stripe_customer)
  261. Customer.unscoped do
  262. - customer = self.account.customers.find_by_stripe_customer_id(stripe_customer.id)
  263. + customer = self.account.customers.find_by_vendor_customer_id(stripe_customer.id)
  264. customer.nil? ? nil : customer.update({deleted: true})
  265. end
  266. end
  267. @@ -288,7 +288,7 @@ class StripeConnection < Connection
  268. def ensure_invoice(stripe_invoice_id)
  269. begin
  270. # Do we have this invoice in the DB already?
  271. - invoice = Invoice.unscoped { self.invoices.find_by_stripe_invoice_id(stripe_invoice_id) }
  272. + invoice = Invoice.unscoped { self.account.invoices.find_by_stripe_invoice_id(stripe_invoice_id) }
  273. # If so, return it
  274. return invoice unless invoice.blank?
  275. # Otherwise go get it:
  276. diff --git a/app/models/customer.rb b/app/models/customer.rb
  277. index c1fd0e0..a619f1f 100644
  278. --- a/app/models/customer.rb
  279. +++ b/app/models/customer.rb
  280. @@ -42,8 +42,8 @@ class Customer < ActiveRecord::Base
  281. has_many :invoices, dependent: :destroy
  282. has_many :charges, dependent: :destroy
  283. has_many :subscriptions,
  284. - foreign_key: :stripe_customer_id,
  285. - primary_key: :stripe_customer_id,
  286. + foreign_key: :vendor_customer_id,
  287. + primary_key: :vendor_customer_id,
  288. dependent: :destroy
  289. has_many :activities, dependent: :destroy
  290.  
  291. @@ -135,7 +135,7 @@ class Customer < ActiveRecord::Base
  292. def stripe_admin_url
  293. url = 'https://dashboard.stripe.com/'
  294. url += 'test/' unless self.live
  295. - url += 'customers/' + self.stripe_customer_id
  296. + url += 'customers/' + self.vendor_customer_id
  297.  
  298. url
  299. end
  300. @@ -247,7 +247,7 @@ class Customer < ActiveRecord::Base
  301. super
  302. else
  303. # whitelist only the title and body attributes for other users
  304. - super & %w(email stripe_customer_id created_at)
  305. + super & %w(email vendor_customer_id created_at)
  306. end
  307. end
  308.  
  309. diff --git a/app/services/braintree_services/sync_service.rb b/app/services/braintree_services/sync_service.rb
  310. index f218a72..496d468 100644
  311. --- a/app/services/braintree_services/sync_service.rb
  312. +++ b/app/services/braintree_services/sync_service.rb
  313. @@ -1,10 +1,10 @@
  314. class BraintreeServices::SyncService < SharedServices::BaseSyncService
  315. private
  316. def sync_customers
  317. - sync_message("Customers")
  318. + sync_message('Customers')
  319.  
  320. updated_customers = []
  321. - braintree_customers = Braintree::Gateway.new(access_token: @connection.access_token).customer.all.ids
  322. + braintree_customers = @connection.client.customers
  323. braintree_customers.each do |customer|
  324. updated_customers << customer
  325. sync_vendor_customer(customer)
  326. @@ -37,11 +37,20 @@ class BraintreeServices::SyncService < SharedServices::BaseSyncService
  327. def sync_vendor_customer(braintree_customer)
  328. attempts ||= 2
  329.  
  330. - customer = @account.customers.where(vendor_customer_id: braintree_customer).first_or_initialize
  331. - if braintree_customer.try(:deleted)
  332. - customer.update_attributes({ deleted: true, live: livemode? })
  333. - else
  334. - customer.assign_attributes({ })
  335. + customer = @account.customers.where(vendor_customer_id: braintree_customer.id).first_or_initialize
  336. + if braintree_customer
  337. + customer.update_attributes({ deleted: true, live: false })
  338. + customer.assign_attributes({
  339. + email: braintree_customer.email.try(:delete, "\000"),
  340. + default_source: default_card(braintree_customer),
  341. + # description: braintree_customer.description || '',
  342. + # metadata_raw: braintree_customer.metadata.to_json,
  343. + live: [email protected]_id.include?('sandbox'),
  344. + # account_balance: braintree_customer.account_balance,
  345. + # currency: braintree_customer.currency,
  346. + # business_vat_id: braintree_customer.try(:business_vat_id),
  347. + created_at: Time.at(braintree_customer.created_at)
  348. + })
  349. customer.save!
  350. end
  351. customer
  352. @@ -50,4 +59,12 @@ class BraintreeServices::SyncService < SharedServices::BaseSyncService
  353. retry unless (attempts -= 1).zero?
  354. raise ex
  355. end
  356. +
  357. + def default_card(customer)
  358. + customer.payment_methods.each do |payment_methods|
  359. + if payment_methods.default?
  360. + return payment_methods.token
  361. + end
  362. + end
  363. + end
  364. end
  365. diff --git a/app/services/stripe_services/sync_service.rb b/app/services/stripe_services/sync_service.rb
  366. index 387abca..519cb19 100644
  367. --- a/app/services/stripe_services/sync_service.rb
  368. +++ b/app/services/stripe_services/sync_service.rb
  369. @@ -1,5 +1,5 @@
  370. class StripeServices::SyncService < SharedServices::BaseSyncService
  371. - private
  372. + # private
  373. def sync_customers
  374. sync_message('Customers')
  375. updated_customers = []
  376. @@ -26,7 +26,7 @@ class StripeServices::SyncService < SharedServices::BaseSyncService
  377. options = {limit: 100}
  378. while (stripe_invoices = @connection.client.invoices(options).to_a).any?
  379. options[:starting_after] = stripe_invoices.last.id
  380. - @connection.client.add_or_update_invoices(stripe_invoices)
  381. + @connection.add_or_update_invoices(stripe_invoices)
  382. end
  383. end
  384.  
  385. @@ -67,7 +67,7 @@ class StripeServices::SyncService < SharedServices::BaseSyncService
  386. while (stripe_subscriptions = @connection.client.subscriptions(options).to_a).any?
  387. options[:starting_after] = stripe_subscriptions.last.id
  388. stripe_subscriptions.each do |dp|
  389. - @connection.client.add_or_update_subscription(dp)
  390. + @connection.add_or_update_subscription(dp)
  391. end
  392. end
  393. # Theoretically this isn't necessary (we should only have added new)
  394. @@ -120,8 +120,8 @@ class StripeServices::SyncService < SharedServices::BaseSyncService
  395. raise ex
  396. end
  397.  
  398. - def sync_customer(stripe_customer_id)
  399. - stripe_customer = @connection.client.customer(stripe_customer_id)
  400. + def sync_customer(vendor_customer_id)
  401. + stripe_customer = @connection.client.customer(vendor_customer_id)
  402. sync_vendor_customer(stripe_customer)
  403. end
  404.  
  405. diff --git a/app/views/admin/customers/_form.html.haml b/app/views/admin/customers/_form.html.haml
  406. index f48301d..51cd519 100644
  407. --- a/app/views/admin/customers/_form.html.haml
  408. +++ b/app/views/admin/customers/_form.html.haml
  409. @@ -10,8 +10,8 @@
  410. = f.label :public_key
  411. = f.text_field :public_key
  412. .field
  413. - = f.label :stripe_customer_id
  414. - = f.text_field :stripe_customer_id
  415. + = f.label :vendor_customer_id
  416. + = f.text_field :vendor_customer_id
  417. .field
  418. = f.label :email
  419. = f.text_field :email
  420. diff --git a/app/views/admin/customers/index.html.haml b/app/views/admin/customers/index.html.haml
  421. index a680a94..973a3ff 100644
  422. --- a/app/views/admin/customers/index.html.haml
  423. +++ b/app/views/admin/customers/index.html.haml
  424. @@ -17,7 +17,7 @@
  425. - @customers.each do |customer|
  426. %tr
  427. %td= link_to customer.public_key, [:admin, customer]
  428. - %td= customer.stripe_customer_id
  429. + %td= customer.vendor_customer_id
  430. %td= customer.email
  431. %td= link_to customer.account_id, [:admin, customer.account]
  432. %td= customer.subscriptions
  433. diff --git a/app/views/admin/customers/show.html.haml b/app/views/admin/customers/show.html.haml
  434. index fab39b3..9791e28 100644
  435. --- a/app/views/admin/customers/show.html.haml
  436. +++ b/app/views/admin/customers/show.html.haml
  437. @@ -5,7 +5,7 @@
  438. = @customer.public_key
  439. %p
  440. %b Stripe customer:
  441. - = @customer.stripe_customer_id
  442. + = @customer.vendor_customer_id
  443. %p
  444. %b Email:
  445. = @customer.email
  446. diff --git a/app/views/direct/base/app.html.haml b/app/views/direct/base/app.html.haml
  447. index a57edb7..d5c4f37 100644
  448. --- a/app/views/direct/base/app.html.haml
  449. +++ b/app/views/direct/base/app.html.haml
  450. @@ -7,8 +7,8 @@
  451. = javascript_include_tag '//api.filestackapi.com/filestack.js', 'application', 'angular'
  452. :javascript
  453. var app = angular.module('app', [])
  454. - .constant('TRAN', "#{direct_customer_transactions_path(@account.public_key, @customer.stripe_customer_id, format: :json)}")
  455. - .constant('SETTING', "#{direct_customer_settings_path(@account.public_key, @customer.stripe_customer_id, format: :json)}")
  456. + .constant('TRAN', "#{direct_customer_transactions_path(@account.public_key, @customer.vendor_customer_id, format: :json)}")
  457. + .constant('SETTING', "#{direct_customer_settings_path(@account.public_key, @customer.vendor_customer_id, format: :json)}")
  458. .constant('BULK', {email: "#{charges_bulk_email_path(key: @account.public_key, format: :json)}", zip: "#{charges_bulk_zip_url(key: @account.public_key)}"})
  459. .constant('EMAIL', "#{@customer.email}")
  460. .constant('DISPLAY_NAME', "#{@account.setting.email_sender_display_name}")
  461. diff --git a/app/views/direct/modals/_settings.html.haml b/app/views/direct/modals/_settings.html.haml
  462. index 6bb332b..655a7e5 100644
  463. --- a/app/views/direct/modals/_settings.html.haml
  464. +++ b/app/views/direct/modals/_settings.html.haml
  465. @@ -4,7 +4,7 @@
  466. %div
  467. %header Receipt Settings
  468. %a.closeBtn.fa.fa-close{href: '#', 'ng-click'=>"closeModal('settings')"}
  469. - = form_tag direct_customer_settings_path(direct_id: @account.public_key, customer_id: @customer.stripe_customer_id, format: :json), remote: true, role: 'form' do
  470. + = form_tag direct_customer_settings_path(direct_id: @account.public_key, customer_id: @customer.vendor_customer_id, format: :json), remote: true, role: 'form' do
  471. .body
  472. .columns.clearfix
  473. .column
  474. diff --git a/app/views/team/customers/index.html.haml b/app/views/team/customers/index.html.haml
  475. index ca3cb66..0fe12ce 100644
  476. --- a/app/views/team/customers/index.html.haml
  477. +++ b/app/views/team/customers/index.html.haml
  478. @@ -57,11 +57,11 @@
  479. = customer.charges.count
  480. %td.text-right
  481. - if current_account.allow_payment_page_control
  482. - = link_to 'Credit Card Page', edit_direct_customer_payment_path(current_account.public_key, customer.stripe_customer_id), class:'btn btn-default btn-xs tooltip-btn', 'data-toggle' => 'tooltip', 'data-placement' => 'left', 'data-original-title' => 'You can share this URL with your customers to allow them to update their card', target: '_blank'
  483. + = link_to 'Credit Card Page', edit_direct_customer_payment_path(current_account.public_key, customer.vendor_customer_id), class:'btn btn-default btn-xs tooltip-btn', 'data-toggle' => 'tooltip', 'data-placement' => 'left', 'data-original-title' => 'You can share this URL with your customers to allow them to update their card', target: '_blank'
  484. - if nil # These should move to a customer detail page
  485. = link_to 'Send Payment Failed', send_payment_failed_team_customer_path(customer.public_key), class:'btn btn-default btn-xs tooltip-btn', 'data-toggle' => 'tooltip', 'data-placement' => 'left', 'data-original-title' => 'Send the first payment failure notice in your series.'
  486. = link_to 'Send Charge Notice', send_charge_notice_team_customer_path(customer.public_key), class:'btn btn-default btn-xs tooltip-btn', 'data-toggle' => 'tooltip', 'data-placement' => 'left', 'data-original-title' => 'Send charge notice.'
  487. - %button.btn.btn-primary.btn-xs.tooltip-btn{type:"button", 'data-toggle'=>"tooltip",'data-placement'=>"left",'data-original-title'=>"This is what your customer will see", 'data-customerid'=>"#{customer.stripe_customer_id}"}
  488. + %button.btn.btn-primary.btn-xs.tooltip-btn{type:"button", 'data-toggle'=>"tooltip",'data-placement'=>"left",'data-original-title'=>"This is what your customer will see", 'data-customerid'=>"#{customer.vendor_customer_id}"}
  489. View Billing
  490. %hr
  491. .row
  492. diff --git a/lib/braintree_client.rb b/lib/braintree_client.rb
  493. index 630358d..cc8d58e 100644
  494. --- a/lib/braintree_client.rb
  495. +++ b/lib/braintree_client.rb
  496. @@ -17,12 +17,101 @@ class BraintreeClient
  497. publishable_key: ''
  498. }
  499. end
  500. +
  501. + def self.get_account(vendor_user_id, access_token)
  502. + Braintree::Customer.find(vendor_user_id, access_token)
  503. + end
  504. +
  505. + def initialize(access_token)
  506. + @access_token = access_token
  507. + end
  508. +
  509. + [:customer].each do |m|
  510. + braintree_class = "Braintree::#{m.capitalize}".constantize
  511. + define_method(m) do |*opts|
  512. + braintree_class.find(*opts, @access_token)
  513. + end
  514. + define_method(m.to_s.pluralize) do |opts={}|
  515. + bt_customers = []
  516. + gateway(@access_token).customer.all.ids.each do |bt_customer|
  517. + bt_customers << gateway(@access_token).customer.find(bt_customer)
  518. + end
  519. + return bt_customers
  520. + end
  521. + # TODO
  522. + define_method("#{m.to_s.pluralize}_count") do
  523. + return -1 unless @access_token
  524. + braintree_class.all({ include: ['total_count'], limit: 1}, @access_token).total_count rescue -1
  525. + end
  526. + end
  527. +
  528. + # Cards need customer..
  529. + def cards(customer_id, options)
  530. + Braintree::Customer.find(customer_id, @access_token).credit_cards
  531. + end
  532. +
  533. + # it's plug, see more http://www.rubydoc.info/github/braintree/braintree_ruby/Braintree/Dispute
  534. + def update_dispute(vendor_dispute_id, new_evidence)
  535. + true
  536. + end
  537. +
  538. + def source(vendor_source_id)
  539. + Braintree::CreditCard.find(vendor_source_id)
  540. + end
  541. +
  542. + def user_subscriptions(customer_id)
  543. + customer = self.customer(customer_id)
  544. + customer.credit_cards.map(&:subscriptions).flatten
  545. + end
  546. +
  547. + # it's plug
  548. + def refunds_by_charge (vendor_charge_id, options = {})
  549. + true
  550. + end
  551. +
  552. + # it's plug
  553. + def upcoming_invoice(vendor_customer_id)
  554. + true
  555. + end
  556. +
  557. + def create_customer(email)
  558. + Braintree::Customer.create({email: email}, @access_token)
  559. + end
  560. +
  561. + def create_card(customer_id, params)
  562. + card = Braintree::CreditCard.create(
  563. + customer_id: customer_id,
  564. + number: params[:number],
  565. + expiration_month: params[:expiration_month],
  566. + expiration_year: params[:expiration_year],
  567. + cvv: params[:cvv],
  568. + options: {:verify_card => true}
  569. + )
  570. + end
  571. +
  572. + # TODO string-89 method first! (should be defoult, but we can not add default card)
  573. + def create_subscription_for_plan (customer_id, plan_id)
  574. + customer = self.customer(customer_id)
  575. + Braintree::Subscription.create(
  576. + plan_id: plan_id,
  577. + payment_method_nonce: customer.payment_methods.first.token
  578. + )
  579. + end
  580. +
  581. + # it's plug
  582. + def ping
  583. + true
  584. + end
  585.  
  586. def initialize(access_token)
  587. @access_token = access_token
  588. end
  589.  
  590. private
  591. + def gateway(access_token)
  592. + Braintree::Gateway.new(:access_token => access_token)
  593. + end
  594. +
  595. def self.oauth_gateway
  596. @gateway ||= Braintree::Gateway.new({
  597. client_id: config[:client_id],
  598. (END)
Advertisement
Add Comment
Please, Sign In to add comment