lib/braintree/customer.rb in braintree-1.0.1 vs lib/braintree/customer.rb in braintree-1.1.0
- old
+ new
@@ -1,12 +1,12 @@
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, :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
# loop do
@@ -23,11 +23,11 @@
attributes[:items] = Util.extract_attribute_as_array(attributes, :customer).map do |customer_attributes|
new customer_attributes
end
PagedCollection.new(attributes) { |page_number| Customer.all(:page => page_number) }
end
-
+
# Creates a customer using the given +attributes+. If <tt>:id</tt> is not passed,
# the gateway will generate it.
#
# result = Braintree::Customer.create(
# :first_name => "John",
@@ -45,11 +45,11 @@
# end
def self.create(attributes = {})
Util.verify_keys(_create_signature, attributes)
_do_create "/customers", :customer => attributes
end
-
+
def self.create!(attributes = {})
return_object_or_raise(:customer) { create(attributes) }
end
def self.create_customer_url
@@ -58,39 +58,39 @@
def self.create_from_transparent_redirect(query_string)
params = TransparentRedirect.parse_and_validate_query_string query_string
_do_create("/customers/all/confirm_transparent_redirect_request", :id => params[:id])
end
-
+
def self.create_customer_transparent_redirect_url
"#{Braintree::Configuration.base_merchant_url}/customers"
end
-
+
def self.credit(customer_id, transaction_attributes)
Transaction.credit(transaction_attributes.merge(:customer_id => customer_id))
end
-
+
def self.credit!(customer_id, transaction_attributes)
return_object_or_raise(:transaction){ credit(customer_id, transaction_attributes) }
end
def self.delete(customer_id)
Http.delete("/customers/#{customer_id}")
SuccessfulResult.new
end
-
+
def self.find(customer_id)
response = Http.get("/customers/#{customer_id}")
new(response[:customer])
rescue NotFoundError
raise NotFoundError, "customer with id #{customer_id.inspect} not found"
end
-
+
def self.sale(customer_id, transaction_attributes)
Transaction.sale(transaction_attributes.merge(:customer_id => customer_id))
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+.
@@ -101,16 +101,16 @@
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
-
+
def self.update!(customer_id, attributes)
return_object_or_raise(:customer) { update(customer_id, attributes) }
end
def self.update_customer_url
@@ -119,43 +119,43 @@
def self.update_from_transparent_redirect(query_string)
params = TransparentRedirect.parse_and_validate_query_string(query_string)
_do_update(:post, "/customers/all/confirm_transparent_redirect_request", :id => params[:id])
end
-
+
def initialize(attributes) # :nodoc:
set_instance_variables_from_hash(attributes)
@credit_cards = (@credit_cards || []).map { |pm| CreditCard._new pm }
@addresses = (@addresses || []).map { |addr| Address._new addr }
end
-
+
def credit(transaction_attributes)
Customer.credit(id, transaction_attributes)
end
-
+
def credit!(transaction_attributes)
return_object_or_raise(:transaction) { credit(transaction_attributes) }
end
-
+
def delete
Customer.delete(id)
end
-
+
def inspect # :nodoc:
first = [:id]
last = [:addresses, :credit_cards]
order = first + (self.class._attributes - first - last) + last
nice_attributes = order.map do |attr|
"#{attr}: #{send(attr).inspect}"
end
"#<#{self.class} #{nice_attributes.join(', ')}>"
end
-
+
def sale(transaction_attributes)
Customer.sale(id, transaction_attributes)
end
-
+
def sale!(transaction_attributes)
return_object_or_raise(:transaction) { sale(transaction_attributes) }
end
# Returns a PagedCollection of transactions for the customer.
@@ -170,13 +170,13 @@
SuccessfulResult.new(:customer => self)
elsif response[:api_error_response]
ErrorResult.new(response[:api_error_response])
else
raise "expected :customer or :errors"
- end
+ end
end
-
+
def update!(attributes)
return_object_or_raise(:customer) { update(attributes) }
end
def ==(other)
@@ -226,10 +226,10 @@
end
end
def self._new(*args) # :nodoc:
self.new *args
- end
+ end
def self._update_signature # :nodoc:
[ :company, :email, :fax, :first_name, :id, :last_name, :phone, :website, {:custom_fields => :_any_key_} ]
end
end