braintree 2.4.0 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/README.rdoc +4 -0
  2. data/lib/braintree.rb +43 -32
  3. data/lib/braintree/add_on.rb +4 -0
  4. data/lib/braintree/address.rb +18 -72
  5. data/lib/braintree/address_gateway.rb +76 -0
  6. data/lib/braintree/advanced_search.rb +31 -13
  7. data/lib/braintree/base_module.rb +6 -0
  8. data/lib/braintree/configuration.rb +57 -39
  9. data/lib/braintree/credit_card.rb +75 -129
  10. data/lib/braintree/credit_card_gateway.rb +133 -0
  11. data/lib/braintree/credit_card_verification.rb +8 -0
  12. data/lib/braintree/customer.rb +70 -123
  13. data/lib/braintree/customer_gateway.rb +121 -0
  14. data/lib/braintree/digest.rb +2 -2
  15. data/lib/braintree/discount.rb +4 -0
  16. data/lib/braintree/error_codes.rb +50 -5
  17. data/lib/braintree/error_result.rb +4 -18
  18. data/lib/braintree/errors.rb +1 -2
  19. data/lib/braintree/exceptions.rb +11 -16
  20. data/lib/braintree/gateway.rb +39 -0
  21. data/lib/braintree/http.rb +30 -26
  22. data/lib/braintree/modification.rb +23 -0
  23. data/lib/braintree/resource_collection.rb +1 -1
  24. data/lib/braintree/subscription.rb +29 -129
  25. data/lib/braintree/subscription_gateway.rb +122 -0
  26. data/lib/braintree/subscription_search.rb +6 -7
  27. data/lib/braintree/successful_result.rb +1 -12
  28. data/lib/braintree/test/credit_card_numbers.rb +4 -2
  29. data/lib/braintree/test/transaction_amounts.rb +3 -0
  30. data/lib/braintree/transaction.rb +83 -243
  31. data/lib/braintree/transaction/credit_card_details.rb +4 -4
  32. data/lib/braintree/transaction_gateway.rb +124 -0
  33. data/lib/braintree/transaction_search.rb +5 -3
  34. data/lib/braintree/transparent_redirect.rb +19 -112
  35. data/lib/braintree/transparent_redirect_gateway.rb +105 -0
  36. data/lib/braintree/util.rb +4 -0
  37. data/lib/braintree/validation_error.rb +1 -0
  38. data/lib/braintree/validation_error_collection.rb +5 -23
  39. data/lib/braintree/version.rb +2 -2
  40. data/lib/braintree/xml/parser.rb +1 -1
  41. data/lib/braintree/xml/rexml.rb +2 -2
  42. data/spec/integration/braintree/advanced_search_spec.rb +532 -0
  43. data/spec/integration/braintree/credit_card_spec.rb +5 -8
  44. data/spec/integration/braintree/http_spec.rb +53 -39
  45. data/spec/integration/braintree/subscription_spec.rb +678 -213
  46. data/spec/integration/braintree/transaction_search_spec.rb +318 -43
  47. data/spec/integration/braintree/transaction_spec.rb +134 -3
  48. data/spec/integration/braintree/transparent_redirect_spec.rb +1 -1
  49. data/spec/spec_helper.rb +55 -4
  50. data/spec/unit/braintree/address_spec.rb +8 -8
  51. data/spec/unit/braintree/base_module_spec.rb +1 -1
  52. data/spec/unit/braintree/configuration_spec.rb +34 -29
  53. data/spec/unit/braintree/credit_card_spec.rb +14 -12
  54. data/spec/unit/braintree/credit_card_verification_spec.rb +16 -0
  55. data/spec/unit/braintree/customer_spec.rb +10 -8
  56. data/spec/unit/braintree/digest_spec.rb +8 -17
  57. data/spec/unit/braintree/error_result_spec.rb +12 -2
  58. data/spec/unit/braintree/http_spec.rb +2 -2
  59. data/spec/unit/braintree/subscription_search_spec.rb +77 -0
  60. data/spec/unit/braintree/subscription_spec.rb +16 -8
  61. data/spec/unit/braintree/transaction_spec.rb +17 -12
  62. data/spec/unit/braintree/transparent_redirect_spec.rb +12 -12
  63. data/spec/unit/braintree/util_spec.rb +24 -0
  64. data/spec/unit/braintree/xml/parser_spec.rb +1 -1
  65. data/spec/unit/braintree_spec.rb +1 -1
  66. metadata +16 -5
@@ -0,0 +1,122 @@
1
+ module Braintree
2
+ class SubscriptionGateway # :nodoc:
3
+ def initialize(gateway)
4
+ @gateway = gateway
5
+ @config = gateway.config
6
+ end
7
+
8
+ def cancel(subscription_id)
9
+ response = @config.http.put "/subscriptions/#{subscription_id}/cancel"
10
+ if response[:subscription]
11
+ SuccessfulResult.new(:subscription => Subscription._new(@gateway, response[:subscription]))
12
+ elsif response[:api_error_response]
13
+ ErrorResult.new(@gateway, response[:api_error_response])
14
+ else
15
+ raise UnexpectedError, "expected :subscription or :api_error_response"
16
+ end
17
+ rescue NotFoundError
18
+ raise NotFoundError, "subscription with id #{subscription_id.inspect} not found"
19
+ end
20
+
21
+ def create(attributes)
22
+ Util.verify_keys(SubscriptionGateway._create_signature, attributes)
23
+ _do_create "/subscriptions", :subscription => attributes
24
+ end
25
+
26
+ def find(id)
27
+ response = @config.http.get "/subscriptions/#{id}"
28
+ Subscription._new(@gateway, response[:subscription])
29
+ rescue NotFoundError
30
+ raise NotFoundError, "subscription with id #{id.inspect} not found"
31
+ end
32
+
33
+ def search(&block)
34
+ search = SubscriptionSearch.new
35
+ block.call(search) if block
36
+
37
+ response = @config.http.post "/subscriptions/advanced_search_ids", {:search => search.to_hash}
38
+ ResourceCollection.new(response) { |ids| _fetch_subscriptions(search, ids) }
39
+ end
40
+
41
+ def update(subscription_id, attributes)
42
+ Util.verify_keys(SubscriptionGateway._update_signature, attributes)
43
+ response = @config.http.put "/subscriptions/#{subscription_id}", :subscription => attributes
44
+ if response[:subscription]
45
+ SuccessfulResult.new(:subscription => Subscription._new(@gateway, response[:subscription]))
46
+ elsif response[:api_error_response]
47
+ ErrorResult.new(@gateway, response[:api_error_response])
48
+ else
49
+ raise UnexpectedError, "expected :subscription or :api_error_response"
50
+ end
51
+ end
52
+
53
+ def self._create_signature # :nodoc:
54
+ [
55
+ :billing_day_of_month,
56
+ :first_billing_date,
57
+ :id,
58
+ :merchant_account_id,
59
+ :never_expires,
60
+ :number_of_billing_cycles,
61
+ :payment_method_token,
62
+ :plan_id,
63
+ :price,
64
+ :trial_duration,
65
+ :trial_duration_unit,
66
+ :trial_period,
67
+ {:options => [:do_not_inherit_add_ons_or_discounts, :start_immediately]},
68
+ ] + _add_on_discount_signature
69
+ end
70
+
71
+ def self._update_signature # :nodoc:
72
+ [
73
+ :id,
74
+ :merchant_account_id,
75
+ :never_expires,
76
+ :number_of_billing_cycles,
77
+ :payment_method_token,
78
+ :plan_id,
79
+ :price,
80
+ {:options => [:prorate_charges, :replace_all_add_ons_and_discounts]},
81
+ ] + _add_on_discount_signature
82
+ end
83
+
84
+ def self._add_on_discount_signature # :nodoc:
85
+ [
86
+ {
87
+ :add_ons => [
88
+ {:add => [:amount, :inherited_from_id, :never_expires, :number_of_billing_cycles, :quantity]},
89
+ {:update => [:amount, :existing_id, :never_expires, :number_of_billing_cycles, :quantity]},
90
+ {:remove => [:_any_key_]}
91
+ ]
92
+ },
93
+ {
94
+ :discounts => [
95
+ {:add => [:amount, :inherited_from_id, :never_expires, :number_of_billing_cycles, :quantity]},
96
+ {:update => [:amount, :existing_id, :never_expires, :number_of_billing_cycles, :quantity]},
97
+ {:remove => [:_any_key_]}
98
+ ]
99
+ }
100
+ ]
101
+ end
102
+
103
+ def _do_create(url, params) # :nodoc:
104
+ response = @config.http.post url, params
105
+ if response[:subscription]
106
+ SuccessfulResult.new(:subscription => Subscription._new(@gateway, response[:subscription]))
107
+ elsif response[:api_error_response]
108
+ ErrorResult.new(@gateway, response[:api_error_response])
109
+ else
110
+ raise UnexpectedError, "expected :subscription or :api_error_response"
111
+ end
112
+ end
113
+
114
+ def _fetch_subscriptions(search, ids) # :nodoc:
115
+ search.ids.in ids
116
+ response = @config.http.post "/subscriptions/advanced_search", {:search => search.to_hash}
117
+ attributes = response[:subscriptions]
118
+ Util.extract_attribute_as_array(attributes, :subscription).map { |attrs| Subscription._new(@gateway, attrs) }
119
+ end
120
+ end
121
+ end
122
+
@@ -1,11 +1,10 @@
1
1
  module Braintree
2
- class SubscriptionSearch < AdvancedSearch
2
+ class SubscriptionSearch < AdvancedSearch # :nodoc:
3
3
  multiple_value_field :ids
4
- search_fields :plan_id, :days_past_due
5
- multiple_value_field :status, :allows => [
6
- Subscription::Status::Active,
7
- Subscription::Status::Canceled,
8
- Subscription::Status::PastDue
9
- ]
4
+ text_fields :id
5
+ multiple_value_or_text_field :plan_id
6
+ multiple_value_field :status, :allows => Subscription::Status::All
7
+ multiple_value_field :merchant_account_id
8
+ range_fields :price, :days_past_due, :billing_cycles_remaining
10
9
  end
11
10
  end
@@ -1,16 +1,5 @@
1
1
  module Braintree
2
- # A SuccessfulResult will be returned from non-bang methods when
3
- # validations pass. It will provide access to the created resource.
4
- # For example, when creating a customer, SuccessfulResult will
5
- # respond to +customer+ like so:
6
- #
7
- # result = Customer.create(:first_name => "John")
8
- # if result.success?
9
- # # have a SuccessfulResult
10
- # puts "Created customer #{result.customer.id}
11
- # else
12
- # # have an ErrorResult
13
- # end
2
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/general/result_objects
14
3
  class SuccessfulResult
15
4
  include BaseModule
16
5
 
@@ -3,6 +3,8 @@ module Braintree
3
3
  # The constants contained in the Braintree::Test::CreditCardNumbers module provide
4
4
  # credit card numbers that should be used when working in the sandbox environment. The sandbox
5
5
  # will not accept any credit card numbers other than the ones listed below.
6
+ #
7
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/reference/sandbox
6
8
  module CreditCardNumbers
7
9
  AmExes = %w[
8
10
  378282246310005
@@ -30,7 +32,7 @@ module Braintree
30
32
  4009348888881881
31
33
  4012888888881881
32
34
  4111111111111111
33
- 4222222222222
35
+ 4000111111111115
34
36
  ]
35
37
  Unknowns = %w[
36
38
  1000000000000008
@@ -40,7 +42,7 @@ module Braintree
40
42
  AmEx = "378734493671000"
41
43
  Discover = "6011000990139424"
42
44
  MasterCard = "5105105105105100"
43
- Visa = "4222222222222"
45
+ Visa = "4000111111111115"
44
46
  Numbers = [AmEx, Discover, MasterCard, Visa]
45
47
  end
46
48
 
@@ -2,9 +2,12 @@ module Braintree
2
2
  module Test # :nodoc:
3
3
  # The constants in this module can be used to create transactions with
4
4
  # the desired status in the sandbox environment.
5
+ #
6
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/reference/sandbox
5
7
  module TransactionAmounts
6
8
  Authorize = "1000.00"
7
9
  Decline = "2000.00"
10
+ Fail = "3000.00"
8
11
  end
9
12
  end
10
13
  end
@@ -1,125 +1,5 @@
1
1
  module Braintree
2
- # == Creating a Transaction
3
- #
4
- # At minimum, an amount, credit card number, and credit card expiration date are required. Minimalistic
5
- # example:
6
- # Braintree::Transaction.sale!(
7
- # :amount => "100.00",
8
- # :credit_card => {
9
- # :number => "5105105105105100",
10
- # :expiration_date => "05/2012"
11
- # }
12
- # )
13
- #
14
- # Full example:
15
- #
16
- # Braintree::Transaction.sale!(
17
- # :amount => "100.00",
18
- # :order_id => "123",
19
- # :credit_card => {
20
- # # if :token is omitted, the gateway will generate a token
21
- # :token => "credit_card_123",
22
- # :number => "5105105105105100",
23
- # :expiration_date => "05/2011",
24
- # :cvv => "123"
25
- # },
26
- # :customer => {
27
- # # if :id is omitted, the gateway will generate an id
28
- # :id => "customer_123",
29
- # :first_name => "Dan",
30
- # :last_name => "Smith",
31
- # :company => "Braintree Payment Solutions",
32
- # :email => "dan@example.com",
33
- # :phone => "419-555-1234",
34
- # :fax => "419-555-1235",
35
- # :website => "http://braintreepaymentsolutions.com"
36
- # },
37
- # :billing => {
38
- # :first_name => "Carl",
39
- # :last_name => "Jones",
40
- # :company => "Braintree",
41
- # :street_address => "123 E Main St",
42
- # :extended_address => "Suite 403",
43
- # :locality => "Chicago",
44
- # :region => "IL",
45
- # :postal_code => "60622",
46
- # :country_name => "United States of America"
47
- # },
48
- # :shipping => {
49
- # :first_name => "Andrew",
50
- # :last_name => "Mason",
51
- # :company => "Braintree",
52
- # :street_address => "456 W Main St",
53
- # :extended_address => "Apt 2F",
54
- # :locality => "Bartlett",
55
- # :region => "IL",
56
- # :postal_code => "60103",
57
- # :country_name => "United States of America"
58
- # },
59
- # :custom_fields => {
60
- # :birthdate => "11/13/1954"
61
- # }
62
- # )
63
- #
64
- # == Storing in the Vault
65
- #
66
- # The customer and credit card information used for
67
- # a transaction can be stored in the vault by setting
68
- # <tt>transaction[options][store_in_vault]</tt> to true.
69
- #
70
- # transaction = Braintree::Transaction.sale!(
71
- # :customer => {
72
- # :first_name => "Adam",
73
- # :last_name => "Williams"
74
- # },
75
- # :credit_card => {
76
- # :number => "5105105105105100",
77
- # :expiration_date => "05/2012"
78
- # },
79
- # :options => {
80
- # :store_in_vault => true
81
- # }
82
- # )
83
- # transaction.customer_details.id
84
- # # => "865534"
85
- # transaction.credit_card_details.token
86
- # # => "6b6m"
87
- #
88
- # To also store the billing address in the vault, pass the
89
- # +add_billing_address_to_payment_method+ option.
90
- #
91
- # Braintree::Transaction.sale!(
92
- # # ...
93
- # :options => {
94
- # :store_in_vault => true
95
- # :add_billing_address_to_payment_method => true
96
- # }
97
- # )
98
- #
99
- # == Submitting for Settlement
100
- #
101
- # This can only be done when the transction's
102
- # status is +authorized+. If +amount+ is not specified, the full authorized amount will be
103
- # settled. If you would like to settle less than the full authorized amount, pass the
104
- # desired amount. You cannot settle more than the authorized amount.
105
- #
106
- # A transaction can be submitted for settlement when created by setting
107
- # transaction[options][submit_for_settlement] to true.
108
- #
109
- # transaction = Braintree::Transaction.sale!(
110
- # :amount => "100.00",
111
- # :credit_card => {
112
- # :number => "5105105105105100",
113
- # :expiration_date => "05/2012"
114
- # },
115
- # :options => {
116
- # :submit_for_settlement => true
117
- # }
118
- # )
119
- #
120
- # == More Information
121
- #
122
- # For more detailed documentation on Transactions, see http://www.braintreepaymentsolutions.com/gateway/transaction-api
2
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/overview
123
3
  class Transaction
124
4
  include BaseModule
125
5
 
@@ -130,9 +10,9 @@ module Braintree
130
10
 
131
11
  module GatewayRejectionReason
132
12
  AVS = "avs"
133
- AVS_AND_CVV = "avs_and_cvv"
13
+ AVSAndCVV = "avs_and_cvv"
134
14
  CVV = "cvv"
135
- DUPLICATE = "duplicate"
15
+ Duplicate = "duplicate"
136
16
  end
137
17
 
138
18
  module Status
@@ -176,6 +56,7 @@ module Braintree
176
56
  # The response text from the processor.
177
57
  attr_reader :processor_response_text
178
58
  attr_reader :refund_id, :refunded_transaction_id
59
+ attr_reader :settlement_batch_id
179
60
  # See Transaction::Status
180
61
  attr_reader :status
181
62
  attr_reader :status_history
@@ -183,102 +64,98 @@ module Braintree
183
64
  # Will either be "sale" or "credit"
184
65
  attr_reader :type
185
66
  attr_reader :updated_at
67
+ attr_reader :add_ons, :discounts
186
68
 
69
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create
187
70
  def self.create(attributes)
188
- Util.verify_keys(_create_signature, attributes)
189
- _do_create "/transactions", :transaction => attributes
71
+ Configuration.gateway.transaction.create(attributes)
190
72
  end
191
73
 
74
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create
192
75
  def self.create!(attributes)
193
76
  return_object_or_raise(:transaction) { create(attributes) }
194
77
  end
195
78
 
79
+ # Deprecated. Use Braintree::TransparentRedirect.confirm
80
+ #
81
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create_tr
196
82
  def self.create_from_transparent_redirect(query_string)
197
83
  warn "[DEPRECATED] Transaction.create_from_transparent_redirect is deprecated. Please use TransparentRedirect.confirm"
198
- params = TransparentRedirect.parse_and_validate_query_string query_string
199
- _do_create("/transactions/all/confirm_transparent_redirect_request", :id => params[:id])
84
+ Configuration.gateway.transaction.create_from_transparent_redirect(query_string)
200
85
  end
201
86
 
202
- # The URL to use to create transactions via transparent redirect.
87
+ # Deprecated. Use Braintree::TransparentRedirect.url
88
+ #
89
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create_tr
203
90
  def self.create_transaction_url
204
91
  warn "[DEPRECATED] Transaction.create_transaction_url is deprecated. Please use TransparentRedirect.url"
205
- "#{Braintree::Configuration.base_merchant_url}/transactions/all/create_via_transparent_redirect_request"
92
+ Configuration.gateway.transaction.create_transaction_url
206
93
  end
207
94
 
208
- # Creates a credit transaction.
209
95
  def self.credit(attributes)
210
- create(attributes.merge(:type => 'credit'))
96
+ Configuration.gateway.transaction.credit(attributes)
211
97
  end
212
98
 
213
99
  def self.credit!(attributes)
214
100
  return_object_or_raise(:transaction) { credit(attributes) }
215
101
  end
216
102
 
217
- # Finds the transaction with the given id. Raises a Braintree::NotFoundError
218
- # if the transaction cannot be found.
103
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/search
219
104
  def self.find(id)
220
- response = Http.get "/transactions/#{id}"
221
- new(response[:transaction])
222
- rescue NotFoundError
223
- raise NotFoundError, "transaction with id #{id.inspect} not found"
105
+ Configuration.gateway.transaction.find(id)
224
106
  end
225
107
 
226
- # Creates a sale transaction.
108
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/refund
109
+ def self.refund(id, amount = nil)
110
+ Configuration.gateway.transaction.refund(id, amount)
111
+ end
112
+
113
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create
227
114
  def self.sale(attributes)
228
- create(attributes.merge(:type => 'sale'))
115
+ Configuration.gateway.transaction.sale(attributes)
229
116
  end
230
117
 
118
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/create
231
119
  def self.sale!(attributes)
232
120
  return_object_or_raise(:transaction) { sale(attributes) }
233
121
  end
234
122
 
235
- # Returns a ResourceCollection of transactions matching the search query.
236
- # If <tt>query</tt> is a string, the search will be a basic search.
237
- # If <tt>query</tt> is a hash, the search will be an advanced search.
238
- # See: http://www.braintreepaymentsolutions.com/gateway/transaction-api#searching
123
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/search
239
124
  def self.search(&block)
240
- search = TransactionSearch.new
241
- block.call(search) if block
242
-
243
- response = Http.post "/transactions/advanced_search_ids", {:search => search.to_hash}
244
- ResourceCollection.new(response) { |ids| _fetch_transactions(search, ids) }
125
+ Configuration.gateway.transaction.search(&block)
245
126
  end
246
127
 
247
- # Submits transaction with +transaction_id+ for settlement.
128
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/submit_for_settlement
248
129
  def self.submit_for_settlement(transaction_id, amount = nil)
249
- raise ArgumentError, "transaction_id is invalid" unless transaction_id =~ /\A[0-9a-z]+\z/
250
- response = Http.put "/transactions/#{transaction_id}/submit_for_settlement", :transaction => {:amount => amount}
251
- if response[:transaction]
252
- SuccessfulResult.new(:transaction => new(response[:transaction]))
253
- elsif response[:api_error_response]
254
- ErrorResult.new(response[:api_error_response])
255
- else
256
- raise UnexpectedError, "expected :transaction or :response"
257
- end
130
+ Configuration.gateway.transaction.submit_for_settlement(transaction_id, amount)
258
131
  end
259
132
 
133
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/submit_for_settlement
260
134
  def self.submit_for_settlement!(transaction_id, amount = nil)
261
135
  return_object_or_raise(:transaction) { submit_for_settlement(transaction_id, amount) }
262
136
  end
263
137
 
264
- # Voids the transaction with the given <tt>transaction_id</tt>
138
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/void
265
139
  def self.void(transaction_id)
266
- response = Http.put "/transactions/#{transaction_id}/void"
267
- if response[:transaction]
268
- SuccessfulResult.new(:transaction => new(response[:transaction]))
269
- elsif response[:api_error_response]
270
- ErrorResult.new(response[:api_error_response])
271
- else
272
- raise UnexpectedError, "expected :transaction or :api_error_response"
273
- end
140
+ Configuration.gateway.transaction.void(transaction_id)
274
141
  end
275
142
 
143
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/void
276
144
  def self.void!(transaction_id)
277
145
  return_object_or_raise(:transaction) { void(transaction_id) }
278
146
  end
279
147
 
280
- def initialize(attributes) # :nodoc:
281
- _init attributes
148
+ def initialize(gateway, attributes) # :nodoc:
149
+ @gateway = gateway
150
+ set_instance_variables_from_hash(attributes)
151
+ @amount = Util.to_big_decimal(amount)
152
+ @credit_card_details = CreditCardDetails.new(@credit_card)
153
+ @customer_details = CustomerDetails.new(@customer)
154
+ @billing_details = AddressDetails.new(@billing)
155
+ @shipping_details = AddressDetails.new(@shipping)
156
+ @status_history = attributes[:status_history] ? attributes[:status_history].map { |s| StatusDetails.new(s) } : []
157
+ add_ons.map! { |attrs| AddOn._new(attrs) } if add_ons
158
+ discounts.map! { |attrs| Discount._new(attrs) } if discounts
282
159
  end
283
160
 
284
161
  # True if <tt>other</tt> is a Braintree::Transaction with the same id.
@@ -300,16 +177,17 @@ module Braintree
300
177
  "#<#{self.class} #{nice_attributes.join(', ')}>"
301
178
  end
302
179
 
303
- # Creates a credit transaction that refunds this transaction.
180
+ # Deprecated. Use Braintree::Transaction.refund
181
+ #
182
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/refund
304
183
  def refund(amount = nil)
305
- response = Http.post "/transactions/#{id}/refund", :transaction => {:amount => amount}
306
- if response[:transaction]
307
- # TODO: need response to return original_transaction so that we can update status, updated_at, etc.
308
- SuccessfulResult.new(:new_transaction => Transaction._new(response[:transaction]))
309
- elsif response[:api_error_response]
310
- ErrorResult.new(response[:api_error_response])
184
+ warn "[DEPRECATED] refund as an instance method is deprecated. Please use CreditCard.refund"
185
+ result = @gateway.transaction.refund(id, amount)
186
+
187
+ if result.success?
188
+ SuccessfulResult.new(:new_transaction => result.transaction)
311
189
  else
312
- raise UnexpectedError, "expected :transaction or :api_error_response"
190
+ result
313
191
  end
314
192
  end
315
193
 
@@ -318,20 +196,23 @@ module Braintree
318
196
  !@refund_id.nil?
319
197
  end
320
198
 
321
- # Submits the transaction for settlement.
199
+ # Deprecated. Use Braintree::Transaction.submit_for_settlement
200
+ #
201
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/submit_for_settlement
322
202
  def submit_for_settlement(amount = nil)
323
- response = Http.put "/transactions/#{id}/submit_for_settlement", :transaction => {:amount => amount}
324
- if response[:transaction]
325
- _init(response[:transaction])
326
- SuccessfulResult.new :transaction => self
327
- elsif response[:api_error_response]
328
- ErrorResult.new(response[:api_error_response])
329
- else
330
- raise UnexpectedError, "expected transaction or api_error_response"
203
+ warn "[DEPRECATED] submit_for_settlement as an instance method is deprecated. Please use CreditCard.submit_for_settlement"
204
+ result = @gateway.transaction.submit_for_settlement(id, amount)
205
+ if result.success?
206
+ copy_instance_variables_from_object result.transaction
331
207
  end
208
+ result
332
209
  end
333
210
 
211
+ # Deprecated. Use Braintree::Transaction.submit_for_settlement!
212
+ #
213
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/submit_for_settlement
334
214
  def submit_for_settlement!(amount = nil)
215
+ warn "[DEPRECATED] submit_for_settlement! as an instance method is deprecated. Please use CreditCard.submit_for_settlement!"
335
216
  return_object_or_raise(:transaction) { submit_for_settlement(amount) }
336
217
  end
337
218
 
@@ -341,7 +222,7 @@ module Braintree
341
222
  # on vault_billing_address may not match the attributes on billing_details.
342
223
  def vault_billing_address
343
224
  return nil if billing_details.id.nil?
344
- Address.find(customer_details.id, billing_details.id)
225
+ @gateway.address.find(customer_details.id, billing_details.id)
345
226
  end
346
227
 
347
228
  # If this transaction was stored in the vault, or created from vault records,
@@ -350,7 +231,7 @@ module Braintree
350
231
  # on vault_credit_card may not match the attributes on credit_card_details.
351
232
  def vault_credit_card
352
233
  return nil if credit_card_details.token.nil?
353
- CreditCard.find(credit_card_details.token)
234
+ @gateway.credit_card.find(credit_card_details.token)
354
235
  end
355
236
 
356
237
  # If this transaction was stored in the vault, or created from vault records,
@@ -359,7 +240,7 @@ module Braintree
359
240
  # on vault_customer may not match the attributes on customer_details.
360
241
  def vault_customer
361
242
  return nil if customer_details.id.nil?
362
- Customer.find(customer_details.id)
243
+ @gateway.customer.find(customer_details.id)
363
244
  end
364
245
 
365
246
  # If this transaction was stored in the vault, or created from vault records,
@@ -368,23 +249,26 @@ module Braintree
368
249
  # on vault_shipping_address may not match the attributes on shipping_details.
369
250
  def vault_shipping_address
370
251
  return nil if shipping_details.id.nil?
371
- Address.find(customer_details.id, shipping_details.id)
252
+ @gateway.address.find(customer_details.id, shipping_details.id)
372
253
  end
373
254
 
374
- # Voids the transaction.
255
+ # Deprecated. Use Braintree::Transaction.void
256
+ #
257
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/void
375
258
  def void
376
- response = Http.put "/transactions/#{id}/void"
377
- if response[:transaction]
378
- _init response[:transaction]
379
- SuccessfulResult.new(:transaction => self)
380
- elsif response[:api_error_response]
381
- ErrorResult.new(response[:api_error_response])
382
- else
383
- raise UnexpectedError, "expected :transaction or :api_error_response"
259
+ warn "[DEPRECATED] void as an instance method is deprecated. Please use CreditCard.void"
260
+ result = @gateway.transaction.void(id)
261
+ if result.success?
262
+ copy_instance_variables_from_object result.transaction
384
263
  end
264
+ result
385
265
  end
386
266
 
267
+ # Deprecated. Use Braintree::Transaction.void!
268
+ #
269
+ # See http://www.braintreepaymentsolutions.com/docs/ruby/transactions/void
387
270
  def void!
271
+ warn "[DEPRECATED] void! as an instance method is deprecated. Please use CreditCard.void!"
388
272
  return_object_or_raise(:transaction) { void }
389
273
  end
390
274
 
@@ -395,52 +279,8 @@ module Braintree
395
279
  end
396
280
  end
397
281
 
398
- def self._do_create(url, params=nil) # :nodoc:
399
- response = Http.post url, params
400
- if response[:transaction]
401
- SuccessfulResult.new(:transaction => new(response[:transaction]))
402
- elsif response[:api_error_response]
403
- ErrorResult.new(response[:api_error_response])
404
- else
405
- raise UnexpectedError, "expected :transaction or :api_error_response"
406
- end
407
- end
408
-
409
282
  def self._attributes # :nodoc:
410
283
  [:amount, :created_at, :credit_card_details, :customer_details, :id, :status, :type, :updated_at]
411
284
  end
412
-
413
- def self._create_signature # :nodoc:
414
- [
415
- :amount, :customer_id, :merchant_account_id, :order_id, :payment_method_token, :type,
416
- {:credit_card => [:token, :cardholder_name, :cvv, :expiration_date, :expiration_month, :expiration_year, :number]},
417
- {:customer => [:id, :company, :email, :fax, :first_name, :last_name, :phone, :website]},
418
- {
419
- :billing => Address._shared_signature
420
- },
421
- {
422
- :shipping => Address._shared_signature
423
- },
424
- {:options => [:store_in_vault, :submit_for_settlement, :add_billing_address_to_payment_method, :store_shipping_address_in_vault]},
425
- {:custom_fields => :_any_key_}
426
- ]
427
- end
428
-
429
- def self._fetch_transactions(search, ids)
430
- search.ids.in ids
431
- response = Http.post "/transactions/advanced_search", {:search => search.to_hash}
432
- attributes = response[:credit_card_transactions]
433
- Util.extract_attribute_as_array(attributes, :transaction).map { |attrs| _new(attrs) }
434
- end
435
-
436
- def _init(attributes) # :nodoc:
437
- set_instance_variables_from_hash(attributes)
438
- @amount = Util.to_big_decimal(amount)
439
- @credit_card_details = CreditCardDetails.new(@credit_card)
440
- @customer_details = CustomerDetails.new(@customer)
441
- @billing_details = AddressDetails.new(@billing)
442
- @shipping_details = AddressDetails.new(@shipping)
443
- @status_history = attributes[:status_history] ? attributes[:status_history].map { |s| StatusDetails.new(s) } : []
444
- end
445
285
  end
446
286
  end