fake_braintree 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -3
- data/.hound.yml +2 -0
- data/.javascript_ignore +2 -0
- data/.travis.yml +4 -1
- data/CONTRIBUTING.md +6 -2
- data/NEWS.md +16 -0
- data/README.md +41 -10
- data/Rakefile +56 -0
- data/asset_versions.yml +3 -0
- data/fake_braintree.gemspec +3 -2
- data/lib/fake_braintree.rb +17 -22
- data/lib/fake_braintree/address.rb +2 -0
- data/lib/fake_braintree/braintree_assets/dropin/1.7.0/braintree-dropin-internal.min.js +12 -0
- data/lib/fake_braintree/braintree_assets/dropin/1.7.0/braintree-dropin.css +4321 -0
- data/lib/fake_braintree/braintree_assets/dropin/1.7.0/images/2x-sf9a66b4f5a.png +0 -0
- data/lib/fake_braintree/braintree_assets/dropin/1.7.0/inline-frame.html +48 -0
- data/lib/fake_braintree/braintree_assets/dropin/1.7.0/modal-frame.html +25 -0
- data/lib/fake_braintree/braintree_assets/dropin/1.7.0/vendor/jquery-2.1.0.js +4 -0
- data/lib/fake_braintree/braintree_assets/dropin/1.7.0/vendor/modernizr.js +4 -0
- data/lib/fake_braintree/braintree_assets/dropin/1.7.0/vendor/normalize.css +339 -0
- data/lib/fake_braintree/client_token.rb +15 -0
- data/lib/fake_braintree/credit_card.rb +20 -0
- data/lib/fake_braintree/credit_card_serializer.rb +20 -0
- data/lib/fake_braintree/customer.rb +11 -3
- data/lib/fake_braintree/helpers.rb +3 -0
- data/lib/fake_braintree/merchant_account.rb +95 -0
- data/lib/fake_braintree/payment_method.rb +11 -0
- data/lib/fake_braintree/redirect.rb +6 -0
- data/lib/fake_braintree/registry.rb +10 -8
- data/lib/fake_braintree/server.rb +12 -4
- data/lib/fake_braintree/sinatra_app.rb +148 -14
- data/lib/fake_braintree/subscription.rb +48 -13
- data/lib/fake_braintree/transaction.rb +42 -0
- data/lib/fake_braintree/version.rb +1 -1
- data/spec/dummy/checkout_app.rb +44 -0
- data/spec/dummy/public/braintree.js +5 -0
- data/spec/dummy/public/jquery-2.1.0.js +4 -0
- data/spec/dummy/views/advanced_checkout.html.erb +36 -0
- data/spec/dummy/views/credit_cards.html.erb +16 -0
- data/spec/dummy/views/custom_checkout.html.erb +22 -0
- data/spec/dummy/views/dropin_checkout.html.erb +21 -0
- data/spec/fake_braintree/checkout_spec.rb +45 -0
- data/spec/fake_braintree/client_token_spec.rb +19 -0
- data/spec/fake_braintree/credit_card_spec.rb +15 -0
- data/spec/fake_braintree/merchant_account_spec.rb +83 -0
- data/spec/fake_braintree/payment_method_spec.rb +122 -0
- data/spec/fake_braintree/registry_spec.rb +4 -2
- data/spec/fake_braintree/subscription_spec.rb +51 -2
- data/spec/fake_braintree_spec.rb +34 -1
- data/spec/spec_helper.rb +11 -1
- data/spec/support/matchers/clear_hash_when_cleared.rb +1 -1
- data/spec/support/matchers/have_accessor_for.rb +1 -1
- data/spec/support/merchant_account_helpers.rb +24 -0
- data/spec/support/subscription_helpers.rb +4 -0
- metadata +60 -6
@@ -0,0 +1,20 @@
|
|
1
|
+
module FakeBraintree
|
2
|
+
class CreditCardSerializer
|
3
|
+
def initialize(credit_card)
|
4
|
+
@credit_card = credit_card
|
5
|
+
end
|
6
|
+
|
7
|
+
def to_h
|
8
|
+
last_2 = @credit_card.last_4[-2..-1]
|
9
|
+
card_type = @credit_card.card_type
|
10
|
+
{
|
11
|
+
type: 'CreditCard',
|
12
|
+
description: "ending in #{last_2}",
|
13
|
+
details: {
|
14
|
+
cardType: card_type,
|
15
|
+
lastTwo: last_2
|
16
|
+
}
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -1,3 +1,7 @@
|
|
1
|
+
require 'fake_braintree/helpers'
|
2
|
+
require 'fake_braintree/credit_card'
|
3
|
+
require 'fake_braintree/valid_credit_cards'
|
4
|
+
|
1
5
|
module FakeBraintree
|
2
6
|
class Customer
|
3
7
|
include Helpers
|
@@ -39,8 +43,12 @@ module FakeBraintree
|
|
39
43
|
end
|
40
44
|
|
41
45
|
def delete
|
42
|
-
|
43
|
-
|
46
|
+
if customer_exists_in_registry?
|
47
|
+
delete_customer_with_id(customer_id)
|
48
|
+
deletion_response
|
49
|
+
else
|
50
|
+
response_for_customer_not_found
|
51
|
+
end
|
44
52
|
end
|
45
53
|
|
46
54
|
private
|
@@ -145,7 +153,7 @@ module FakeBraintree
|
|
145
153
|
end
|
146
154
|
|
147
155
|
def delete_customer_with_id(id)
|
148
|
-
FakeBraintree.registry.customers
|
156
|
+
FakeBraintree.registry.customers.delete(id)
|
149
157
|
end
|
150
158
|
|
151
159
|
def deletion_response
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module FakeBraintree
|
2
|
+
class MerchantAccount
|
3
|
+
include Helpers
|
4
|
+
|
5
|
+
def initialize(merchant_account_hash_from_params, options)
|
6
|
+
@merchant_account_hash = {
|
7
|
+
'id' => options[:id]
|
8
|
+
}
|
9
|
+
@merchant_account_hash.merge!(merchant_account_hash_from_params)
|
10
|
+
set_merchant_account_id
|
11
|
+
end
|
12
|
+
|
13
|
+
def create
|
14
|
+
if invalid?
|
15
|
+
response_for_invalid_merchant_account
|
16
|
+
else
|
17
|
+
create_merchant_account_with(merchant_account_hash)
|
18
|
+
response_for_created_merchant_account(merchant_account_hash)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def update
|
23
|
+
if merchant_account_exists_in_registry?
|
24
|
+
updates = merchant_account_hash
|
25
|
+
updated_merchant_account = update_existing_merchant_account(updates)
|
26
|
+
response_for_updated_merchant_account(updated_merchant_account)
|
27
|
+
else
|
28
|
+
response_for_merchant_account_not_found
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete
|
33
|
+
delete_merchant_account_with_id(merchant_account_id)
|
34
|
+
deletion_response
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def invalid?
|
40
|
+
false
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_merchant_account_with(hash)
|
44
|
+
FakeBraintree.registry.merchant_accounts[hash['id'].to_s] = hash
|
45
|
+
end
|
46
|
+
|
47
|
+
def update_existing_merchant_account(updates_hash)
|
48
|
+
merchant_account_from_registry.merge!(updates_hash)
|
49
|
+
end
|
50
|
+
|
51
|
+
def merchant_account_hash
|
52
|
+
@merchant_account_hash
|
53
|
+
end
|
54
|
+
|
55
|
+
def merchant_account_from_registry
|
56
|
+
FakeBraintree.registry.merchant_accounts[merchant_account_id]
|
57
|
+
end
|
58
|
+
|
59
|
+
def merchant_account_exists_in_registry?
|
60
|
+
FakeBraintree.registry.merchant_accounts.key?(merchant_account_id)
|
61
|
+
end
|
62
|
+
|
63
|
+
def delete_merchant_account_with_id(id)
|
64
|
+
FakeBraintree.registry.merchant_accounts[id] = nil
|
65
|
+
end
|
66
|
+
|
67
|
+
def deletion_response
|
68
|
+
gzipped_response(200, '')
|
69
|
+
end
|
70
|
+
|
71
|
+
def response_for_created_merchant_account(hash)
|
72
|
+
gzipped_response(201, hash.to_xml(root: 'merchant_account'))
|
73
|
+
end
|
74
|
+
|
75
|
+
def response_for_updated_merchant_account(hash)
|
76
|
+
gzipped_response(200, hash.to_xml(root: 'merchant_account'))
|
77
|
+
end
|
78
|
+
|
79
|
+
def response_for_merchant_account_not_found
|
80
|
+
failure_response(404)
|
81
|
+
end
|
82
|
+
|
83
|
+
def failure_response(code)
|
84
|
+
gzipped_response(code, '')
|
85
|
+
end
|
86
|
+
|
87
|
+
def merchant_account_id
|
88
|
+
merchant_account_hash['id']
|
89
|
+
end
|
90
|
+
|
91
|
+
def set_merchant_account_id
|
92
|
+
@merchant_account_hash['id'] ||= create_id(@merchant_account_hash['merchant_id'])
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'active_support/core_ext/hash/keys'
|
2
|
+
|
3
|
+
module FakeBraintree
|
4
|
+
class PaymentMethod
|
5
|
+
def self.tokenize_card(attributes)
|
6
|
+
token = (Time.now.to_f * 1000).round.to_s
|
7
|
+
FakeBraintree.registry.payment_methods[token] = attributes.stringify_keys
|
8
|
+
token
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -4,16 +4,18 @@ class FakeBraintree::Registry
|
|
4
4
|
end
|
5
5
|
|
6
6
|
attr_accessor :customers,:subscriptions, :failures, :transactions, :redirects,
|
7
|
-
:credit_cards, :addresses
|
7
|
+
:credit_cards, :addresses, :payment_methods, :merchant_accounts
|
8
8
|
|
9
9
|
def clear!
|
10
|
-
@addresses
|
11
|
-
@customers
|
12
|
-
@subscriptions
|
13
|
-
@failures
|
14
|
-
@transactions
|
15
|
-
@redirects
|
16
|
-
@credit_cards
|
10
|
+
@addresses = {}
|
11
|
+
@customers = {}
|
12
|
+
@subscriptions = {}
|
13
|
+
@failures = {}
|
14
|
+
@transactions = {}
|
15
|
+
@redirects = {}
|
16
|
+
@credit_cards = {}
|
17
|
+
@payment_methods = {}
|
18
|
+
@merchant_accounts = {}
|
17
19
|
end
|
18
20
|
|
19
21
|
def failure?(card_number)
|
@@ -1,7 +1,15 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'capybara'
|
3
|
+
require 'fake_braintree/sinatra_app'
|
4
|
+
|
1
5
|
class FakeBraintree::Server
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
SERVER_HOST = '127.0.0.1'
|
7
|
+
|
8
|
+
extend Forwardable
|
9
|
+
def_delegators :@server, :port, :boot
|
10
|
+
|
11
|
+
def initialize(options = {})
|
12
|
+
app = FakeBraintree::SinatraApp
|
13
|
+
@server = Capybara::Server.new(app, options.fetch(:port, nil), SERVER_HOST)
|
6
14
|
end
|
7
15
|
end
|
@@ -1,8 +1,23 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'active_support/core_ext/hash/conversions'
|
3
|
+
require 'fake_braintree/customer'
|
4
|
+
require 'fake_braintree/subscription'
|
5
|
+
require 'fake_braintree/redirect'
|
6
|
+
require 'fake_braintree/credit_card'
|
7
|
+
require 'fake_braintree/address'
|
8
|
+
require 'fake_braintree/payment_method'
|
9
|
+
require 'fake_braintree/transaction'
|
10
|
+
require 'fake_braintree/client_token'
|
11
|
+
require 'fake_braintree/credit_card_serializer'
|
12
|
+
require 'fake_braintree/merchant_account'
|
13
|
+
|
1
14
|
module FakeBraintree
|
2
15
|
class SinatraApp < Sinatra::Base
|
3
16
|
set :show_exceptions, false
|
4
17
|
set :dump_errors, true
|
5
18
|
set :raise_errors, true
|
19
|
+
set :public_folder, File.dirname(__FILE__) + '/braintree_assets'
|
20
|
+
set :protection, except: :frame_options
|
6
21
|
disable :logging
|
7
22
|
|
8
23
|
include Helpers
|
@@ -18,6 +33,52 @@ module FakeBraintree
|
|
18
33
|
end
|
19
34
|
end
|
20
35
|
|
36
|
+
# braintree.api.Client.prototype.tokenizeCard()
|
37
|
+
get '/merchants/:merchant_id/client_api/v1/payment_methods/credit_cards' do
|
38
|
+
request_hash = params
|
39
|
+
|
40
|
+
callback = request_hash.delete('callback')
|
41
|
+
nonce = FakeBraintree::PaymentMethod.tokenize_card(request_hash['creditCard'])
|
42
|
+
|
43
|
+
headers = {
|
44
|
+
'Content-Encoding' => 'gzip',
|
45
|
+
'Content-Type' => 'application/javascript; charset=utf-8'
|
46
|
+
}
|
47
|
+
json = {
|
48
|
+
creditCards: [nonce: nonce],
|
49
|
+
status: 201
|
50
|
+
}.to_json
|
51
|
+
response = "#{callback}(#{json})"
|
52
|
+
[200, headers, gzip(response)]
|
53
|
+
end
|
54
|
+
|
55
|
+
# braintree.api.Client.prototype.getCreditCards()
|
56
|
+
get '/merchants/:merchant_id/client_api/v1/payment_methods' do
|
57
|
+
request_hash = params
|
58
|
+
|
59
|
+
callback = request_hash.delete('callback')
|
60
|
+
customer_id = request_hash['authorizationFingerprint']
|
61
|
+
begin
|
62
|
+
customer = Braintree::Customer.find(customer_id)
|
63
|
+
credit_cards = customer.credit_cards.collect do |card|
|
64
|
+
FakeBraintree::CreditCardSerializer.new(card).to_h
|
65
|
+
end
|
66
|
+
rescue Braintree::NotFoundError, ArgumentError
|
67
|
+
credit_cards = []
|
68
|
+
end
|
69
|
+
|
70
|
+
headers = {
|
71
|
+
'Content-Encoding' => 'gzip',
|
72
|
+
'Content-Type' => 'application/javascript; charset=utf-8'
|
73
|
+
}
|
74
|
+
json = {
|
75
|
+
paymentMethods: credit_cards,
|
76
|
+
status: 200
|
77
|
+
}.to_json
|
78
|
+
response = "#{callback}(#{json})"
|
79
|
+
[200, headers, gzip(response)]
|
80
|
+
end
|
81
|
+
|
21
82
|
# Braintree::Customer.create
|
22
83
|
post '/merchants/:merchant_id/customers' do
|
23
84
|
customer_hash = hash_from_request_body_with_key('customer')
|
@@ -82,9 +143,27 @@ module FakeBraintree
|
|
82
143
|
|
83
144
|
# Braintree::Subscription.cancel
|
84
145
|
put '/merchants/:merchant_id/subscriptions/:id/cancel' do
|
85
|
-
updates = {'status' => Braintree::Subscription::Status::Canceled}
|
86
146
|
options = {id: params[:id], merchant_id: params[:merchant_id]}
|
87
|
-
Subscription.new(
|
147
|
+
Subscription.new({}, options).cancel
|
148
|
+
end
|
149
|
+
|
150
|
+
# Braintree::PaymentMethod.find
|
151
|
+
get '/merchants/:merchant_id/payment_methods/any/:token' do
|
152
|
+
credit_card = FakeBraintree.registry.credit_cards[params[:token]]
|
153
|
+
if credit_card
|
154
|
+
gzipped_response(200, credit_card.to_xml(root: 'credit_card'))
|
155
|
+
else
|
156
|
+
gzipped_response(404, {})
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
# Braintree::PaymentMethod.update
|
161
|
+
put '/merchants/:merchant_id/payment_methods/any/:token' do
|
162
|
+
credit_card = FakeBraintree.registry.credit_cards[params[:token]]
|
163
|
+
updates = hash_from_request_body_with_key('payment_method')
|
164
|
+
options = {token: params[:token], merchant_id: params[:merchant_id]}
|
165
|
+
|
166
|
+
CreditCard.new(updates, options).update
|
88
167
|
end
|
89
168
|
|
90
169
|
# Braintree::CreditCard.find
|
@@ -106,9 +185,28 @@ module FakeBraintree
|
|
106
185
|
CreditCard.new(updates, options).update
|
107
186
|
end
|
108
187
|
|
188
|
+
# Braintree::CreditCard.delete
|
189
|
+
delete '/merchants/:merchant_id/payment_methods/credit_card/:credit_card_token' do
|
190
|
+
cc_hash = {}
|
191
|
+
options = {token: params[:credit_card_token], merchant_id: params[:merchant_id]}
|
192
|
+
|
193
|
+
CreditCard.new(cc_hash, options).delete
|
194
|
+
end
|
195
|
+
|
196
|
+
# Braintree::PaymentMethod.create
|
109
197
|
# Braintree::CreditCard.create
|
110
198
|
post '/merchants/:merchant_id/payment_methods' do
|
111
|
-
|
199
|
+
request_hash = Hash.from_xml(request.body)
|
200
|
+
request.body.rewind
|
201
|
+
|
202
|
+
credit_card_hash =
|
203
|
+
if request_hash.key?('credit_card')
|
204
|
+
hash_from_request_body_with_key('credit_card')
|
205
|
+
else
|
206
|
+
payment_method_hash = hash_from_request_body_with_key('payment_method')
|
207
|
+
nonce = payment_method_hash.delete('payment_method_nonce')
|
208
|
+
FakeBraintree.registry.payment_methods[nonce].merge(payment_method_hash)
|
209
|
+
end
|
112
210
|
options = {merchant_id: params[:merchant_id]}
|
113
211
|
|
114
212
|
if credit_card_hash['options']
|
@@ -122,18 +220,13 @@ module FakeBraintree
|
|
122
220
|
# Braintree::CreditCard.sale
|
123
221
|
post '/merchants/:merchant_id/transactions' do
|
124
222
|
if FakeBraintree.decline_all_cards?
|
125
|
-
gzipped_response(422, FakeBraintree.create_failure.to_xml(root:
|
223
|
+
gzipped_response(422, FakeBraintree.create_failure.to_xml(root: "api_error_response"))
|
126
224
|
else
|
127
|
-
|
225
|
+
data = hash_from_request_body_with_key("transaction")
|
128
226
|
transaction_id = md5("#{params[:merchant_id]}#{Time.now.to_f}")
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
status = "submitted_for_settlement"
|
133
|
-
end
|
134
|
-
transaction_response = {'id' => transaction_id, 'amount' => transaction['amount'], 'status' => status, 'type' => 'sale'}
|
135
|
-
FakeBraintree.registry.transactions[transaction_id] = transaction_response
|
136
|
-
gzipped_response(200, transaction_response.to_xml(root: 'transaction'))
|
227
|
+
transaction = FakeBraintree::Transaction.new(data, transaction_id)
|
228
|
+
response = transaction.create
|
229
|
+
gzipped_response(200, response.to_xml(root: "transaction"))
|
137
230
|
end
|
138
231
|
end
|
139
232
|
|
@@ -150,7 +243,7 @@ module FakeBraintree
|
|
150
243
|
# Braintree::Transaction.refund
|
151
244
|
post '/merchants/:merchant_id/transactions/:transaction_id/refund' do
|
152
245
|
transaction = hash_from_request_body_with_key('transaction')
|
153
|
-
transaction_id = md5(
|
246
|
+
transaction_id = md5("#{params[:merchant_id]}#{Time.now.to_f}")
|
154
247
|
transaction_response = {'id' => transaction_id, 'amount' => transaction['amount'], 'type' => 'credit'}
|
155
248
|
FakeBraintree.registry.transactions[transaction_id] = transaction_response
|
156
249
|
gzipped_response(200, transaction_response.to_xml(root: 'transaction'))
|
@@ -194,5 +287,46 @@ module FakeBraintree
|
|
194
287
|
redirect = FakeBraintree.registry.redirects[params[:id]]
|
195
288
|
redirect.confirm
|
196
289
|
end
|
290
|
+
|
291
|
+
# Braintree::ClientToken.generate
|
292
|
+
post '/merchants/:merchant_id/client_token' do
|
293
|
+
client_token_hash = hash_from_request_body_with_key('client_token')
|
294
|
+
token = FakeBraintree::ClientToken.generate(client_token_hash)
|
295
|
+
response = { value: token }.to_xml(root: :client_token)
|
296
|
+
gzipped_response(200, response)
|
297
|
+
end
|
298
|
+
|
299
|
+
get '/config' do
|
300
|
+
headers = {
|
301
|
+
'Content-Encoding' => 'gzip',
|
302
|
+
'Content-Type' => 'application/javascript; charset=utf-8'
|
303
|
+
}
|
304
|
+
response = "#{params['callback']}(#{{ status: 200 }.to_json})"
|
305
|
+
[200, headers, gzip(response)]
|
306
|
+
end
|
307
|
+
|
308
|
+
#Braintree::MerchantAccount.find
|
309
|
+
get '/merchants/:merchant_id/merchant_accounts/:merchant_account_id' do
|
310
|
+
merchant_account = FakeBraintree.registry.merchant_accounts[params[:merchant_account_id]]
|
311
|
+
if merchant_account
|
312
|
+
gzipped_response(200, merchant_account.to_xml(root: 'merchant_account'))
|
313
|
+
else
|
314
|
+
gzipped_response(404, {})
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
# Braintree::MerchantAccount.update
|
319
|
+
put '/merchants/:merchant_id/merchant_accounts/:merchant_account_id/update_via_api' do
|
320
|
+
merchant_account_hash = hash_from_request_body_with_key('merchant_account')
|
321
|
+
options = {id: params[:merchant_account_id], merchant_id: params[:merchant_id]}
|
322
|
+
MerchantAccount.new(merchant_account_hash, options).update
|
323
|
+
end
|
324
|
+
|
325
|
+
# Braintree::MerchantAccount.create
|
326
|
+
post '/merchants/:merchant_id/merchant_accounts/create_via_api' do
|
327
|
+
merchant_account_hash = hash_from_request_body_with_key('merchant_account')
|
328
|
+
options = {merchant_id: params[:merchant_id]}
|
329
|
+
MerchantAccount.new(merchant_account_hash, options).create
|
330
|
+
end
|
197
331
|
end
|
198
332
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'fake_braintree/helpers'
|
2
|
+
|
1
3
|
module FakeBraintree
|
2
4
|
class Subscription
|
3
5
|
include Helpers
|
@@ -14,6 +16,10 @@ module FakeBraintree
|
|
14
16
|
|
15
17
|
def create
|
16
18
|
create_subscription_with(subscription_hash)
|
19
|
+
if credit_card = FakeBraintree.registry.credit_cards[payment_method_token]
|
20
|
+
credit_card['subscriptions'] ||= []
|
21
|
+
credit_card['subscriptions'] << subscription_hash
|
22
|
+
end
|
17
23
|
response_for_created_subscription(subscription_hash)
|
18
24
|
end
|
19
25
|
|
@@ -26,13 +32,22 @@ module FakeBraintree
|
|
26
32
|
end
|
27
33
|
end
|
28
34
|
|
35
|
+
def cancel
|
36
|
+
if subscription_exists_in_registry?
|
37
|
+
canceled_subscription = update_existing_subscription('status' => canceled_status)
|
38
|
+
response_for_canceled_subscription(canceled_subscription)
|
39
|
+
else
|
40
|
+
response_for_subscription_not_found
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
29
44
|
private
|
30
45
|
|
31
46
|
def subscription_hash
|
32
47
|
@subscription_hash.merge(
|
33
48
|
'transactions' => [],
|
34
|
-
'add_ons' =>
|
35
|
-
'discounts' =>
|
49
|
+
'add_ons' => add_ons,
|
50
|
+
'discounts' => discounts,
|
36
51
|
'next_billing_date' => braintree_formatted_date(next_billing_date),
|
37
52
|
'billing_day_of_month' => billing_day_of_month,
|
38
53
|
'billing_period_start_date' => braintree_formatted_date(billing_period_start_date),
|
@@ -61,17 +76,33 @@ module FakeBraintree
|
|
61
76
|
date.strftime('%Y-%m-%d')
|
62
77
|
end
|
63
78
|
|
64
|
-
def
|
65
|
-
|
66
|
-
@subscription_hash['add_ons']['add'].map { |add_on| { 'id' => add_on['inherited_from_id'] } }
|
67
|
-
else
|
68
|
-
[]
|
69
|
-
end
|
79
|
+
def add_ons
|
80
|
+
discounts_or_add_ons(@subscription_hash['add_ons'])
|
70
81
|
end
|
71
82
|
|
72
|
-
def
|
73
|
-
|
74
|
-
|
83
|
+
def discounts
|
84
|
+
discounts_or_add_ons(@subscription_hash['discounts'])
|
85
|
+
end
|
86
|
+
|
87
|
+
def discounts_or_add_ons(discount_or_add_on)
|
88
|
+
return [] unless discount_or_add_on.is_a?(Hash)
|
89
|
+
|
90
|
+
if discount_or_add_on['add']
|
91
|
+
discount_or_add_on['add'].map do |hsh|
|
92
|
+
{
|
93
|
+
'id' => hsh['inherited_from_id'],
|
94
|
+
'quantity' => hsh['quantity'],
|
95
|
+
'amount' => hsh['amount']
|
96
|
+
}
|
97
|
+
end
|
98
|
+
elsif discount_or_add_on['update']
|
99
|
+
discount_or_add_on['update'].map do |hsh|
|
100
|
+
{
|
101
|
+
'id' => hsh['existing_id'],
|
102
|
+
'quantity' => hsh['quantity'],
|
103
|
+
'amount' => hsh['amount']
|
104
|
+
}
|
105
|
+
end
|
75
106
|
else
|
76
107
|
[]
|
77
108
|
end
|
@@ -117,6 +148,10 @@ module FakeBraintree
|
|
117
148
|
Braintree::Subscription::Status::Active
|
118
149
|
end
|
119
150
|
|
151
|
+
def canceled_status
|
152
|
+
Braintree::Subscription::Status::Canceled
|
153
|
+
end
|
154
|
+
|
120
155
|
def response_for_created_subscription(hash)
|
121
156
|
gzipped_response(201, hash.to_xml(root: 'subscription'))
|
122
157
|
end
|
@@ -125,8 +160,8 @@ module FakeBraintree
|
|
125
160
|
gzipped_response(404, {})
|
126
161
|
end
|
127
162
|
|
128
|
-
def
|
129
|
-
gzipped_response(
|
163
|
+
def response_for_canceled_subscription(hash)
|
164
|
+
gzipped_response(200, hash.to_xml(root: 'subscription'))
|
130
165
|
end
|
131
166
|
end
|
132
167
|
end
|