Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dima@dima-Aspire-ES1-512:~/rails_project/accountdock-rails$ git diff
- diff --cc app/controllers/braintree_controller.rb
- index a8b7af5,bda8c5c..0000000
- --- a/app/controllers/braintree_controller.rb
- +++ b/app/controllers/braintree_controller.rb
- @@@ -41,9 -40,10 +41,8 @@@ class BraintreeController < Application
- end
- token_params[:vendor_customer_id] = params[:merchantId]
- --
- connection = BraintreeConnection.find_or_initialize_by(account_id: account.id)
- -binding.pry
- - connection.update(token_params)
- + connection.update!(token_params)
- AccountSyncJob.perform_later(account) unless connection.synced
- diff --git a/app/controllers/direct/base_controller.rb b/app/controllers/direct/base_controller.rb
- index 4511afd..b7122bb 100644
- --- a/app/controllers/direct/base_controller.rb
- +++ b/app/controllers/direct/base_controller.rb
- @@ -24,11 +24,12 @@ class Direct::BaseController < ApplicationController
- end
- private
- +
- def fetch_account
- account_public_key = params[:direct_id] || params[:key] # Last is legacy
- @account = Account.find_by_public_key(account_public_key)
- - raise "Can't find the account" if @account.nil? or [email protected]?
- - @account.update_attribute(:installed, true) unless @account.installed?
- + raise "Can't find the account" if @account.nil? or @account.connections.find_by(type: 'StripeConnection').nil?
- + @account.connections.find_by(type: 'StripeConnection').update_attribute(:installed, true) unless @account.connections.find_by(type: 'StripeConnection').installed?
- end
- # The main purpose of this modal HTML is to show in
- # someone else's website in an IFrame. So have to relax
- diff --git a/app/controllers/direct/settings_controller.rb b/app/controllers/direct/settings_controller.rb
- index 543440a..37aaf18 100644
- --- a/app/controllers/direct/settings_controller.rb
- +++ b/app/controllers/direct/settings_controller.rb
- @@ -1,6 +1,6 @@
- class Direct::SettingsController < Direct::BaseController
- def index
- - @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
- + @customer = @account.customers.find_by_vendor_customer_id(params[:customer_id])
- raise "Can't find the customer" if @customer.nil?
- render layout: false
- @@ -8,7 +8,7 @@ class Direct::SettingsController < Direct::BaseController
- def create
- unless @account.is_demo?
- - @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
- + @customer = @account.customers.find_by_vendor_customer_id(params[:customer_id])
- raise "Can't find the customer" if @customer.nil?
- settings = settings_params
- diff --git a/app/controllers/direct/transactions_controller.rb b/app/controllers/direct/transactions_controller.rb
- index 4ba46e6..b416b08 100644
- --- a/app/controllers/direct/transactions_controller.rb
- +++ b/app/controllers/direct/transactions_controller.rb
- @@ -1,6 +1,6 @@
- class Direct::TransactionsController < Direct::BaseController
- def index
- - @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
- + @customer = @account.customers.find_by_stripe_customer_id(params[:customer_id])
- raise "Can't find the customer" if @customer.nil?
- # TODO: This was in the PHP javascript code to pass in timezone.
- @@ -13,7 +13,7 @@ class Direct::TransactionsController < Direct::BaseController
- @collection.sort! {|a, b| b.created_at <=> a.created_at}
- begin
- - @upcoming_invoice = @account.stripe.upcoming_invoice(@customer.stripe_customer_id)
- + @upcoming_invoice = @account.client.upcoming_invoice(@customer.vendor_customer_id)
- rescue => e
- end
- diff --git a/app/controllers/stripe_controller.rb b/app/controllers/stripe_controller.rb
- index 82555bf..893a355 100644
- --- a/app/controllers/stripe_controller.rb
- +++ b/app/controllers/stripe_controller.rb
- @@ -46,6 +46,7 @@ class StripeController < ApplicationController
- end
- connection = StripeConnection.find_or_initialize_by(account_id: account.id)
- +
- connection.update(token_params)
- AccountSyncJob.perform_later(account) unless connection.synced
- diff --git a/app/models/account.rb b/app/models/account.rb
- index befc591..a6f6d4c 100644
- --- a/app/models/account.rb
- +++ b/app/models/account.rb
- @@ -114,7 +114,7 @@ class Account < ActiveRecord::Base
- # Is there a Vendor connection?
- def has_connection?
- - self.connections.present?
- + self.connections.braintree.present? && self.connections.stripe.present?
- end
- # Vendor Accessors
- diff --git a/app/models/connections/braintree_connection.rb b/app/models/connections/braintree_connection.rb
- index 3929f9b..acdab0f 100644
- --- a/app/models/connections/braintree_connection.rb
- +++ b/app/models/connections/braintree_connection.rb
- @@ -40,7 +40,7 @@ class BraintreeConnection < Connection
- def client(account)
- BraintreeClient.new(self.access_token)
- - @account = account
- + # @account = account
- end
- def connection_present?
- diff --git a/app/services/braintree_services/sync_service.rb b/app/services/braintree_services/sync_service.rb
- index 7e50a1a..c4d082e 100644
- --- a/app/services/braintree_services/sync_service.rb
- +++ b/app/services/braintree_services/sync_service.rb
- @@ -1,10 +1,10 @@
- class BraintreeServices::SyncService < SharedServices::BaseSyncService
- private
- def sync_customers
- - sync_message("Customers")
- + sync_message('Customers')
- updated_customers = []
- - braintree_customers = Braintree::Gateway.new(access_token: @connection.access_token).customer.all.ids
- + braintree_customers = @connection.client(@account).customers
- braintree_customers.each do |customer|
- updated_customers << customer
- sync_braintree_customer(customer)
- @@ -38,11 +38,20 @@ class BraintreeServices::SyncService < SharedServices::BaseSyncService
- def sync_braintree_customer(braintree_customer)
- attempts ||= 2
- - customer = @account.customers.where(vendor_customer_id: braintree_customer).first_or_initialize
- - if braintree_customer.try(:deleted)
- - customer.update_attributes({ deleted: true, live: livemode? })
- - else
- - customer.assign_attributes({ })
- + customer = @account.customers.where(vendor_customer_id: braintree_customer.id).first_or_initialize
- + if braintree_customer
- + customer.update_attributes({ deleted: true, live: false })
- + customer.assign_attributes({
- + email: braintree_customer.email.try(:delete, "\000"),
- + default_source: default_card(braintree_customer),
- + # description: braintree_customer.description || '',
- + # metadata_raw: braintree_customer.metadata.to_json,
- + live: @connection.client_id.include?('sandbox'),
- + # account_balance: braintree_customer.account_balance,
- + # currency: braintree_customer.currency,
- + # business_vat_id: braintree_customer.try(:business_vat_id),
- + created_at: Time.at(braintree_customer.created_at)
- + })
- customer.save!
- end
- customer
- @@ -51,4 +60,12 @@ class BraintreeServices::SyncService < SharedServices::BaseSyncService
- retry unless (attempts -= 1).zero?
- raise ex
- end
- +
- + def default_card(customer)
- + customer.payment_methods.each do |payment_methods|
- + if payment_methods.default?
- + return payment_methods.token
- + end
- + end
- + end
- end
- diff --git a/app/services/stripe_services/sync_service.rb b/app/services/stripe_services/sync_service.rb
- index e200538..4048a7f 100644
- --- a/app/services/stripe_services/sync_service.rb
- +++ b/app/services/stripe_services/sync_service.rb
- @@ -1,5 +1,5 @@
- class StripeServices::SyncService < SharedServices::BaseSyncService
- - private
- + # private
- def sync_customers
- sync_message('Customers')
- updated_customers = []
- diff --git a/app/views/direct/base/app.html.haml b/app/views/direct/base/app.html.haml
- index a57edb7..d5c4f37 100644
- --- a/app/views/direct/base/app.html.haml
- +++ b/app/views/direct/base/app.html.haml
- @@ -7,8 +7,8 @@
- = javascript_include_tag '//api.filestackapi.com/filestack.js', 'application', 'angular'
- :javascript
- var app = angular.module('app', [])
- - .constant('TRAN', "#{direct_customer_transactions_path(@account.public_key, @customer.stripe_customer_id, format: :json)}")
- - .constant('SETTING', "#{direct_customer_settings_path(@account.public_key, @customer.stripe_customer_id, format: :json)}")
- + .constant('TRAN', "#{direct_customer_transactions_path(@account.public_key, @customer.vendor_customer_id, format: :json)}")
- + .constant('SETTING', "#{direct_customer_settings_path(@account.public_key, @customer.vendor_customer_id, format: :json)}")
- .constant('BULK', {email: "#{charges_bulk_email_path(key: @account.public_key, format: :json)}", zip: "#{charges_bulk_zip_url(key: @account.public_key)}"})
- .constant('EMAIL', "#{@customer.email}")
- .constant('DISPLAY_NAME', "#{@account.setting.email_sender_display_name}")
- diff --git a/lib/braintree_client.rb b/lib/braintree_client.rb
- index 630358d..cc8d58e 100644
- --- a/lib/braintree_client.rb
- +++ b/lib/braintree_client.rb
- @@ -17,12 +17,101 @@ class BraintreeClient
- publishable_key: ''
- }
- end
- +
- + def self.get_account(vendor_user_id, access_token)
- + Braintree::Customer.find(vendor_user_id, access_token)
- + end
- +
- + def initialize(access_token)
- + @access_token = access_token
- + end
- +
- + [:customer].each do |m|
- + braintree_class = "Braintree::#{m.capitalize}".constantize
- + define_method(m) do |*opts|
- + braintree_class.find(*opts, @access_token)
- + end
- + define_method(m.to_s.pluralize) do |opts={}|
- + bt_customers = []
- + gateway(@access_token).customer.all.ids.each do |bt_customer|
- + bt_customers << gateway(@access_token).customer.find(bt_customer)
- + end
- + return bt_customers
- + end
- + # TODO
- + define_method("#{m.to_s.pluralize}_count") do
- + return -1 unless @access_token
- + braintree_class.all({ include: ['total_count'], limit: 1}, @access_token).total_count rescue -1
- + end
- + end
- +
- + # Cards need customer..
- + def cards(customer_id, options)
- + Braintree::Customer.find(customer_id, @access_token).credit_cards
- + end
- +
- + # it's plug, see more http://www.rubydoc.info/github/braintree/braintree_ruby/Braintree/Dispute
- + def update_dispute(vendor_dispute_id, new_evidence)
- + true
- + end
- +
- + def source(vendor_source_id)
- + Braintree::CreditCard.find(vendor_source_id)
- + end
- +
- + def user_subscriptions(customer_id)
- + customer = self.customer(customer_id)
- + customer.credit_cards.map(&:subscriptions).flatten
- + end
- +
- + # it's plug
- + def refunds_by_charge (vendor_charge_id, options = {})
- + true
- + end
- +
- + # it's plug
- + def upcoming_invoice(vendor_customer_id)
- + true
- + end
- +
- + def create_customer(email)
- + Braintree::Customer.create({email: email}, @access_token)
- + end
- +
- + def create_card(customer_id, params)
- + card = Braintree::CreditCard.create(
- + customer_id: customer_id,
- + number: params[:number],
- + expiration_month: params[:expiration_month],
- + expiration_year: params[:expiration_year],
- + cvv: params[:cvv],
- + options: {:verify_card => true}
- + )
- + end
- +
- + # TODO string-89 method first! (should be defoult, but we can not add default card)
- + def create_subscription_for_plan (customer_id, plan_id)
- + customer = self.customer(customer_id)
- + Braintree::Subscription.create(
- + plan_id: plan_id,
- + payment_method_nonce: customer.payment_methods.first.token
- + )
- + end
- +
- + # it's plug
- + def ping
- + true
- + end
- def initialize(access_token)
- @access_token = access_token
- end
- private
- + def gateway(access_token)
- + Braintree::Gateway.new(:access_token => access_token)
- + end
- +
- def self.oauth_gateway
- @gateway ||= Braintree::Gateway.new({
- client_id: config[:client_id],
- (END)
Advertisement
Add Comment
Please, Sign In to add comment