braintree 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -57,6 +57,12 @@ Example of using bang method:
57
57
  We recommend using the bang methods when you assume that the data is valid and do not expect validations to fail.
58
58
  Otherwise, we recommend using the non-bang methods.
59
59
 
60
+ == Tests
61
+
62
+ The unit specs can be run by anyone on any system, but the integration specs are meant to be run against a local development
63
+ server of our gateway code. These integration specs are not meant for public consumption and will likely fail if run on
64
+ your system.
65
+
60
66
  == License
61
67
 
62
68
  See the LICENSE file.
data/lib/braintree.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'bigdecimal'
2
2
  require "cgi"
3
+ require "date"
3
4
  require "digest/sha1"
4
5
  require "logger"
5
6
  require "net/http"
@@ -18,15 +18,15 @@ module Braintree
18
18
  end
19
19
 
20
20
  def self.production_expiration_date # :nodoc:
21
- Date.new(2012, 1, 8)
21
+ Date.civil(2012, 1, 8)
22
22
  end
23
23
 
24
24
  def self.sandbox_expiration_date # :nodoc:
25
- Date.new(2010, 12, 1)
25
+ Date.civil(2010, 12, 1)
26
26
  end
27
27
 
28
28
  def self.qa_expiration_date # :nodoc:
29
- Date.new(2010, 12, 1)
29
+ Date.civil(2010, 12, 1)
30
30
  end
31
31
  end
32
32
  end
@@ -94,9 +94,7 @@ module Braintree
94
94
 
95
95
  def _init(attributes) # :nodoc:
96
96
  set_instance_variables_from_hash(attributes)
97
- if self.price
98
- instance_variable_set :@price, BigDecimal.new(self.price)
99
- end
97
+ @price = Util.to_big_decimal(price)
100
98
  end
101
99
 
102
100
  def self._update_signature # :nodoc:
@@ -27,7 +27,7 @@ module Braintree
27
27
 
28
28
  def inspect # :nodoc:
29
29
  inspected_attributes = @attrs.map { |attr| "#{attr}:#{send(attr).inspect}" }
30
- "#<#{self.class} #{inspected_attributes}>"
30
+ "#<#{self.class} #{inspected_attributes.join(" ")}>"
31
31
  end
32
32
 
33
33
  # Always returns true.
@@ -6,23 +6,5 @@ module Braintree
6
6
  Authorize = "1000.00"
7
7
  Decline = "2000.00"
8
8
  end
9
-
10
- module Plans
11
- TrialPlan = {
12
- :description => "Plan for integration tests -- with trial",
13
- :id => "integration_trial_plan",
14
- :price => "43.21",
15
- :trial_period => true,
16
- :trial_duration => 2,
17
- :trial_duration_unit => Subscription::TrialDurationUnit::Day
18
- }
19
-
20
- TriallessPlan = {
21
- :description => "Plan for integration tests -- without a trial",
22
- :id => "integration_trialless_plan",
23
- :price => "12.34",
24
- :trial_period => false
25
- }
26
- end
27
9
  end
28
10
  end
@@ -392,9 +392,7 @@ module Braintree
392
392
 
393
393
  def _init(attributes) # :nodoc:
394
394
  set_instance_variables_from_hash(attributes)
395
- if self.amount
396
- instance_variable_set :@amount, BigDecimal.new(self.amount)
397
- end
395
+ @amount = Util.to_big_decimal(amount)
398
396
  @credit_card_details = CreditCardDetails.new(@credit_card)
399
397
  @customer_details = CustomerDetails.new(@customer)
400
398
  @billing_details = AddressDetails.new(@billing)
@@ -29,16 +29,15 @@ module Braintree
29
29
  end
30
30
 
31
31
  def self.symbolize_keys(hash)
32
- hash.each do |key, value|
33
- hash.delete(key)
34
- hash[key.to_sym] = value
32
+ hash.inject({}) do |new_hash, (key, value)|
35
33
  if value.is_a?(Hash)
36
- symbolize_keys(value)
34
+ value = symbolize_keys(value)
37
35
  elsif value.is_a?(Array) && value.all? { |v| v.is_a?(Hash) }
38
- value.each { |v| symbolize_keys(v) }
36
+ value = value.map { |v| symbolize_keys(v) }
39
37
  end
38
+
39
+ new_hash.merge(key.to_sym => value)
40
40
  end
41
- hash
42
41
  end
43
42
 
44
43
  def self.raise_exception_for_status_code(status_code)
@@ -58,6 +57,17 @@ module Braintree
58
57
  end
59
58
  end
60
59
 
60
+ def self.to_big_decimal(decimal)
61
+ case decimal
62
+ when BigDecimal, NilClass
63
+ decimal
64
+ when String
65
+ BigDecimal.new(decimal)
66
+ else
67
+ raise ArgumentError, "Argument must be a String or BigDecimal"
68
+ end
69
+ end
70
+
61
71
  def self.verify_keys(valid_keys, hash)
62
72
  flattened_valid_keys = _flatten_valid_keys(valid_keys)
63
73
  invalid_keys = _flatten_hash_keys(hash) - flattened_valid_keys
@@ -2,7 +2,7 @@ module Braintree
2
2
  module Version
3
3
  Major = 1
4
4
  Minor = 1
5
- Tiny = 0
5
+ Tiny = 1
6
6
 
7
7
  String = "#{Major}.#{Minor}.#{Tiny}"
8
8
  end
@@ -1,6 +1,23 @@
1
1
  require File.dirname(__FILE__) + "/../spec_helper"
2
2
 
3
3
  describe Braintree::Subscription do
4
+
5
+ TrialPlan = {
6
+ :description => "Plan for integration tests -- with trial",
7
+ :id => "integration_trial_plan",
8
+ :price => BigDecimal.new("43.21"),
9
+ :trial_period => true,
10
+ :trial_duration => 2,
11
+ :trial_duration_unit => Braintree::Subscription::TrialDurationUnit::Day
12
+ }
13
+
14
+ TriallessPlan = {
15
+ :description => "Plan for integration tests -- without a trial",
16
+ :id => "integration_trialless_plan",
17
+ :price => BigDecimal.new("12.34"),
18
+ :trial_period => false
19
+ }
20
+
4
21
  before(:each) do
5
22
  @credit_card = Braintree::Customer.create!(
6
23
  :credit_card => {
@@ -14,7 +31,7 @@ describe Braintree::Subscription do
14
31
  it "is successful with a miniumum of params" do
15
32
  result = Braintree::Subscription.create(
16
33
  :payment_method_token => @credit_card.token,
17
- :plan_id => Braintree::Test::Plans::TriallessPlan[:id]
34
+ :plan_id => TriallessPlan[:id]
18
35
  )
19
36
 
20
37
  date_format = /^\d{4}\D\d{1,2}\D\d{1,2}$/
@@ -36,7 +53,7 @@ describe Braintree::Subscription do
36
53
  new_id = rand(36**9).to_s(36)
37
54
  result = Braintree::Subscription.create(
38
55
  :payment_method_token => @credit_card.token,
39
- :plan_id => Braintree::Test::Plans::TriallessPlan[:id],
56
+ :plan_id => TriallessPlan[:id],
40
57
  :id => new_id
41
58
  )
42
59
 
@@ -50,7 +67,7 @@ describe Braintree::Subscription do
50
67
  it "with no trial" do
51
68
  result = Braintree::Subscription.create(
52
69
  :payment_method_token => @credit_card.token,
53
- :plan_id => Braintree::Test::Plans::TriallessPlan[:id]
70
+ :plan_id => TriallessPlan[:id]
54
71
  )
55
72
 
56
73
  result.subscription.trial_period.should == false
@@ -61,7 +78,7 @@ describe Braintree::Subscription do
61
78
  it "with a trial" do
62
79
  result = Braintree::Subscription.create(
63
80
  :payment_method_token => @credit_card.token,
64
- :plan_id => Braintree::Test::Plans::TrialPlan[:id]
81
+ :plan_id => TrialPlan[:id]
65
82
  )
66
83
 
67
84
  result.subscription.trial_period.should == true
@@ -72,7 +89,7 @@ describe Braintree::Subscription do
72
89
  it "can alter the trial period params" do
73
90
  result = Braintree::Subscription.create(
74
91
  :payment_method_token => @credit_card.token,
75
- :plan_id => Braintree::Test::Plans::TrialPlan[:id],
92
+ :plan_id => TrialPlan[:id],
76
93
  :trial_duration => 5,
77
94
  :trial_duration_unit => Braintree::Subscription::TrialDurationUnit::Month
78
95
  )
@@ -85,7 +102,7 @@ describe Braintree::Subscription do
85
102
  it "can override the trial_period param" do
86
103
  result = Braintree::Subscription.create(
87
104
  :payment_method_token => @credit_card.token,
88
- :plan_id => Braintree::Test::Plans::TrialPlan[:id],
105
+ :plan_id => TrialPlan[:id],
89
106
  :trial_period => false
90
107
  )
91
108
 
@@ -95,19 +112,19 @@ describe Braintree::Subscription do
95
112
  it "creates a transaction if no trial period" do
96
113
  result = Braintree::Subscription.create(
97
114
  :payment_method_token => @credit_card.token,
98
- :plan_id => Braintree::Test::Plans::TriallessPlan[:id]
115
+ :plan_id => TriallessPlan[:id]
99
116
  )
100
117
 
101
118
  result.subscription.transactions.size.should == 1
102
119
  result.subscription.transactions.first.should be_a(Braintree::Transaction)
103
- result.subscription.transactions.first.amount.should == BigDecimal.new("12.34")
120
+ result.subscription.transactions.first.amount.should == TriallessPlan[:price]
104
121
  result.subscription.transactions.first.type.should == Braintree::Transaction::Type::Sale
105
122
  end
106
123
 
107
124
  it "doesn't create a transaction if there's a trial period" do
108
125
  result = Braintree::Subscription.create(
109
126
  :payment_method_token => @credit_card.token,
110
- :plan_id => Braintree::Test::Plans::TrialPlan[:id]
127
+ :plan_id => TrialPlan[:id]
111
128
  )
112
129
 
113
130
  result.subscription.transactions.size.should == 0
@@ -118,16 +135,16 @@ describe Braintree::Subscription do
118
135
  it "defaults to the plan's price" do
119
136
  result = Braintree::Subscription.create(
120
137
  :payment_method_token => @credit_card.token,
121
- :plan_id => Braintree::Test::Plans::TrialPlan[:id]
138
+ :plan_id => TrialPlan[:id]
122
139
  )
123
140
 
124
- result.subscription.price.should == BigDecimal.new("43.21")
141
+ result.subscription.price.should == TrialPlan[:price]
125
142
  end
126
143
 
127
144
  it "can be overridden" do
128
145
  result = Braintree::Subscription.create(
129
146
  :payment_method_token => @credit_card.token,
130
- :plan_id => Braintree::Test::Plans::TrialPlan[:id],
147
+ :plan_id => TrialPlan[:id],
131
148
  :price => 98.76
132
149
  )
133
150
 
@@ -140,7 +157,7 @@ describe Braintree::Subscription do
140
157
  it "has validation errors on id" do
141
158
  result = Braintree::Subscription.create(
142
159
  :payment_method_token => @credit_card.token,
143
- :plan_id => Braintree::Test::Plans::TrialPlan[:id],
160
+ :plan_id => TrialPlan[:id],
144
161
  :id => "invalid token"
145
162
  )
146
163
  result.success?.should == false
@@ -151,14 +168,14 @@ describe Braintree::Subscription do
151
168
  duplicate_token = "duplicate_token_#{rand(36**8).to_s(36)}"
152
169
  result = Braintree::Subscription.create(
153
170
  :payment_method_token => @credit_card.token,
154
- :plan_id => Braintree::Test::Plans::TrialPlan[:id],
171
+ :plan_id => TrialPlan[:id],
155
172
  :id => duplicate_token
156
173
  )
157
174
  result.success?.should == true
158
175
 
159
176
  result = Braintree::Subscription.create(
160
177
  :payment_method_token => @credit_card.token,
161
- :plan_id => Braintree::Test::Plans::TrialPlan[:id],
178
+ :plan_id => TrialPlan[:id],
162
179
  :id => duplicate_token
163
180
  )
164
181
  result.success?.should == false
@@ -168,7 +185,7 @@ describe Braintree::Subscription do
168
185
  it "trial duration required" do
169
186
  result = Braintree::Subscription.create(
170
187
  :payment_method_token => @credit_card.token,
171
- :plan_id => Braintree::Test::Plans::TrialPlan[:id],
188
+ :plan_id => TrialPlan[:id],
172
189
  :trial_period => true,
173
190
  :trial_duration => nil
174
191
  )
@@ -179,7 +196,7 @@ describe Braintree::Subscription do
179
196
  it "trial duration unit required" do
180
197
  result = Braintree::Subscription.create(
181
198
  :payment_method_token => @credit_card.token,
182
- :plan_id => Braintree::Test::Plans::TrialPlan[:id],
199
+ :plan_id => TrialPlan[:id],
183
200
  :trial_period => true,
184
201
  :trial_duration_unit => nil
185
202
  )
@@ -194,7 +211,7 @@ describe Braintree::Subscription do
194
211
  it "finds a subscription" do
195
212
  result = Braintree::Subscription.create(
196
213
  :payment_method_token => @credit_card.token,
197
- :plan_id => Braintree::Test::Plans::TriallessPlan[:id]
214
+ :plan_id => TriallessPlan[:id]
198
215
  )
199
216
  result.success?.should == true
200
217
 
@@ -213,7 +230,7 @@ describe Braintree::Subscription do
213
230
  @subscription = Braintree::Subscription.create(
214
231
  :payment_method_token => @credit_card.token,
215
232
  :price => 54.32,
216
- :plan_id => Braintree::Test::Plans::TriallessPlan[:id]
233
+ :plan_id => TriallessPlan[:id]
217
234
  ).subscription
218
235
  end
219
236
 
@@ -223,12 +240,12 @@ describe Braintree::Subscription do
223
240
  result = Braintree::Subscription.update(@subscription.id,
224
241
  :id => new_id,
225
242
  :price => 9999.88,
226
- :plan_id => Braintree::Test::Plans::TrialPlan[:id]
243
+ :plan_id => TrialPlan[:id]
227
244
  )
228
245
 
229
246
  result.success?.should == true
230
247
  result.subscription.id.should =~ /#{new_id}/
231
- result.subscription.plan_id.should == Braintree::Test::Plans::TrialPlan[:id]
248
+ result.subscription.plan_id.should == TrialPlan[:id]
232
249
  result.subscription.price.should == BigDecimal.new("9999.88")
233
250
  end
234
251
 
@@ -239,8 +256,7 @@ describe Braintree::Subscription do
239
256
 
240
257
  result.success?.should == true
241
258
  result.subscription.price.to_f.should == @subscription.price.to_f + 1
242
- result.subscription.transactions.size.should ==
243
- @subscription.transactions.size + 1
259
+ result.subscription.transactions.size.should == @subscription.transactions.size + 1
244
260
  end
245
261
 
246
262
  it "doesn't prorate if price decreases" do
@@ -250,8 +266,7 @@ describe Braintree::Subscription do
250
266
 
251
267
  result.success?.should == true
252
268
  result.subscription.price.to_f.should == @subscription.price.to_f - 1
253
- result.subscription.transactions.size.should ==
254
- @subscription.transactions.size
269
+ result.subscription.transactions.size.should == @subscription.transactions.size
255
270
  end
256
271
  end
257
272
 
@@ -259,7 +274,7 @@ describe Braintree::Subscription do
259
274
  before(:each) do
260
275
  @subscription = Braintree::Subscription.create(
261
276
  :payment_method_token => @credit_card.token,
262
- :plan_id => Braintree::Test::Plans::TrialPlan[:id]
277
+ :plan_id => TrialPlan[:id]
263
278
  ).subscription
264
279
  end
265
280
 
@@ -293,7 +308,7 @@ describe Braintree::Subscription do
293
308
  duplicate_id = "new_id_#{rand(36**6).to_s(36)}"
294
309
  duplicate = Braintree::Subscription.create(
295
310
  :payment_method_token => @credit_card.token,
296
- :plan_id => Braintree::Test::Plans::TrialPlan[:id],
311
+ :plan_id => TrialPlan[:id],
297
312
  :id => duplicate_id
298
313
  )
299
314
  result = Braintree::Subscription.update(
@@ -308,7 +323,7 @@ describe Braintree::Subscription do
308
323
  subscription = Braintree::Subscription.create(
309
324
  :payment_method_token => @credit_card.token,
310
325
  :price => 54.32,
311
- :plan_id => Braintree::Test::Plans::TriallessPlan[:id]
326
+ :plan_id => TriallessPlan[:id]
312
327
  ).subscription
313
328
 
314
329
  result = Braintree::Subscription.cancel(subscription.id)
@@ -329,7 +344,7 @@ describe Braintree::Subscription do
329
344
  subscription = Braintree::Subscription.create(
330
345
  :payment_method_token => @credit_card.token,
331
346
  :price => 54.32,
332
- :plan_id => Braintree::Test::Plans::TriallessPlan[:id]
347
+ :plan_id => TriallessPlan[:id]
333
348
  ).subscription
334
349
 
335
350
  result = Braintree::Subscription.cancel(subscription.id)
@@ -347,7 +362,7 @@ describe Braintree::Subscription do
347
362
  subscription = Braintree::Subscription.create(
348
363
  :payment_method_token => @credit_card.token,
349
364
  :price => 54.32,
350
- :plan_id => Braintree::Test::Plans::TriallessPlan[:id]
365
+ :plan_id => TriallessPlan[:id]
351
366
  ).subscription
352
367
 
353
368
  result = Braintree::Subscription.cancel(subscription.id)
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Braintree::Subscription do
4
+ context "price" do
5
+ it "accepts price as either a String or a BigDecimal" do
6
+ Braintree::Subscription.new(:price => "12.34", :transactions => []).price.should == BigDecimal.new("12.34")
7
+ Braintree::Subscription.new(:price => BigDecimal.new("12.34"), :transactions => []).price.should == BigDecimal.new("12.34")
8
+ end
9
+
10
+ it "blows up if price is not a string or BigDecimal" do
11
+ expect {
12
+ Braintree::Subscription.new(:price => 12.34, :transactions => [])
13
+ }.to raise_error(/Argument must be a String or BigDecimal/)
14
+ end
15
+ end
16
+ end
@@ -99,6 +99,17 @@ describe Braintree::Transaction do
99
99
  :custom => "\n "
100
100
  )
101
101
  end
102
+
103
+ it "accepts amount as either a String or a BigDecimal" do
104
+ Braintree::Transaction._new(:amount => "12.34").amount.should == BigDecimal.new("12.34")
105
+ Braintree::Transaction._new(:amount => BigDecimal.new("12.34")).amount.should == BigDecimal.new("12.34")
106
+ end
107
+
108
+ it "blows up if amount is not a string or BigDecimal" do
109
+ expect {
110
+ Braintree::Transaction._new(:amount => 12.34)
111
+ }.to raise_error(/Argument must be a String or BigDecimal/)
112
+ end
102
113
  end
103
114
 
104
115
  describe "inspect" do
@@ -1,6 +1,26 @@
1
1
  require File.dirname(__FILE__) + "/../spec_helper"
2
2
 
3
3
  describe Braintree::Util do
4
+ describe "self.symbolize_keys" do
5
+ it "does not modify the hash" do
6
+ original = {"a" => "b", "c" => "d"}
7
+ new = Braintree::Util.symbolize_keys(original)
8
+
9
+ original["a"].should == "b"
10
+ new[:a].should == "b"
11
+ end
12
+
13
+ it "symbolizes nested keys" do
14
+ hash = {"a" => {"b" => {"c" => "d" }}}
15
+ Braintree::Util.symbolize_keys(hash).should == {:a => {:b => {:c => "d"}}}
16
+ end
17
+
18
+ it "symbolizes nested keys in arrays" do
19
+ hash = {"a" => ["b" => {"c" => "d" }]}
20
+ Braintree::Util.symbolize_keys(hash).should == {:a => [:b => {:c => "d"}]}
21
+ end
22
+ end
23
+
4
24
  describe "self.verify_keys" do
5
25
  it "raises an exception if the hash contains an invalid key" do
6
26
  expect do
@@ -170,6 +190,25 @@ describe Braintree::Util do
170
190
  end
171
191
  end
172
192
 
193
+ describe "self.to_big_decimal" do
194
+ it "returns the BigDecimal when given a BigDecimal" do
195
+ Braintree::Util.to_big_decimal(BigDecimal.new("12.34")).should == BigDecimal.new("12.34")
196
+ end
197
+
198
+ it "returns a BigDecimal when given a string" do
199
+ Braintree::Util.to_big_decimal("12.34").should == BigDecimal.new("12.34")
200
+ end
201
+
202
+ it "returns nil when given nil" do
203
+ Braintree::Util.to_big_decimal(nil).should be_nil
204
+ end
205
+
206
+ it "blows up when not given a String or BigDecimal" do
207
+ expect {
208
+ Braintree::Util.to_big_decimal(12.34)
209
+ }.to raise_error(/Argument must be a String or BigDecimal/)
210
+ end
211
+ end
173
212
 
174
213
  describe "self.url_encode" do
175
214
  it "url encodes the given text" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: braintree
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Braintree Payment Solutions
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-25 00:00:00 -06:00
12
+ date: 2010-02-26 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -81,6 +81,7 @@ files:
81
81
  - spec/unit/braintree/http_spec.rb
82
82
  - spec/unit/braintree/paged_collection_spec.rb
83
83
  - spec/unit/braintree/ssl_expiration_check_spec.rb
84
+ - spec/unit/braintree/subscription_spec.rb
84
85
  - spec/unit/braintree/successful_result_spec.rb
85
86
  - spec/unit/braintree/transaction/credit_card_details_spec.rb
86
87
  - spec/unit/braintree/transaction/customer_details_spec.rb