Class: Balanced::Account
- Inherits:
-
Object
- Object
- Balanced::Account
- 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
Class Method Summary collapse
-
.find_by_email(email) ⇒ Account?
Attempts to find an existing buyer account by email.
Instance Method Summary collapse
-
#add_bank_account(bank_account) ⇒ Account
Associates the BankAccount represented by
bank_account_uriwith this Account. -
#add_card(card) ⇒ Account
Associates the Card represented by
card_uriwith this Account. -
#credit(*args) ⇒ Credit
Returns a new Credit representing a transfer of funds from your Marketplace to this Account.
-
#debit(*args) ⇒ Debit
Returns a new Debit that represents a flow of money from this Account to your Marketplace’s escrow account.
-
#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.
-
#initialize(attributes = {}) ⇒ Account
constructor
A new instance of Account.
-
#promote_to_merchant(merchant) ⇒ Object
Adds the role Merchant to this Account.
Methods included from Resource
#copy_from, #destroy, #find, included, #method_missing, #reload, #sanitize, #save, #unstore, #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
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) ⇒ Account
Associates the BankAccount represented by bank_account_uri with this Account.
125 126 127 128 129 130 131 |
# File 'lib/balanced/resources/account.rb', line 125 def add_bank_account(bank_account) if bank_account.kind_of?(Balanced::BankAccount) && bank_account.fingerprint.nil? bank_account.save end self.bank_account_uri = Balanced::Utils.extract_uri_from_object(bank_account) save end |
#add_card(card) ⇒ Account
Associates the Card represented by card_uri with this Account.
115 116 117 118 119 |
# File 'lib/balanced/resources/account.rb', line 115 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(*args) ⇒ Credit
Returns a new Credit representing a 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.
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 = args.last.is_a?(Hash) ? args.pop : {} amount = args[0] || .fetch(:amount) { nil } appears_on_statement_as = args[1] || .fetch(:appears_on_statement_as) { nil } hold_uri = args[2] || .fetch(:hold_uri) { nil } = args[3] || .fetch(:meta) { nil } description = args[4] || .fetch(:description) { nil } source_uri = args[5] || .fetch(:source_uri) { nil } on_behalf_of = args[6] || .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 => , :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.
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 = args.last.is_a?(Hash) ? args.pop : {} amount = args[0] || .fetch(:amount) { } = args[1] || .fetch(:meta) { nil } source_uri = args[2] || .fetch(:source_uri) { nil } hold = Hold.new( :uri => self.holds_uri, :amount => amount, :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
}
}
167 168 169 170 171 172 173 174 |
# File 'lib/balanced/resources/account.rb', line 167 def promote_to_merchant merchant if merchant.is_a?(String) self.merchant_uri = merchant else self.merchant = merchant end save end |