Dima_S

Untitled

Oct 7th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.82 KB | None | 0 0
  1.  
  2. diff --cc app/controllers/braintree_controller.rb
  3. index a8b7af5,bda8c5c..0000000
  4. --- a/app/controllers/braintree_controller.rb
  5. +++ b/app/controllers/braintree_controller.rb
  6. @@@ -41,9 -40,10 +41,8 @@@ class BraintreeController < Application
  7. end
  8.  
  9. token_params[:vendor_customer_id] = params[:merchantId]
  10. --
  11. connection = BraintreeConnection.find_or_initialize_by(account_id: account.id)
  12. -binding.pry
  13. - connection.update(token_params)
  14. + connection.update!(token_params)
  15.  
  16. AccountSyncJob.perform_later(account) unless connection.synced
  17.  
  18. diff --git a/app/controllers/direct/base_controller.rb b/app/controllers/direct/base_controller.rb
  19. index 4511afd..b7122bb 100644
  20. --- a/app/controllers/direct/base_controller.rb
  21. +++ b/app/controllers/direct/base_controller.rb
  22. @@ -24,11 +24,12 @@ class Direct::BaseController < ApplicationController
  23. end
  24.  
  25. private
  26. +
  27. def fetch_account
  28. account_public_key = params[:direct_id] || params[:key] # Last is legacy
  29. @account = Account.find_by_public_key(account_public_key)
  30. - raise "Can't find the account" if @account.nil? or [email protected]?
  31. - @account.update_attribute(:installed, true) unless @account.installed?
  32. + raise "Can't find the account" if @account.nil? or @account.connections.find_by(type: 'StripeConnection').nil?
  33. + @account.connections.find_by(type: 'StripeConnection').update_attribute(:installed, true) unless @account.connections.find_by(type: 'StripeConnection').installed?
  34. end
  35. # The main purpose of this modal HTML is to show in
  36. # someone else's website in an IFrame. So have to relax
  37. diff --git a/app/controllers/direct/settings_controller.rb b/app/controllers/direct/settings_controller.rb
  38. index 543440a..37aaf18 100644
  39. --- a/app/controllers/direct/settings_controller.rb
  40. +++ b/app/controllers/direct/settings_controller.rb
  41. @@ -1,6 +1,6 @@
  42. class Direct::SettingsController < Direct::BaseController
  43. def index
  44. - @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
  45. + @customer = @account.customers.find_by_vendor_customer_id(params[:customer_id])
  46. raise "Can't find the customer" if @customer.nil?
  47.  
  48. render layout: false
  49. @@ -8,7 +8,7 @@ class Direct::SettingsController < Direct::BaseController
  50.  
  51. def create
  52. unless @account.is_demo?
  53. - @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
  54. + @customer = @account.customers.find_by_vendor_customer_id(params[:customer_id])
  55. raise "Can't find the customer" if @customer.nil?
  56.  
  57. settings = settings_params
  58. diff --git a/app/controllers/direct/transactions_controller.rb b/app/controllers/direct/transactions_controller.rb
  59. index 4ba46e6..b416b08 100644
  60. --- a/app/controllers/direct/transactions_controller.rb
  61. +++ b/app/controllers/direct/transactions_controller.rb
  62. @@ -1,6 +1,6 @@
  63. class Direct::TransactionsController < Direct::BaseController
  64. def index
  65. - @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
  66. + @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
  67. raise "Can't find the customer" if @customer.nil?
  68.  
  69. # TODO: This was in the PHP javascript code to pass in timezone.
  70. @@ -13,7 +13,7 @@ class Direct::TransactionsController < Direct::BaseController
  71. @collection.sort! {|a, b| b.created_at <=> a.created_at}
  72.  
  73. begin
  74. - @upcoming_invoice = @account.stripe.upcoming_invoice(@customer.stripe_customer_id)
  75. + @upcoming_invoice = @account.client.upcoming_invoice(@customer.vendor_customer_id)
  76. rescue => e
  77. end
  78.  
  79. diff --git a/app/controllers/stripe_controller.rb b/app/controllers/stripe_controller.rb
  80. index 82555bf..893a355 100644
  81. --- a/app/controllers/stripe_controller.rb
  82. +++ b/app/controllers/stripe_controller.rb
  83. @@ -46,6 +46,7 @@ class StripeController < ApplicationController
  84. end
  85.  
  86. connection = StripeConnection.find_or_initialize_by(account_id: account.id)
  87. +
  88. connection.update(token_params)
  89.  
  90. AccountSyncJob.perform_later(account) unless connection.synced
  91. diff --git a/app/models/account.rb b/app/models/account.rb
  92. index befc591..a6f6d4c 100644
  93. --- a/app/models/account.rb
  94. +++ b/app/models/account.rb
  95. @@ -114,7 +114,7 @@ class Account < ActiveRecord::Base
  96.  
  97. # Is there a Vendor connection?
  98. def has_connection?
  99. - self.connections.present?
  100. + self.connections.braintree.present? && self.connections.stripe.present?
  101. end
  102.  
  103. # Vendor Accessors
  104. diff --git a/app/models/connections/braintree_connection.rb b/app/models/connections/braintree_connection.rb
  105. index 3929f9b..acdab0f 100644
  106. --- a/app/models/connections/braintree_connection.rb
  107. +++ b/app/models/connections/braintree_connection.rb
  108. @@ -40,7 +40,7 @@ class BraintreeConnection < Connection
  109.  
  110. def client(account)
  111. BraintreeClient.new(self.access_token)
  112. - @account = account
  113. + # @account = account
  114. end
  115.  
  116. def connection_present?
  117. diff --git a/app/services/braintree_services/sync_service.rb b/app/services/braintree_services/sync_service.rb
  118. index 7e50a1a..c4d082e 100644
  119. --- a/app/services/braintree_services/sync_service.rb
  120. +++ b/app/services/braintree_services/sync_service.rb
  121. @@ -1,10 +1,10 @@
  122. class BraintreeServices::SyncService < SharedServices::BaseSyncService
  123. private
  124. def sync_customers
  125. - sync_message("Customers")
  126. + sync_message('Customers')
  127.  
  128. updated_customers = []
  129. - braintree_customers = Braintree::Gateway.new(access_token: @connection.access_token).customer.all.ids
  130. + braintree_customers = @connection.client(@account).customers
  131. braintree_customers.each do |customer|
  132. updated_customers << customer
  133. sync_braintree_customer(customer)
  134. @@ -38,11 +38,20 @@ class BraintreeServices::SyncService < SharedServices::BaseSyncService
  135. def sync_braintree_customer(braintree_customer)
  136. attempts ||= 2
  137.  
  138. - customer = @account.customers.where(vendor_customer_id: braintree_customer).first_or_initialize
  139. - if braintree_customer.try(:deleted)
  140. - customer.update_attributes({ deleted: true, live: livemode? })
  141. - else
  142. - customer.assign_attributes({ })
  143. + customer = @account.customers.where(vendor_customer_id: braintree_customer.id).first_or_initialize
  144. + if braintree_customer
  145. + customer.update_attributes({ deleted: true, live: false })
  146. + customer.assign_attributes({
  147. + email: braintree_customer.email.try(:delete, "\000"),
  148. + default_source: default_card(braintree_customer),
  149. + # description: braintree_customer.description || '',
  150. + # metadata_raw: braintree_customer.metadata.to_json,
  151. + live: @connection.client_id.include?('sandbox'),
  152. + # account_balance: braintree_customer.account_balance,
  153. + # currency: braintree_customer.currency,
  154. + # business_vat_id: braintree_customer.try(:business_vat_id),
  155. + created_at: Time.at(braintree_customer.created_at)
  156. + })
  157. customer.save!
  158. end
  159. customer
  160. @@ -51,4 +60,12 @@ class BraintreeServices::SyncService < SharedServices::BaseSyncService
  161. retry unless (attempts -= 1).zero?
  162. raise ex
  163. end
  164. +
  165. + def default_card(customer)
  166. + customer.payment_methods.each do |payment_methods|
  167. + if payment_methods.default?
  168. + return payment_methods.token
  169. + end
  170. + end
  171. + end
  172. end
  173. diff --git a/app/services/stripe_services/sync_service.rb b/app/services/stripe_services/sync_service.rb
  174. index e200538..4048a7f 100644
  175. --- a/app/services/stripe_services/sync_service.rb
  176. +++ b/app/services/stripe_services/sync_service.rb
  177. @@ -1,5 +1,5 @@
  178. class StripeServices::SyncService < SharedServices::BaseSyncService
  179. - private
  180. + # private
  181. def sync_customers
  182. sync_message('Customers')
  183. updated_customers = []
  184. diff --git a/app/views/direct/base/app.html.haml b/app/views/direct/base/app.html.haml
  185. index a57edb7..d5c4f37 100644
  186. --- a/app/views/direct/base/app.html.haml
  187. +++ b/app/views/direct/base/app.html.haml
  188. @@ -7,8 +7,8 @@
  189. = javascript_include_tag '//api.filestackapi.com/filestack.js', 'application', 'angular'
  190. :javascript
  191. var app = angular.module('app', [])
  192. - .constant('TRAN', "#{direct_customer_transactions_path(@account.public_key, @customer.stripe_customer_id, format: :json)}")
  193. - .constant('SETTING', "#{direct_customer_settings_path(@account.public_key, @customer.stripe_customer_id, format: :json)}")
  194. + .constant('TRAN', "#{direct_customer_transactions_path(@account.public_key, @customer.vendor_customer_id, format: :json)}")
  195. + .constant('SETTING', "#{direct_customer_settings_path(@account.public_key, @customer.vendor_customer_id, format: :json)}")
  196. .constant('BULK', {email: "#{charges_bulk_email_path(key: @account.public_key, format: :json)}", zip: "#{charges_bulk_zip_url(key: @account.public_key)}"})
  197. .constant('EMAIL', "#{@customer.email}")
  198. .constant('DISPLAY_NAME', "#{@account.setting.email_sender_display_name}")
  199. diff --git a/lib/braintree_client.rb b/lib/braintree_client.rb
  200. index 630358d..cc8d58e 100644
  201. --- a/lib/braintree_client.rb
  202. +++ b/lib/braintree_client.rb
  203. @@ -17,12 +17,101 @@ class BraintreeClient
  204. publishable_key: ''
  205. }
  206. end
  207. +
  208. + def self.get_account(vendor_user_id, access_token)
  209. + Braintree::Customer.find(vendor_user_id, access_token)
  210. + end
  211. +
  212. + def initialize(access_token)
  213. + @access_token = access_token
  214. + end
  215. +
  216. + [:customer].each do |m|
  217. + braintree_class = "Braintree::#{m.capitalize}".constantize
  218. + define_method(m) do |*opts|
  219. + braintree_class.find(*opts, @access_token)
  220. + end
  221. + define_method(m.to_s.pluralize) do |opts={}|
  222. + bt_customers = []
  223. + gateway(@access_token).customer.all.ids.each do |bt_customer|
  224. + bt_customers << gateway(@access_token).customer.find(bt_customer)
  225. + end
  226. + return bt_customers
  227. + end
  228. + # TODO
  229. + define_method("#{m.to_s.pluralize}_count") do
  230. + return -1 unless @access_token
  231. + braintree_class.all({ include: ['total_count'], limit: 1}, @access_token).total_count rescue -1
  232. + end
  233. + end
  234. +
  235. + # Cards need customer..
  236. + def cards(customer_id, options)
  237. + Braintree::Customer.find(customer_id, @access_token).credit_cards
  238. + end
  239. +
  240. + # it's plug, see more http://www.rubydoc.info/github/braintree/braintree_ruby/Braintree/Dispute
  241. + def update_dispute(vendor_dispute_id, new_evidence)
  242. + true
  243. + end
  244. +
  245. + def source(vendor_source_id)
  246. + Braintree::CreditCard.find(vendor_source_id)
  247. + end
  248. +
  249. + def user_subscriptions(customer_id)
  250. + customer = self.customer(customer_id)
  251. + customer.credit_cards.map(&:subscriptions).flatten
  252. + end
  253. +
  254. + # it's plug
  255. + def refunds_by_charge (vendor_charge_id, options = {})
  256. + true
  257. + end
  258. +
  259. + # it's plug
  260. + def upcoming_invoice(vendor_customer_id)
  261. + true
  262. + end
  263. +
  264. + def create_customer(email)
  265. + Braintree::Customer.create({email: email}, @access_token)
  266. + end
  267. +
  268. + def create_card(customer_id, params)
  269. + card = Braintree::CreditCard.create(
  270. + customer_id: customer_id,
  271. + number: params[:number],
  272. + expiration_month: params[:expiration_month],
  273. + expiration_year: params[:expiration_year],
  274. + cvv: params[:cvv],
  275. + options: {:verify_card => true}
  276. + )
  277. + end
  278. +
  279. + # TODO string-89 method first! (should be defoult, but we can not add default card)
  280. + def create_subscription_for_plan (customer_id, plan_id)
  281. + customer = self.customer(customer_id)
  282. + Braintree::Subscription.create(
  283. + plan_id: plan_id,
  284. + payment_method_nonce: customer.payment_methods.first.token
  285. + )
  286. + end
  287. +
  288. + # it's plug
  289. + def ping
  290. + true
  291. + end
  292.  
  293. def initialize(access_token)
  294. @access_token = access_token
  295. end
  296.  
  297. private
  298. + def gateway(access_token)
  299. + Braintree::Gateway.new(:access_token => access_token)
  300. + end
Add Comment
Please, Sign In to add comment