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 4eaa46a,02db7eb..0000000
- --- a/app/controllers/braintree_controller.rb
- +++ b/app/controllers/braintree_controller.rb
- @@@ -5,10 -5,48 +5,11 @@@ class BraintreeController < Application
- end
- def callback
- -binding.pry
- token_params = BraintreeClient.get_token(params[:code])
- -
- - # TODO Create a new account with the adding payment service provider
- - if current_account
- - account_connection = current_account.connections.braintree.find_or_initialize_by(vendor_customer_id: params[:merchantId])
- - account = account_connection.account
- - else
- - account = Account.first_or_initialize
- - end
- -
- - # TODO Get merchant name, email from Braintree Auth when it will be provide this functionality.
- - # TODO Now it doesn't. https://github.com/braintree/braintree_ruby/issues/124
- - braintree_account_name = 'example_name'
- - braintree_account_email = '[email protected]'
- -
- - if account.new_record?
- - account.set_handle(braintree_account_name || braintree_account_email[/[^@]+/])
- - if account.valid?
- - account.save
- - else
- - raise "Can't create account object!"
- - end
- -
- - account.build_setting
- - account.setting.save!
- - end
- -
- - unless current_user.accounts.where(id: account.id).any?
- - UserAccount.create!({
- - role: account.user_accounts.count > 0 ? 'owner' : 'member',
- - user_id: current_user.id,
- - account_id: account.id}
- - )
- - end
- -
- token_params[:vendor_customer_id] = params[:merchantId]
- - connection = BraintreeConnection.find_or_initialize_by(account_id: account.id)
- - connection.update!(token_params)
- - AccountSyncJob.perform_later(account) unless connection.synced
- + account = BraintreeServices::ConnectService.new(token_params, current_user, current_account).call
- +
- redirect_to team_switch_path(public_key: account.public_key, mode: params[:mode], path: 'dashboard')
- end
- end
- diff --cc app/controllers/stripe_controller.rb
- index b364145,893a355..0000000
- --- a/app/controllers/stripe_controller.rb
- +++ b/app/controllers/stripe_controller.rb
- diff --cc app/models/connections/braintree_connection.rb
- index 34f2226,acdab0f..0000000
- --- a/app/models/connections/braintree_connection.rb
- +++ b/app/models/connections/braintree_connection.rb
- diff --git a/app/controllers/direct/base_controller.rb b/app/controllers/direct/base_controller.rb
- index 4511afd..0691216 100644
- --- a/app/controllers/direct/base_controller.rb
- +++ b/app/controllers/direct/base_controller.rb
- @@ -10,7 +10,7 @@ class Direct::BaseController < ApplicationController
- format.html {
- fetch_account
- begin
- - @customer = @account.ensure_customer(params[:customer])
- + @customer = @account.connections.find_by(type: 'StripeConnection').ensure_customer(params[:customer])
- @handler = params[:handler]
- @container = params[:container]
- @container = 'window' if @container.nil?
- @@ -24,10 +24,11 @@ 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]?
- + raise "Can't find the account" if @account.nil? or @account.connections.find_by(type: 'StripeConnection').nil?
- @account.update_attribute(:installed, true) unless @account.installed?
- end
- # The main purpose of this modal HTML is to show in
- 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..a3a79ca 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_vendor_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/team/home_controller.rb b/app/controllers/team/home_controller.rb
- index 9aa61a4..c8fad97 100644
- --- a/app/controllers/team/home_controller.rb
- +++ b/app/controllers/team/home_controller.rb
- @@ -18,7 +18,7 @@ class Team::HomeController < Team::BaseController
- def view_payment_failed_email
- @customer = current_account.customers.new(email: current_user.email,
- - stripe_customer_id: 'cus_1234')
- + vendor_customer_id: 'cus_1234')
- @customer.cards.new(name: "Test Customer")
- render text: process_template(@customer, current_account.setting.first_failed_payment_body)
- end
- diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
- index 3a40af4..5b06291 100644
- --- a/app/controllers/users_controller.rb
- +++ b/app/controllers/users_controller.rb
- @@ -19,11 +19,11 @@ class UsersController < ApplicationController
- end
- def save_creditcard
- - if current_user.stripe_customer_id.blank?
- + if current_user.vendor_customer_id.blank?
- customer = STRIPE.create_customer(current_user.email)
- - current_user.update({stripe_customer_id: customer.id})
- + current_user.update({vendor_customer_id: customer.id})
- end
- - card = STRIPE.create_card(current_user.stripe_customer_id, params[:stripe_token])
- + card = STRIPE.create_card(current_user.vendor_customer_id, params[:stripe_token])
- redirect_to team_home_path, notice: 'Credit card is added successfully.' if current_account.is_pro
- redirect_to users_upgrade_path, notice: 'Credit card is added successfully.' unless current_account.is_pro
- @@ -37,7 +37,7 @@ class UsersController < ApplicationController
- plan = Plan.find_by_id(params[:plan_id])
- redirect_to users_upgrade_path if plan.nil?
- - subscription = STRIPE.create_subscription_for_plan(current_user.stripe_customer_id, plan.stripe_id)
- + subscription = STRIPE.create_subscription_for_plan(current_user.vendor_customer_id, plan.stripe_id)
- if subscription.id
- current_account.update({is_pro: true})
- current_account.setting.update({show_branding: false})
- @@ -66,7 +66,7 @@ class UsersController < ApplicationController
- end
- def has_stripe_customer
- - redirect_to users_creditcard_path if current_user.stripe_customer_id.blank?
- + redirect_to users_creditcard_path if current_user.vendor_customer_id.blank?
- end
- end
- diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
- index ad0749b..6cb3664 100644
- --- a/app/helpers/application_helper.rb
- +++ b/app/helpers/application_helper.rb
- @@ -48,8 +48,8 @@ module ApplicationHelper
- def process_template(customer, txt)
- txt.gsub!(/{account.displayName}/, customer.account.setting.display_name || '')
- - txt.gsub!(/{customer.id}/, customer.stripe_customer_id)
- - txt.gsub!(/{customer.urls.payment}/, edit_direct_customer_payment_url(customer.account.public_key, customer.stripe_customer_id))
- + txt.gsub!(/{customer.id}/, customer.vendor_customer_id)
- + txt.gsub!(/{customer.urls.payment}/, edit_direct_customer_payment_url(customer.account.public_key, customer.vendor_customer_id))
- txt.gsub!(/{customer.firstName( \|\| '([^']+)')?}/) { |q| customer.first_name || $2 }
- txt.gsub!(/{customer.name( \|\| '([^']+)')?}/) { |q| customer.raw_name || $2 }
- txt.gsub!(/{customer.email}/, customer.notifications_email_address)
- diff --git a/app/helpers/charges_helper.rb b/app/helpers/charges_helper.rb
- index 1e15ada..267ca70 100644
- --- a/app/helpers/charges_helper.rb
- +++ b/app/helpers/charges_helper.rb
- @@ -15,7 +15,7 @@ module ChargesHelper
- end
- if charge.customer.present?
- - note.gsub!('{customer.id}', charge.customer.stripe_customer_id)
- + note.gsub!('{customer.id}', charge.customer.vendor_customer_id)
- note.gsub!('{customer.email}', charge.customer.email || '')
- note.gsub!('{customer.description}', charge.customer.description.blank? ? '' : charge.customer.description)
- charge.customer.metadata.each do |key, value|
- diff --git a/app/mailers/customer_mailer.rb b/app/mailers/customer_mailer.rb
- index 244ee2b..d7607ee 100644
- --- a/app/mailers/customer_mailer.rb
- +++ b/app/mailers/customer_mailer.rb
- @@ -29,7 +29,7 @@ class CustomerMailer < ApplicationMailer
- if microdata
- # This should only show on emails that are for expiring credit cards
- microdata_html = render partial: 'customer_mailer/microdata', # One-click action from GMail
- - locals: { url: edit_direct_customer_payment_url(customer.account.public_key, customer.stripe_customer_id) }
- + locals: { url: edit_direct_customer_payment_url(customer.account.public_key, customer.vendor_customer_id) }
- html_body += microdata_html
- end
- diff --git a/app/models/account.rb b/app/models/account.rb
- index 2fa9f03..d22cff6 100644
- --- a/app/models/account.rb
- +++ b/app/models/account.rb
- @@ -110,7 +110,7 @@ class Account < ActiveRecord::Base
- # Is there a Vendor connection?
- def has_connection?
- - self.connections.present?
- + self.connections.last.present?
- end
- def vendor_customers_estimate
- diff --git a/app/models/connections/stripe_connection.rb b/app/models/connections/stripe_connection.rb
- index 297a77d..88c8cdf 100644
- --- a/app/models/connections/stripe_connection.rb
- +++ b/app/models/connections/stripe_connection.rb
- @@ -67,7 +67,7 @@ class StripeConnection < Connection
- Invoice.unscoped do
- # Get all the invoices in the db that match the stripe invoices:
- db_invoices = self.account.invoices.where(stripe_invoice_id: stripe_invoices.map { |s| s.id })
- - db_customers = self.account.customers.where(stripe_customer_id: stripe_invoices.map { |s| s.customer })
- + db_customers = self.account.customers.where(vendor_customer_id: stripe_invoices.map { |s| s.customer })
- # Then iterate over all the stripe invoices and add/update DB invoices as needed:
- stripe_invoices.collect do |stripe_invoice|
- # Use the invoice if we have it in the db:
- @@ -75,7 +75,7 @@ class StripeConnection < Connection
- # Or build a new one if we don't:
- db_invoice ||= self.account.invoices.build(stripe_invoice_id: stripe_invoice.id)
- # Make sure we have a customer and invoice is not orphaned:
- - db_customer = db_customers.select { |c| c.stripe_customer_id == stripe_invoice.customer }.first
- + db_customer = db_customers.select { |c| c.vendor_customer_id == stripe_invoice.customer }.first
- # Update the customer only if it wasn't already in the DB:
- db_customer ||= self.account.ensure_customer(stripe_invoice.customer)
- # Update the db / save the record:
- @@ -203,7 +203,7 @@ class StripeConnection < Connection
- def remove_customer(stripe_customer)
- Customer.unscoped do
- - customer = self.account.customers.find_by_stripe_customer_id(stripe_customer.id)
- + customer = self.account.customers.find_by_vendor_customer_id(stripe_customer.id)
- customer.nil? ? nil : customer.update({deleted: true})
- end
- end
- @@ -288,7 +288,7 @@ class StripeConnection < Connection
- def ensure_invoice(stripe_invoice_id)
- begin
- # Do we have this invoice in the DB already?
- - invoice = Invoice.unscoped { self.invoices.find_by_stripe_invoice_id(stripe_invoice_id) }
- + invoice = Invoice.unscoped { self.account.invoices.find_by_stripe_invoice_id(stripe_invoice_id) }
- # If so, return it
- return invoice unless invoice.blank?
- # Otherwise go get it:
- diff --git a/app/models/customer.rb b/app/models/customer.rb
- index c1fd0e0..a619f1f 100644
- --- a/app/models/customer.rb
- +++ b/app/models/customer.rb
- @@ -42,8 +42,8 @@ class Customer < ActiveRecord::Base
- has_many :invoices, dependent: :destroy
- has_many :charges, dependent: :destroy
- has_many :subscriptions,
- - foreign_key: :stripe_customer_id,
- - primary_key: :stripe_customer_id,
- + foreign_key: :vendor_customer_id,
- + primary_key: :vendor_customer_id,
- dependent: :destroy
- has_many :activities, dependent: :destroy
- @@ -135,7 +135,7 @@ class Customer < ActiveRecord::Base
- def stripe_admin_url
- url = 'https://dashboard.stripe.com/'
- url += 'test/' unless self.live
- - url += 'customers/' + self.stripe_customer_id
- + url += 'customers/' + self.vendor_customer_id
- url
- end
- @@ -247,7 +247,7 @@ class Customer < ActiveRecord::Base
- super
- else
- # whitelist only the title and body attributes for other users
- - super & %w(email stripe_customer_id created_at)
- + super & %w(email vendor_customer_id created_at)
- end
- end
- diff --git a/app/services/braintree_services/sync_service.rb b/app/services/braintree_services/sync_service.rb
- index f218a72..496d468 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.customers
- braintree_customers.each do |customer|
- updated_customers << customer
- sync_vendor_customer(customer)
- @@ -37,11 +37,20 @@ class BraintreeServices::SyncService < SharedServices::BaseSyncService
- def sync_vendor_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: [email protected]_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
- @@ -50,4 +59,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 387abca..519cb19 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 = []
- @@ -26,7 +26,7 @@ class StripeServices::SyncService < SharedServices::BaseSyncService
- options = {limit: 100}
- while (stripe_invoices = @connection.client.invoices(options).to_a).any?
- options[:starting_after] = stripe_invoices.last.id
- - @connection.client.add_or_update_invoices(stripe_invoices)
- + @connection.add_or_update_invoices(stripe_invoices)
- end
- end
- @@ -67,7 +67,7 @@ class StripeServices::SyncService < SharedServices::BaseSyncService
- while (stripe_subscriptions = @connection.client.subscriptions(options).to_a).any?
- options[:starting_after] = stripe_subscriptions.last.id
- stripe_subscriptions.each do |dp|
- - @connection.client.add_or_update_subscription(dp)
- + @connection.add_or_update_subscription(dp)
- end
- end
- # Theoretically this isn't necessary (we should only have added new)
- @@ -120,8 +120,8 @@ class StripeServices::SyncService < SharedServices::BaseSyncService
- raise ex
- end
- - def sync_customer(stripe_customer_id)
- - stripe_customer = @connection.client.customer(stripe_customer_id)
- + def sync_customer(vendor_customer_id)
- + stripe_customer = @connection.client.customer(vendor_customer_id)
- sync_vendor_customer(stripe_customer)
- end
- diff --git a/app/views/admin/customers/_form.html.haml b/app/views/admin/customers/_form.html.haml
- index f48301d..51cd519 100644
- --- a/app/views/admin/customers/_form.html.haml
- +++ b/app/views/admin/customers/_form.html.haml
- @@ -10,8 +10,8 @@
- = f.label :public_key
- = f.text_field :public_key
- .field
- - = f.label :stripe_customer_id
- - = f.text_field :stripe_customer_id
- + = f.label :vendor_customer_id
- + = f.text_field :vendor_customer_id
- .field
- = f.label :email
- = f.text_field :email
- diff --git a/app/views/admin/customers/index.html.haml b/app/views/admin/customers/index.html.haml
- index a680a94..973a3ff 100644
- --- a/app/views/admin/customers/index.html.haml
- +++ b/app/views/admin/customers/index.html.haml
- @@ -17,7 +17,7 @@
- - @customers.each do |customer|
- %tr
- %td= link_to customer.public_key, [:admin, customer]
- - %td= customer.stripe_customer_id
- + %td= customer.vendor_customer_id
- %td= customer.email
- %td= link_to customer.account_id, [:admin, customer.account]
- %td= customer.subscriptions
- diff --git a/app/views/admin/customers/show.html.haml b/app/views/admin/customers/show.html.haml
- index fab39b3..9791e28 100644
- --- a/app/views/admin/customers/show.html.haml
- +++ b/app/views/admin/customers/show.html.haml
- @@ -5,7 +5,7 @@
- = @customer.public_key
- %p
- %b Stripe customer:
- - = @customer.stripe_customer_id
- + = @customer.vendor_customer_id
- %p
- %b Email:
- = @customer.email
- 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/app/views/direct/modals/_settings.html.haml b/app/views/direct/modals/_settings.html.haml
- index 6bb332b..655a7e5 100644
- --- a/app/views/direct/modals/_settings.html.haml
- +++ b/app/views/direct/modals/_settings.html.haml
- @@ -4,7 +4,7 @@
- %div
- %header Receipt Settings
- %a.closeBtn.fa.fa-close{href: '#', 'ng-click'=>"closeModal('settings')"}
- - = form_tag direct_customer_settings_path(direct_id: @account.public_key, customer_id: @customer.stripe_customer_id, format: :json), remote: true, role: 'form' do
- + = form_tag direct_customer_settings_path(direct_id: @account.public_key, customer_id: @customer.vendor_customer_id, format: :json), remote: true, role: 'form' do
- .body
- .columns.clearfix
- .column
- diff --git a/app/views/team/customers/index.html.haml b/app/views/team/customers/index.html.haml
- index ca3cb66..0fe12ce 100644
- --- a/app/views/team/customers/index.html.haml
- +++ b/app/views/team/customers/index.html.haml
- @@ -57,11 +57,11 @@
- = customer.charges.count
- %td.text-right
- - if current_account.allow_payment_page_control
- - = 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'
- + = 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'
- - if nil # These should move to a customer detail page
- = 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.'
- = 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.'
- - %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}"}
- + %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}"}
- View Billing
- %hr
- .row
- 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