braintree 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -17,15 +17,25 @@ The Braintree gem provides integration access to the Braintree Gateway.
17
17
  Braintree::Configuration.public_key = "a_public_key"
18
18
  Braintree::Configuration.private_key = "a_private_key"
19
19
 
20
- transaction = Braintree::Transaction.sale!(
21
- :amount => "100.00",
20
+ result = Braintree::Transaction.sale(
21
+ :amount => "1000.00",
22
22
  :credit_card => {
23
23
  :number => "5105105105105100",
24
24
  :expiration_date => "05/12"
25
25
  }
26
26
  )
27
- puts "Transaction ID: #{transaction.id}"
28
- puts "Status: #{transaction.status}"
27
+
28
+ if result.success?
29
+ if result.transaction.status == Braintree::Transaction::Status::Authorized
30
+ puts "sucess!: #{result.transaction.id}"
31
+ else
32
+ puts "Error processing transaction:"
33
+ puts " code: #{result.transaction.processor_response_code}"
34
+ puts " text: #{result.transaction.processor_response_text}"
35
+ end
36
+ else
37
+ p result.errors
38
+ end
29
39
 
30
40
  == Bang Methods
31
41
 
@@ -224,7 +224,7 @@ module Braintree
224
224
  def self._update_signature # :nodoc:
225
225
  [
226
226
  :cardholder_name, :cvv, :expiration_date, :expiration_month, :expiration_year, :number, :token,
227
- {:options => [:verify_card]},
227
+ {:options => [:make_default, :verify_card]},
228
228
  {:billing_address => [:company, :country_name, :extended_address, :first_name, :last_name, :locality, :postal_code, :region, :street_address]}
229
229
  ]
230
230
  end
@@ -119,6 +119,19 @@ module Braintree
119
119
  class Transaction
120
120
  include BaseModule
121
121
 
122
+ module Status
123
+ Authorizing = 'authorizing'
124
+ Authorized = 'authorized'
125
+ GatewayRejected = 'gateway_rejected'
126
+ Failed = 'failed'
127
+ ProcessorDeclined = 'processor_declined'
128
+ Settled = 'settled'
129
+ SettlementFailed = 'settlement_failed'
130
+ SubmittedForSettlement = 'submitted_for_settlement'
131
+ Unknown = 'unknown'
132
+ Voided = 'voided'
133
+ end
134
+
122
135
  module Type # :nodoc:
123
136
  Credit = "credit" # :nodoc:
124
137
  Sale = "sale" # :nodoc:
@@ -35,6 +35,8 @@ module Braintree
35
35
  @errors[index]
36
36
  end
37
37
 
38
+ # Returns an array of ValidationError objects at this level and all nested levels in the error
39
+ # hierarchy
38
40
  def deep_errors
39
41
  ([@errors] + @nested.values.map { |error_collection| error_collection.deep_errors }).flatten
40
42
  end
@@ -63,6 +65,11 @@ module Braintree
63
65
  @errors.select { |error| error.attribute == attribute.to_s }
64
66
  end
65
67
 
68
+ # Returns an array of ValidationError objects at the given level in the error hierarchy
69
+ def shallow_errors
70
+ @errors.dup
71
+ end
72
+
66
73
  # The number of errors at this level. This does not include nested errors.
67
74
  def size
68
75
  @errors.size
@@ -2,7 +2,7 @@ module Braintree
2
2
  module Version
3
3
  Major = 1
4
4
  Minor = 2
5
- Tiny = 0
5
+ Tiny = 1
6
6
 
7
7
  String = "#{Major}.#{Minor}.#{Tiny}"
8
8
  end
@@ -67,7 +67,7 @@ describe Braintree::CreditCard do
67
67
  :options => {:verify_card => true}
68
68
  )
69
69
  result.success?.should == false
70
- result.credit_card_verification.status.should == "processor_declined"
70
+ result.credit_card_verification.status.should == Braintree::Transaction::Status::ProcessorDeclined
71
71
  result.credit_card_verification.processor_response_code.should == "2000"
72
72
  result.credit_card_verification.processor_response_text.should == "Do Not Honor"
73
73
  result.credit_card_verification.cvv_response_code.should == "I"
@@ -116,6 +116,28 @@ describe Braintree::CreditCard do
116
116
  result.success?.should == false
117
117
  result.errors.for(:credit_card).on(:expiration_date)[0].message.should == "Expiration date is invalid."
118
118
  end
119
+
120
+ it "can set the default flag" do
121
+ customer = Braintree::Customer.create!
122
+ card1 = Braintree::CreditCard.create(
123
+ :customer_id => customer.id,
124
+ :number => Braintree::Test::CreditCardNumbers::Visa,
125
+ :expiration_date => "05/2009"
126
+ ).credit_card
127
+ card1.should be_default
128
+
129
+ card2 = Braintree::CreditCard.create(
130
+ :customer_id => customer.id,
131
+ :number => Braintree::Test::CreditCardNumbers::Visa,
132
+ :expiration_date => "05/2009",
133
+ :options => {
134
+ :make_default => true
135
+ }
136
+ ).credit_card
137
+ card2.should be_default
138
+
139
+ Braintree::CreditCard.find(card1.token).should_not be_default
140
+ end
119
141
  end
120
142
 
121
143
  describe "self.create!" do
@@ -221,6 +243,39 @@ describe Braintree::CreditCard do
221
243
  credit_card.customer_id.should == customer.id
222
244
  end
223
245
 
246
+ it "create card as default" do
247
+ customer = Braintree::Customer.create!(
248
+ :credit_card => {
249
+ :cardholder_name => "Old Cardholder Name",
250
+ :number => Braintree::Test::CreditCardNumbers::Visa,
251
+ :expiration_date => "05/2012"
252
+ }
253
+ )
254
+ card1 = customer.credit_cards[0]
255
+
256
+ params = {
257
+ :credit_card => {
258
+ :cardholder_name => "Card Holder",
259
+ :number => Braintree::Test::CreditCardNumbers::Visa,
260
+ :expiration_date => "05/2012",
261
+ :options => {:make_default => true}
262
+ }
263
+ }
264
+ tr_data_params = {
265
+ :credit_card => {
266
+ :customer_id => customer.id
267
+ }
268
+ }
269
+ tr_data = Braintree::TransparentRedirect.create_credit_card_data({:redirect_url => "http://example.com"}.merge(tr_data_params))
270
+ query_string_response = SpecHelper.simulate_form_post_for_tr(Braintree::CreditCard.create_credit_card_url, tr_data, params)
271
+ result = Braintree::CreditCard.create_from_transparent_redirect(query_string_response)
272
+ result.success?.should == true
273
+ card2 = result.credit_card
274
+
275
+ Braintree::CreditCard.find(card1.token).should_not be_default
276
+ card2.should be_default
277
+ end
278
+
224
279
  it "returns xml with nested errors if validation errors" do
225
280
  customer = Braintree::Customer.create.customer
226
281
  params = {
@@ -307,7 +362,7 @@ describe Braintree::CreditCard do
307
362
  :options => {:verify_card => true}
308
363
  )
309
364
  update_result.success?.should == false
310
- update_result.credit_card_verification.status.should == "processor_declined"
365
+ update_result.credit_card_verification.status.should == Braintree::Transaction::Status::ProcessorDeclined
311
366
  end
312
367
 
313
368
  it "can update the billing address" do
@@ -373,6 +428,28 @@ describe Braintree::CreditCard do
373
428
  update_result.success?.should == false
374
429
  update_result.errors.for(:credit_card).on(:number)[0].message.should == "Credit card number must be 12-19 digits."
375
430
  end
431
+
432
+ it "can update the default" do
433
+ customer = Braintree::Customer.create!
434
+ card1 = Braintree::CreditCard.create(
435
+ :customer_id => customer.id,
436
+ :number => Braintree::Test::CreditCardNumbers::Visa,
437
+ :expiration_date => "05/2009"
438
+ ).credit_card
439
+ card2 = Braintree::CreditCard.create(
440
+ :customer_id => customer.id,
441
+ :number => Braintree::Test::CreditCardNumbers::Visa,
442
+ :expiration_date => "05/2009"
443
+ ).credit_card
444
+
445
+ card1.should be_default
446
+ card2.should_not be_default
447
+
448
+ Braintree::CreditCard.update(card2.token, :options => {:make_default => true})
449
+
450
+ Braintree::CreditCard.find(card1.token).should_not be_default
451
+ Braintree::CreditCard.find(card2.token).should be_default
452
+ end
376
453
  end
377
454
 
378
455
  describe "self.update!" do
@@ -451,6 +528,41 @@ describe Braintree::CreditCard do
451
528
  credit_card.expiration_date.should == "05/2014"
452
529
  credit_card.token.should == new_token
453
530
  end
531
+
532
+ it "updates the default credit card" do
533
+ customer = Braintree::Customer.create!(
534
+ :credit_card => {
535
+ :cardholder_name => "Old Cardholder Name",
536
+ :number => Braintree::Test::CreditCardNumbers::Visa,
537
+ :expiration_date => "05/2012"
538
+ }
539
+ )
540
+ card1 = customer.credit_cards[0]
541
+
542
+ card2 = Braintree::CreditCard.create(
543
+ :customer_id => customer.id,
544
+ :number => Braintree::Test::CreditCardNumbers::Visa,
545
+ :expiration_date => "05/2009"
546
+ ).credit_card
547
+
548
+ card1.should be_default
549
+ card2.should_not be_default
550
+
551
+ params = {
552
+ :credit_card => {
553
+ :options => {:make_default => true}
554
+ }
555
+ }
556
+ tr_data_params = {
557
+ :payment_method_token => card2.token
558
+ }
559
+ tr_data = Braintree::TransparentRedirect.update_credit_card_data({:redirect_url => "http://example.com"}.merge(tr_data_params))
560
+ query_string_response = SpecHelper.simulate_form_post_for_tr(Braintree::CreditCard.update_credit_card_url, tr_data, params)
561
+ result = Braintree::CreditCard.update_from_transparent_redirect(query_string_response)
562
+
563
+ Braintree::CreditCard.find(card1.token).should_not be_default
564
+ Braintree::CreditCard.find(card2.token).should be_default
565
+ end
454
566
  end
455
567
 
456
568
  describe "credit" do
@@ -744,7 +856,7 @@ describe Braintree::CreditCard do
744
856
  :options => {:verify_card => true}
745
857
  )
746
858
  update_result.success?.should == false
747
- update_result.credit_card_verification.status.should == "processor_declined"
859
+ update_result.credit_card_verification.status.should == Braintree::Transaction::Status::ProcessorDeclined
748
860
  end
749
861
 
750
862
  it "can update the billing address" do
@@ -110,7 +110,7 @@ describe Braintree::Customer do
110
110
  }
111
111
  )
112
112
  result.success?.should == false
113
- result.credit_card_verification.status.should == "processor_declined"
113
+ result.credit_card_verification.status.should == Braintree::Transaction::Status::ProcessorDeclined
114
114
  end
115
115
 
116
116
  it "can create a customer, payment method, and billing address at the same time" do
@@ -10,7 +10,7 @@ describe Braintree::Test::TransactionAmounts do
10
10
  :expiration_date => "12/2012"
11
11
  }
12
12
  )
13
- transaction.status.should == "authorized"
13
+ transaction.status.should == Braintree::Transaction::Status::Authorized
14
14
  end
15
15
  end
16
16
 
@@ -23,7 +23,7 @@ describe Braintree::Test::TransactionAmounts do
23
23
  :expiration_date => "12/2012"
24
24
  }
25
25
  )
26
- transaction.status.should == "processor_declined"
26
+ transaction.status.should == Braintree::Transaction::Status::ProcessorDeclined
27
27
  end
28
28
  end
29
29
  end
@@ -34,7 +34,7 @@ describe Braintree::Transaction do
34
34
  result.success?.should == true
35
35
  result.transaction.id.should =~ /^\w{6}$/
36
36
  result.transaction.type.should == "sale"
37
- result.transaction.status.should == "processor_declined"
37
+ result.transaction.status.should == Braintree::Transaction::Status::ProcessorDeclined
38
38
  result.transaction.processor_response_code.should == "2000"
39
39
  result.transaction.processor_response_text.should == "Do Not Honor"
40
40
  end
@@ -296,7 +296,7 @@ describe Braintree::Transaction do
296
296
  transaction = result.transaction
297
297
  transaction.id.should =~ /\A\w{6}\z/
298
298
  transaction.type.should == "sale"
299
- transaction.status.should == "authorized"
299
+ transaction.status.should == Braintree::Transaction::Status::Authorized
300
300
  transaction.amount.should == BigDecimal.new("100.00")
301
301
  transaction.order_id.should == "123"
302
302
  transaction.processor_response_code.should == "1000"
@@ -461,7 +461,7 @@ describe Braintree::Transaction do
461
461
  }
462
462
  )
463
463
  result.success?.should == true
464
- result.transaction.status.should == "submitted_for_settlement"
464
+ result.transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
465
465
  end
466
466
 
467
467
  it "can specify the customer id and payment method token" do
@@ -563,7 +563,7 @@ describe Braintree::Transaction do
563
563
  result = Braintree::Transaction.submit_for_settlement(transaction.id, "999.99")
564
564
  result.success?.should == true
565
565
  result.transaction.amount.should == BigDecimal.new("999.99")
566
- result.transaction.status.should == "submitted_for_settlement"
566
+ result.transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
567
567
  result.transaction.updated_at.between?(Time.now - 5, Time.now).should == true
568
568
  end
569
569
 
@@ -606,7 +606,7 @@ describe Braintree::Transaction do
606
606
  }
607
607
  )
608
608
  transaction = Braintree::Transaction.submit_for_settlement!(original_transaction.id)
609
- transaction.status.should == "submitted_for_settlement"
609
+ transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
610
610
  transaction.id.should == original_transaction.id
611
611
  end
612
612
 
@@ -773,7 +773,7 @@ describe Braintree::Transaction do
773
773
  transaction = result.transaction
774
774
  transaction.id.should =~ /\A\w{6}\z/
775
775
  transaction.type.should == "sale"
776
- transaction.status.should == "authorized"
776
+ transaction.status.should == Braintree::Transaction::Status::Authorized
777
777
  transaction.amount.should == BigDecimal.new("100.00")
778
778
  transaction.order_id.should == "123"
779
779
  transaction.processor_response_code.should == "1000"
@@ -874,7 +874,7 @@ describe Braintree::Transaction do
874
874
  result = Braintree::Transaction.void(transaction.id)
875
875
  result.success?.should == true
876
876
  result.transaction.id.should == transaction.id
877
- result.transaction.status.should == "voided"
877
+ result.transaction.status.should == Braintree::Transaction::Status::Voided
878
878
  end
879
879
 
880
880
  it "returns an error result if unsuccessful" do
@@ -902,7 +902,7 @@ describe Braintree::Transaction do
902
902
  )
903
903
  returned_transaction = Braintree::Transaction.void!(transaction.id)
904
904
  returned_transaction.should == transaction
905
- returned_transaction.status.should == "voided"
905
+ returned_transaction.status.should == Braintree::Transaction::Status::Voided
906
906
  end
907
907
 
908
908
  it "raises a ValidationsFailed if unsuccessful" do
@@ -922,7 +922,7 @@ describe Braintree::Transaction do
922
922
  describe "refund" do
923
923
  it "returns a successful result if successful" do
924
924
  transaction = create_transaction_to_refund
925
- transaction.status.should == "settled"
925
+ transaction.status.should == Braintree::Transaction::Status::Settled
926
926
  result = transaction.refund
927
927
  result.success?.should == true
928
928
  result.new_transaction.type.should == "credit"
@@ -991,7 +991,7 @@ describe Braintree::Transaction do
991
991
  result = transaction.submit_for_settlement("999.99")
992
992
  result.success?.should == true
993
993
  transaction.amount.should == BigDecimal.new("999.99")
994
- transaction.status.should == "submitted_for_settlement"
994
+ transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
995
995
  transaction.updated_at.between?(Time.now - 5, Time.now).should == true
996
996
  end
997
997
 
@@ -1021,7 +1021,7 @@ describe Braintree::Transaction do
1021
1021
  }
1022
1022
  )
1023
1023
  transaction = original_transaction.submit_for_settlement!
1024
- transaction.status.should == "submitted_for_settlement"
1024
+ transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
1025
1025
  transaction.id.should == original_transaction.id
1026
1026
  end
1027
1027
 
@@ -1149,8 +1149,8 @@ describe Braintree::Transaction do
1149
1149
  )
1150
1150
  transaction.submit_for_settlement!
1151
1151
  transaction.status_history.size.should == 2
1152
- transaction.status_history[0].status.should == "authorized"
1153
- transaction.status_history[1].status.should == "submitted_for_settlement"
1152
+ transaction.status_history[0].status.should == Braintree::Transaction::Status::Authorized
1153
+ transaction.status_history[1].status.should == Braintree::Transaction::Status::SubmittedForSettlement
1154
1154
  end
1155
1155
  end
1156
1156
 
@@ -1216,7 +1216,7 @@ describe Braintree::Transaction do
1216
1216
  )
1217
1217
  result.success?.should == true
1218
1218
  transaction = result.transaction
1219
- transaction.status.should == "authorized"
1219
+ transaction.status.should == Braintree::Transaction::Status::Authorized
1220
1220
  void_result = transaction.void
1221
1221
  void_result.success?.should == true
1222
1222
  void_result.transaction.should == transaction
@@ -1231,7 +1231,7 @@ describe Braintree::Transaction do
1231
1231
  :expiration_date => "05/2009"
1232
1232
  }
1233
1233
  )
1234
- transaction.status.should == "processor_declined"
1234
+ transaction.status.should == Braintree::Transaction::Status::ProcessorDeclined
1235
1235
  result = transaction.void
1236
1236
  result.success?.should == false
1237
1237
  result.errors.for(:transaction).on(:base)[0].code.should == Braintree::ErrorCodes::Transaction::CannotBeVoided
@@ -1248,7 +1248,7 @@ describe Braintree::Transaction do
1248
1248
  }
1249
1249
  )
1250
1250
  transaction.void!.should == transaction
1251
- transaction.status.should == "voided"
1251
+ transaction.status.should == Braintree::Transaction::Status::Voided
1252
1252
  end
1253
1253
 
1254
1254
  it "raises a ValidationsFailed if unsuccessful" do
@@ -1259,7 +1259,7 @@ describe Braintree::Transaction do
1259
1259
  :expiration_date => "05/2009"
1260
1260
  }
1261
1261
  )
1262
- transaction.status.should == "processor_declined"
1262
+ transaction.status.should == Braintree::Transaction::Status::ProcessorDeclined
1263
1263
  expect do
1264
1264
  transaction.void!
1265
1265
  end.to raise_error(Braintree::ValidationsFailed)
@@ -82,14 +82,14 @@ describe Braintree::Transaction do
82
82
  transaction = Braintree::Transaction._new(
83
83
  :status_history => [
84
84
  { :timestamp => time, :amount => "12.00", :transaction_source => "API",
85
- :user => "larry", :status => "authorized" },
85
+ :user => "larry", :status => Braintree::Transaction::Status::Authorized },
86
86
  { :timestamp => Time.utc(2010,1,15), :amount => "12.00", :transaction_source => "API",
87
87
  :user => "curly", :status => "scheduled_for_settlement"}
88
88
  ])
89
89
  transaction.status_history.size.should == 2
90
90
  transaction.status_history[0].user.should == "larry"
91
91
  transaction.status_history[0].amount.should == "12.00"
92
- transaction.status_history[0].status.should == "authorized"
92
+ transaction.status_history[0].status.should == Braintree::Transaction::Status::Authorized
93
93
  transaction.status_history[0].transaction_source.should == "API"
94
94
  transaction.status_history[0].timestamp.should == time
95
95
  transaction.status_history[1].user.should == "curly"
@@ -119,7 +119,7 @@ describe Braintree::Transaction do
119
119
  :id => "1234",
120
120
  :type => "sale",
121
121
  :amount => "100.00",
122
- :status => "authorized"
122
+ :status => Braintree::Transaction::Status::Authorized
123
123
  )
124
124
  output = transaction.inspect
125
125
  output.should include(%Q(#<Braintree::Transaction id: "1234", type: "sale", amount: "100.0", status: "authorized"))
@@ -145,4 +145,31 @@ describe Braintree::ValidationErrorCollection do
145
145
  end
146
146
  end
147
147
 
148
+ describe "shallow_errors" do
149
+ it "returns errors on one level" do
150
+ errors = Braintree::ValidationErrorCollection.new(
151
+ :errors => [
152
+ { :attribute => "one", :code => 1, :message => "bad juju" },
153
+ { :attribute => "two", :code => 2, :message => "bad juju" }],
154
+ :nested => {
155
+ :errors => [{ :attribute => "three", :code => 3, :message => "badder juju"}],
156
+ :nested_again => {
157
+ :errors => [{ :attribute => "four", :code => 4, :message => "badder juju"}]
158
+ }
159
+ }
160
+ )
161
+ errors.shallow_errors.map {|e| e.code}.should == [1, 2]
162
+ errors.for(:nested).shallow_errors.map {|e| e.code}.should == [3]
163
+ end
164
+
165
+ it "returns an clone of the real array" do
166
+ errors = Braintree::ValidationErrorCollection.new(
167
+ :errors => [
168
+ { :attribute => "one", :code => 1, :message => "bad juju" },
169
+ { :attribute => "two", :code => 2, :message => "bad juju" }]
170
+ )
171
+ errors.shallow_errors.pop
172
+ errors.shallow_errors.map {|e| e.code}.should == [1, 2]
173
+ end
174
+ end
148
175
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 0
9
- version: 1.2.0
8
+ - 1
9
+ version: 1.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Braintree Payment Solutions
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-09 00:00:00 -05:00
17
+ date: 2010-04-13 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20