Class: Braintree::CustomerGateway

Inherits:
Object
  • Object
show all
Includes:
BaseModule
Defined in:
lib/braintree/customer_gateway.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseModule

included

Methods included from BaseModule::Methods

#copy_instance_variables_from_object, #return_object_or_raise, #set_instance_variables_from_hash, #singleton_class

Constructor Details

#initialize(gateway) ⇒ CustomerGateway

Returns a new instance of CustomerGateway.



5
6
7
8
9
# File 'lib/braintree/customer_gateway.rb', line 5

def initialize(gateway)
  @gateway = gateway
  @config = gateway.config
  @config.assert_has_access_token_or_keys
end

Class Method Details

._create_signatureObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/braintree/customer_gateway.rb', line 63

def self._create_signature
  credit_card_signature = CreditCardGateway._create_signature - [:customer_id]
   = PayPalAccountGateway._create_nested_signature
  paypal_options_shipping_signature = AddressGateway._shared_signature
  options = [
    :paypal => [
      :payee_email,
      :order_id,
      :custom_field,
      :description,
      :amount,
      {:shipping => paypal_options_shipping_signature}
    ],
  ]
  [
    :company, :email, :fax, :first_name, :id,
    {:international_phone => [:country_code, :national_number]},
    :last_name, :phone, :website,
    :device_data, :payment_method_nonce,
    {:risk_data => [:customer_browser, :customer_ip]},
    {:credit_card => credit_card_signature},
    {:paypal_account => },
    {:tax_identifiers => [:country_code, :identifier]},
    {:options => options},
    {:custom_fields => :_any_key_}
  ]
end

._update_signatureObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/braintree/customer_gateway.rb', line 128

def self._update_signature
  credit_card_signature = CreditCardGateway._update_signature - [:customer_id]
  credit_card_options = credit_card_signature.find { |item| item.respond_to?(:keys) && item.keys == [:options] }
  credit_card_options[:options] << :update_existing_token
  paypal_options_shipping_signature = AddressGateway._shared_signature
  options = [
    :paypal => [
      :payee_email,
      :order_id,
      :custom_field,
      :description,
      :amount,
      {:shipping => paypal_options_shipping_signature}
    ],
  ]
  [
    :company, :email, :fax, :first_name, :id,
    {:international_phone => [:country_code, :national_number]},
    :last_name, :phone, :website,
    :device_data, :payment_method_nonce, :default_payment_method_token,
    {:credit_card => credit_card_signature},
    {:tax_identifiers => [:country_code, :identifier]},
    {:options => options},
    {:custom_fields => :_any_key_}
  ]
end

Instance Method Details

#_do_create(path, params = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/braintree/customer_gateway.rb', line 91

def _do_create(path, params=nil)
  response = @config.http.post("#{@config.base_merchant_path}#{path}", params)
  if response[:customer]
    SuccessfulResult.new(:customer => Customer._new(@gateway, response[:customer]))
  elsif response[:api_error_response]
    ErrorResult.new(@gateway, response[:api_error_response])
  else
    raise "expected :customer or :api_error_response"
  end
end

#_do_update(http_verb, path, params) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/braintree/customer_gateway.rb', line 102

def _do_update(http_verb, path, params)
  response = @config.http.send(http_verb, "#{@config.base_merchant_path}#{path}", params)
  if response[:customer]
    SuccessfulResult.new(:customer => Customer._new(@gateway, response[:customer]))
  elsif response[:api_error_response]
    ErrorResult.new(@gateway, response[:api_error_response])
  else
    raise UnexpectedError, "expected :customer or :api_error_response"
  end
end

#_fetch_customers(search, ids) ⇒ Object



113
114
115
116
117
118
# File 'lib/braintree/customer_gateway.rb', line 113

def _fetch_customers(search, ids)
  search.ids.in ids
  response = @config.http.post("#{@config.base_merchant_path}/customers/advanced_search", {:search => search.to_hash})
  attributes = response[:customers]
  Util.extract_attribute_as_array(attributes, :customer).map { |attrs| Customer._new(@gateway, attrs) }
end

#_fetch_transactions(customer_id, ids) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/braintree/customer_gateway.rb', line 120

def _fetch_transactions(customer_id, ids)
  response = @config.http.post("#{@config.base_merchant_path}/customers/#{customer_id}/transactions", :search => {:ids => ids})
  attributes = response[:credit_card_transactions]
  Util.extract_attribute_as_array(attributes, :transaction).map do |transaction_attributes|
    Transaction._new @gateway, transaction_attributes
  end
end

#allObject



11
12
13
14
# File 'lib/braintree/customer_gateway.rb', line 11

def all
  response = @config.http.post("#{@config.base_merchant_path}/customers/advanced_search_ids")
  ResourceCollection.new(response) { |ids| _fetch_customers(CustomerSearch.new, ids) }
end

#create(attributes = {}) ⇒ Object



16
17
18
19
# File 'lib/braintree/customer_gateway.rb', line 16

def create(attributes = {})
  Util.verify_keys(CustomerGateway._create_signature, attributes)
  _do_create "/customers", :customer => attributes
end

#create!(*args) ⇒ Object



21
22
23
# File 'lib/braintree/customer_gateway.rb', line 21

def create!(*args)
  return_object_or_raise(:customer) { create(*args) }
end

#delete(customer_id) ⇒ Object



25
26
27
28
# File 'lib/braintree/customer_gateway.rb', line 25

def delete(customer_id)
  @config.http.delete("#{@config.base_merchant_path}/customers/#{customer_id}")
  SuccessfulResult.new
end

#find(customer_id, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/braintree/customer_gateway.rb', line 30

def find(customer_id, options = {})
  raise ArgumentError, "customer_id contains invalid characters" unless customer_id.to_s =~ /\A[\w-]+\z/
  raise ArgumentError, "customer_id cannot be blank" if customer_id.nil?|| customer_id.to_s.strip == ""

  query_params = options[:association_filter_id].nil? ? "" : "?association_filter_id=#{options[:association_filter_id]}"
  response = @config.http.get("#{@config.base_merchant_path}/customers/#{customer_id}#{query_params}")
  Customer._new(@gateway, response[:customer])
rescue NotFoundError
  raise NotFoundError, "customer with id #{customer_id.inspect} not found"
end

#search(&block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/braintree/customer_gateway.rb', line 41

def search(&block)
  search = CustomerSearch.new
  block.call(search) if block

  response = @config.http.post("#{@config.base_merchant_path}/customers/advanced_search_ids", {:search => search.to_hash})
  ResourceCollection.new(response) { |ids| _fetch_customers(search, ids) }
end

#transactions(customer_id, options = {}) ⇒ Object



49
50
51
52
# File 'lib/braintree/customer_gateway.rb', line 49

def transactions(customer_id, options = {})
  response = @config.http.post("#{@config.base_merchant_path}/customers/#{customer_id}/transaction_ids")
  ResourceCollection.new(response) { |ids| _fetch_transactions(customer_id, ids) }
end

#update(customer_id, attributes) ⇒ Object



54
55
56
57
# File 'lib/braintree/customer_gateway.rb', line 54

def update(customer_id, attributes)
  Util.verify_keys(CustomerGateway._update_signature, attributes)
  _do_update(:put, "/customers/#{customer_id}", :customer => attributes)
end

#update!(*args) ⇒ Object



59
60
61
# File 'lib/braintree/customer_gateway.rb', line 59

def update!(*args)
  return_object_or_raise(:customer) { update(*args) }
end