stripe-ruby-mock 1.10.1.7 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/README.md +70 -3
  4. data/Rakefile +1 -1
  5. data/lib/stripe_mock/api/client.rb +1 -0
  6. data/lib/stripe_mock/api/instance.rb +1 -0
  7. data/lib/stripe_mock/api/live.rb +15 -0
  8. data/lib/stripe_mock/api/server.rb +24 -21
  9. data/lib/stripe_mock/api/test_helpers.rb +24 -0
  10. data/lib/stripe_mock/client.rb +4 -8
  11. data/lib/stripe_mock/data.rb +54 -30
  12. data/lib/stripe_mock/instance.rb +15 -5
  13. data/lib/stripe_mock/request_handlers/cards.rb +29 -18
  14. data/lib/stripe_mock/request_handlers/charges.rb +34 -6
  15. data/lib/stripe_mock/request_handlers/coupons.rb +1 -3
  16. data/lib/stripe_mock/request_handlers/customers.rb +3 -9
  17. data/lib/stripe_mock/request_handlers/events.rb +1 -3
  18. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +16 -9
  19. data/lib/stripe_mock/request_handlers/helpers/charge_helpers.rb +16 -0
  20. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +9 -2
  21. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +3 -1
  22. data/lib/stripe_mock/request_handlers/invoice_items.rb +32 -2
  23. data/lib/stripe_mock/request_handlers/invoices.rb +7 -3
  24. data/lib/stripe_mock/request_handlers/plans.rb +2 -5
  25. data/lib/stripe_mock/request_handlers/recipients.rb +26 -4
  26. data/lib/stripe_mock/request_handlers/subscriptions.rb +26 -33
  27. data/lib/stripe_mock/request_handlers/tokens.rb +24 -4
  28. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +18 -0
  29. data/lib/stripe_mock/server.rb +4 -5
  30. data/lib/stripe_mock/test_strategies/base.rb +27 -0
  31. data/lib/stripe_mock/test_strategies/live.rb +22 -0
  32. data/lib/stripe_mock/test_strategies/mock.rb +19 -0
  33. data/lib/stripe_mock/util.rb +5 -0
  34. data/lib/stripe_mock/version.rb +1 -1
  35. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +3 -2
  36. data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +16 -9
  37. data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +3 -2
  38. data/lib/stripe_mock/webhook_fixtures/customer.card.created.json +1 -0
  39. data/lib/stripe_mock/webhook_fixtures/customer.card.deleted.json +1 -0
  40. data/lib/stripe_mock/webhook_fixtures/customer.card.updated.json +1 -0
  41. data/lib/stripe_mock/webhook_fixtures/customer.created.json +1 -0
  42. data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +2 -1
  43. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +1 -0
  44. data/lib/stripe_mock.rb +9 -1
  45. data/spec/fixtures/create_refund.yml +126 -0
  46. data/spec/instance_spec.rb +4 -2
  47. data/spec/integration_examples/charge_token_examples.rb +49 -0
  48. data/spec/integration_examples/customer_card_examples.rb +42 -0
  49. data/spec/integration_examples/prepare_error_examples.rb +18 -0
  50. data/spec/readme_spec.rb +2 -1
  51. data/spec/server_spec.rb +12 -3
  52. data/spec/shared_stripe_examples/card_examples.rb +108 -3
  53. data/spec/shared_stripe_examples/card_token_examples.rb +26 -0
  54. data/spec/shared_stripe_examples/charge_examples.rb +55 -39
  55. data/spec/shared_stripe_examples/coupon_examples.rb +2 -17
  56. data/spec/shared_stripe_examples/customer_examples.rb +30 -39
  57. data/spec/shared_stripe_examples/error_mock_examples.rb +1 -1
  58. data/spec/shared_stripe_examples/invoice_examples.rb +31 -15
  59. data/spec/shared_stripe_examples/invoice_item_examples.rb +62 -10
  60. data/spec/shared_stripe_examples/plan_examples.rb +29 -18
  61. data/spec/shared_stripe_examples/recipient_examples.rb +55 -5
  62. data/spec/shared_stripe_examples/refund_examples.rb +90 -0
  63. data/spec/shared_stripe_examples/subscription_examples.rb +159 -82
  64. data/spec/shared_stripe_examples/validation_examples.rb +19 -0
  65. data/spec/spec_helper.rb +32 -1
  66. data/spec/stripe_mock_spec.rb +70 -0
  67. data/spec/support/stripe_examples.rb +7 -14
  68. data/spec/util_spec.rb +8 -0
  69. data/stripe-ruby-mock.gemspec +2 -2
  70. metadata +38 -34
  71. data/lib/stripe_mock/api/strict.rb +0 -11
@@ -23,8 +23,7 @@ module StripeMock
23
23
 
24
24
  if params[:plan]
25
25
  plan_id = params[:plan].to_s
26
- plan = plans[plan_id]
27
- assert_existance :plan, plan_id, plan
26
+ plan = assert_existance :plan, plan_id, plans[plan_id]
28
27
 
29
28
  if params[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
30
29
  raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
@@ -42,14 +41,12 @@ module StripeMock
42
41
 
43
42
  def update_customer(route, method_url, params, headers)
44
43
  route =~ method_url
45
- assert_existance :customer, $1, customers[$1]
46
-
47
- cus = customers[$1] ||= Data.mock_customer([], :id => $1)
44
+ cus = assert_existance :customer, $1, customers[$1]
48
45
  cus.merge!(params)
49
46
 
50
47
  if params[:card]
51
48
  new_card = get_card_by_token(params.delete(:card))
52
- add_card_to_customer(new_card, cus)
49
+ add_card_to_object(:customer, new_card, cus, true)
53
50
  cus[:default_card] = new_card[:id]
54
51
  end
55
52
 
@@ -64,14 +61,11 @@ module StripeMock
64
61
  id: customers[$1][:id],
65
62
  deleted: true
66
63
  }
67
-
68
- customers[$1]
69
64
  end
70
65
 
71
66
  def get_customer(route, method_url, params, headers)
72
67
  route =~ method_url
73
68
  assert_existance :customer, $1, customers[$1]
74
- customers[$1] ||= Data.mock_customer([], :id => $1)
75
69
  end
76
70
 
77
71
  def list_customers(route, method_url, params, headers)
@@ -8,9 +8,7 @@ module StripeMock
8
8
 
9
9
  def retrieve_event(route, method_url, params, headers)
10
10
  route =~ method_url
11
- event = events[$1]
12
- assert_existance :event, $1, event
13
- event
11
+ assert_existance :event, $1, events[$1]
14
12
  end
15
13
 
16
14
  end
@@ -2,24 +2,31 @@ module StripeMock
2
2
  module RequestHandlers
3
3
  module Helpers
4
4
 
5
- def get_customer_card(customer, token)
6
- customer[:cards][:data].find{|cc| cc[:id] == token }
5
+ def get_card(object, card_id, class_name='Customer')
6
+ card = object[:cards][:data].find{|cc| cc[:id] == card_id }
7
+ if card.nil?
8
+ msg = "#{class_name} #{object[:id]} does not have card #{card_id}"
9
+ raise Stripe::InvalidRequestError.new(msg, 'card', 404)
10
+ end
11
+ card
7
12
  end
8
13
 
9
- def add_card_to_customer(card, cus)
10
- card[:customer] = cus[:id]
14
+ def add_card_to_object(type, card, object, replace_current=false)
15
+ card[type] = object[:id]
11
16
 
12
- if cus[:cards][:count] == 0
13
- cus[:cards][:count] += 1
17
+ if replace_current
18
+ object[:cards][:data].delete_if {|card| card[:id] == object[:default_card]}
19
+ object[:default_card] = card[:id]
14
20
  else
15
- cus[:cards][:data].delete_if {|card| card[:id] == cus[:default_card]}
21
+ object[:cards][:count] += 1
16
22
  end
17
23
 
18
- cus[:cards][:data] << card
24
+ object[:default_card] = card[:id] unless object[:default_card]
25
+ object[:cards][:data] << card
19
26
 
20
27
  card
21
28
  end
22
29
 
23
30
  end
24
31
  end
25
- end
32
+ end
@@ -0,0 +1,16 @@
1
+ module StripeMock
2
+ module RequestHandlers
3
+ module Helpers
4
+
5
+ def add_refund_to_charge(refund, charge)
6
+ refunds = charge[:refunds]
7
+ refunds[:data] << refund
8
+ refunds[:total_count] = refunds[:data].count
9
+
10
+ charge[:amount_refunded] = refunds[:data].reduce(0) {|sum, r| sum + r[:amount].to_i }
11
+ charge[:refunded] = true
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -11,7 +11,7 @@ module StripeMock
11
11
 
12
12
  start_time = options[:current_period_start] || Time.now.utc.to_i
13
13
  params = { plan: plan, customer: cus[:id], current_period_start: start_time }
14
- params.merge! options.select {|k,v| k =~ /application_fee_percent|quantity/}
14
+ params.merge! options.select {|k,v| k =~ /application_fee_percent|quantity|metadata/}
15
15
  # TODO: Implement coupon logic
16
16
 
17
17
  if (plan[:trial_period_days].nil? && options[:trial_end].nil?) || options[:trial_end] == "now"
@@ -27,7 +27,14 @@ module StripeMock
27
27
 
28
28
  def add_subscription_to_customer(cus, sub)
29
29
  cus[:subscriptions][:count] = (cus[:subscriptions][:count] || 0) + 1
30
- cus[:subscriptions][:data] << sub
30
+ cus[:subscriptions][:data].unshift sub
31
+ end
32
+
33
+ def delete_subscription_from_customer(cus, subscription)
34
+ cus[:subscriptions][:data].reject!{|sub|
35
+ sub[:id] == subscription[:id]
36
+ }
37
+ cus[:subscriptions][:count] -=1
31
38
  end
32
39
 
33
40
  # `intervals` is set to 1 when calculating current_period_end from current_period_start & plan
@@ -25,7 +25,9 @@ module StripeMock
25
25
 
26
26
  def get_card_by_token(token)
27
27
  if token.nil? || @card_tokens[token].nil?
28
- Data.mock_card :id => new_id('cc')
28
+ # TODO: Make this strict
29
+ msg = "Invalid token id: #{token}"
30
+ raise Stripe::InvalidRequestError.new(msg, 'tok', 404)
29
31
  else
30
32
  @card_tokens.delete(token)
31
33
  end
@@ -3,11 +3,41 @@ module StripeMock
3
3
  module InvoiceItems
4
4
 
5
5
  def InvoiceItems.included(klass)
6
- klass.add_handler 'post /v1/invoiceitems', :new_invoice_item
6
+ klass.add_handler 'post /v1/invoiceitems', :new_invoice_item
7
+ klass.add_handler 'post /v1/invoiceitems/(.*)', :update_invoice_item
8
+ klass.add_handler 'get /v1/invoiceitems/(.*)', :get_invoice_item
9
+ klass.add_handler 'get /v1/invoiceitems', :list_invoice_items
10
+ klass.add_handler 'delete /v1/invoiceitems/(.*)', :delete_invoice_item
7
11
  end
8
12
 
9
13
  def new_invoice_item(route, method_url, params, headers)
10
- Data.mock_invoice_item(params)
14
+ params[:id] ||= new_id('ii')
15
+ invoice_items[params[:id]] = Data.mock_invoice_item(params)
16
+ end
17
+
18
+ def update_invoice_item(route, method_url, params, headers)
19
+ route =~ method_url
20
+ list_item = assert_existance :list_item, $1, invoice_items[$1]
21
+ list_item.merge!(params)
22
+ end
23
+
24
+ def delete_invoice_item(route, method_url, params, headers)
25
+ route =~ method_url
26
+ assert_existance :list_item, $1, invoice_items[$1]
27
+
28
+ invoice_items[$1] = {
29
+ id: invoice_items[$1][:id],
30
+ deleted: true
31
+ }
32
+ end
33
+
34
+ def list_invoice_items(route, method_url, params, headers)
35
+ invoice_items.values
36
+ end
37
+
38
+ def get_invoice_item(route, method_url, params, headers)
39
+ route =~ method_url
40
+ assert_existance :invoice_item, $1, invoice_items[$1]
11
41
  end
12
42
 
13
43
  end
@@ -8,6 +8,7 @@ module StripeMock
8
8
  klass.add_handler 'get /v1/invoices/(.*)', :get_invoice
9
9
  klass.add_handler 'get /v1/invoices', :list_invoices
10
10
  klass.add_handler 'post /v1/invoices/(.*)/pay', :pay_invoice
11
+ klass.add_handler 'post /v1/invoices/(.*)', :update_invoice
11
12
  end
12
13
 
13
14
  def new_invoice(route, method_url, params, headers)
@@ -16,6 +17,12 @@ module StripeMock
16
17
  invoices[id] = Data.mock_invoice([invoice_item], params.merge(:id => id))
17
18
  end
18
19
 
20
+ def update_invoice(route, method_url, params, headers)
21
+ route =~ method_url
22
+ assert_existance :invoice, $1, invoices[$1]
23
+ invoices[$1].merge!(params)
24
+ end
25
+
19
26
  def list_invoices(route, method_url, params, headers)
20
27
  params[:offset] ||= 0
21
28
  params[:count] ||= 10
@@ -32,13 +39,11 @@ module StripeMock
32
39
  def get_invoice(route, method_url, params, headers)
33
40
  route =~ method_url
34
41
  assert_existance :invoice, $1, invoices[$1]
35
- invoices[$1] ||= Data.mock_invoice([], :id => $1)
36
42
  end
37
43
 
38
44
  def pay_invoice(route, method_url, params, headers)
39
45
  route =~ method_url
40
46
  assert_existance :invoice, $1, invoices[$1]
41
- invoices[$1] ||= Data.mock_invoice([], :id => $1)
42
47
  invoices[$1].merge!(:paid => true, :attempted => true, :charge => 'ch_1fD6uiR9FAA2zc')
43
48
  end
44
49
 
@@ -48,7 +53,6 @@ module StripeMock
48
53
 
49
54
  customer = customers[params[:customer]]
50
55
  assert_existance :customer, params[:customer], customer
51
- customer ||= Data.mock_customer([], :id => params[:customer])
52
56
 
53
57
  raise Stripe::InvalidRequestError.new("No upcoming invoices for customer: #{customer[:id]}", nil, 404) if customer[:subscriptions][:data].length == 0
54
58
 
@@ -11,27 +11,24 @@ module StripeMock
11
11
  end
12
12
 
13
13
  def new_plan(route, method_url, params, headers)
14
- params[:id] = ( params[:id] || new_id('plan') ).to_s
14
+ validate_create_plan_params(params)
15
15
  plans[ params[:id] ] = Data.mock_plan(params)
16
16
  end
17
17
 
18
18
  def update_plan(route, method_url, params, headers)
19
19
  route =~ method_url
20
20
  assert_existance :plan, $1, plans[$1]
21
- plans[$1] ||= Data.mock_plan(:id => $1)
22
21
  plans[$1].merge!(params)
23
22
  end
24
23
 
25
24
  def get_plan(route, method_url, params, headers)
26
25
  route =~ method_url
27
26
  assert_existance :plan, $1, plans[$1]
28
- plans[$1] ||= Data.mock_plan(:id => $1)
29
27
  end
30
28
 
31
29
  def delete_plan(route, method_url, params, headers)
32
30
  route =~ method_url
33
- assert_existance :plan, $1, plans[$1]
34
- plans.delete($1)
31
+ assert_existance :plan, $1, plans.delete($1)
35
32
  end
36
33
 
37
34
  def list_plans(route, method_url, params, headers)
@@ -4,22 +4,44 @@ module StripeMock
4
4
 
5
5
  def Recipients.included(klass)
6
6
  klass.add_handler 'post /v1/recipients', :new_recipient
7
+ klass.add_handler 'post /v1/recipients/(.*)', :update_recipient
7
8
  klass.add_handler 'get /v1/recipients/(.*)', :get_recipient
8
9
  end
9
10
 
10
11
  def new_recipient(route, method_url, params, headers)
11
- id = new_id('rp')
12
+ params[:id] ||= new_id('rp')
13
+ cards = []
14
+
12
15
  if params[:bank_account]
13
16
  params[:active_account] = get_bank_by_token(params.delete(:bank_account))
14
17
  end
15
- recipients[id] = Data.mock_recipient(params.merge :id => id)
16
- recipients[id]
18
+
19
+ if params[:card]
20
+ cards << get_card_by_token(params.delete(:card))
21
+ params[:default_card] = cards.first[:id]
22
+ end
23
+
24
+ recipients[ params[:id] ] = Data.mock_recipient(cards, params)
25
+ recipients[ params[:id] ]
26
+ end
27
+
28
+ def update_recipient(route, method_url, params, headers)
29
+ route =~ method_url
30
+ recipient = assert_existance :recipient, $1, recipients[$1]
31
+ recipient.merge!(params)
32
+
33
+ if params[:card]
34
+ new_card = get_card_by_token(params.delete(:card))
35
+ add_card_to_object(:recipient, new_card, recipient, true)
36
+ recipient[:default_card] = new_card[:id]
37
+ end
38
+
39
+ recipient
17
40
  end
18
41
 
19
42
  def get_recipient(route, method_url, params, headers)
20
43
  route =~ method_url
21
44
  assert_existance :recipient, $1, recipients[$1]
22
- recipients[$1] ||= Data.mock_recipient(:id => $1)
23
45
  end
24
46
  end
25
47
  end
@@ -12,61 +12,51 @@ module StripeMock
12
12
 
13
13
  def create_subscription(route, method_url, params, headers)
14
14
  route =~ method_url
15
+ customer = assert_existance :customer, $1, customers[$1]
15
16
 
16
- customer = customers[$1]
17
- assert_existance :customer, $1, customer
18
-
19
- plan = plans[params[:plan]]
20
- assert_existance :plan, params[:plan], plan
17
+ plan_id = params[:plan]
18
+ plan = assert_existance :plan, plan_id, plans[plan_id]
21
19
 
22
20
  if params[:card]
23
21
  new_card = get_card_by_token(params.delete(:card))
24
- add_card_to_customer(new_card, customer)
22
+ add_card_to_object(:customer, new_card, customer)
25
23
  customer[:default_card] = new_card[:id]
26
24
  end
27
25
 
28
26
  # Ensure customer has card to charge if plan has no trial and is not free
29
- verify_card_present(customer, plan)
27
+ verify_card_present(customer, plan, params)
30
28
 
31
29
  subscription = Data.mock_subscription({ id: (params[:id] || new_id('su')) })
32
30
  subscription.merge!(custom_subscription_params(plan, customer, params))
33
31
  add_subscription_to_customer(customer, subscription)
34
32
 
35
- # oddly, subscription returned from 'create_subscription' does not expand plan
36
- subscription.merge(plan: params[:plan])
33
+ subscription
37
34
  end
38
35
 
39
36
  def retrieve_subscription(route, method_url, params, headers)
40
37
  route =~ method_url
41
38
 
42
- customer = customers[$1]
43
- assert_existance :customer, $1, customer
44
- subscription = get_customer_subscription(customer, $2)
45
- assert_existance :subscription, $2, subscription
46
-
47
- subscription
39
+ customer = assert_existance :customer, $1, customers[$1]
40
+ assert_existance :subscription, $2, get_customer_subscription(customer, $2)
48
41
  end
49
42
 
50
43
  def retrieve_subscriptions(route, method_url, params, headers)
51
44
  route =~ method_url
52
45
 
53
- customer = customers[$1]
54
- assert_existance :customer, $1, customer
55
-
46
+ customer = assert_existance :customer, $1, customers[$1]
56
47
  customer[:subscriptions]
57
48
  end
58
49
 
59
50
  def update_subscription(route, method_url, params, headers)
60
51
  route =~ method_url
52
+ customer = assert_existance :customer, $1, customers[$1]
61
53
 
62
- customer = customers[$1]
63
- assert_existance :customer, $1, customer
64
54
  subscription = get_customer_subscription(customer, $2)
65
55
  assert_existance :subscription, $2, subscription
66
56
 
67
57
  if params[:card]
68
58
  new_card = get_card_by_token(params.delete(:card))
69
- add_card_to_customer(new_card, customer)
59
+ add_card_to_object(:customer, new_card, customer)
70
60
  customer[:default_card] = new_card[:id]
71
61
  end
72
62
 
@@ -78,26 +68,30 @@ module StripeMock
78
68
  params[:plan] = plan if params[:plan]
79
69
  verify_card_present(customer, plan)
80
70
 
71
+ if subscription[:cancel_at_period_end]
72
+ subscription[:cancel_at_period_end] = false
73
+ subscription[:canceled_at] = nil
74
+ end
75
+
81
76
  subscription.merge!(custom_subscription_params(plan, customer, params))
82
77
 
83
78
  # delete the old subscription, replace with the new subscription
84
79
  customer[:subscriptions][:data].reject! { |sub| sub[:id] == subscription[:id] }
85
80
  customer[:subscriptions][:data] << subscription
86
81
 
87
- # oddly, subscription returned from 'create_subscription' does not expand plan
88
- subscription.merge(plan: plan_name)
82
+ subscription
89
83
  end
90
84
 
91
85
  def cancel_subscription(route, method_url, params, headers)
92
86
  route =~ method_url
87
+ customer = assert_existance :customer, $1, customers[$1]
93
88
 
94
- customer = customers[$1]
95
- assert_existance :customer, $1, customer
96
89
  subscription = get_customer_subscription(customer, $2)
97
90
  assert_existance :subscription, $2, subscription
98
91
 
99
92
  cancel_params = { canceled_at: Time.now.utc.to_i }
100
- if params[:at_period_end] == true
93
+ cancelled_at_period_end = (params[:at_period_end] == true)
94
+ if cancelled_at_period_end
101
95
  cancel_params[:cancel_at_period_end] = true
102
96
  else
103
97
  cancel_params[:status] = "canceled"
@@ -107,19 +101,18 @@ module StripeMock
107
101
 
108
102
  subscription.merge!(cancel_params)
109
103
 
110
- customer[:subscriptions][:data].reject!{|sub|
111
- sub[:id] == subscription[:id]
112
- }
104
+ unless cancelled_at_period_end
105
+ delete_subscription_from_customer customer, subscription
106
+ end
113
107
 
114
- customer[:subscriptions][:data] << subscription
115
108
  subscription
116
109
  end
117
110
 
118
111
  private
119
112
 
120
- def verify_card_present(customer, plan)
121
- if customer[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
122
- raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
113
+ def verify_card_present(customer, plan, params={})
114
+ if customer[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0 && plan[:trial_end].nil? && params[:trial_end].nil?
115
+ raise Stripe::InvalidRequestError.new('You must supply a valid card xoxo', nil, 400)
123
116
  end
124
117
  end
125
118
 
@@ -8,9 +8,29 @@ module StripeMock
8
8
  end
9
9
 
10
10
  def create_token(route, method_url, params, headers)
11
- # "Sanitize" card number
12
- params[:card][:last4] = params[:card][:number][-4,4]
13
- token_id = generate_card_token(params[:card])
11
+ if params[:customer].nil? && params[:card].nil?
12
+ raise Stripe::InvalidRequestError.new('You must supply either a card, customer, or bank account to create a token.', nil, 400)
13
+ end
14
+
15
+ cus_id = params[:customer]
16
+
17
+ if cus_id && params[:card]
18
+ customer = assert_existance :customer, cus_id, customers[cus_id]
19
+
20
+ # params[:card] is an id; grab it from the db
21
+ customer_card = get_card(customer, params[:card])
22
+ assert_existance :card, params[:card], customer_card
23
+ elsif params[:card]
24
+ # params[:card] is a hash of cc info; "Sanitize" the card number
25
+ params[:card][:fingerprint] = StripeMock::Util.fingerprint(params[:card][:number])
26
+ params[:card][:last4] = params[:card][:number][-4,4]
27
+ customer_card = params[:card]
28
+ else
29
+ customer = assert_existance :customer, cus_id, customers[cus_id]
30
+ customer_card = get_card(customer, customer[:default_card])
31
+ end
32
+
33
+ token_id = generate_card_token(customer_card)
14
34
  card = @card_tokens[token_id]
15
35
 
16
36
  Data.mock_token(params.merge :id => token_id, :card => card)
@@ -30,4 +50,4 @@ module StripeMock
30
50
  end
31
51
  end
32
52
  end
33
- end
53
+ end
@@ -0,0 +1,18 @@
1
+ module StripeMock
2
+ module RequestHandlers
3
+ module ParamValidators
4
+
5
+ def validate_create_plan_params(params)
6
+ params[:id] = params[:id].to_s
7
+
8
+ @base_strategy.create_plan_params.keys.each do |name|
9
+ raise Stripe::InvalidRequestError.new("Missing required param: #{name}", name) if params[name].nil?
10
+ end
11
+ if plans[ params[:id] ]
12
+ raise Stripe::InvalidRequestError.new("Plan already exists.", :id)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -35,6 +35,10 @@ module StripeMock
35
35
  @instance.send(key)
36
36
  end
37
37
 
38
+ def destroy_resource(type, id)
39
+ @instance.send(type).delete(id)
40
+ end
41
+
38
42
  def clear_data
39
43
  @instance = Instance.new
40
44
  end
@@ -43,10 +47,6 @@ module StripeMock
43
47
  @instance.debug = toggle
44
48
  end
45
49
 
46
- def set_strict(toggle)
47
- @instance.strict = toggle
48
- end
49
-
50
50
  def set_global_id_prefix(value)
51
51
  StripeMock.global_id_prefix = value
52
52
  end
@@ -68,7 +68,6 @@ module StripeMock
68
68
  end
69
69
 
70
70
  def debug?; @instance.debug; end
71
- def strict?; @instance.strict; end
72
71
  def ping; true; end
73
72
  end
74
73
 
@@ -0,0 +1,27 @@
1
+ module StripeMock
2
+ module TestStrategies
3
+ class Base
4
+
5
+ def create_plan_params(params={})
6
+ {
7
+ :id => 'stripe_mock_default_plan_id',
8
+ :name => 'StripeMock Default Plan ID',
9
+ :amount => 1337,
10
+ :currency => 'usd',
11
+ :interval => 'month'
12
+ }.merge(params)
13
+ end
14
+
15
+ def generate_card_token(card_params={})
16
+ card = {
17
+ :number => "4242424242424242", :exp_month => 9, :exp_year => 2018, :cvc => "999"
18
+ }.merge(card_params)
19
+ card[:fingerprint] = StripeMock::Util.fingerprint(card[:number])
20
+
21
+ stripe_token = Stripe::Token.create(:card => card)
22
+ stripe_token.id
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ module StripeMock
2
+ module TestStrategies
3
+ class Live < Base
4
+
5
+ def create_plan(params={})
6
+ raise "create_plan requires an :id" if params[:id].nil?
7
+ delete_plan(params[:id])
8
+ Stripe::Plan.create create_plan_params(params)
9
+ end
10
+
11
+ def delete_plan(plan_id)
12
+ begin
13
+ plan = Stripe::Plan.retrieve(plan_id)
14
+ plan.delete
15
+ rescue Stripe::StripeError => e
16
+ # Do nothing; we just want to make sure this plan ceases to exists
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module StripeMock
2
+ module TestStrategies
3
+ class Mock < Base
4
+
5
+ def create_plan(params={})
6
+ Stripe::Plan.create create_plan_params(params)
7
+ end
8
+
9
+ def delete_plan(plan_id)
10
+ if StripeMock.state == 'remote'
11
+ StripeMock.client.destroy_resource('plans', plan_id)
12
+ elsif StripeMock.state == 'local'
13
+ StripeMock.instance.plans.delete(plan_id)
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -7,6 +7,7 @@ module StripeMock
7
7
 
8
8
  desh_hash.merge(source_hash) do |key, oldval, newval|
9
9
  if oldval.is_a?(Array) && newval.is_a?(Array)
10
+ oldval.fill(nil, oldval.length...newval.length)
10
11
  oldval.zip(newval).map {|elems|
11
12
  elems[1].nil? ? elems[0] : rmerge(elems[0], elems[1])
12
13
  }
@@ -18,5 +19,9 @@ module StripeMock
18
19
  end
19
20
  end
20
21
 
22
+ def self.fingerprint(source)
23
+ Digest::SHA1.base64digest(source).gsub(/[^a-z]/i, '')[0..15]
24
+ end
25
+
21
26
  end
22
27
  end
@@ -1,4 +1,4 @@
1
1
  module StripeMock
2
2
  # stripe-ruby-mock version
3
- VERSION = "1.10.1.7"
3
+ VERSION = "2.0.0"
4
4
  end
@@ -19,6 +19,7 @@
19
19
  "object": "card",
20
20
  "last4": "4242",
21
21
  "type": "Visa",
22
+ "brand": "Visa",
22
23
  "exp_month": 12,
23
24
  "exp_year": 2013,
24
25
  "fingerprint": "wXWJT135mEK107G8",
@@ -36,9 +37,9 @@
36
37
  "address_zip_check": null
37
38
  },
38
39
  "captured": true,
39
- "refunds": [
40
+ "refunds": {
40
41
 
41
- ],
42
+ },
42
43
  "balance_transaction": "txn_00000000000000",
43
44
  "failure_message": null,
44
45
  "failure_code": null,