lib/braintree/customer.rb in braintree-1.0.0 vs lib/braintree/customer.rb in braintree-1.0.1

- old
+ new

@@ -1,11 +1,11 @@ module Braintree class Customer include BaseModule attr_reader :addresses, :company, :created_at, :credit_cards, :email, :fax, :first_name, :id, :last_name, - :phone, :updated_at, :website + :phone, :updated_at, :website, :custom_fields # Returns a PagedCollection of all customers stored in the vault. Due to race conditions, this method # may not reliably return all customers stored in the vault. # # page = Braintree::Customer.all @@ -90,10 +90,21 @@ end def self.sale!(customer_id, transaction_attributes) return_object_or_raise(:transaction){ sale(customer_id, transaction_attributes) } end + + # Returns a PagedCollection of transactions for the customer with the given +customer_id+. + def self.transactions(customer_id, options = {}) + page_number = options[:page] || 1 + response = Http.get "/customers/#{customer_id}/transactions?page=#{page_number}" + attributes = response[:credit_card_transactions] + attributes[:items] = Util.extract_attribute_as_array(attributes, :transaction).map do |transaction_attributes| + Transaction._new transaction_attributes + end + PagedCollection.new(attributes) { |page_number| Customer.transactions(customer_id, :page => page_number) } + end def self.update(customer_id, attributes) Util.verify_keys(_update_signature, attributes) _do_update(:put, "/customers/#{customer_id}", :customer => attributes) end @@ -116,11 +127,11 @@ @credit_cards = (@credit_cards || []).map { |pm| CreditCard._new pm } @addresses = (@addresses || []).map { |addr| Address._new addr } end def credit(transaction_attributes) - Customer.credit(self.id, transaction_attributes) + Customer.credit(id, transaction_attributes) end def credit!(transaction_attributes) return_object_or_raise(:transaction) { credit(transaction_attributes) } end @@ -138,26 +149,20 @@ end "#<#{self.class} #{nice_attributes.join(', ')}>" end def sale(transaction_attributes) - Customer.sale(self.id, transaction_attributes) + Customer.sale(id, transaction_attributes) end def sale!(transaction_attributes) return_object_or_raise(:transaction) { sale(transaction_attributes) } end - # Finds transactions associated to the customer. Returns a PagedCollection. + # Returns a PagedCollection of transactions for the customer. def transactions(options = {}) - page_number = options[:page] || 1 - response = Http.get "/customers/#{id}/transactions?page=#{page_number}" - attributes = response[:credit_card_transactions] - attributes[:items] = Util.extract_attribute_as_array(attributes, :transaction).map do |transaction_attributes| - Transaction._new transaction_attributes - end - PagedCollection.new(attributes) { |page_number| self.transactions(:page => page_number) } + Customer.transactions(id, options) end def update(attributes) response = Http.put "/customers/#{id}", :customer => attributes if response[:customer] @@ -192,11 +197,12 @@ def self._create_signature # :nodoc: credit_card_signature = CreditCard._create_signature - [:customer_id] [ :company, :email, :fax, :first_name, :id, :last_name, :phone, :website, - {:credit_card => credit_card_signature} + {:credit_card => credit_card_signature}, + {:custom_fields => :_any_key_} ] end def self._do_create(url, params) # :nodoc: response = Http.post url, params @@ -223,9 +229,9 @@ def self._new(*args) # :nodoc: self.new *args end def self._update_signature # :nodoc: - [ :company, :email, :fax, :first_name, :id, :last_name, :phone, :website ] + [ :company, :email, :fax, :first_name, :id, :last_name, :phone, :website, {:custom_fields => :_any_key_} ] end end end