-
Notifications
You must be signed in to change notification settings - Fork 195
/
customer.rb
195 lines (164 loc) · 7.46 KB
/
customer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
module Braintree
# See http://www.braintreepaymentsolutions.com/docs/ruby
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
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/search
def self.all
Configuration.gateway.customer.all
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/create
def self.create(attributes = {})
Configuration.gateway.customer.create(attributes)
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/create
def self.create!(attributes = {})
return_object_or_raise(:customer) { create(attributes) }
end
# Deprecated. Use Braintree::TransparentRedirect.url
#
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/create_tr
def self.create_customer_url
warn "[DEPRECATED] Customer.create_customer_url is deprecated. Please use TransparentRedirect.url"
Configuration.gateway.customer.create_customer_url
end
# Deprecated. Use Braintree::TransparentRedirect.confirm
#
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/create_tr
def self.create_from_transparent_redirect(query_string)
warn "[DEPRECATED] Customer.create_from_transparent_redirect is deprecated. Please use TransparentRedirect.confirm"
Configuration.gateway.customer.create_from_transparent_redirect(query_string)
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create_from_vault
def self.credit(customer_id, transaction_attributes)
Transaction.credit(transaction_attributes.merge(:customer_id => customer_id))
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create_from_vault
def self.credit!(customer_id, transaction_attributes)
return_object_or_raise(:transaction){ credit(customer_id, transaction_attributes) }
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/delete
def self.delete(customer_id)
Configuration.gateway.customer.delete(customer_id)
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/search
def self.find(customer_id)
Configuration.gateway.customer.find(customer_id)
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create_from_vault
def self.sale(customer_id, transaction_attributes)
Transaction.sale(transaction_attributes.merge(:customer_id => customer_id))
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create_from_vault
def self.sale!(customer_id, transaction_attributes)
return_object_or_raise(:transaction) { sale(customer_id, transaction_attributes) }
end
# Returns a ResourceCollection of transactions for the customer with the given +customer_id+.
def self.transactions(customer_id, options = {})
Configuration.gateway.customer.transactions(customer_id, options = {})
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/update
def self.update(customer_id, attributes)
Configuration.gateway.customer.update(customer_id, attributes)
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/update
def self.update!(customer_id, attributes)
return_object_or_raise(:customer) { update(customer_id, attributes) }
end
# Deprecated. Use Braintree::TransparentRedirect.url
#
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/update_tr
def self.update_customer_url
warn "[DEPRECATED] Customer.update_customer_url is deprecated. Please use TransparentRedirect.url"
Configuration.gateway.customer.update_customer_url
end
# Deprecated. Use Braintree::TransparentRedirect.confirm
#
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/update_tr
def self.update_from_transparent_redirect(query_string)
warn "[DEPRECATED] Customer.update_from_transparent_redirect is deprecated. Please use TransparentRedirect.confirm"
Configuration.gateway.customer.update_from_transparent_redirect(query_string)
end
def initialize(gateway, attributes) # :nodoc:
@gateway = gateway
set_instance_variables_from_hash(attributes)
@credit_cards = (@credit_cards || []).map { |pm| CreditCard._new gateway, pm }
@addresses = (@addresses || []).map { |addr| Address._new gateway, addr }
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create_from_vault
def credit(transaction_attributes)
@gateway.transaction.credit(transaction_attributes.merge(:customer_id => id))
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create_from_vault
def credit!(transaction_attributes)
return_object_or_raise(:transaction) { credit(transaction_attributes) }
end
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/delete
def delete
@gateway.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
# Deprecated. Use Braintree::Customer.sale
#
# See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create_from_vault
def sale(transaction_attributes)
warn "[DEPRECATED] sale as an instance method is deprecated. Please use Customer.sale"
@gateway.transaction.sale(transaction_attributes.merge(:customer_id => id))
end
# Deprecated. Use Braintree::Customer.sale!
#
# See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create_from_vault
def sale!(transaction_attributes)
warn "[DEPRECATED] sale! as an instance method is deprecated. Please use Customer.sale!"
return_object_or_raise(:transaction) { sale(transaction_attributes) }
end
# Returns a ResourceCollection of transactions for the customer.
def transactions(options = {})
@gateway.customer.transactions(id, options)
end
# Deprecated. Use Braintree::Customer.update
#
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/update
def update(attributes)
warn "[DEPRECATED] update as an instance method is deprecated. Please use Customer.update"
result = @gateway.customer.update(id, attributes)
if result.success?
copy_instance_variables_from_object result.customer
end
result
end
# Deprecated. Use Braintree::Customer.update!
#
# See http://www.braintreepaymentsolutions.com/docs/ruby/customers/update
def update!(attributes)
warn "[DEPRECATED] update! as an instance method is deprecated. Please use Customer.update!"
return_object_or_raise(:customer) { update(attributes) }
end
# Returns true if +other+ is a Customer with the same id
def ==(other)
return false unless other.is_a?(Customer)
id == other.id
end
class << self
protected :new
end
def self._new(*args) # :nodoc:
self.new *args
end
def self._attributes # :nodoc:
[
:addresses, :company, :credit_cards, :email, :fax, :first_name, :id, :last_name, :phone, :website,
:created_at, :updated_at
]
end
end
end