summaryrefslogtreecommitdiff
path: root/billing/app/models/customer.rb
blob: fa43b96570b55b765695d7e564ab7a47d76e4624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Customer < CouchRest::Model::Base

  #FIELDS = [:first_name, :last_name, :phone, :website, :company, :fax, :addresses, :credit_cards, :custom_fields]

  use_database "customers"
  belongs_to :user
  property :braintree_customer_id

  design do
    view :by_user_id
    view :by_braintree_customer_id
  end

  def has_payment_info?
    !!braintree_customer_id
  end

  # from braintree_ruby_examples/rails3_tr_devise and should be tweaked
=begin
  def with_braintree_data!
    return self unless has_payment_info?
    braintree_data = Braintree::Customer.find(braintree_customer_id)

    #FIELDS.each do |field|
    #  send(:"#{field}=", braintree_data.send(field))
    #end
    self
  end
=end

  #slow to get Braintree Customer data, so pass it if have already retrieved it
  # won't really have multiple credit cards on file
  def default_credit_card(braintree_data = nil)
    return unless has_payment_info?
    braintree_data = braintree_data || Braintree::Customer.find(braintree_customer_id)
    braintree_data.credit_cards.find { |cc| cc.default? }
  end

  #todo will this be plural?
  def active_subscriptions(braintree_data=nil)
    subscriptions = Array.new
    braintree_data = braintree_data || Braintree::Customer.find(braintree_customer_id)
    braintree_data.credit_cards.each do |cc|
      cc.subscriptions.each do |sub|
        subscriptions << sub if sub.status == 'Active'
      end
    end
    subscriptions
  end

  def single_subscription(braintree_data=nil)
    self.active_subscriptions(braintree_data).first
  end



end