braintree 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/lib/braintree.rb +36 -46
  2. data/lib/braintree/address.rb +14 -14
  3. data/lib/braintree/base_module.rb +3 -3
  4. data/lib/braintree/configuration.rb +16 -16
  5. data/lib/braintree/credit_card.rb +39 -30
  6. data/lib/braintree/credit_card_verification.rb +4 -3
  7. data/lib/braintree/customer.rb +22 -22
  8. data/lib/braintree/digest.rb +2 -8
  9. data/lib/braintree/error_codes.rb +16 -3
  10. data/lib/braintree/error_result.rb +5 -5
  11. data/lib/braintree/exceptions.rb +58 -0
  12. data/lib/braintree/http.rb +7 -7
  13. data/lib/braintree/paged_collection.rb +14 -14
  14. data/lib/braintree/ssl_expiration_check.rb +5 -1
  15. data/lib/braintree/subscription.rb +110 -0
  16. data/lib/braintree/successful_result.rb +3 -3
  17. data/lib/braintree/test/credit_card_numbers.rb +1 -1
  18. data/lib/braintree/test/transaction_amounts.rb +18 -0
  19. data/lib/braintree/transaction.rb +52 -25
  20. data/lib/braintree/transaction/address_details.rb +2 -2
  21. data/lib/braintree/transaction/credit_card_details.rb +12 -4
  22. data/lib/braintree/transaction/customer_details.rb +9 -1
  23. data/lib/braintree/transaction/status_details.rb +1 -1
  24. data/lib/braintree/transparent_redirect.rb +15 -15
  25. data/lib/braintree/util.rb +7 -7
  26. data/lib/braintree/validation_error.rb +1 -1
  27. data/lib/braintree/validation_error_collection.rb +6 -6
  28. data/lib/braintree/version.rb +3 -3
  29. data/lib/braintree/xml/generator.rb +2 -2
  30. data/lib/braintree/xml/libxml.rb +1 -1
  31. data/lib/braintree/xml/parser.rb +1 -1
  32. data/spec/integration/braintree/address_spec.rb +12 -12
  33. data/spec/integration/braintree/credit_card_spec.rb +189 -37
  34. data/spec/integration/braintree/customer_spec.rb +35 -35
  35. data/spec/integration/braintree/http_spec.rb +3 -3
  36. data/spec/integration/braintree/subscription_spec.rb +362 -0
  37. data/spec/integration/braintree/transaction_spec.rb +130 -58
  38. data/spec/integration/spec_helper.rb +1 -1
  39. data/spec/spec_helper.rb +4 -4
  40. data/spec/unit/braintree/address_spec.rb +6 -6
  41. data/spec/unit/braintree/base_module_spec.rb +18 -0
  42. data/spec/unit/braintree/configuration_spec.rb +10 -10
  43. data/spec/unit/braintree/credit_card_spec.rb +15 -15
  44. data/spec/unit/braintree/credit_card_verification_spec.rb +4 -2
  45. data/spec/unit/braintree/customer_spec.rb +8 -8
  46. data/spec/unit/braintree/digest_spec.rb +4 -0
  47. data/spec/unit/braintree/http_spec.rb +2 -2
  48. data/spec/unit/braintree/paged_collection_spec.rb +12 -12
  49. data/spec/unit/braintree/ssl_expiration_check_spec.rb +18 -9
  50. data/spec/unit/braintree/transaction/credit_card_details_spec.rb +15 -0
  51. data/spec/unit/braintree/transaction/customer_details_spec.rb +19 -0
  52. data/spec/unit/braintree/transaction_spec.rb +14 -14
  53. data/spec/unit/braintree/transparent_redirect_spec.rb +11 -11
  54. data/spec/unit/braintree/util_spec.rb +8 -8
  55. data/spec/unit/braintree/validation_error_collection_spec.rb +6 -6
  56. data/spec/unit/braintree/validation_error_spec.rb +2 -2
  57. data/spec/unit/braintree/xml/libxml_spec.rb +5 -5
  58. data/spec/unit/braintree/xml_spec.rb +16 -16
  59. data/spec/unit/braintree_spec.rb +11 -0
  60. metadata +8 -2
@@ -10,7 +10,7 @@ unless defined?(INTEGRATION_SPEC_HELPER_LOADED)
10
10
  GATEWAY_SERVER_PORT = 3000
11
11
  GATEWAY_PID_FILE = "/tmp/gateway_server_#{Braintree::Configuration.port}.pid"
12
12
  SPHINX_PID_FILE = "#{GATEWAY_ROOT}/log/searchd.integration.pid"
13
-
13
+
14
14
  gateway_already_started = File.exist?(GATEWAY_PID_FILE)
15
15
  sphinx_already_started = File.exist?(SPHINX_PID_FILE)
16
16
  config.before(:suite) do
data/spec/spec_helper.rb CHANGED
@@ -8,7 +8,7 @@ unless defined?(SPEC_HELPER_LOADED)
8
8
  braintree_lib = "#{project_root}/lib"
9
9
  $LOAD_PATH << braintree_lib
10
10
  require "braintree"
11
-
11
+
12
12
  Braintree::Configuration.environment = :development
13
13
  Braintree::Configuration.merchant_id = "integration_merchant_id"
14
14
  Braintree::Configuration.public_key = "integration_public_key"
@@ -21,7 +21,7 @@ unless defined?(SPEC_HELPER_LOADED)
21
21
  Time.class_eval do
22
22
  class << self
23
23
  alias original_now now
24
- end
24
+ end
25
25
  end
26
26
  (class << Time; self; end).class_eval do
27
27
  define_method(:now) { desired_time }
@@ -31,8 +31,8 @@ unless defined?(SPEC_HELPER_LOADED)
31
31
  Time.class_eval do
32
32
  class << self
33
33
  alias now original_now
34
- end
35
- end
34
+ end
35
+ end
36
36
  end
37
37
  end
38
38
  end
@@ -5,19 +5,19 @@ describe Braintree::Address do
5
5
  it "returns true if given an address with the same id and customer_id" do
6
6
  first = Braintree::Address._new(:customer_id => "c1", :id => 'a1')
7
7
  second = Braintree::Address._new(:customer_id => "c1", :id => "a1")
8
-
8
+
9
9
  first.should == second
10
10
  second.should == first
11
11
  end
12
-
12
+
13
13
  it "returns false if given an address with a different id and the same customer_id" do
14
14
  first = Braintree::Address._new(:customer_id => "c1", :id => "a1")
15
15
  second = Braintree::Address._new(:customer_id => "c1", :id => "not a1")
16
-
16
+
17
17
  first.should_not == second
18
18
  second.should_not == first
19
19
  end
20
-
20
+
21
21
  it "returns false if given an address with a different customer_id and the same id" do
22
22
  first = Braintree::Address._new(:customer_id => "c1", :id => "a1")
23
23
  second = Braintree::Address._new(:customer_id => "not c1", :id => "a1")
@@ -51,7 +51,7 @@ describe Braintree::Address do
51
51
  end.to raise_error(ArgumentError, "invalid keys: invalid_key")
52
52
  end
53
53
  end
54
-
54
+
55
55
  describe "self.update" do
56
56
  it "raises an exception if hash includes an invalid key" do
57
57
  expect do
@@ -67,7 +67,7 @@ describe Braintree::Address do
67
67
  end.to raise_error(ArgumentError, "customer_id contains invalid characters")
68
68
  end
69
69
  end
70
-
70
+
71
71
  describe "self.new" do
72
72
  it "is protected" do
73
73
  expect do
@@ -0,0 +1,18 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Braintree::BaseModule do
4
+ describe "return_object_or_raise" do
5
+ it "inspects the error_result when inspecting the exception" do
6
+ result = Braintree::ErrorResult.new(:errors => {})
7
+ begin
8
+ klass = Class.new { include Braintree::BaseModule }
9
+ klass.return_object_or_raise(:obj) { result }
10
+ rescue Braintree::ValidationsFailed => ex
11
+ end
12
+ ex.should_not == nil
13
+ ex.error_result.should == result
14
+ ex.inspect.should include(result.inspect)
15
+ ex.to_s.should include(result.inspect)
16
+ end
17
+ end
18
+ end
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + "/../spec_helper"
2
2
 
3
3
  describe Braintree::Configuration do
4
-
4
+
5
5
  before do
6
6
  @original_merchant_id = Braintree::Configuration.merchant_id
7
7
  @original_public_key = Braintree::Configuration.public_key
@@ -11,11 +11,11 @@ describe Braintree::Configuration do
11
11
 
12
12
  after do
13
13
  Braintree::Configuration.merchant_id = @original_merchant_id
14
- Braintree::Configuration.public_key = @original_public_key
14
+ Braintree::Configuration.public_key = @original_public_key
15
15
  Braintree::Configuration.private_key = @original_private_key
16
16
  Braintree::Configuration.environment = @original_environment
17
17
  end
18
-
18
+
19
19
  describe "self.base_merchant_path" do
20
20
  it "returns /merchants/{merchant_id}" do
21
21
  Braintree::Configuration.base_merchant_path.should == "/merchants/integration_merchant_id"
@@ -70,7 +70,7 @@ describe Braintree::Configuration do
70
70
  end.to raise_error(Braintree::ConfigurationError, "Braintree::Configuration.environment needs to be set")
71
71
  end
72
72
  end
73
-
73
+
74
74
  describe "self.environment=" do
75
75
  it "raises an exception if the environment is invalid" do
76
76
  expect do
@@ -78,7 +78,7 @@ describe Braintree::Configuration do
78
78
  end.to raise_error(ArgumentError, ":invalid_environment is not a valid environment")
79
79
  end
80
80
  end
81
-
81
+
82
82
  describe "self.logger" do
83
83
  it "defaults to logging to stdout with log_level info" do
84
84
  begin
@@ -99,7 +99,7 @@ describe Braintree::Configuration do
99
99
  end.to raise_error(Braintree::ConfigurationError, "Braintree::Configuration.merchant_id needs to be set")
100
100
  end
101
101
  end
102
-
102
+
103
103
  describe "self.public_key" do
104
104
  it "raises an exception if it hasn't been set yet" do
105
105
  Braintree::Configuration.instance_variable_set(:@public_key, nil)
@@ -137,12 +137,12 @@ describe Braintree::Configuration do
137
137
 
138
138
  describe "self.protocol" do
139
139
  it "is http for development" do
140
- Braintree::Configuration.environment = :development
140
+ Braintree::Configuration.environment = :development
141
141
  Braintree::Configuration.protocol.should == "http"
142
142
  end
143
143
 
144
144
  it "is https for production" do
145
- Braintree::Configuration.environment = :production
145
+ Braintree::Configuration.environment = :production
146
146
  Braintree::Configuration.protocol.should == "https"
147
147
  end
148
148
 
@@ -169,7 +169,7 @@ describe Braintree::Configuration do
169
169
  Braintree::Configuration.server.should == "sandbox.braintreegateway.com"
170
170
  end
171
171
  end
172
-
172
+
173
173
  describe "self.ssl?" do
174
174
  it "returns false for development" do
175
175
  Braintree::Configuration.environment = :development
@@ -186,5 +186,5 @@ describe Braintree::Configuration do
186
186
  Braintree::Configuration.ssl?.should == true
187
187
  end
188
188
  end
189
-
189
+
190
190
  end
@@ -21,41 +21,41 @@ describe Braintree::CreditCard do
21
21
  it "returns the url" do
22
22
  Braintree::CreditCard.create_credit_card_url.should == "http://localhost:3000/merchants/integration_merchant_id/payment_methods/all/create_via_transparent_redirect_request"
23
23
  end
24
- end
24
+ end
25
25
 
26
26
  describe "==" do
27
27
  it "returns true if given a credit card with the same token" do
28
28
  first = Braintree::CreditCard._new(:token => 123)
29
29
  second = Braintree::CreditCard._new(:token => 123)
30
-
30
+
31
31
  first.should == second
32
32
  second.should == first
33
33
  end
34
-
34
+
35
35
  it "returns false if given a credit card with a different token" do
36
36
  first = Braintree::CreditCard._new(:token => 123)
37
37
  second = Braintree::CreditCard._new(:token => 124)
38
-
38
+
39
39
  first.should_not == second
40
- second.should_not == first
40
+ second.should_not == first
41
41
  end
42
-
42
+
43
43
  it "returns false if not given a credit card" do
44
44
  credit_card = Braintree::CreditCard._new(:token => 123)
45
45
  credit_card.should_not == "not a credit card"
46
46
  end
47
47
  end
48
-
48
+
49
49
  describe "default?" do
50
50
  it "is true if the credit card is the default credit card for the customer" do
51
51
  Braintree::CreditCard._new(:default => true).default?.should == true
52
52
  end
53
-
53
+
54
54
  it "is false if the credit card is not the default credit card for the customer" do
55
55
  Braintree::CreditCard._new(:default => false).default?.should == false
56
56
  end
57
57
  end
58
-
58
+
59
59
  describe "expired?" do
60
60
  it "is true if the payment method is this year and the month has passed" do
61
61
  SpecHelper.stub_time_dot_now(Time.mktime(2009, 10, 20)) do
@@ -63,18 +63,18 @@ describe Braintree::CreditCard do
63
63
  expired_pm.expired?.should == true
64
64
  end
65
65
  end
66
-
66
+
67
67
  it "is true if the payment method is in a previous year" do
68
68
  expired_pm = Braintree::CreditCard._new(:expiration_month => "12", :expiration_year => (Time.now.year - 1).to_s)
69
69
  expired_pm.expired?.should == true
70
70
  end
71
-
71
+
72
72
  it "is false if the payment method is not expired" do
73
73
  not_expired_pm = Braintree::CreditCard._new(:expiration_month => "01", :expiration_year => (Time.now.year + 1).to_s)
74
74
  not_expired_pm.expired?.should == false
75
75
  end
76
76
  end
77
-
77
+
78
78
  describe "inspect" do
79
79
  it "includes the token first" do
80
80
  output = Braintree::CreditCard._new(:token => "cc123").inspect
@@ -108,7 +108,7 @@ describe Braintree::CreditCard do
108
108
  output.should include(%Q(created_at: #{credit_card.created_at.inspect}))
109
109
  end
110
110
  end
111
-
111
+
112
112
  describe "masked_number" do
113
113
  it "uses the bin and last_4 to build the masked number" do
114
114
  credit_card = Braintree::CreditCard._new(
@@ -118,7 +118,7 @@ describe Braintree::CreditCard do
118
118
  credit_card.masked_number.should == "510510******5100"
119
119
  end
120
120
  end
121
-
121
+
122
122
  describe "self.update" do
123
123
  it "raises an exception if attributes contain an invalid key" do
124
124
  expect do
@@ -126,7 +126,7 @@ describe Braintree::CreditCard do
126
126
  end.to raise_error(ArgumentError, "invalid keys: invalid_key")
127
127
  end
128
128
  end
129
-
129
+
130
130
  describe "self.new" do
131
131
  it "is protected" do
132
132
  expect do
@@ -8,9 +8,11 @@ describe Braintree::CreditCardVerification do
8
8
  :avs_error_response_code => "I",
9
9
  :avs_postal_code_response_code => "I",
10
10
  :avs_street_address_response_code => "I",
11
- :cvv_response_code => "I"
11
+ :cvv_response_code => "I",
12
+ :processor_response_code => "2000",
13
+ :processor_response_text => "Do Not Honor"
12
14
  )
13
- verification.inspect.should == %(#<Braintree::CreditCardVerification status: "verified", cvv_response_code: "I", avs_error_response_code: "I", avs_postal_code_response_code: "I", avs_street_address_response_code: "I">)
15
+ verification.inspect.should == %(#<Braintree::CreditCardVerification status: "verified", processor_response_code: "2000", processor_response_text: "Do Not Honor", cvv_response_code: "I", avs_error_response_code: "I", avs_postal_code_response_code: "I", avs_street_address_response_code: "I">)
14
16
  end
15
17
  end
16
18
  end
@@ -6,7 +6,7 @@ describe Braintree::Customer do
6
6
  output = Braintree::Customer._new({:first_name => 'Dan', :id => '1234'}).inspect
7
7
  output.should include("#<Braintree::Customer id: \"1234\",")
8
8
  end
9
-
9
+
10
10
  it "includes all customer attributes" do
11
11
  customer = Braintree::Customer._new(
12
12
  :company => "Company",
@@ -31,7 +31,7 @@ describe Braintree::Customer do
31
31
  output.should include(%Q(updated_at: #{customer.updated_at.inspect}))
32
32
  end
33
33
  end
34
-
34
+
35
35
  describe "self.create" do
36
36
  it "raises an exception if hash includes an invalid key" do
37
37
  expect do
@@ -39,7 +39,7 @@ describe Braintree::Customer do
39
39
  end.to raise_error(ArgumentError, "invalid keys: invalid_key")
40
40
  end
41
41
  end
42
-
42
+
43
43
  describe "self.update" do
44
44
  it "raises an exception if hash includes an invalid key" do
45
45
  expect do
@@ -60,19 +60,19 @@ describe Braintree::Customer do
60
60
  it "returns true when given a customer with the same id" do
61
61
  first = Braintree::Customer._new(:id => 123)
62
62
  second = Braintree::Customer._new(:id => 123)
63
-
63
+
64
64
  first.should == second
65
65
  second.should == first
66
66
  end
67
-
67
+
68
68
  it "returns false when given a customer with a different id" do
69
69
  first = Braintree::Customer._new(:id => 123)
70
70
  second = Braintree::Customer._new(:id => 124)
71
-
71
+
72
72
  first.should_not == second
73
73
  second.should_not == first
74
74
  end
75
-
75
+
76
76
  it "returns false when not given a customer" do
77
77
  customer = Braintree::Customer._new(:id => 123)
78
78
  customer.should_not == "not a customer"
@@ -92,7 +92,7 @@ describe Braintree::Customer do
92
92
  customer.credit_cards[1].token.should == "pm2"
93
93
  end
94
94
  end
95
-
95
+
96
96
  describe "new" do
97
97
  it "is protected" do
98
98
  expect do
@@ -23,6 +23,10 @@ describe Braintree::Digest do
23
23
  Braintree::Configuration.private_key = original_key
24
24
  end
25
25
  end
26
+
27
+ it "doesn't blow up if message is nil" do
28
+ expect { Braintree::Digest.hexdigest(nil) }.to_not raise_error
29
+ end
26
30
  end
27
31
  end
28
32
 
@@ -17,7 +17,7 @@ END
17
17
  END
18
18
  Braintree::Http._format_and_sanitize_body_for_log(input_xml).should == expected_xml
19
19
  end
20
-
20
+
21
21
  it "sanitizes credit card number and cvv" do
22
22
  input_xml = <<-END
23
23
  <customer>
@@ -27,7 +27,7 @@ END
27
27
  <cvv>123</cvv>
28
28
  </customer>
29
29
  END
30
-
30
+
31
31
  expected_xml = <<-END
32
32
  [Braintree] <customer>
33
33
  [Braintree] <first-name>Joe</first-name>
@@ -14,7 +14,7 @@ describe "Braintree::PagedCollection" do
14
14
  collection[3].should == nil
15
15
  end
16
16
  end
17
-
17
+
18
18
  describe "each" do
19
19
  it "iterates over the contents" do
20
20
  expected = ["apples", "bananas", "cherries"]
@@ -28,7 +28,7 @@ describe "Braintree::PagedCollection" do
28
28
  actual.should == expected
29
29
  end
30
30
  end
31
-
31
+
32
32
  describe "first" do
33
33
  it "returns the first element" do
34
34
  collection = Braintree::PagedCollection.new(
@@ -37,7 +37,7 @@ describe "Braintree::PagedCollection" do
37
37
  collection.first.should == "apples"
38
38
  end
39
39
  end
40
-
40
+
41
41
  describe "initialize" do
42
42
  it "initializes attributes as expected" do
43
43
  collection = Braintree::PagedCollection.new(
@@ -64,7 +64,7 @@ describe "Braintree::PagedCollection" do
64
64
  collection.last_page?.should == false
65
65
  end
66
66
  end
67
-
67
+
68
68
  describe "next_page" do
69
69
  it "returns the next page of results" do
70
70
  collection = Braintree::PagedCollection.new(
@@ -73,20 +73,20 @@ describe "Braintree::PagedCollection" do
73
73
  :total_items => 2
74
74
  ) do |page_num|
75
75
  "contents of page #{page_num}"
76
- end
76
+ end
77
77
  collection.next_page.should == "contents of page 2"
78
78
  end
79
-
79
+
80
80
  it "returns nil if on last page" do
81
81
  collection = Braintree::PagedCollection.new(
82
82
  :current_page_number => 2,
83
83
  :page_size => 2,
84
84
  :total_items => 4
85
- )
85
+ )
86
86
  collection.next_page.should == nil
87
87
  end
88
88
  end
89
-
89
+
90
90
  describe "next_page_number" do
91
91
  it "returns the next page number when not on the last page" do
92
92
  collection = Braintree::PagedCollection.new(
@@ -96,17 +96,17 @@ describe "Braintree::PagedCollection" do
96
96
  )
97
97
  collection.next_page_number.should == 3
98
98
  end
99
-
99
+
100
100
  it "returns nil when on the last page" do
101
101
  collection = Braintree::PagedCollection.new(
102
102
  :current_page_number => 1,
103
103
  :page_size => 1,
104
104
  :total_items => 1
105
105
  )
106
- collection.next_page_number.should == nil
106
+ collection.next_page_number.should == nil
107
107
  end
108
108
  end
109
-
109
+
110
110
  describe "total_pages" do
111
111
  it "calculates the total number of pages when total items is not evenly divisible by page size" do
112
112
  collection = Braintree::PagedCollection.new(
@@ -124,5 +124,5 @@ describe "Braintree::PagedCollection" do
124
124
  collection.total_pages.should == 4
125
125
  end
126
126
  end
127
-
127
+
128
128
  end