braintree 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -45,7 +45,7 @@ module Braintree
45
45
  if params[:http_status] == nil
46
46
  raise UnexpectedError, "expected query string to have an http_status param"
47
47
  elsif params[:http_status] != '200'
48
- Util.raise_exception_for_status_code(params[:http_status])
48
+ Util.raise_exception_for_status_code(params[:http_status], params[:bt_message])
49
49
  end
50
50
 
51
51
  if _hash(query_string_without_hash) == params[:hash]
@@ -1,6 +1,7 @@
1
1
  module Braintree
2
2
  module Util # :nodoc:
3
3
  def self.extract_attribute_as_array(hash, attribute)
4
+ raise UnexpectedError.new("Unprocessable entity due to an invalid request") if hash.nil?
4
5
  value = hash.has_key?(attribute) ? hash.delete(attribute) : []
5
6
  value.is_a?(Array) ? value : [value]
6
7
  end
@@ -40,12 +41,12 @@ module Braintree
40
41
  end
41
42
  end
42
43
 
43
- def self.raise_exception_for_status_code(status_code)
44
+ def self.raise_exception_for_status_code(status_code, message=nil)
44
45
  case status_code.to_i
45
46
  when 401
46
47
  raise AuthenticationError
47
48
  when 403
48
- raise AuthorizationError
49
+ raise AuthorizationError, message
49
50
  when 404
50
51
  raise NotFoundError
51
52
  when 500
@@ -1,7 +1,7 @@
1
1
  module Braintree
2
2
  module Version
3
3
  Major = 2
4
- Minor = 0
4
+ Minor = 1
5
5
  Tiny = 0
6
6
 
7
7
  String = "#{Major}.#{Minor}.#{Tiny}"
@@ -11,9 +11,22 @@ module Braintree
11
11
  "DateTime" => "datetime",
12
12
  "Time" => "datetime",
13
13
  }
14
+
15
+ XML_FORMATTING_NAMES = {
16
+ "BigDecimal" => "bigdecimal",
17
+ "Symbol" => "symbol"
18
+ }.merge(XML_TYPE_NAMES)
19
+
14
20
  XML_FORMATTING = {
15
- "symbol" => Proc.new { |symbol| symbol.to_s },
16
- "datetime" => Proc.new { |time| time.xmlschema },
21
+ "symbol" => Proc.new { |symbol| symbol.to_s },
22
+ "datetime" => Proc.new { |time| time.xmlschema },
23
+ "bigdecimal" => Proc.new do |bigdecimal|
24
+ str = bigdecimal.to_s('F')
25
+ if str =~ /\.\d$/
26
+ str += "0"
27
+ end
28
+ str
29
+ end
17
30
  }
18
31
 
19
32
  def self.hash_to_xml(hash)
@@ -49,8 +62,9 @@ module Braintree
49
62
  attributes[:nil] = true
50
63
  end
51
64
 
65
+ formatting_name = XML_FORMATTING_NAMES[value.class.name]
52
66
  options[:builder].tag!(_xml_escape(key),
53
- XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value,
67
+ XML_FORMATTING[formatting_name] ? XML_FORMATTING[formatting_name].call(value) : value,
54
68
  attributes
55
69
  )
56
70
  end
@@ -326,6 +326,53 @@ describe Braintree::CreditCard do
326
326
  updated_credit_card.cardholder_name.should == "New Holder"
327
327
  end
328
328
 
329
+ context "billing address" do
330
+ it "creates a new billing address by default" do
331
+ customer = Braintree::Customer.create!
332
+ credit_card = Braintree::CreditCard.create!(
333
+ :customer_id => customer.id,
334
+ :number => Braintree::Test::CreditCardNumbers::Visa,
335
+ :expiration_date => "05/2012",
336
+ :billing_address => {
337
+ :street_address => "123 Nigeria Ave"
338
+ }
339
+ )
340
+ update_result = Braintree::CreditCard.update(credit_card.token,
341
+ :billing_address => {
342
+ :region => "IL"
343
+ }
344
+ )
345
+ update_result.success?.should == true
346
+ updated_credit_card = update_result.credit_card
347
+ updated_credit_card.billing_address.region.should == "IL"
348
+ updated_credit_card.billing_address.street_address.should == nil
349
+ updated_credit_card.billing_address.id.should_not == credit_card.billing_address.id
350
+ end
351
+
352
+ it "updates the billing address if option is specified" do
353
+ customer = Braintree::Customer.create!
354
+ credit_card = Braintree::CreditCard.create!(
355
+ :customer_id => customer.id,
356
+ :number => Braintree::Test::CreditCardNumbers::Visa,
357
+ :expiration_date => "05/2012",
358
+ :billing_address => {
359
+ :street_address => "123 Nigeria Ave"
360
+ }
361
+ )
362
+ update_result = Braintree::CreditCard.update(credit_card.token,
363
+ :billing_address => {
364
+ :region => "IL",
365
+ :options => {:update_existing => true}
366
+ }
367
+ )
368
+ update_result.success?.should == true
369
+ updated_credit_card = update_result.credit_card
370
+ updated_credit_card.billing_address.region.should == "IL"
371
+ updated_credit_card.billing_address.street_address.should == "123 Nigeria Ave"
372
+ updated_credit_card.billing_address.id.should == credit_card.billing_address.id
373
+ end
374
+ end
375
+
329
376
  it "can pass expiration_month and expiration_year" do
330
377
  customer = Braintree::Customer.create!
331
378
  credit_card = Braintree::CreditCard.create!(
@@ -17,9 +17,6 @@ describe Braintree::Subscription do
17
17
  :trial_period => false
18
18
  }
19
19
 
20
- DefaultMerchantAccountId = "sandbox_credit_card"
21
- NonDefaultMerchantAccountId = "sandbox_credit_card_non_default"
22
-
23
20
  before(:each) do
24
21
  @credit_card = Braintree::Customer.create!(
25
22
  :credit_card => {
@@ -72,18 +69,18 @@ describe Braintree::Subscription do
72
69
  )
73
70
 
74
71
  result.success?.should == true
75
- result.subscription.merchant_account_id.should == DefaultMerchantAccountId
72
+ result.subscription.merchant_account_id.should == SpecHelper::DefaultMerchantAccountId
76
73
  end
77
74
 
78
75
  it "allows setting the merchant_account_id" do
79
76
  result = Braintree::Subscription.create(
80
77
  :payment_method_token => @credit_card.token,
81
78
  :plan_id => TriallessPlan[:id],
82
- :merchant_account_id => NonDefaultMerchantAccountId
79
+ :merchant_account_id => SpecHelper::NonDefaultMerchantAccountId
83
80
  )
84
81
 
85
82
  result.success?.should == true
86
- result.subscription.merchant_account_id.should == NonDefaultMerchantAccountId
83
+ result.subscription.merchant_account_id.should == SpecHelper::NonDefaultMerchantAccountId
87
84
  end
88
85
  end
89
86
 
@@ -263,11 +260,11 @@ describe Braintree::Subscription do
263
260
  context "merchant_account_id" do
264
261
  it "allows changing the merchant_account_id" do
265
262
  result = Braintree::Subscription.update(@subscription.id,
266
- :merchant_account_id => NonDefaultMerchantAccountId
263
+ :merchant_account_id => SpecHelper::NonDefaultMerchantAccountId
267
264
  )
268
265
 
269
266
  result.success?.should == true
270
- result.subscription.merchant_account_id.should == NonDefaultMerchantAccountId
267
+ result.subscription.merchant_account_id.should == SpecHelper::NonDefaultMerchantAccountId
271
268
  end
272
269
  end
273
270
 
@@ -616,4 +613,38 @@ describe Braintree::Subscription do
616
613
  end
617
614
  end
618
615
  end
616
+
617
+ describe "self.retry_charge" do
618
+ it "is successful with only subscription id" do
619
+ subscription = Braintree::Subscription.search do |search|
620
+ search.status.in Braintree::Subscription::Status::PastDue
621
+ end.first
622
+
623
+ result = Braintree::Subscription.retry_charge(subscription.id)
624
+
625
+ result.success?.should == true
626
+ transaction = result.transaction
627
+
628
+ transaction.amount.should == subscription.price
629
+ transaction.processor_authorization_code.should_not be_nil
630
+ transaction.type.should == Braintree::Transaction::Type::Sale
631
+ transaction.status.should == Braintree::Transaction::Status::Authorized
632
+ end
633
+
634
+ it "is successful with subscription id and amount" do
635
+ subscription = Braintree::Subscription.search do |search|
636
+ search.status.in Braintree::Subscription::Status::PastDue
637
+ end.first
638
+
639
+ result = Braintree::Subscription.retry_charge(subscription.id, Braintree::Test::TransactionAmounts::Authorize)
640
+
641
+ result.success?.should == true
642
+ transaction = result.transaction
643
+
644
+ transaction.amount.should == BigDecimal.new(Braintree::Test::TransactionAmounts::Authorize)
645
+ transaction.processor_authorization_code.should_not be_nil
646
+ transaction.type.should == Braintree::Transaction::Type::Sale
647
+ transaction.status.should == Braintree::Transaction::Status::Authorized
648
+ end
649
+ end
619
650
  end
@@ -0,0 +1,734 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Braintree::Transaction, "search" do
4
+ context "advanced" do
5
+ it "correctly returns a result with no matches" do
6
+ collection = Braintree::Transaction.search do |search|
7
+ search.billing_first_name.is "thisnameisnotreal"
8
+ end
9
+
10
+ collection._approximate_size.should == 0
11
+ end
12
+
13
+ it "returns one result for text field search" do
14
+ first_name = "Tim#{rand(10000)}"
15
+ token = "creditcard#{rand(10000)}"
16
+ customer_id = "customer#{rand(10000)}"
17
+
18
+ transaction = Braintree::Transaction.sale!(
19
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
20
+ :credit_card => {
21
+ :number => Braintree::Test::CreditCardNumbers::Visa,
22
+ :expiration_date => "05/2012",
23
+ :cardholder_name => "Tom Smith",
24
+ :token => token,
25
+ },
26
+ :billing => {
27
+ :company => "Braintree",
28
+ :country_name => "United States of America",
29
+ :extended_address => "Suite 123",
30
+ :first_name => first_name,
31
+ :last_name => "Smith",
32
+ :locality => "Chicago",
33
+ :postal_code => "12345",
34
+ :region => "IL",
35
+ :street_address => "123 Main St"
36
+ },
37
+ :customer => {
38
+ :company => "Braintree",
39
+ :email => "smith@example.com",
40
+ :fax => "5551231234",
41
+ :first_name => "Tom",
42
+ :id => customer_id,
43
+ :last_name => "Smith",
44
+ :phone => "5551231234",
45
+ :website => "http://example.com",
46
+ },
47
+ :options => {
48
+ :store_in_vault => true
49
+ },
50
+ :order_id => "myorder",
51
+ :shipping => {
52
+ :company => "Braintree P.S.",
53
+ :country_name => "Mexico",
54
+ :extended_address => "Apt 456",
55
+ :first_name => "Thomas",
56
+ :last_name => "Smithy",
57
+ :locality => "Braintree",
58
+ :postal_code => "54321",
59
+ :region => "MA",
60
+ :street_address => "456 Road"
61
+ }
62
+ )
63
+
64
+ search_criteria = {
65
+ :billing_company => "Braintree",
66
+ :billing_country_name => "United States of America",
67
+ :billing_extended_address => "Suite 123",
68
+ :billing_first_name => first_name,
69
+ :billing_last_name => "Smith",
70
+ :billing_locality => "Chicago",
71
+ :billing_postal_code => "12345",
72
+ :billing_region => "IL",
73
+ :billing_street_address => "123 Main St",
74
+ :credit_card_cardholder_name => "Tom Smith",
75
+ :credit_card_expiration_date => "05/2012",
76
+ :credit_card_number => Braintree::Test::CreditCardNumbers::Visa,
77
+ :customer_company => "Braintree",
78
+ :customer_email => "smith@example.com",
79
+ :customer_fax => "5551231234",
80
+ :customer_first_name => "Tom",
81
+ :customer_id => customer_id,
82
+ :customer_last_name => "Smith",
83
+ :customer_phone => "5551231234",
84
+ :customer_website => "http://example.com",
85
+ :order_id => "myorder",
86
+ :payment_method_token => token,
87
+ :processor_authorization_code => transaction.processor_authorization_code,
88
+ :shipping_company => "Braintree P.S.",
89
+ :shipping_country_name => "Mexico",
90
+ :shipping_extended_address => "Apt 456",
91
+ :shipping_first_name => "Thomas",
92
+ :shipping_last_name => "Smithy",
93
+ :shipping_locality => "Braintree",
94
+ :shipping_postal_code => "54321",
95
+ :shipping_region => "MA",
96
+ :shipping_street_address => "456 Road"
97
+ }
98
+
99
+ search_criteria.each do |criterion, value|
100
+ collection = Braintree::Transaction.search do |search|
101
+ search.id.is transaction.id
102
+ search.send(criterion).is value
103
+ end
104
+ collection._approximate_size.should == 1
105
+ collection.first.id.should == transaction.id
106
+
107
+ collection = Braintree::Transaction.search do |search|
108
+ search.id.is transaction.id
109
+ search.send(criterion).is("invalid_attribute")
110
+ end
111
+ collection.should be_empty
112
+ end
113
+ end
114
+
115
+ it "searches all fields at once" do
116
+ first_name = "Tim#{rand(10000)}"
117
+ token = "creditcard#{rand(10000)}"
118
+ customer_id = "customer#{rand(10000)}"
119
+
120
+ transaction = Braintree::Transaction.sale!(
121
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
122
+ :credit_card => {
123
+ :number => Braintree::Test::CreditCardNumbers::Visa,
124
+ :expiration_date => "05/2012",
125
+ :cardholder_name => "Tom Smith",
126
+ :token => token,
127
+ },
128
+ :billing => {
129
+ :company => "Braintree",
130
+ :country_name => "United States of America",
131
+ :extended_address => "Suite 123",
132
+ :first_name => first_name,
133
+ :last_name => "Smith",
134
+ :locality => "Chicago",
135
+ :postal_code => "12345",
136
+ :region => "IL",
137
+ :street_address => "123 Main St"
138
+ },
139
+ :customer => {
140
+ :company => "Braintree",
141
+ :email => "smith@example.com",
142
+ :fax => "5551231234",
143
+ :first_name => "Tom",
144
+ :id => customer_id,
145
+ :last_name => "Smith",
146
+ :phone => "5551231234",
147
+ :website => "http://example.com",
148
+ },
149
+ :options => {
150
+ :store_in_vault => true
151
+ },
152
+ :order_id => "myorder",
153
+ :shipping => {
154
+ :company => "Braintree P.S.",
155
+ :country_name => "Mexico",
156
+ :extended_address => "Apt 456",
157
+ :first_name => "Thomas",
158
+ :last_name => "Smithy",
159
+ :locality => "Braintree",
160
+ :postal_code => "54321",
161
+ :region => "MA",
162
+ :street_address => "456 Road"
163
+ })
164
+
165
+ collection = Braintree::Transaction.search do |search|
166
+ search.billing_company.is "Braintree"
167
+ search.billing_country_name.is "United States of America"
168
+ search.billing_extended_address.is "Suite 123"
169
+ search.billing_first_name.is first_name
170
+ search.billing_last_name.is "Smith"
171
+ search.billing_locality.is "Chicago"
172
+ search.billing_postal_code.is "12345"
173
+ search.billing_region.is "IL"
174
+ search.billing_street_address.is "123 Main St"
175
+ search.credit_card_cardholder_name.is "Tom Smith"
176
+ search.credit_card_expiration_date.is "05/2012"
177
+ search.credit_card_number.is Braintree::Test::CreditCardNumbers::Visa
178
+ search.customer_company.is "Braintree"
179
+ search.customer_email.is "smith@example.com"
180
+ search.customer_fax.is "5551231234"
181
+ search.customer_first_name.is "Tom"
182
+ search.customer_id.is customer_id
183
+ search.customer_last_name.is "Smith"
184
+ search.customer_phone.is "5551231234"
185
+ search.customer_website.is "http://example.com"
186
+ search.order_id.is "myorder"
187
+ search.payment_method_token.is token
188
+ search.processor_authorization_code.is transaction.processor_authorization_code
189
+ search.shipping_company.is "Braintree P.S."
190
+ search.shipping_country_name.is "Mexico"
191
+ search.shipping_extended_address.is "Apt 456"
192
+ search.shipping_first_name.is "Thomas"
193
+ search.shipping_last_name.is "Smithy"
194
+ search.shipping_locality.is "Braintree"
195
+ search.shipping_postal_code.is "54321"
196
+ search.shipping_region.is "MA"
197
+ search.shipping_street_address.is "456 Road"
198
+ search.id.is transaction.id
199
+ end
200
+
201
+ collection._approximate_size.should == 1
202
+ collection.first.id.should == transaction.id
203
+ end
204
+
205
+ context "multiple value fields" do
206
+ it "searches on created_using" do
207
+ transaction = Braintree::Transaction.sale!(
208
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
209
+ :credit_card => {
210
+ :number => Braintree::Test::CreditCardNumbers::Visa,
211
+ :expiration_date => "05/12"
212
+ }
213
+ )
214
+
215
+ collection = Braintree::Transaction.search do |search|
216
+ search.id.is transaction.id
217
+ search.created_using.is Braintree::Transaction::CreatedUsing::FullInformation
218
+ end
219
+
220
+ collection._approximate_size.should == 1
221
+
222
+ collection = Braintree::Transaction.search do |search|
223
+ search.id.is transaction.id
224
+ search.created_using.in Braintree::Transaction::CreatedUsing::FullInformation, Braintree::Transaction::CreatedUsing::Token
225
+ end
226
+
227
+ collection._approximate_size.should == 1
228
+
229
+ collection = Braintree::Transaction.search do |search|
230
+ search.id.is transaction.id
231
+ search.created_using.is Braintree::Transaction::CreatedUsing::Token
232
+ end
233
+
234
+ collection._approximate_size.should == 0
235
+ end
236
+
237
+ it "searches on credit_card_customer_location" do
238
+ transaction = Braintree::Transaction.sale!(
239
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
240
+ :credit_card => {
241
+ :number => Braintree::Test::CreditCardNumbers::Visa,
242
+ :expiration_date => "05/12"
243
+ }
244
+ )
245
+
246
+ collection = Braintree::Transaction.search do |search|
247
+ search.id.is transaction.id
248
+ search.credit_card_customer_location.is Braintree::CreditCard::CustomerLocation::US
249
+ end
250
+
251
+ collection._approximate_size.should == 1
252
+
253
+ collection = Braintree::Transaction.search do |search|
254
+ search.id.is transaction.id
255
+ search.credit_card_customer_location.in Braintree::CreditCard::CustomerLocation::US, Braintree::CreditCard::CustomerLocation::International
256
+ end
257
+
258
+ collection._approximate_size.should == 1
259
+
260
+ collection = Braintree::Transaction.search do |search|
261
+ search.id.is transaction.id
262
+ search.credit_card_customer_location.is Braintree::CreditCard::CustomerLocation::International
263
+ end
264
+
265
+ collection._approximate_size.should == 0
266
+ end
267
+
268
+ it "searches on merchant_account_id" do
269
+ transaction = Braintree::Transaction.sale!(
270
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
271
+ :credit_card => {
272
+ :number => Braintree::Test::CreditCardNumbers::Visa,
273
+ :expiration_date => "05/12"
274
+ }
275
+ )
276
+
277
+ collection = Braintree::Transaction.search do |search|
278
+ search.id.is transaction.id
279
+ search.merchant_account_id.is transaction.merchant_account_id
280
+ end
281
+
282
+ collection._approximate_size.should == 1
283
+
284
+ collection = Braintree::Transaction.search do |search|
285
+ search.id.is transaction.id
286
+ search.merchant_account_id.in transaction.merchant_account_id, "bogus_merchant_account_id"
287
+ end
288
+
289
+ collection._approximate_size.should == 1
290
+
291
+ collection = Braintree::Transaction.search do |search|
292
+ search.id.is transaction.id
293
+ search.merchant_account_id.is "bogus_merchant_account_id"
294
+ end
295
+
296
+ collection._approximate_size.should == 0
297
+ end
298
+
299
+ it "searches on credit_card_card_type" do
300
+ transaction = Braintree::Transaction.sale!(
301
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
302
+ :credit_card => {
303
+ :number => Braintree::Test::CreditCardNumbers::Visa,
304
+ :expiration_date => "05/12"
305
+ }
306
+ )
307
+
308
+ collection = Braintree::Transaction.search do |search|
309
+ search.id.is transaction.id
310
+ search.credit_card_card_type.is Braintree::CreditCard::CardType::Visa
311
+ end
312
+
313
+ collection._approximate_size.should == 1
314
+
315
+ collection = Braintree::Transaction.search do |search|
316
+ search.id.is transaction.id
317
+ search.credit_card_card_type.is transaction.credit_card_details.card_type
318
+ end
319
+
320
+ collection._approximate_size.should == 1
321
+
322
+ collection = Braintree::Transaction.search do |search|
323
+ search.id.is transaction.id
324
+ search.credit_card_card_type.in Braintree::CreditCard::CardType::Visa, Braintree::CreditCard::CardType::MasterCard
325
+ end
326
+
327
+ collection._approximate_size.should == 1
328
+
329
+ collection = Braintree::Transaction.search do |search|
330
+ search.id.is transaction.id
331
+ search.credit_card_card_type.is Braintree::CreditCard::CardType::MasterCard
332
+ end
333
+
334
+ collection._approximate_size.should == 0
335
+ end
336
+
337
+ it "searches on status" do
338
+ transaction = Braintree::Transaction.sale!(
339
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
340
+ :credit_card => {
341
+ :number => Braintree::Test::CreditCardNumbers::Visa,
342
+ :expiration_date => "05/12"
343
+ }
344
+ )
345
+
346
+ collection = Braintree::Transaction.search do |search|
347
+ search.id.is transaction.id
348
+ search.status.is Braintree::Transaction::Status::Authorized
349
+ end
350
+
351
+ collection._approximate_size.should == 1
352
+
353
+ collection = Braintree::Transaction.search do |search|
354
+ search.id.is transaction.id
355
+ search.status.in Braintree::Transaction::Status::Authorized, Braintree::Transaction::Status::ProcessorDeclined
356
+ end
357
+
358
+ collection._approximate_size.should == 1
359
+
360
+ collection = Braintree::Transaction.search do |search|
361
+ search.id.is transaction.id
362
+ search.status.is Braintree::Transaction::Status::ProcessorDeclined
363
+ end
364
+
365
+ collection._approximate_size.should == 0
366
+ end
367
+
368
+ it "searches on source" do
369
+ transaction = Braintree::Transaction.sale!(
370
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
371
+ :credit_card => {
372
+ :number => Braintree::Test::CreditCardNumbers::Visa,
373
+ :expiration_date => "05/12"
374
+ }
375
+ )
376
+
377
+ collection = Braintree::Transaction.search do |search|
378
+ search.id.is transaction.id
379
+ search.source.is Braintree::Transaction::Source::Api
380
+ end
381
+
382
+ collection._approximate_size.should == 1
383
+
384
+ collection = Braintree::Transaction.search do |search|
385
+ search.id.is transaction.id
386
+ search.source.in Braintree::Transaction::Source::Api, Braintree::Transaction::Source::ControlPanel
387
+ end
388
+
389
+ collection._approximate_size.should == 1
390
+
391
+ collection = Braintree::Transaction.search do |search|
392
+ search.id.is transaction.id
393
+ search.source.is Braintree::Transaction::Source::ControlPanel
394
+ end
395
+
396
+ collection._approximate_size.should == 0
397
+ end
398
+
399
+ it "searches on type" do
400
+ cardholder_name = "refunds#{rand(10000)}"
401
+ credit_transaction = Braintree::Transaction.credit!(
402
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
403
+ :credit_card => {
404
+ :cardholder_name => cardholder_name,
405
+ :number => Braintree::Test::CreditCardNumbers::Visa,
406
+ :expiration_date => "05/12"
407
+ }
408
+ )
409
+
410
+ transaction = Braintree::Transaction.sale!(
411
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
412
+ :credit_card => {
413
+ :cardholder_name => cardholder_name,
414
+ :number => Braintree::Test::CreditCardNumbers::Visa,
415
+ :expiration_date => "05/12"
416
+ },
417
+ :options => { :submit_for_settlement => true }
418
+ )
419
+ Braintree::Http.put "/transactions/#{transaction.id}/settle"
420
+
421
+ refund_transaction = transaction.refund.new_transaction
422
+
423
+ collection = Braintree::Transaction.search do |search|
424
+ search.credit_card_cardholder_name.is cardholder_name
425
+ search.type.is Braintree::Transaction::Type::Credit
426
+ end
427
+
428
+ collection._approximate_size.should == 2
429
+
430
+ collection = Braintree::Transaction.search do |search|
431
+ search.credit_card_cardholder_name.is cardholder_name
432
+ search.type.is Braintree::Transaction::Type::Credit
433
+ search.refund.is true
434
+ end
435
+
436
+ collection._approximate_size.should == 1
437
+ collection.first.id.should == refund_transaction.id
438
+
439
+ collection = Braintree::Transaction.search do |search|
440
+ search.credit_card_cardholder_name.is cardholder_name
441
+ search.type.is Braintree::Transaction::Type::Credit
442
+ search.refund.is false
443
+ end
444
+
445
+ collection._approximate_size.should == 1
446
+ collection.first.id.should == credit_transaction.id
447
+ end
448
+ end
449
+
450
+ context "range fields" do
451
+ context "amount" do
452
+ it "searches on amount" do
453
+ transaction = Braintree::Transaction.sale!(
454
+ :amount => "1000.00",
455
+ :credit_card => {
456
+ :number => Braintree::Test::CreditCardNumbers::Visa,
457
+ :expiration_date => "05/12"
458
+ }
459
+ )
460
+
461
+ collection = Braintree::Transaction.search do |search|
462
+ search.id.is transaction.id
463
+ search.amount.between "500.00", "1500.00"
464
+ end
465
+
466
+ collection._approximate_size.should == 1
467
+ collection.first.id.should == transaction.id
468
+
469
+ collection = Braintree::Transaction.search do |search|
470
+ search.id.is transaction.id
471
+ search.amount >= "500.00"
472
+ end
473
+
474
+ collection._approximate_size.should == 1
475
+ collection.first.id.should == transaction.id
476
+
477
+ collection = Braintree::Transaction.search do |search|
478
+ search.id.is transaction.id
479
+ search.amount <= "1500.00"
480
+ end
481
+
482
+ collection._approximate_size.should == 1
483
+ collection.first.id.should == transaction.id
484
+
485
+ collection = Braintree::Transaction.search do |search|
486
+ search.id.is transaction.id
487
+ search.amount.between "500.00", "900.00"
488
+ end
489
+
490
+ collection._approximate_size.should == 0
491
+ end
492
+
493
+ it "can also take BigDecimal for amount" do
494
+ transaction = Braintree::Transaction.sale!(
495
+ :amount => BigDecimal.new("1000.00"),
496
+ :credit_card => {
497
+ :number => Braintree::Test::CreditCardNumbers::Visa,
498
+ :expiration_date => "05/12"
499
+ }
500
+ )
501
+
502
+ collection = Braintree::Transaction.search do |search|
503
+ search.id.is transaction.id
504
+ search.amount <= BigDecimal.new("1000.00")
505
+ end
506
+
507
+ collection._approximate_size.should == 1
508
+ end
509
+ end
510
+
511
+ it "searches on created_at in UTC" do
512
+ transaction = Braintree::Transaction.sale!(
513
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
514
+ :credit_card => {
515
+ :number => Braintree::Test::CreditCardNumbers::Visa,
516
+ :expiration_date => "05/12"
517
+ }
518
+ )
519
+
520
+ created_at = transaction.created_at
521
+ created_at.should be_utc
522
+
523
+ collection = Braintree::Transaction.search do |search|
524
+ search.id.is transaction.id
525
+ search.created_at.between(
526
+ created_at - 60,
527
+ created_at + 60
528
+ )
529
+ end
530
+
531
+ collection._approximate_size.should == 1
532
+ collection.first.id.should == transaction.id
533
+
534
+ collection = Braintree::Transaction.search do |search|
535
+ search.id.is transaction.id
536
+ search.created_at >= created_at - 1
537
+ end
538
+
539
+ collection._approximate_size.should == 1
540
+ collection.first.id.should == transaction.id
541
+
542
+ collection = Braintree::Transaction.search do |search|
543
+ search.id.is transaction.id
544
+ search.created_at <= created_at + 1
545
+ end
546
+
547
+ collection._approximate_size.should == 1
548
+ collection.first.id.should == transaction.id
549
+
550
+ collection = Braintree::Transaction.search do |search|
551
+ search.id.is transaction.id
552
+ search.created_at.between(
553
+ created_at - 300,
554
+ created_at - 100
555
+ )
556
+ end
557
+
558
+ collection._approximate_size.should == 0
559
+ end
560
+
561
+ it "searches on created_at in local time" do
562
+ transaction = Braintree::Transaction.sale!(
563
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
564
+ :credit_card => {
565
+ :number => Braintree::Test::CreditCardNumbers::Visa,
566
+ :expiration_date => "05/12"
567
+ }
568
+ )
569
+
570
+ now = Time.now
571
+
572
+ collection = Braintree::Transaction.search do |search|
573
+ search.id.is transaction.id
574
+ search.created_at.between(
575
+ now - 60,
576
+ now + 60
577
+ )
578
+ end
579
+
580
+ collection._approximate_size.should == 1
581
+ collection.first.id.should == transaction.id
582
+
583
+ collection = Braintree::Transaction.search do |search|
584
+ search.id.is transaction.id
585
+ search.created_at >= now - 60
586
+ end
587
+
588
+ collection._approximate_size.should == 1
589
+ collection.first.id.should == transaction.id
590
+
591
+ collection = Braintree::Transaction.search do |search|
592
+ search.id.is transaction.id
593
+ search.created_at <= now + 60
594
+ end
595
+
596
+ collection._approximate_size.should == 1
597
+ collection.first.id.should == transaction.id
598
+
599
+ collection = Braintree::Transaction.search do |search|
600
+ search.id.is transaction.id
601
+ search.created_at.between(
602
+ now - 300,
603
+ now - 100
604
+ )
605
+ end
606
+
607
+ collection._approximate_size.should == 0
608
+ end
609
+ end
610
+
611
+ it "returns multiple results" do
612
+ collection = Braintree::Transaction.search do |search|
613
+ search.credit_card_number.starts_with "411"
614
+ end
615
+ collection._approximate_size.should > 100
616
+
617
+ transaction_ids = collection.map {|t| t.id }.uniq.compact
618
+ transaction_ids.size.should == collection._approximate_size
619
+ end
620
+
621
+ context "text node operations" do
622
+ before(:each) do
623
+ @transaction = Braintree::Transaction.sale!(
624
+ :amount => Braintree::Test::TransactionAmounts::Authorize,
625
+ :credit_card => {
626
+ :number => Braintree::Test::CreditCardNumbers::Visa,
627
+ :expiration_date => "05/2012",
628
+ :cardholder_name => "Tom Smith"
629
+ }
630
+ )
631
+ end
632
+
633
+ it "is" do
634
+ collection = Braintree::Transaction.search do |search|
635
+ search.id.is @transaction.id
636
+ search.credit_card_cardholder_name.is "Tom Smith"
637
+ end
638
+
639
+ collection._approximate_size.should == 1
640
+ collection.first.id.should == @transaction.id
641
+
642
+ collection = Braintree::Transaction.search do |search|
643
+ search.id.is @transaction.id
644
+ search.credit_card_cardholder_name.is "Invalid"
645
+ end
646
+
647
+ collection._approximate_size.should == 0
648
+ end
649
+
650
+ it "is_not" do
651
+ collection = Braintree::Transaction.search do |search|
652
+ search.id.is @transaction.id
653
+ search.credit_card_cardholder_name.is_not "Anybody Else"
654
+ end
655
+
656
+ collection._approximate_size.should == 1
657
+ collection.first.id.should == @transaction.id
658
+
659
+ collection = Braintree::Transaction.search do |search|
660
+ search.id.is @transaction.id
661
+ search.credit_card_cardholder_name.is_not "Tom Smith"
662
+ end
663
+
664
+ collection._approximate_size.should == 0
665
+ end
666
+
667
+ it "ends_with" do
668
+ collection = Braintree::Transaction.search do |search|
669
+ search.id.is @transaction.id
670
+ search.credit_card_cardholder_name.ends_with "m Smith"
671
+ end
672
+
673
+ collection._approximate_size.should == 1
674
+ collection.first.id.should == @transaction.id
675
+
676
+ collection = Braintree::Transaction.search do |search|
677
+ search.id.is @transaction.id
678
+ search.credit_card_cardholder_name.ends_with "Tom S"
679
+ end
680
+
681
+ collection._approximate_size.should == 0
682
+ end
683
+
684
+ it "starts_with" do
685
+ collection = Braintree::Transaction.search do |search|
686
+ search.id.is @transaction.id
687
+ search.credit_card_cardholder_name.starts_with "Tom S"
688
+ end
689
+
690
+ collection._approximate_size.should == 1
691
+ collection.first.id.should == @transaction.id
692
+
693
+ collection = Braintree::Transaction.search do |search|
694
+ search.id.is @transaction.id
695
+ search.credit_card_cardholder_name.starts_with "m Smith"
696
+ end
697
+
698
+ collection._approximate_size.should == 0
699
+ end
700
+
701
+ it "contains" do
702
+ collection = Braintree::Transaction.search do |search|
703
+ search.id.is @transaction.id
704
+ search.credit_card_cardholder_name.contains "m Sm"
705
+ end
706
+
707
+ collection._approximate_size.should == 1
708
+ collection.first.id.should == @transaction.id
709
+
710
+ collection = Braintree::Transaction.search do |search|
711
+ search.id.is @transaction.id
712
+ search.credit_card_cardholder_name.contains "Anybody Else"
713
+ end
714
+
715
+ collection._approximate_size.should == 0
716
+ end
717
+ end
718
+ end
719
+
720
+ context "basic" do
721
+ it "returns transactions matching the given search terms" do
722
+ transactions = Braintree::Transaction.search "1111"
723
+ transactions._approximate_size.should > 0
724
+ end
725
+
726
+ it "can iterate over the entire collection" do
727
+ transactions = Braintree::Transaction.search "411111"
728
+ transactions._approximate_size.should > 100
729
+
730
+ transaction_ids = transactions.map {|t| t.id }.uniq.compact
731
+ transaction_ids.size.should == transactions._approximate_size
732
+ end
733
+ end
734
+ end