Dima_S

Untitled

Oct 3rd, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 13.76 KB | None | 0 0
  1. dima@dima-Aspire-ES1-512:~/rails_project/accountdock-rails-30-09$ git diff
  2. diff --git a/app/controllers/direct/base_controller.rb b/app/controllers/direct/base_controller.rb
  3. index 4511afd..74744f5 100644
  4. --- a/app/controllers/direct/base_controller.rb
  5. +++ b/app/controllers/direct/base_controller.rb
  6. @@ -27,8 +27,8 @@ class Direct::BaseController < ApplicationController
  7.    def fetch_account
  8.      account_public_key = params[:direct_id] || params[:key] # Last is legacy
  9.      @account = Account.find_by_public_key(account_public_key)
  10. -    raise "Can't find the account" if @account.nil? or !@account.connection.present?
  11. -    @account.update_attribute(:installed, true) unless @account.installed?
  12. +    raise "Can't find the account" if @account.nil? or @account.connections.find_by(type: 'StripeConnection').nil?
  13. +    @account.connections.find_by(type: 'StripeConnection').update_attribute(:installed, true) unless @account.connections.find_by(type: 'StripeConnection').installed?
  14.    end
  15.    # The main purpose of this modal HTML is to show in
  16.    # someone else's website in an IFrame. So have to relax
  17. diff --git a/app/controllers/direct/settings_controller.rb b/app/controllers/direct/settings_controller.rb
  18. index 543440a..37aaf18 100644
  19. --- a/app/controllers/direct/settings_controller.rb
  20. +++ b/app/controllers/direct/settings_controller.rb
  21. @@ -1,6 +1,6 @@
  22.  class Direct::SettingsController < Direct::BaseController
  23.    def index
  24. -    @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
  25. +    @customer = @account.customers.find_by_vendor_customer_id(params[:customer_id])
  26.      raise "Can't find the customer" if @customer.nil?
  27.  
  28.      render layout: false
  29. @@ -8,7 +8,7 @@ class Direct::SettingsController < Direct::BaseController
  30.  
  31.    def create
  32.      unless @account.is_demo?
  33. -      @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
  34. +      @customer = @account.customers.find_by_vendor_customer_id(params[:customer_id])
  35.        raise "Can't find the customer" if @customer.nil?
  36.        
  37.        settings = settings_params
  38. diff --git a/app/controllers/direct/transactions_controller.rb b/app/controllers/direct/transactions_controller.rb
  39. index 4ba46e6..402064d 100644
  40. --- a/app/controllers/direct/transactions_controller.rb
  41. +++ b/app/controllers/direct/transactions_controller.rb
  42. @@ -1,6 +1,6 @@
  43.  class Direct::TransactionsController < Direct::BaseController
  44.    def index
  45. -    @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
  46. +    @customer = @account.customers.find_by_vendor_customer_id(params[:customer_id])
  47.      raise "Can't find the customer" if @customer.nil?
  48.      
  49.      # TODO: This was in the PHP javascript code to pass in timezone.
  50. @@ -13,7 +13,7 @@ class Direct::TransactionsController < Direct::BaseController
  51.      @collection.sort! {|a, b| b.created_at <=> a.created_at}
  52.  
  53.      begin
  54. -      @upcoming_invoice = @account.stripe.upcoming_invoice(@customer.stripe_customer_id)
  55. +      @upcoming_invoice = @account.client.upcoming_invoice(@customer.vendor_customer_id)
  56.      rescue => e
  57.      end
  58.  
  59. diff --git a/app/controllers/stripe_controller.rb b/app/controllers/stripe_controller.rb
  60. index 91b8d7d..1f899eb 100644
  61. --- a/app/controllers/stripe_controller.rb
  62. +++ b/app/controllers/stripe_controller.rb
  63. @@ -18,6 +18,7 @@ class StripeController < ApplicationController
  64.  
  65.      if account.new_record?
  66.        #generate Account
  67. +  binding.pry
  68.        account.set_handle(stripe_account.display_name || stripe_account.email[/[^@]+/])
  69.        if account.valid?
  70.          account.save
  71. diff --git a/app/controllers/team/connection_controller.rb b/app/controllers/team/connection_controller.rb
  72. index 88d764c..0716cee 100644
  73. --- a/app/controllers/team/connection_controller.rb
  74. +++ b/app/controllers/team/connection_controller.rb
  75. @@ -4,6 +4,7 @@ class Team::ConnectionController < ApplicationController
  76.    end
  77.  
  78.    def switch
  79. +binding.pry
  80.      account = current_user.accounts.find_by_public_key(params[:public_key])
  81.      not_found if account.nil?
  82.  
  83. diff --git a/app/models/account.rb b/app/models/account.rb
  84. index 4023555..7a1ba0a 100644
  85. --- a/app/models/account.rb
  86. +++ b/app/models/account.rb
  87. @@ -112,7 +112,7 @@ class Account < ActiveRecord::Base
  88.  
  89.    # Is there a Vendor connection?
  90.    def has_connection?
  91. -    self.connections.present?
  92. +    self.connections.braintree_connection.present? && self.connections.stripe_connection.present?
  93.    end
  94.  
  95.    # Vendor Accessors
  96. diff --git a/app/models/account_stripe_event.rb b/app/models/account_stripe_event.rb
  97. index f7506b5..adb4b7f 100644
  98. --- a/app/models/account_stripe_event.rb
  99. +++ b/app/models/account_stripe_event.rb
  100. @@ -53,7 +53,7 @@ class AccountStripeEvent < ActiveRecord::Base
  101.      case event.type
  102.        # Customers
  103.        when 'customer.created', 'customer.updated' # Includes updating default card
  104. -        account.sync_stripe_customer(event.data.object)
  105. +        account.sync_customer(event.data.object)
  106.        when 'customer.deleted'
  107.          account.connections.stripe_connection.remove_customer(event.data.object)
  108.        # TODO: Remove this OLD API shiz when we upgrade the Stripe API:
  109. diff --git a/app/models/connections/braintree_connection.rb b/app/models/connections/braintree_connection.rb
  110. index 8c176ff..cb8858c 100644
  111. --- a/app/models/connections/braintree_connection.rb
  112. +++ b/app/models/connections/braintree_connection.rb
  113. @@ -36,8 +36,8 @@ class BraintreeConnection < Connection
  114.    end
  115.  
  116.    def braintree(account)
  117. -    @braintree ||= BraintreeClient.new(account.connections.braintree_connection.try(:secret_key))
  118. -    @account = account
  119. +    return @braintree ||= BraintreeClient.new(account.connections.braintree_connection.try(:secret_key))
  120. +    # @account = account
  121.    end
  122.  
  123.    def vendor
  124. diff --git a/app/models/connections/stripe_connection.rb b/app/models/connections/stripe_connection.rb
  125. index e31e387..e5ccbe4 100644
  126. --- a/app/models/connections/stripe_connection.rb
  127. +++ b/app/models/connections/stripe_connection.rb
  128. @@ -37,7 +37,7 @@ class StripeConnection < Connection
  129.  
  130.    def stripe(account)
  131.      @stripe ||= StripeClient.new(account.connections.stripe_connection.try(:secret_key))
  132. -    @account = account
  133. +    # @account = account
  134.    end
  135.  
  136.    def vendor
  137. diff --git a/app/services/braintree_services/sync_service.rb b/app/services/braintree_services/sync_service.rb
  138. index 6a9a16c..aeb7fab 100644
  139. --- a/app/services/braintree_services/sync_service.rb
  140. +++ b/app/services/braintree_services/sync_service.rb
  141. @@ -1,10 +1,11 @@
  142.  class BraintreeServices::SyncService < SharedServices::BaseSyncService
  143.    private
  144.    def sync_customers
  145. +
  146.      sync_message("Customers")
  147.  
  148.      updated_customers = []
  149. -    braintree_customers = Braintree::Gateway.new(access_token: @account.connections.braintree_connection.secret_key).customer.all.ids
  150. +    braintree_customers = @account.connections.braintree_connection.braintree(@account).customers.all.ids
  151.      braintree_customers.each do |customer|
  152.        updated_customers << customer
  153.        sync_braintree_customer(customer)
  154. @@ -42,7 +43,17 @@ class BraintreeServices::SyncService < SharedServices::BaseSyncService
  155.      if braintree_customer.try(:deleted)
  156.        customer.update_attributes({ deleted: true, live: livemode? })
  157.      else
  158. -      customer.assign_attributes({ })
  159. +      customer.assign_attributes({
  160. +        email: braintree_customer.email.try(:delete, "\000"),
  161. +        default_source: default_card(braintree_customer),
  162. +        # description: braintree_customer.description || '',
  163. +        # metadata_raw: braintree_customer.metadata.to_json,
  164. +        live: Braintree::Configuration.environment.to_s == 'production',
  165. +        # account_balance: braintree_customer.account_balance,
  166. +        # currency: braintree_customer.currency,
  167. +        # business_vat_id: braintree_customer.try(:business_vat_id),
  168. +        created_at: Time.at(braintree_customer.created)
  169. +      })
  170.        customer.save!
  171.      end
  172.      customer
  173. @@ -51,4 +62,12 @@ class BraintreeServices::SyncService < SharedServices::BaseSyncService
  174.      retry unless (attempts -= 1).zero?
  175.      raise ex
  176.    end
  177. +
  178. +  def default_card(customer)
  179. +    customer.payment_methods.each do |payment_methods|
  180. +      if payment_methods.default?
  181. +        return payment_methods.token
  182. +      end
  183. +    end
  184. +  end
  185.  end
  186. diff --git a/app/services/stripe_services/sync_service.rb b/app/services/stripe_services/sync_service.rb
  187. index 28582db..2cf2bb0 100644
  188. --- a/app/services/stripe_services/sync_service.rb
  189. +++ b/app/services/stripe_services/sync_service.rb
  190. @@ -1,7 +1,6 @@
  191.  class StripeServices::SyncService < SharedServices::BaseSyncService
  192. -  private
  193. +  # private
  194.    def sync_customers
  195. -    byebug
  196.      sync_message('Customers')
  197.      updated_customers = []
  198.      options = {limit: 100}
  199. @@ -13,7 +12,7 @@ class StripeServices::SyncService < SharedServices::BaseSyncService
  200.        end
  201.      end
  202.      # This marks historic data from AccountDock PHP... future webhooks should do this
  203. -    @account.customers.where.not(stripe_customer_id: updated_customers).update_all(deleted: true)
  204. +    @account.customers.where.not(vendor_customer_id: updated_customers).update_all(deleted: true)
  205.      # Theoretically this isn't necessary (we should only have added new)
  206.      unless @account.customers.count == @account.customers_count
  207.        puts "Customer Cache varied by #{@account.customers_count - @account.customers.count}."
  208. @@ -85,7 +84,7 @@ class StripeServices::SyncService < SharedServices::BaseSyncService
  209.    def sync_stripe_customer(stripe_customer)
  210.      attempts ||= 2
  211.  
  212. -    customer = @account.customers.where(stripe_customer_id: stripe_customer.id).first_or_initialize
  213. +    customer = @account.customers.where(vendor_customer_id: stripe_customer.id).first_or_initialize
  214.      # Note stripe can give us a neutered version of a customer if the customer has been
  215.      # deleted. It looks like: #<Stripe::Customer:0x3f id=cus_3cT> JSON: { "id": "cus_3cT", "deleted": true }
  216.      if stripe_customer.try(:deleted) # Non-deleted customers, don't even respond to deleted.
  217. @@ -136,7 +135,7 @@ class StripeServices::SyncService < SharedServices::BaseSyncService
  218.    def sync_cards(customer, default_source = '')
  219.      updated_cards = []
  220.      options = {limit: 100}
  221. -    while (stripe_cards = @account.connections.stripe_connection.cards(customer.stripe_customer_id, options).to_a).any?
  222. +    while (stripe_cards = @account.connections.stripe_connection.cards(customer.vendor_customer_id, options).to_a).any?
  223.        options[:starting_after] = stripe_cards.last.id
  224.        stripe_cards.each do |cc|
  225.          updated_cards << cc.id # Store the stripe ID for cleaning below
  226. diff --git a/app/views/direct/base/app.html.haml b/app/views/direct/base/app.html.haml
  227. index a57edb7..d5c4f37 100644
  228. --- a/app/views/direct/base/app.html.haml
  229. +++ b/app/views/direct/base/app.html.haml
  230. @@ -7,8 +7,8 @@
  231.      = javascript_include_tag '//api.filestackapi.com/filestack.js', 'application', 'angular'
  232.      :javascript
  233.        var app = angular.module('app', [])
  234. -        .constant('TRAN', "#{direct_customer_transactions_path(@account.public_key, @customer.stripe_customer_id, format: :json)}")
  235. -        .constant('SETTING', "#{direct_customer_settings_path(@account.public_key, @customer.stripe_customer_id, format: :json)}")
  236. +        .constant('TRAN', "#{direct_customer_transactions_path(@account.public_key, @customer.vendor_customer_id, format: :json)}")
  237. +        .constant('SETTING', "#{direct_customer_settings_path(@account.public_key, @customer.vendor_customer_id, format: :json)}")
  238.          .constant('BULK', {email: "#{charges_bulk_email_path(key: @account.public_key, format: :json)}", zip: "#{charges_bulk_zip_url(key: @account.public_key)}"})
  239.          .constant('EMAIL', "#{@customer.email}")
  240.          .constant('DISPLAY_NAME', "#{@account.setting.email_sender_display_name}")
  241. diff --git a/app/views/direct/modals/_settings.html.haml b/app/views/direct/modals/_settings.html.haml
  242. index 6bb332b..655a7e5 100644
  243. --- a/app/views/direct/modals/_settings.html.haml
  244. +++ b/app/views/direct/modals/_settings.html.haml
  245. @@ -4,7 +4,7 @@
  246.      %div
  247.        %header Receipt Settings
  248.        %a.closeBtn.fa.fa-close{href: '#', 'ng-click'=>"closeModal('settings')"}
  249. -      = form_tag  direct_customer_settings_path(direct_id: @account.public_key, customer_id: @customer.stripe_customer_id, format: :json), remote: true, role: 'form' do
  250. +      = form_tag  direct_customer_settings_path(direct_id: @account.public_key, customer_id: @customer.vendor_customer_id, format: :json), remote: true, role: 'form' do
  251.          .body
  252.            .columns.clearfix
  253.              .column
  254. diff --git a/lib/braintree_client.rb b/lib/braintree_client.rb
  255. index 18ffd6b..0a871d8 100644
  256. --- a/lib/braintree_client.rb
  257. +++ b/lib/braintree_client.rb
  258. @@ -27,12 +27,17 @@ class BraintreeClient
  259.  
  260.    [:customer].each do |m|
  261.      braintree_class = "Braintree::#{m.capitalize}".constantize
  262. +    # BraintreeClient.oauth_gateway
  263.      define_method(m) do |*opts|
  264. -      braintree_class.find(*opts, @access_token)
  265. +      braintree_class.find(*opts)
  266.      end
  267.      define_method(m.to_s.pluralize) do |opts={}|
  268. -      default_options = { limit: 100 }
  269. -      braintree_class.all(default_options.merge(opts), @access_token)
  270. +      # default_options = { limit: 100 }
  271. +      bt_customers = []
  272. +      braintree_class.all.ids.each do |bt_customer|
  273. +        bt_customers << Braintree::Customer.find(bt_customer)
  274. +      end
  275. +      return bt_customers
  276.      end
  277.      # TODO
  278.      define_method("#{m.to_s.pluralize}_count") do
  279. @@ -104,7 +109,7 @@ class BraintreeClient
  280.      @gateway ||= Braintree::Gateway.new({
  281.        client_id:     config[:client_id],
  282.        client_secret: config[:client_secret],
  283. -      client_public: config[:client_public],
  284. +      # client_public: config[:client_public],
  285.        environment:   config[:environment]
  286.      }).oauth
  287.    end
Advertisement
Add Comment
Please, Sign In to add comment