Class: Balanced::Customer

Inherits:
Object
  • Object
show all
Includes:
Resource
Defined in:
lib/balanced/resources/customer.rb

Overview

A customer represents a business or person within your Marketplace. A customer can have many funding instruments such as cards and bank accounts associated to them.

Instance Attribute Summary

Attributes included from Resource

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

#copy_from, #destroy, #find, included, #method_missing, #reload, #sanitize, #save, #unstore, #warn_on_positional

Constructor Details

#initialize(attributes = {}) ⇒ Customer

Returns a new instance of Customer.



14
15
16
17
18
19
20
# File 'lib/balanced/resources/customer.rb', line 14

def initialize attributes = {}
  Balanced::Utils.stringify_keys! attributes
  unless attributes.has_key? 'uri'
    attributes['uri'] = self.class.uri
  end
  super attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Balanced::Resource

Class Method Details

.find_by_email(email) ⇒ Customer?

Attempts to find an existing customer by email

NOTE: There is no unique constraint on email_address.

Multiple customers with the same email may exist.
Only one Customer is returned.

Parameters:

  • email (String)

    An email of a customer

Returns:

  • (Customer)

    if customer is found

  • (nil)

    if customer is not found



31
32
33
# File 'lib/balanced/resources/customer.rb', line 31

def self.find_by_email email
  self.find(:first, :email => email)
end

.uriObject



9
10
11
12
# File 'lib/balanced/resources/customer.rb', line 9

def self.uri
  # Override the default nesting
   self.collection_path
end

Instance Method Details

#active_bank_accountObject



105
106
107
108
109
110
111
112
# File 'lib/balanced/resources/customer.rb', line 105

def 
  pager = Pager.new(
    self.bank_accounts_uri,
    :is_active => true,
    :sort => 'created_at,desc',
    :limit => 1)
  pager.first
end

#active_cardObject



96
97
98
99
100
101
102
103
# File 'lib/balanced/resources/customer.rb', line 96

def active_card
  pager = Pager.new(
    self.cards_uri,
    :is_active => true,
    :sort => 'created_at,desc',
    :limit => 1)
  pager.first
end

#add_bank_account(bank_account) ⇒ Customer

Associates the BankAccount represented by bank_account with this Customer.

Returns:



88
89
90
91
92
93
94
# File 'lib/balanced/resources/customer.rb', line 88

def ()
  if .kind_of?(Balanced::BankAccount) && .fingerprint.nil?
    .save
  end
  self. = Balanced::Utils.extract_uri_from_object()
  save
end

#add_card(card) ⇒ Customer

Associates the Card represented by ‘card’ with this Customer.

Returns:



78
79
80
81
82
# File 'lib/balanced/resources/customer.rb', line 78

def add_card(card)
  card.save if card.kind_of?(Balanced::Card) && card.hash.nil?
  self.card_uri = Balanced::Utils.extract_uri_from_object(card)
  save
end

#credit(options = {}) ⇒ Object



69
70
71
72
# File 'lib/balanced/resources/customer.rb', line 69

def credit(options = {})
  options.merge!(:uri => self.credits_uri)
  Credit.new(options).save
end

#debit(options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/balanced/resources/customer.rb', line 35

def debit(options = {})
  amount = options[:amount]
  appears_on_statement_as = options[:appears_on_statement_as]
  hold_uri = options[:hold_uri]
  meta = options[:meta]
  description = options[:description]
  source_uri = options[:source_uri]
  on_behalf_of = options[:on_behalf_of] || options[:on_behalf_of_uri]

  if on_behalf_of
    if on_behalf_of.respond_to? :uri
      on_behalf_of = on_behalf_of.uri
    end
    if !on_behalf_of.is_a?(String)
      raise ArgumentError, 'The on_behalf_of parameter needs to be a customer URI'
    end
    if on_behalf_of == self.uri
      raise ArgumentError, 'The on_behalf_of parameter MAY NOT be the same account as the customer you are debiting!'
    end
  end

  debit = Debit.new(
      :uri => self.debits_uri,
      :amount => amount,
      :appears_on_statement_as => appears_on_statement_as,
      :hold_uri => hold_uri,
      :meta => meta,
      :description => description,
      :source_uri => source_uri,
      :on_behalf_of_uri => on_behalf_of,
  )
  debit.save
end