stripe-ruby-mock 2.0.5 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 78209961f8a5b11ad4a2146246a6162b534af59e
4
+ data.tar.gz: 3192d4420c6f44537422804b4b60fb479584f6db
5
+ SHA512:
6
+ metadata.gz: fcec4c716ccd97608cb2e81c001baf9e174cc32e29d7578dd1ff5cb2eff1b412ba74299c5e0ae913e4f4fab01951cc3ea3f7f2927f040c6eca9b62891f30ad3a
7
+ data.tar.gz: 4a643be3f0acca2ad89dfc44dc76484df58c400f0bfcafca62338a0095dafdd4d7f5104ed9fa200d1cd6321db6dab616032bdbb6c6c8a88bb33f60117f510664
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  In your gemfile:
10
10
 
11
- gem 'stripe-ruby-mock', '~> 2.0.5', :require => 'stripe_mock'
11
+ gem 'stripe-ruby-mock', '~> 2.1.0', :require => 'stripe_mock'
12
12
 
13
13
  ## Features
14
14
 
@@ -19,9 +19,19 @@ In your gemfile:
19
19
 
20
20
  ### Specifications
21
21
 
22
- **STRIPE API TARGET VERSION:** 2014-06-17
22
+ **STRIPE API TARGET VERSION:** 2015-02-18 (master)
23
23
 
24
- * Strict params: Plan, Token#create
24
+ Older API version branches:
25
+
26
+ - [api-2014-06-17](https://github.com/rebelidealist/stripe-ruby-mock/tree/api-2014-06-17)
27
+
28
+ ### Versioning System
29
+
30
+ Since StripeMock tries to keep up with Stripe's API version, its version system is a little different:
31
+
32
+ - The **major** number (1.x.x) is for breaking changes involving how you use StripeMock itself
33
+ - The **minor** number (x.1.x) is for breaking changes involving Stripe's API
34
+ - The **patch** number (x.x.0) is for non-breaking changes/fixes involving Stripe's API, or for non-breaking changes/fixes/features for StripeMock itself.
25
35
 
26
36
  ## Description
27
37
 
data/lib/stripe_mock.rb CHANGED
@@ -43,6 +43,7 @@ require 'stripe_mock/request_handlers/validators/param_validators.rb'
43
43
 
44
44
  require 'stripe_mock/request_handlers/charges.rb'
45
45
  require 'stripe_mock/request_handlers/cards.rb'
46
+ require 'stripe_mock/request_handlers/sources.rb'
46
47
  require 'stripe_mock/request_handlers/customers.rb'
47
48
  require 'stripe_mock/request_handlers/coupons.rb'
48
49
  require 'stripe_mock/request_handlers/events.rb'
@@ -1,9 +1,9 @@
1
1
  module StripeMock
2
2
  module Data
3
3
 
4
- def self.mock_customer(cards, params)
4
+ def self.mock_customer(sources, params)
5
5
  cus_id = params[:id] || "test_cus_default"
6
- cards.each {|card| card[:customer] = cus_id}
6
+ sources.each {|source| source[:customer] = cus_id}
7
7
  {
8
8
  email: 'stripe_mock@example.com',
9
9
  description: 'an auto-generated stripe customer data mock',
@@ -14,11 +14,11 @@ module StripeMock
14
14
  delinquent: false,
15
15
  discount: nil,
16
16
  account_balance: 0,
17
- cards: {
17
+ sources: {
18
18
  object: "list",
19
- total_count: cards.size,
20
- url: "/v1/customers/#{cus_id}/cards",
21
- data: cards
19
+ total_count: sources.size,
20
+ url: "/v1/customers/#{cus_id}/sources",
21
+ data: sources
22
22
  },
23
23
  subscriptions: {
24
24
  object: "list",
@@ -26,7 +26,7 @@ module StripeMock
26
26
  url: "/v1/customers/#{cus_id}/subscriptions",
27
27
  data: []
28
28
  },
29
- default_card: nil
29
+ default_source: nil
30
30
  }.merge(params)
31
31
  end
32
32
 
@@ -42,9 +42,10 @@ module StripeMock
42
42
  currency: "usd",
43
43
  refunded: false,
44
44
  fee: 0,
45
+ status: 'succeeded',
45
46
  fee_details: [
46
47
  ],
47
- card: {
48
+ source: {
48
49
  object: "card",
49
50
  last4: "4242",
50
51
  type: "Visa",
@@ -199,7 +200,7 @@ module StripeMock
199
200
  period_start: 1349738950,
200
201
  lines: {
201
202
  object: "list",
202
- count: lines.count,
203
+ total_count: lines.count,
203
204
  url: "/v1/invoices/#{in_id}/lines",
204
205
  data: lines
205
206
  },
@@ -363,6 +364,7 @@ module StripeMock
363
364
  end
364
365
 
365
366
  def self.mock_transfer(params={})
367
+ id = params[:id] || 'tr_test_transfer'
366
368
  {
367
369
  :status => 'pending',
368
370
  :amount => 100,
@@ -375,12 +377,19 @@ module StripeMock
375
377
  :recipient => 'test_recipient',
376
378
  :fee => 0,
377
379
  :fee_details => [],
378
- :id => "tr_test_transfer",
380
+ :id => id,
379
381
  :livemode => false,
380
382
  :currency => "usd",
381
383
  :object => "transfer",
382
384
  :date => 1304114826,
383
385
  :description => "Transfer description",
386
+ :reversed => false,
387
+ :reversals => {
388
+ :object => "list",
389
+ :total_count => 0,
390
+ :has_more => false,
391
+ :url => "/v1/transfers/#{id}/reversals"
392
+ },
384
393
  }.merge(params)
385
394
  end
386
395
 
@@ -20,6 +20,7 @@ module StripeMock
20
20
 
21
21
  include StripeMock::RequestHandlers::Charges
22
22
  include StripeMock::RequestHandlers::Cards
23
+ include StripeMock::RequestHandlers::Sources
23
24
  include StripeMock::RequestHandlers::Subscriptions # must be before Customers
24
25
  include StripeMock::RequestHandlers::Customers
25
26
  include StripeMock::RequestHandlers::Coupons
@@ -3,44 +3,22 @@ module StripeMock
3
3
  module Cards
4
4
 
5
5
  def Cards.included(klass)
6
- klass.add_handler 'get /v1/customers/(.*)/cards', :retrieve_cards
7
- klass.add_handler 'post /v1/customers/(.*)/cards', :create_card
8
- klass.add_handler 'get /v1/customers/(.*)/cards/(.*)', :retrieve_card
9
- klass.add_handler 'delete /v1/customers/(.*)/cards/(.*)', :delete_card
10
- klass.add_handler 'post /v1/customers/(.*)/cards/(.*)', :update_card
11
6
  klass.add_handler 'get /v1/recipients/(.*)/cards', :retrieve_recipient_cards
12
7
  klass.add_handler 'get /v1/recipients/(.*)/cards/(.*)', :retrieve_recipient_card
13
8
  klass.add_handler 'post /v1/recipients/(.*)/cards', :create_recipient_card
14
9
  klass.add_handler 'delete /v1/recipients/(.*)/cards/(.*)', :delete_recipient_card
15
10
  end
16
11
 
17
- def create_card(route, method_url, params, headers)
18
- route =~ method_url
19
- add_card_to(:customer, $1, params, customers)
20
- end
21
-
22
12
  def create_recipient_card(route, method_url, params, headers)
23
13
  route =~ method_url
24
14
  add_card_to(:recipient, $1, params, recipients)
25
15
  end
26
16
 
27
- def retrieve_cards(route, method_url, params, headers)
28
- route =~ method_url
29
- retrieve_object_cards(:customer, $1, customers)
30
- end
31
-
32
17
  def retrieve_recipient_cards(route, method_url, params, headers)
33
18
  route =~ method_url
34
19
  retrieve_object_cards(:recipient, $1, recipients)
35
20
  end
36
21
 
37
- def retrieve_card(route, method_url, params, headers)
38
- route =~ method_url
39
- customer = assert_existence :customer, $1, customers[$1]
40
-
41
- assert_existence :card, $2, get_card(customer, $2)
42
- end
43
-
44
22
  def retrieve_recipient_card(route, method_url, params, headers)
45
23
  route =~ method_url
46
24
  recipient = assert_existence :recipient, $1, recipients[$1]
@@ -48,25 +26,10 @@ module StripeMock
48
26
  assert_existence :card, $2, get_card(recipient, $2, "Recipient")
49
27
  end
50
28
 
51
- def delete_card(route, method_url, params, headers)
52
- route =~ method_url
53
- delete_card_from(:customer, $1, $2, customers)
54
- end
55
-
56
29
  def delete_recipient_card(route, method_url, params, headers)
57
30
  route =~ method_url
58
31
  delete_card_from(:recipient, $1, $2, recipients)
59
32
  end
60
-
61
- def update_card(route, method_url, params, headers)
62
- route =~ method_url
63
- customer = assert_existence :customer, $1, customers[$1]
64
-
65
- card = assert_existence :card, $2, get_card(customer, $2)
66
- card.merge!(params)
67
- card
68
- end
69
-
70
33
  end
71
34
  end
72
35
  end
@@ -14,16 +14,16 @@ module StripeMock
14
14
  def new_charge(route, method_url, params, headers)
15
15
  id = new_id('ch')
16
16
 
17
- if params[:card] && params[:card].is_a?(String)
17
+ if params[:source] && params[:source].is_a?(String)
18
18
  # if a customer is provided, the card parameter is assumed to be the actual
19
19
  # card id, not a token. in this case we'll find the card in the customer
20
20
  # object and return that.
21
21
  if params[:customer]
22
- params[:card] = get_card(customers[params[:customer]], params[:card])
22
+ params[:source] = get_card(customers[params[:customer]], params[:source])
23
23
  else
24
- params[:card] = get_card_by_token(params[:card])
24
+ params[:source] = get_card_by_token(params[:source])
25
25
  end
26
- elsif params[:card] && params[:card][:id]
26
+ elsif params[:source] && params[:source][:id]
27
27
  raise Stripe::InvalidRequestError.new("Invalid token id: #{params[:card]}", 'card', 400)
28
28
  end
29
29
 
@@ -12,20 +12,20 @@ module StripeMock
12
12
 
13
13
  def new_customer(route, method_url, params, headers)
14
14
  params[:id] ||= new_id('cus')
15
- cards = []
15
+ sources = []
16
16
 
17
- if params[:card]
18
- cards << get_card_by_token(params.delete(:card))
19
- params[:default_card] = cards.first[:id]
17
+ if params[:source]
18
+ sources << get_card_by_token(params.delete(:source))
19
+ params[:default_source] = sources.first[:id]
20
20
  end
21
21
 
22
- customers[ params[:id] ] = Data.mock_customer(cards, params)
22
+ customers[ params[:id] ] = Data.mock_customer(sources, params)
23
23
 
24
24
  if params[:plan]
25
25
  plan_id = params[:plan].to_s
26
26
  plan = assert_existence :plan, plan_id, plans[plan_id]
27
27
 
28
- if params[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
28
+ if params[:default_source].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
29
29
  raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
30
30
  end
31
31
 
@@ -44,10 +44,10 @@ module StripeMock
44
44
  cus = assert_existence :customer, $1, customers[$1]
45
45
  cus.merge!(params)
46
46
 
47
- if params[:card]
48
- new_card = get_card_by_token(params.delete(:card))
47
+ if params[:source]
48
+ new_card = get_card_by_token(params.delete(:source))
49
49
  add_card_to_object(:customer, new_card, cus, true)
50
- cus[:default_card] = new_card[:id]
50
+ cus[:default_source] = new_card[:id]
51
51
  end
52
52
 
53
53
  cus
@@ -3,7 +3,8 @@ module StripeMock
3
3
  module Helpers
4
4
 
5
5
  def get_card(object, card_id, class_name='Customer')
6
- card = object[:cards][:data].find{|cc| cc[:id] == card_id }
6
+ cards = object[:cards] || object[:sources]
7
+ card = cards[:data].find{|cc| cc[:id] == card_id }
7
8
  if card.nil?
8
9
  msg = "#{class_name} #{object[:id]} does not have card #{card_id}"
9
10
  raise Stripe::InvalidRequestError.new(msg, 'card', 404)
@@ -13,23 +14,28 @@ module StripeMock
13
14
 
14
15
  def add_card_to_object(type, card, object, replace_current=false)
15
16
  card[type] = object[:id]
17
+ cards_or_sources = object[:cards] || object[:sources]
18
+
19
+ is_customer = object.has_key?(:sources)
16
20
 
17
21
  if replace_current
18
- object[:cards][:data].delete_if {|card| card[:id] == object[:default_card]}
19
- object[:default_card] = card[:id]
22
+ cards_or_sources[:data].delete_if {|card| card[:id] == object[:default_card]}
23
+ object[:default_card] = card[:id] unless is_customer
24
+ object[:default_source] = card[:id] if is_customer
20
25
  else
21
- object[:cards][:total_count] += 1
26
+ cards_or_sources[:total_count] += 1
22
27
  end
23
28
 
24
- object[:default_card] = card[:id] unless object[:default_card]
25
- object[:cards][:data] << card
29
+ object[:default_card] = card[:id] if !is_customer && object[:default_card].nil?
30
+ object[:default_source] = card[:id] if is_customer && object[:default_source].nil?
31
+ cards_or_sources[:data] << card
26
32
 
27
33
  card
28
34
  end
29
35
 
30
36
  def retrieve_object_cards(type, type_id, objects)
31
37
  resource = assert_existence type, type_id, objects[type_id]
32
- cards = resource[:cards]
38
+ cards = resource[:cards] || resource[:sources]
33
39
 
34
40
  Data.mock_list_object(cards[:data])
35
41
  end
@@ -40,17 +46,22 @@ module StripeMock
40
46
  assert_existence :card, card_id, get_card(resource, card_id)
41
47
 
42
48
  card = { id: card_id, deleted: true }
43
- resource[:cards][:data].reject!{|cc|
49
+ cards_or_sources = resource[:cards] || resource[:sources]
50
+ cards_or_sources[:data].reject!{|cc|
44
51
  cc[:id] == card[:id]
45
52
  }
46
- resource[:default_card] = resource[:cards][:data].count > 0 ? resource[:cards][:data].first[:id] : nil
53
+
54
+ is_customer = resource.has_key?(:sources)
55
+ new_default = cards_or_sources[:data].count > 0 ? cards_or_sources[:data].first[:id] : nil
56
+ resource[:default_card] = new_default unless is_customer
57
+ resource[:default_source] = new_default if is_customer
47
58
  card
48
59
  end
49
60
 
50
61
  def add_card_to(type, type_id, params, objects)
51
62
  resource = assert_existence type, type_id, objects[type_id]
52
63
 
53
- card = card_from_params(params[:card])
64
+ card = card_from_params(params[:card] || params[:source])
54
65
  add_card_to_object(type, card, resource)
55
66
  end
56
67
 
@@ -68,7 +79,6 @@ module StripeMock
68
79
  card = get_card_by_token(attrs_or_token)
69
80
  validate_card(card)
70
81
  end
71
-
72
82
  end
73
83
  end
74
84
  end
@@ -0,0 +1,46 @@
1
+ module StripeMock
2
+ module RequestHandlers
3
+ module Sources
4
+
5
+ def Sources.included(klass)
6
+ klass.add_handler 'get /v1/customers/(.*)/sources', :retrieve_sources
7
+ klass.add_handler 'post /v1/customers/(.*)/sources', :create_source
8
+ klass.add_handler 'get /v1/customers/(.*)/sources/(.*)', :retrieve_source
9
+ klass.add_handler 'delete /v1/customers/(.*)/sources/(.*)', :delete_source
10
+ klass.add_handler 'post /v1/customers/(.*)/sources/(.*)', :update_source
11
+ end
12
+
13
+ def create_source(route, method_url, params, headers)
14
+ route =~ method_url
15
+ add_card_to(:customer, $1, params, customers)
16
+ end
17
+
18
+ def retrieve_sources(route, method_url, params, headers)
19
+ route =~ method_url
20
+ retrieve_object_cards(:customer, $1, customers)
21
+ end
22
+
23
+ def retrieve_source(route, method_url, params, headers)
24
+ route =~ method_url
25
+ customer = assert_existence :customer, $1, customers[$1]
26
+
27
+ assert_existence :card, $2, get_card(customer, $2)
28
+ end
29
+
30
+ def delete_source(route, method_url, params, headers)
31
+ route =~ method_url
32
+ delete_card_from(:customer, $1, $2, customers)
33
+ end
34
+
35
+ def update_source(route, method_url, params, headers)
36
+ route =~ method_url
37
+ customer = assert_existence :customer, $1, customers[$1]
38
+
39
+ card = assert_existence :card, $2, get_card(customer, $2)
40
+ card.merge!(params)
41
+ card
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -17,10 +17,10 @@ module StripeMock
17
17
  plan_id = params[:plan]
18
18
  plan = assert_existence :plan, plan_id, plans[plan_id]
19
19
 
20
- if params[:card]
21
- new_card = get_card_by_token(params.delete(:card))
20
+ if params[:source]
21
+ new_card = get_card_by_token(params.delete(:source))
22
22
  add_card_to_object(:customer, new_card, customer)
23
- customer[:default_card] = new_card[:id]
23
+ customer[:default_source] = new_card[:id]
24
24
  end
25
25
 
26
26
  # Ensure customer has card to charge if plan has no trial and is not free
@@ -54,10 +54,10 @@ module StripeMock
54
54
  subscription = get_customer_subscription(customer, $2)
55
55
  assert_existence :subscription, $2, subscription
56
56
 
57
- if params[:card]
58
- new_card = get_card_by_token(params.delete(:card))
57
+ if params[:source]
58
+ new_card = get_card_by_token(params.delete(:source))
59
59
  add_card_to_object(:customer, new_card, customer)
60
- customer[:default_card] = new_card[:id]
60
+ customer[:default_source] = new_card[:id]
61
61
  end
62
62
 
63
63
  # expand the plan for addition to the customer object
@@ -111,7 +111,7 @@ module StripeMock
111
111
  private
112
112
 
113
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?
114
+ if customer[:default_source].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0 && plan[:trial_end].nil? && params[:trial_end].nil?
115
115
  raise Stripe::InvalidRequestError.new('You must supply a valid card xoxo', nil, 400)
116
116
  end
117
117
  end
@@ -14,12 +14,12 @@ module StripeMock
14
14
 
15
15
  cus_id = params[:customer]
16
16
 
17
- if cus_id && params[:card]
17
+ if cus_id && params[:source]
18
18
  customer = assert_existence :customer, cus_id, customers[cus_id]
19
19
 
20
20
  # params[:card] is an id; grab it from the db
21
- customer_card = get_card(customer, params[:card])
22
- assert_existence :card, params[:card], customer_card
21
+ customer_card = get_card(customer, params[:source])
22
+ assert_existence :card, params[:source], customer_card
23
23
  elsif params[:card]
24
24
  # params[:card] is a hash of cc info; "Sanitize" the card number
25
25
  params[:card][:fingerprint] = StripeMock::Util.fingerprint(params[:card][:number])
@@ -27,7 +27,7 @@ module StripeMock
27
27
  customer_card = params[:card]
28
28
  else
29
29
  customer = assert_existence :customer, cus_id, customers[cus_id]
30
- customer_card = get_card(customer, customer[:default_card])
30
+ customer_card = get_card(customer, customer[:default_source])
31
31
  end
32
32
 
33
33
  token_id = generate_card_token(customer_card)