fake_braintree 0.1.1 → 0.2.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.
- data/Changelog.md +6 -0
- data/lib/fake_braintree.rb +15 -6
- data/lib/fake_braintree/credit_card.rb +2 -2
- data/lib/fake_braintree/customer.rb +117 -77
- data/lib/fake_braintree/helpers.rb +2 -2
- data/lib/fake_braintree/redirect.rb +1 -1
- data/lib/fake_braintree/sinatra_app.rb +12 -6
- data/lib/fake_braintree/subscription.rb +66 -32
- data/lib/fake_braintree/version.rb +1 -1
- data/spec/fake_braintree/customer_spec.rb +9 -0
- data/spec/fake_braintree/redirect_spec.rb +0 -0
- data/spec/fake_braintree_spec.rb +9 -4
- data/spec/spec_helper.rb +7 -4
- metadata +30 -28
data/Changelog.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
# 0.2.0
|
|
2
|
+
* Generated transactions (from FakeBraintree.generate_transaction) now include
|
|
3
|
+
the amount.
|
|
4
|
+
* Braintree::Customer.update will reject updates that contain credit cards that
|
|
5
|
+
have been marked as a failure in the registry.
|
|
6
|
+
|
|
1
7
|
# 0.1.1
|
|
2
8
|
* Braintree::CreditCard.update now works
|
|
3
9
|
|
data/lib/fake_braintree.rb
CHANGED
|
@@ -18,12 +18,11 @@ module FakeBraintree
|
|
|
18
18
|
mattr_accessor :registry, :verify_all_cards, :decline_all_cards
|
|
19
19
|
|
|
20
20
|
def self.activate!
|
|
21
|
-
|
|
21
|
+
initialize_registry
|
|
22
22
|
self.verify_all_cards = false
|
|
23
|
-
|
|
24
23
|
set_configuration
|
|
25
24
|
clear!
|
|
26
|
-
|
|
25
|
+
boot_server
|
|
27
26
|
end
|
|
28
27
|
|
|
29
28
|
def self.log_file_path
|
|
@@ -60,12 +59,14 @@ module FakeBraintree
|
|
|
60
59
|
end
|
|
61
60
|
|
|
62
61
|
def self.create_failure
|
|
63
|
-
{
|
|
62
|
+
{
|
|
63
|
+
"message" => "Do Not Honor",
|
|
64
64
|
"verification" => { "status" => "processor_declined",
|
|
65
65
|
"processor_response_text" => "Do Not Honor",
|
|
66
66
|
"processor_response_code" => '2000' },
|
|
67
67
|
"errors" => { 'errors' => [] },
|
|
68
|
-
"params" => {}
|
|
68
|
+
"params" => {}
|
|
69
|
+
}
|
|
69
70
|
end
|
|
70
71
|
|
|
71
72
|
def self.decline_all_cards!
|
|
@@ -87,7 +88,8 @@ module FakeBraintree
|
|
|
87
88
|
created_at = options[:created_at] || Time.now
|
|
88
89
|
{'status_history' => [history_item],
|
|
89
90
|
'subscription_id' => options[:subscription_id],
|
|
90
|
-
'created_at' => created_at
|
|
91
|
+
'created_at' => created_at,
|
|
92
|
+
'amount' => options[:amount] }
|
|
91
93
|
end
|
|
92
94
|
|
|
93
95
|
private
|
|
@@ -99,6 +101,13 @@ module FakeBraintree
|
|
|
99
101
|
Braintree::Configuration.private_key = "xxx"
|
|
100
102
|
end
|
|
101
103
|
|
|
104
|
+
def self.boot_server
|
|
105
|
+
Server.new.boot
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def self.initialize_registry
|
|
109
|
+
self.registry = Registry.new
|
|
110
|
+
end
|
|
102
111
|
end
|
|
103
112
|
|
|
104
113
|
FakeBraintree.activate!
|
|
@@ -9,7 +9,7 @@ module FakeBraintree
|
|
|
9
9
|
|
|
10
10
|
def update
|
|
11
11
|
if credit_card_exists_in_registry?
|
|
12
|
-
|
|
12
|
+
update_existing_credit_card
|
|
13
13
|
response_for_updated_card
|
|
14
14
|
else
|
|
15
15
|
response_for_card_not_found
|
|
@@ -18,7 +18,7 @@ module FakeBraintree
|
|
|
18
18
|
|
|
19
19
|
private
|
|
20
20
|
|
|
21
|
-
def
|
|
21
|
+
def update_existing_credit_card
|
|
22
22
|
@hash = credit_card_from_registry.merge!(@hash)
|
|
23
23
|
end
|
|
24
24
|
|
|
@@ -2,54 +2,43 @@ module FakeBraintree
|
|
|
2
2
|
class Customer
|
|
3
3
|
include Helpers
|
|
4
4
|
|
|
5
|
-
def initialize(
|
|
5
|
+
def initialize(customer_hash_from_params, options)
|
|
6
6
|
@customer_hash = {
|
|
7
|
-
"id"
|
|
7
|
+
"id" => options[:id],
|
|
8
8
|
"merchant_id" => options[:merchant_id]
|
|
9
|
-
}.merge(
|
|
9
|
+
}.merge(customer_hash_from_params)
|
|
10
|
+
|
|
11
|
+
set_customer_id
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
def create
|
|
13
15
|
if invalid?
|
|
14
|
-
|
|
16
|
+
response_for_invalid_card
|
|
15
17
|
else
|
|
16
|
-
|
|
17
|
-
create_customer_with(
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
credit_cards = customer_hash["credit_cards"]
|
|
19
|
+
create_customer_with(customer_hash)
|
|
20
|
+
credit_cards.each { |card| add_credit_card_to_registry(card) }
|
|
21
|
+
response_for_created_customer(customer_hash)
|
|
20
22
|
end
|
|
21
23
|
end
|
|
22
24
|
|
|
23
25
|
def update
|
|
24
|
-
if
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
if customer_exists_in_registry?
|
|
27
|
+
if credit_card_is_failure?
|
|
28
|
+
response_for_invalid_card
|
|
29
|
+
else
|
|
30
|
+
updates = customer_hash
|
|
31
|
+
updated_customer = update_existing_customer(updates)
|
|
32
|
+
response_for_updated_customer(updated_customer)
|
|
33
|
+
end
|
|
27
34
|
else
|
|
28
|
-
|
|
35
|
+
response_for_customer_not_found
|
|
29
36
|
end
|
|
30
37
|
end
|
|
31
38
|
|
|
32
39
|
def delete
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def customer_hash
|
|
38
|
-
hash = @customer_hash.dup
|
|
39
|
-
hash["id"] ||= create_id
|
|
40
|
-
|
|
41
|
-
if hash["credit_card"] && hash["credit_card"].is_a?(Hash)
|
|
42
|
-
if !hash["credit_card"].empty?
|
|
43
|
-
hash["credit_card"]["last_4"] = last_four(hash)
|
|
44
|
-
hash["credit_card"]["token"] = credit_card_token(hash)
|
|
45
|
-
split_expiration_date_into_month_and_year!(hash)
|
|
46
|
-
|
|
47
|
-
credit_card = hash.delete("credit_card")
|
|
48
|
-
hash["credit_cards"] = [credit_card]
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
hash
|
|
40
|
+
delete_customer_with_id(customer_id)
|
|
41
|
+
deletion_response
|
|
53
42
|
end
|
|
54
43
|
|
|
55
44
|
private
|
|
@@ -58,89 +47,140 @@ module FakeBraintree
|
|
|
58
47
|
credit_card_is_failure? || invalid_credit_card?
|
|
59
48
|
end
|
|
60
49
|
|
|
61
|
-
def
|
|
62
|
-
|
|
63
|
-
hash["credit_card"]["expiration_month"] = expiration_date.split('/')[0]
|
|
64
|
-
hash["credit_card"]["expiration_year"] = expiration_date.split('/')[1]
|
|
65
|
-
end
|
|
50
|
+
def create_customer_with(hash)
|
|
51
|
+
FakeBraintree.registry.customers[hash["id"]] = hash
|
|
66
52
|
end
|
|
67
53
|
|
|
68
|
-
def
|
|
69
|
-
|
|
54
|
+
def add_credit_card_to_registry(new_credit_card_hash)
|
|
55
|
+
token = new_credit_card_hash["token"]
|
|
56
|
+
FakeBraintree.registry.credit_cards[token] = new_credit_card_hash
|
|
70
57
|
end
|
|
71
58
|
|
|
72
|
-
def update_existing_customer
|
|
73
|
-
|
|
59
|
+
def update_existing_customer(updates_hash)
|
|
60
|
+
customer_from_registry.merge!(updates_hash)
|
|
74
61
|
end
|
|
75
62
|
|
|
76
|
-
def
|
|
77
|
-
|
|
63
|
+
def customer_hash
|
|
64
|
+
@customer_hash.merge("credit_cards" => generate_credit_cards_from(@customer_hash["credit_card"]))
|
|
78
65
|
end
|
|
79
66
|
|
|
80
|
-
def
|
|
81
|
-
|
|
67
|
+
def customer_from_registry
|
|
68
|
+
FakeBraintree.registry.customers[customer_id]
|
|
82
69
|
end
|
|
83
70
|
|
|
84
|
-
def
|
|
85
|
-
|
|
71
|
+
def customer_exists_in_registry?
|
|
72
|
+
FakeBraintree.registry.customers.key?(customer_id)
|
|
86
73
|
end
|
|
87
74
|
|
|
88
75
|
def credit_card_is_failure?
|
|
89
|
-
|
|
90
|
-
FakeBraintree.failure?(@customer_hash["credit_card"]["number"])
|
|
76
|
+
has_credit_card? && FakeBraintree.failure?(credit_card_number)
|
|
91
77
|
end
|
|
92
78
|
|
|
93
79
|
def invalid_credit_card?
|
|
94
|
-
verify_credit_card?(
|
|
80
|
+
verify_credit_card?(customer_hash) && has_invalid_credit_card?(customer_hash)
|
|
95
81
|
end
|
|
96
82
|
|
|
97
|
-
def verify_credit_card?(
|
|
83
|
+
def verify_credit_card?(customer_hash_for_verification)
|
|
98
84
|
return true if FakeBraintree.verify_all_cards
|
|
99
85
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
86
|
+
credit_card_hash_for_verification = customer_hash_for_verification["credit_card"]
|
|
87
|
+
if credit_card_hash_for_verification.is_a?(Hash) &&
|
|
88
|
+
credit_card_hash_for_verification.key?("options")
|
|
89
|
+
options = credit_card_hash_for_verification["options"]
|
|
90
|
+
options["verify_card"] == true
|
|
91
|
+
end
|
|
105
92
|
end
|
|
106
93
|
|
|
107
94
|
def has_invalid_credit_card?(customer_hash)
|
|
108
|
-
|
|
109
|
-
! FakeBraintree::VALID_CREDIT_CARDS.include?(
|
|
95
|
+
credit_card_number &&
|
|
96
|
+
! FakeBraintree::VALID_CREDIT_CARDS.include?(credit_card_number)
|
|
110
97
|
end
|
|
111
98
|
|
|
112
|
-
def
|
|
113
|
-
|
|
114
|
-
@customer_hash["credit_card"].is_a?(Hash) &&
|
|
115
|
-
@customer_hash["credit_card"].key?("number")
|
|
99
|
+
def credit_card_number
|
|
100
|
+
credit_card_hash["number"]
|
|
116
101
|
end
|
|
117
102
|
|
|
118
|
-
def
|
|
119
|
-
|
|
103
|
+
def generate_credit_cards_from(new_credit_card_hash)
|
|
104
|
+
if new_credit_card_hash.present? && new_credit_card_hash.is_a?(Hash)
|
|
105
|
+
new_credit_card_hash["last_4"] = new_credit_card_hash["number"][-4..-1]
|
|
106
|
+
new_credit_card_hash["token"] = credit_card_token(new_credit_card_hash)
|
|
107
|
+
|
|
108
|
+
if credit_card_expiration_month
|
|
109
|
+
new_credit_card_hash["expiration_month"] = credit_card_expiration_month
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
if credit_card_expiration_year
|
|
113
|
+
new_credit_card_hash["expiration_year"] = credit_card_expiration_year
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
[new_credit_card_hash]
|
|
117
|
+
else
|
|
118
|
+
[]
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def credit_card_expiration_month
|
|
123
|
+
credit_card_expiration_date[0]
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def credit_card_expiration_year
|
|
127
|
+
credit_card_expiration_date[1]
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def credit_card_expiration_date
|
|
131
|
+
if credit_card_hash.key?("expiration_date")
|
|
132
|
+
credit_card_hash["expiration_date"].split('/')
|
|
133
|
+
else
|
|
134
|
+
[]
|
|
135
|
+
end
|
|
120
136
|
end
|
|
121
137
|
|
|
122
|
-
def
|
|
123
|
-
|
|
138
|
+
def delete_customer_with_id(id)
|
|
139
|
+
FakeBraintree.registry.customers[id] = nil
|
|
124
140
|
end
|
|
125
141
|
|
|
126
|
-
def
|
|
142
|
+
def deletion_response
|
|
143
|
+
gzipped_response(200, '')
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def response_for_created_customer(hash)
|
|
127
147
|
gzipped_response(201, hash.to_xml(:root => 'customer'))
|
|
128
148
|
end
|
|
129
149
|
|
|
130
|
-
def
|
|
131
|
-
|
|
150
|
+
def response_for_updated_customer(hash)
|
|
151
|
+
gzipped_response(200, hash.to_xml(:root => 'customer'))
|
|
132
152
|
end
|
|
133
153
|
|
|
134
|
-
def
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
154
|
+
def response_for_invalid_card
|
|
155
|
+
failure_response(422)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def response_for_customer_not_found
|
|
159
|
+
failure_response(404)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def failure_response(code)
|
|
163
|
+
gzipped_response(code, FakeBraintree.failure_response(credit_card_number).to_xml(:root => 'api_error_response'))
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def customer_id
|
|
167
|
+
customer_hash["id"]
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def has_credit_card?
|
|
171
|
+
credit_card_hash.present?
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def credit_card_hash
|
|
175
|
+
@customer_hash["credit_card"] || {}
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def set_customer_id
|
|
179
|
+
@customer_hash["id"] ||= create_id(@customer_hash["merchant_id"])
|
|
140
180
|
end
|
|
141
181
|
|
|
142
|
-
def
|
|
143
|
-
|
|
182
|
+
def credit_card_token(credit_card_hash_without_token)
|
|
183
|
+
md5("#{credit_card_hash_without_token["number"]}#{@customer_hash["merchant_id"]}")
|
|
144
184
|
end
|
|
145
185
|
end
|
|
146
186
|
end
|
|
@@ -9,9 +9,15 @@ module FakeBraintree
|
|
|
9
9
|
|
|
10
10
|
include Helpers
|
|
11
11
|
|
|
12
|
+
helpers do
|
|
13
|
+
def hash_from_request_body_with_key(request, key)
|
|
14
|
+
Hash.from_xml(request.body).delete(key)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
12
18
|
# Braintree::Customer.create
|
|
13
19
|
post "/merchants/:merchant_id/customers" do
|
|
14
|
-
customer_hash =
|
|
20
|
+
customer_hash = hash_from_request_body_with_key(request, "customer")
|
|
15
21
|
options = {:merchant_id => params[:merchant_id]}
|
|
16
22
|
Customer.new(customer_hash, options).create
|
|
17
23
|
end
|
|
@@ -28,7 +34,7 @@ module FakeBraintree
|
|
|
28
34
|
|
|
29
35
|
# Braintree::Customer.update
|
|
30
36
|
put "/merchants/:merchant_id/customers/:id" do
|
|
31
|
-
customer_hash =
|
|
37
|
+
customer_hash = hash_from_request_body_with_key(request, "customer")
|
|
32
38
|
options = {:id => params[:id], :merchant_id => params[:merchant_id]}
|
|
33
39
|
Customer.new(customer_hash, options).update
|
|
34
40
|
end
|
|
@@ -42,7 +48,7 @@ module FakeBraintree
|
|
|
42
48
|
|
|
43
49
|
# Braintree::Subscription.create
|
|
44
50
|
post "/merchants/:merchant_id/subscriptions" do
|
|
45
|
-
subscription_hash =
|
|
51
|
+
subscription_hash = hash_from_request_body_with_key(request, "subscription")
|
|
46
52
|
options = {:merchant_id => params[:merchant_id]}
|
|
47
53
|
Subscription.new(subscription_hash, options).create
|
|
48
54
|
end
|
|
@@ -59,7 +65,7 @@ module FakeBraintree
|
|
|
59
65
|
|
|
60
66
|
# Braintree::Subscription.update
|
|
61
67
|
put "/merchants/:merchant_id/subscriptions/:id" do
|
|
62
|
-
subscription_hash =
|
|
68
|
+
subscription_hash = hash_from_request_body_with_key(request, "subscription")
|
|
63
69
|
options = {:id => params[:id], :merchant_id => params[:merchant_id]}
|
|
64
70
|
Subscription.new(subscription_hash, options).update
|
|
65
71
|
end
|
|
@@ -80,7 +86,7 @@ module FakeBraintree
|
|
|
80
86
|
# Braintree::CreditCard.update
|
|
81
87
|
put "/merchants/:merchant_id/payment_methods/:credit_card_token" do
|
|
82
88
|
credit_card = FakeBraintree.registry.credit_cards[params[:credit_card_token]]
|
|
83
|
-
updates =
|
|
89
|
+
updates = hash_from_request_body_with_key(request, "credit_card")
|
|
84
90
|
options = {:token => params[:credit_card_token], :merchant_id => params[:merchant_id]}
|
|
85
91
|
CreditCard.new(updates, options).update
|
|
86
92
|
end
|
|
@@ -91,7 +97,7 @@ module FakeBraintree
|
|
|
91
97
|
if FakeBraintree.decline_all_cards?
|
|
92
98
|
gzipped_response(422, FakeBraintree.create_failure.to_xml(:root => 'api_error_response'))
|
|
93
99
|
else
|
|
94
|
-
transaction =
|
|
100
|
+
transaction = hash_from_request_body_with_key(request, "transaction")
|
|
95
101
|
transaction_id = md5("#{params[:merchant_id]}#{Time.now.to_f}")
|
|
96
102
|
transaction_response = {"id" => transaction_id, "amount" => transaction["amount"]}
|
|
97
103
|
FakeBraintree.registry.transactions[transaction_id] = transaction_response
|
|
@@ -2,61 +2,59 @@ module FakeBraintree
|
|
|
2
2
|
class Subscription
|
|
3
3
|
include Helpers
|
|
4
4
|
|
|
5
|
-
def initialize(
|
|
6
|
-
@subscription_hash =
|
|
7
|
-
|
|
5
|
+
def initialize(subscription_hash_from_params, options)
|
|
6
|
+
@subscription_hash = subscription_hash_from_params.merge("merchant_id" => options[:merchant_id],
|
|
7
|
+
"id" => options[:id])
|
|
8
|
+
set_subscription_id
|
|
9
|
+
set_subscription_status
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
def create
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
gzipped_response(201, hash.to_xml(:root => 'subscription'))
|
|
13
|
+
create_subscription_with(subscription_hash)
|
|
14
|
+
response_for_created_subscription(subscription_hash)
|
|
14
15
|
end
|
|
15
16
|
|
|
16
17
|
def update
|
|
17
|
-
if
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
if subscription_exists_in_registry?
|
|
19
|
+
updated_subscription = update_existing_subscription(subscription_hash)
|
|
20
|
+
response_for_created_subscription(updated_subscription)
|
|
20
21
|
else
|
|
21
|
-
|
|
22
|
+
response_for_subscription_not_found
|
|
22
23
|
end
|
|
23
24
|
end
|
|
24
25
|
|
|
26
|
+
private
|
|
27
|
+
|
|
25
28
|
def subscription_hash
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
subscription_hash["plan_id"] = @subscription_hash["plan_id"]
|
|
32
|
-
subscription_hash["next_billing_date"] = braintree_formatted_date(1.month.from_now)
|
|
33
|
-
subscription_hash["payment_method_token"] = @subscription_hash["payment_method_token"]
|
|
34
|
-
subscription_hash["status"] ||= Braintree::Subscription::Status::Active
|
|
29
|
+
@subscription_hash.merge({"transactions" => [],
|
|
30
|
+
"add_ons" => added_add_ons,
|
|
31
|
+
"discounts" => added_discounts,
|
|
32
|
+
"next_billing_date" => braintree_formatted_date(1.month.from_now)})
|
|
33
|
+
end
|
|
35
34
|
|
|
36
|
-
|
|
35
|
+
def update_existing_subscription(updates)
|
|
36
|
+
updated_subscription = subscription_from_registry.merge(updates)
|
|
37
|
+
FakeBraintree.registry.subscriptions[subscription_id] = updated_subscription
|
|
37
38
|
end
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
def create_subscription_with(new_subscription_hash)
|
|
41
|
+
FakeBraintree.registry.subscriptions[new_subscription_hash["id"]] = new_subscription_hash
|
|
42
|
+
end
|
|
40
43
|
|
|
41
|
-
def
|
|
42
|
-
|
|
44
|
+
def subscription_from_registry
|
|
45
|
+
FakeBraintree.registry.subscriptions[subscription_id]
|
|
43
46
|
end
|
|
44
47
|
|
|
45
|
-
def
|
|
46
|
-
|
|
47
|
-
FakeBraintree.registry.subscriptions[@subscription_hash['id']] = new_hash
|
|
48
|
+
def subscription_exists_in_registry?
|
|
49
|
+
FakeBraintree.registry.subscriptions.key?(subscription_id)
|
|
48
50
|
end
|
|
49
51
|
|
|
50
52
|
def braintree_formatted_date(date)
|
|
51
53
|
date.strftime('%Y-%m-%d')
|
|
52
54
|
end
|
|
53
55
|
|
|
54
|
-
def subscription_id
|
|
55
|
-
md5("#{@subscription_hash["payment_method_token"]}#{Time.now.to_f}")[0,6]
|
|
56
|
-
end
|
|
57
|
-
|
|
58
56
|
def added_add_ons
|
|
59
|
-
if @subscription_hash["add_ons"] && @subscription_hash["add_ons"]["add"]
|
|
57
|
+
if @subscription_hash["add_ons"].is_a?(Hash) && @subscription_hash["add_ons"]["add"]
|
|
60
58
|
@subscription_hash["add_ons"]["add"].map { |add_on| { "id" => add_on["inherited_from_id"] } }
|
|
61
59
|
else
|
|
62
60
|
[]
|
|
@@ -64,11 +62,47 @@ module FakeBraintree
|
|
|
64
62
|
end
|
|
65
63
|
|
|
66
64
|
def added_discounts
|
|
67
|
-
if @subscription_hash["discounts"] && @subscription_hash["discounts"]["add"]
|
|
65
|
+
if @subscription_hash["discounts"].is_a?(Hash) && @subscription_hash["discounts"]["add"]
|
|
68
66
|
@subscription_hash["discounts"]["add"].map { |discount| { "id" => discount["inherited_from_id"] } }
|
|
69
67
|
else
|
|
70
68
|
[]
|
|
71
69
|
end
|
|
72
70
|
end
|
|
71
|
+
|
|
72
|
+
def set_subscription_id
|
|
73
|
+
@subscription_hash["id"] ||= generate_new_subscription_id
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def set_subscription_status
|
|
77
|
+
@subscription_hash["status"] ||= active_status
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def subscription_id
|
|
81
|
+
subscription_hash["id"]
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def generate_new_subscription_id
|
|
85
|
+
md5("#{payment_method_token}#{Time.now.to_f}")[0,6]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def payment_method_token
|
|
89
|
+
@subscription_hash["payment_method_token"]
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def active_status
|
|
93
|
+
Braintree::Subscription::Status::Active
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def response_for_created_subscription(hash)
|
|
97
|
+
gzipped_response(201, hash.to_xml(:root => "subscription"))
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def response_for_subscription_not_found
|
|
101
|
+
gzipped_response(404, {})
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def response_for_created_subscription(hash)
|
|
105
|
+
gzipped_response(201, hash.to_xml(:root => "subscription"))
|
|
106
|
+
end
|
|
73
107
|
end
|
|
74
108
|
end
|
|
@@ -109,6 +109,15 @@ describe "Braintree::Customer.update" do
|
|
|
109
109
|
it "raises an error for a nonexistent customer" do
|
|
110
110
|
lambda { Braintree::Customer.update("foo", {:first_name => "Bob"}) }.should raise_error(Braintree::NotFoundError)
|
|
111
111
|
end
|
|
112
|
+
|
|
113
|
+
it "does not allow a customer to be updated to a failing credit card" do
|
|
114
|
+
bad_credit_card = "123456"
|
|
115
|
+
FakeBraintree.registry.failures[bad_credit_card] = FakeBraintree.failure_response
|
|
116
|
+
|
|
117
|
+
customer = create_customer
|
|
118
|
+
result = Braintree::Customer.update(customer.customer.id, {:credit_card => { :number => bad_credit_card }})
|
|
119
|
+
result.should_not be_success
|
|
120
|
+
end
|
|
112
121
|
end
|
|
113
122
|
|
|
114
123
|
describe "Braintree::Customer.delete" do
|
|
File without changes
|
data/spec/fake_braintree_spec.rb
CHANGED
|
@@ -89,7 +89,7 @@ describe FakeBraintree, ".failure_response" do
|
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
describe FakeBraintree, ".generate_transaction" do
|
|
92
|
-
it "
|
|
92
|
+
it "allows setting the subscription id" do
|
|
93
93
|
transaction = FakeBraintree.generate_transaction(:subscription_id => 'foobar')
|
|
94
94
|
transaction['subscription_id'].should == 'foobar'
|
|
95
95
|
end
|
|
@@ -107,6 +107,11 @@ describe FakeBraintree, ".generate_transaction" do
|
|
|
107
107
|
end
|
|
108
108
|
end
|
|
109
109
|
|
|
110
|
+
it "has the correct amount" do
|
|
111
|
+
transaction = FakeBraintree.generate_transaction(:amount => "20.00")
|
|
112
|
+
transaction['amount'].should == "20.00"
|
|
113
|
+
end
|
|
114
|
+
|
|
110
115
|
it "allows no arguments" do
|
|
111
116
|
expect { FakeBraintree.generate_transaction }.not_to raise_error
|
|
112
117
|
end
|
|
@@ -120,19 +125,19 @@ describe FakeBraintree, ".generate_transaction" do
|
|
|
120
125
|
Timecop.freeze do
|
|
121
126
|
transaction = FakeBraintree.generate_transaction(:status => Braintree::Transaction::Status::Failed,
|
|
122
127
|
:subscription_id => 'my_subscription_id')
|
|
123
|
-
transaction['status_history'][
|
|
128
|
+
transaction['status_history'].first['timestamp'].should == Time.now
|
|
124
129
|
end
|
|
125
130
|
end
|
|
126
131
|
|
|
127
132
|
it "has the desired amount" do
|
|
128
133
|
transaction = FakeBraintree.generate_transaction(:amount => '20.00')
|
|
129
|
-
transaction['status_history'][
|
|
134
|
+
transaction['status_history'].first['amount'].should == '20.00'
|
|
130
135
|
end
|
|
131
136
|
|
|
132
137
|
it "has the desired status" do
|
|
133
138
|
status = Braintree::Transaction::Status::Failed
|
|
134
139
|
transaction = FakeBraintree.generate_transaction(:status => status)
|
|
135
|
-
transaction['status_history'][
|
|
140
|
+
transaction['status_history'].first['status'].should == status
|
|
136
141
|
end
|
|
137
142
|
end
|
|
138
143
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
require 'spork'
|
|
2
2
|
|
|
3
3
|
Spork.prefork do
|
|
4
|
-
# Requires supporting ruby files with custom matchers and macros, etc,
|
|
5
|
-
# in spec/support/ and its subdirectories.
|
|
6
4
|
require 'rspec'
|
|
7
5
|
require 'fake_braintree'
|
|
8
6
|
require 'timecop'
|
|
9
7
|
require 'bourne'
|
|
8
|
+
|
|
9
|
+
def clear_braintree_log
|
|
10
|
+
Dir.mkdir('tmp') unless File.directory?('tmp')
|
|
11
|
+
File.new('tmp/braintree_log', 'w').close
|
|
12
|
+
end
|
|
13
|
+
|
|
10
14
|
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f}
|
|
11
15
|
|
|
12
|
-
|
|
13
|
-
File.new('tmp/braintree_log', 'w').close
|
|
16
|
+
clear_braintree_log
|
|
14
17
|
|
|
15
18
|
TEST_CC_NUMBER = %w(4111 1111 1111 1111).join
|
|
16
19
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fake_braintree
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,11 +9,11 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-
|
|
12
|
+
date: 2012-02-03 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: capybara
|
|
16
|
-
requirement: &
|
|
16
|
+
requirement: &70218697304620 !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
19
|
- - ! '>='
|
|
@@ -21,10 +21,10 @@ dependencies:
|
|
|
21
21
|
version: '0'
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
|
-
version_requirements: *
|
|
24
|
+
version_requirements: *70218697304620
|
|
25
25
|
- !ruby/object:Gem::Dependency
|
|
26
26
|
name: activesupport
|
|
27
|
-
requirement: &
|
|
27
|
+
requirement: &70218697303980 !ruby/object:Gem::Requirement
|
|
28
28
|
none: false
|
|
29
29
|
requirements:
|
|
30
30
|
- - ! '>='
|
|
@@ -32,10 +32,10 @@ dependencies:
|
|
|
32
32
|
version: '0'
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
|
-
version_requirements: *
|
|
35
|
+
version_requirements: *70218697303980
|
|
36
36
|
- !ruby/object:Gem::Dependency
|
|
37
37
|
name: i18n
|
|
38
|
-
requirement: &
|
|
38
|
+
requirement: &70218697303320 !ruby/object:Gem::Requirement
|
|
39
39
|
none: false
|
|
40
40
|
requirements:
|
|
41
41
|
- - ! '>='
|
|
@@ -43,10 +43,10 @@ dependencies:
|
|
|
43
43
|
version: '0'
|
|
44
44
|
type: :runtime
|
|
45
45
|
prerelease: false
|
|
46
|
-
version_requirements: *
|
|
46
|
+
version_requirements: *70218697303320
|
|
47
47
|
- !ruby/object:Gem::Dependency
|
|
48
48
|
name: sinatra
|
|
49
|
-
requirement: &
|
|
49
|
+
requirement: &70218697315200 !ruby/object:Gem::Requirement
|
|
50
50
|
none: false
|
|
51
51
|
requirements:
|
|
52
52
|
- - ! '>='
|
|
@@ -54,10 +54,10 @@ dependencies:
|
|
|
54
54
|
version: '0'
|
|
55
55
|
type: :runtime
|
|
56
56
|
prerelease: false
|
|
57
|
-
version_requirements: *
|
|
57
|
+
version_requirements: *70218697315200
|
|
58
58
|
- !ruby/object:Gem::Dependency
|
|
59
59
|
name: braintree
|
|
60
|
-
requirement: &
|
|
60
|
+
requirement: &70218697312960 !ruby/object:Gem::Requirement
|
|
61
61
|
none: false
|
|
62
62
|
requirements:
|
|
63
63
|
- - ~>
|
|
@@ -65,10 +65,10 @@ dependencies:
|
|
|
65
65
|
version: '2.5'
|
|
66
66
|
type: :runtime
|
|
67
67
|
prerelease: false
|
|
68
|
-
version_requirements: *
|
|
68
|
+
version_requirements: *70218697312960
|
|
69
69
|
- !ruby/object:Gem::Dependency
|
|
70
70
|
name: mongrel
|
|
71
|
-
requirement: &
|
|
71
|
+
requirement: &70218697327000 !ruby/object:Gem::Requirement
|
|
72
72
|
none: false
|
|
73
73
|
requirements:
|
|
74
74
|
- - ~>
|
|
@@ -76,10 +76,10 @@ dependencies:
|
|
|
76
76
|
version: 1.2.0.pre
|
|
77
77
|
type: :runtime
|
|
78
78
|
prerelease: false
|
|
79
|
-
version_requirements: *
|
|
79
|
+
version_requirements: *70218697327000
|
|
80
80
|
- !ruby/object:Gem::Dependency
|
|
81
81
|
name: rspec
|
|
82
|
-
requirement: &
|
|
82
|
+
requirement: &70218697325220 !ruby/object:Gem::Requirement
|
|
83
83
|
none: false
|
|
84
84
|
requirements:
|
|
85
85
|
- - ~>
|
|
@@ -87,10 +87,10 @@ dependencies:
|
|
|
87
87
|
version: 2.6.0
|
|
88
88
|
type: :development
|
|
89
89
|
prerelease: false
|
|
90
|
-
version_requirements: *
|
|
90
|
+
version_requirements: *70218697325220
|
|
91
91
|
- !ruby/object:Gem::Dependency
|
|
92
92
|
name: bourne
|
|
93
|
-
requirement: &
|
|
93
|
+
requirement: &70218697323900 !ruby/object:Gem::Requirement
|
|
94
94
|
none: false
|
|
95
95
|
requirements:
|
|
96
96
|
- - ~>
|
|
@@ -98,10 +98,10 @@ dependencies:
|
|
|
98
98
|
version: '1.0'
|
|
99
99
|
type: :development
|
|
100
100
|
prerelease: false
|
|
101
|
-
version_requirements: *
|
|
101
|
+
version_requirements: *70218697323900
|
|
102
102
|
- !ruby/object:Gem::Dependency
|
|
103
103
|
name: timecop
|
|
104
|
-
requirement: &
|
|
104
|
+
requirement: &70218697321420 !ruby/object:Gem::Requirement
|
|
105
105
|
none: false
|
|
106
106
|
requirements:
|
|
107
107
|
- - ~>
|
|
@@ -109,10 +109,10 @@ dependencies:
|
|
|
109
109
|
version: 0.3.5
|
|
110
110
|
type: :development
|
|
111
111
|
prerelease: false
|
|
112
|
-
version_requirements: *
|
|
112
|
+
version_requirements: *70218697321420
|
|
113
113
|
- !ruby/object:Gem::Dependency
|
|
114
114
|
name: spork
|
|
115
|
-
requirement: &
|
|
115
|
+
requirement: &70218697334620 !ruby/object:Gem::Requirement
|
|
116
116
|
none: false
|
|
117
117
|
requirements:
|
|
118
118
|
- - ~>
|
|
@@ -120,10 +120,10 @@ dependencies:
|
|
|
120
120
|
version: 0.9.0.rc9
|
|
121
121
|
type: :development
|
|
122
122
|
prerelease: false
|
|
123
|
-
version_requirements: *
|
|
123
|
+
version_requirements: *70218697334620
|
|
124
124
|
- !ruby/object:Gem::Dependency
|
|
125
125
|
name: bundler
|
|
126
|
-
requirement: &
|
|
126
|
+
requirement: &70218697332380 !ruby/object:Gem::Requirement
|
|
127
127
|
none: false
|
|
128
128
|
requirements:
|
|
129
129
|
- - ! '>='
|
|
@@ -131,10 +131,10 @@ dependencies:
|
|
|
131
131
|
version: 1.0.14
|
|
132
132
|
type: :development
|
|
133
133
|
prerelease: false
|
|
134
|
-
version_requirements: *
|
|
134
|
+
version_requirements: *70218697332380
|
|
135
135
|
- !ruby/object:Gem::Dependency
|
|
136
136
|
name: rake
|
|
137
|
-
requirement: &
|
|
137
|
+
requirement: &70218697328960 !ruby/object:Gem::Requirement
|
|
138
138
|
none: false
|
|
139
139
|
requirements:
|
|
140
140
|
- - ! '>='
|
|
@@ -142,7 +142,7 @@ dependencies:
|
|
|
142
142
|
version: '0'
|
|
143
143
|
type: :development
|
|
144
144
|
prerelease: false
|
|
145
|
-
version_requirements: *
|
|
145
|
+
version_requirements: *70218697328960
|
|
146
146
|
description: A fake Braintree that you can run integration tests against
|
|
147
147
|
email:
|
|
148
148
|
- gabe@thoughtbot.com
|
|
@@ -171,6 +171,7 @@ files:
|
|
|
171
171
|
- lib/fake_braintree/version.rb
|
|
172
172
|
- spec/fake_braintree/credit_card_spec.rb
|
|
173
173
|
- spec/fake_braintree/customer_spec.rb
|
|
174
|
+
- spec/fake_braintree/redirect_spec.rb
|
|
174
175
|
- spec/fake_braintree/registry_spec.rb
|
|
175
176
|
- spec/fake_braintree/subscription_spec.rb
|
|
176
177
|
- spec/fake_braintree/transaction_spec.rb
|
|
@@ -196,7 +197,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
196
197
|
version: '0'
|
|
197
198
|
segments:
|
|
198
199
|
- 0
|
|
199
|
-
hash:
|
|
200
|
+
hash: 1813003811579628559
|
|
200
201
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
201
202
|
none: false
|
|
202
203
|
requirements:
|
|
@@ -205,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
205
206
|
version: '0'
|
|
206
207
|
segments:
|
|
207
208
|
- 0
|
|
208
|
-
hash:
|
|
209
|
+
hash: 1813003811579628559
|
|
209
210
|
requirements: []
|
|
210
211
|
rubyforge_project:
|
|
211
212
|
rubygems_version: 1.8.11
|
|
@@ -215,6 +216,7 @@ summary: A fake Braintree that you can run integration tests against
|
|
|
215
216
|
test_files:
|
|
216
217
|
- spec/fake_braintree/credit_card_spec.rb
|
|
217
218
|
- spec/fake_braintree/customer_spec.rb
|
|
219
|
+
- spec/fake_braintree/redirect_spec.rb
|
|
218
220
|
- spec/fake_braintree/registry_spec.rb
|
|
219
221
|
- spec/fake_braintree/subscription_spec.rb
|
|
220
222
|
- spec/fake_braintree/transaction_spec.rb
|