Class: Balanced::Account

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

Overview

An Account represents a user within your Marketplace. An Account can have two roles. If the Account has the buyer role then you may create Debits using this Account. If they have the merchant role then you may create Credits to transfer funds to this Account.

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, #save, #warn_on_positional

Constructor Details

#initialize(attributes = {}) ⇒ Account

Returns a new instance of Account.



10
11
12
13
14
15
16
# File 'lib/balanced/resources/account.rb', line 10

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) ⇒ Account?

Attempts to find an existing buyer account by email

Parameters:

  • email (String)

    An email of an account

Returns:

  • (Account)

    if account is found

  • (nil)

    if account is not found



23
24
25
# File 'lib/balanced/resources/account.rb', line 23

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

Instance Method Details

#add_bank_account(bank_account_uri) ⇒ BankAccount

Associates the BankAccount represented by bank_account_uri with this Account.

Returns:



124
125
126
127
# File 'lib/balanced/resources/account.rb', line 124

def  
  self. = 
  save
end

#add_card(card_uri) ⇒ Card

Associates the Card represented by card_uri with this Account.

Returns:



115
116
117
118
# File 'lib/balanced/resources/account.rb', line 115

def add_card card_uri
  self.card_uri = card_uri
  save
end

#credit(*args) ⇒ Credit

Returns a new Credit representing a transfer of funds from your Marketplace to this Account.

Parameters:

  • destination_uri (String)

    A specific funding destination such as a BankAccount already associated with this account.

Returns:

  • (Credit)

    A Credit representing the transfer of funds from your Marketplace to this Account.



101
102
103
104
105
106
107
108
109
110
# File 'lib/balanced/resources/account.rb', line 101

def credit *args
  warn_on_positional args

  if args.last.is_a? Hash
    args.last.merge! uri: self.credits_uri
  else
    args << { uri: self.credits_uri }
  end
  Credit.new(*args).save
end

#debit(*args) ⇒ Debit

Returns a new Debit that represents a flow of money from this Account to your Marketplace’s escrow account.

Returns:



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
58
59
60
61
62
63
64
65
66
# File 'lib/balanced/resources/account.rb', line 31

def debit *args
  warn_on_positional args

  options = args.last.is_a?(Hash) ? args.pop : {}
  amount = args[0] || options.fetch(:amount) { nil }
  appears_on_statement_as = args[1] || options.fetch(:appears_on_statement_as) { nil }
  hold_uri = args[2] || options.fetch(:hold_uri) { nil }
  meta = args[3] || options.fetch(:meta) { nil }
  description = args[4] || options.fetch(:description) { nil }
  source_uri = args[5] || options.fetch(:source_uri) { nil }
  on_behalf_of = args[6] || options.fetch(:on_behalf_of) { nil }

  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 an account URI'
    end
    if on_behalf_of == self.uri
      raise ArgumentError, 'The on_behalf_of parameter MAY NOT be the same account as the account 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

#hold(*args) ⇒ Hold

Returns a new Hold that represents a reservation of money on this Account which can be transferred via a Debit to your Marketplace up to 7 days later.

Parameters:

  • source_uri (String)

    A specific funding source such as a Card associated with this account. If not specified the most recently added Card is used.

Returns:

  • (Hold)

    A Hold representing the reservation of funds from this Account to your Marketplace.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/balanced/resources/account.rb', line 77

def hold *args
  warn_on_positional args

  options = args.last.is_a?(Hash) ? args.pop : {}
  amount = args[0] || options.fetch(:amount) { }
  meta = args[1] || options.fetch(:meta) { nil }
  source_uri = args[2] || options.fetch(:source_uri) { nil }

  hold = Hold.new(
      :uri => self.holds_uri,
      :amount => amount,
      :meta => meta,
      :source_uri => source_uri,
  )
  hold.save
end

#promote_to_merchant(merchant) ⇒ Object

Adds the role Merchant to this Account.

The merchant data for a person should look like:

{
  :type => "person",
  :name => "William James",        # Legal name
  :street_address => "167 West 74th Street",
  :postal_code => "10023",
  :country_code => "USA",          # ISO 3166-1 alpha-3
  :dob => "1842-01",
  :phone_number => "+16505551234"
}

For a business, the payload should look like:

{
  :name => "Levain Bakery",
  :tax_id => "253912384",          # Optional
  :street_address => "167 West 74th Street",
  :postal_code => "10023",
  :phone_number => "+16505551234",
  :country_code => "USA",          # ISO 3166-1 alpha-3
  :person => {
     :name => "William James",     # Legal name
     :tax_id => "393483992",       # Optional
     :street_address => "167 West 74th Street",  # Home address
     :postal_code => "10023",
     :dob => "1842-01-01",
     :phone_number => "+16505551234",
     :country_code => "USA"        # ISO 3166-1 alpha-3
   }
}


163
164
165
166
167
168
169
170
# File 'lib/balanced/resources/account.rb', line 163

def promote_to_merchant merchant
  if merchant.is_a?(String)
    self.merchant_uri = merchant
  else
    self.merchant = merchant
  end
  save
end