Skip to content
lyang edited this page Mar 17, 2013 · 7 revisions

Attributes

Create

[:company, :custom_fields, :email, :fax, :first_name, :id, :last_name, :options, :phone, :website]

Update

[:company, :custom_fields, :email, :fax, :first_name, :last_name, :options, :phone, :website]

Readonly

[:created_at, :updated_at]

Associations

Addresses

customer = BraintreeRails::Customer.find('customer_id')
customer.addresses # eager loaded collection from Braintree::Customer.find

# initialize a address ready to be added to the customer
address = customer.addresses.build(:postal_code => '12345')
address.save!

# add an address to the customer
address = customer.addresses.create(:postal_code => '12345')

# find a address from the cached collection instead of firing API call
address = customer.addresses.find('address_id') # Note, this find does not need the extra customer_id

CreditCards

customer = BraintreeRails::Customer.find('customer_id')
customer.credit_cards # eager loaded collection from Braintree::Customer.find

# initialize a credit card ready to be added to the customer
credit_card = customer.credit_cards.build(:number => 'encrypted_by_braintree_js')
credit_card.save!

# add a credit card to the customer
credit_card = customer.credit_cards.create(:number => 'encrypted_by_braintree_js')

# find a credit card from the cached collection instead of firing API call
credit_card = customer.credit_cards.find('credit_card_token')

Transactions

customer = BraintreeRails::Customer.find('customer_id')
customer.transactions # lazy loaded collection through Braintree::Transaction.search

# initialize a transaction ready to be created using the default credit card of the customer
transaction = customer.transactions.build(:amount => '10.00')
transaction.save!

# initialize a transaction ready to be created for this customer using a new credit card
transaction = customer.transactions.build(:amount => '10.00', :credit_card => {...}, :billing => {...})

# create a transaction using the default card
transaction = customer.transactions.create(:amount => '11.00')

Default Validations

[:id, :format => {:with => /\A[-_a-z0-9]*\z/i}, :length => {:maximum => 36}, :exclusion => {:in => %w(all new)}],
[:first_name, :last_name, :company, :website, :phone, :fax, :length => {:maximum => 255}]

Default Callbacks

(before|after)_validate
(before|around|after)_save
(before|around|after)_create
(before|around|after)_update
(before|around|after)_destroy

Convenient Methods

Customer#full_name

def full_name
  "#{first_name} #{last_name}".strip
end

Customer#default_credit_card

def default_credit_card
  credit_cards.find(&:default?)
end

Supported REST APIs

Create

Find

Update

Destroy