stripe-ruby-mock 1.8.7.7 → 1.10.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -2,6 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
+ - 2.1.0
5
6
  before_script:
6
7
  - "sudo touch /var/log/stripe-mock-server.log"
7
8
  - "sudo chown travis /var/log/stripe-mock-server.log"
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  In your gemfile:
10
10
 
11
- gem 'stripe-ruby-mock', '~> 1.8.7.7'
11
+ gem 'stripe-ruby-mock', '>= 1.10.1.0'
12
12
 
13
13
  ## Features
14
14
 
@@ -282,7 +282,8 @@ If you always want debug to be on (it's quite verbose), you should put this in a
282
282
 
283
283
  To run the tests:
284
284
 
285
- $ rspec
285
+ $ bundle install
286
+ $ bundle exec rspec
286
287
 
287
288
  Patches are welcome and greatly appreciated! If you're contributing to fix a problem,
288
289
  be sure to write tests that illustrate the problem being fixed.
data/lib/stripe_mock.rb CHANGED
@@ -37,6 +37,7 @@ require 'stripe_mock/request_handlers/invoices.rb'
37
37
  require 'stripe_mock/request_handlers/invoice_items.rb'
38
38
  require 'stripe_mock/request_handlers/plans.rb'
39
39
  require 'stripe_mock/request_handlers/recipients.rb'
40
+ require 'stripe_mock/request_handlers/subscriptions.rb'
40
41
  require 'stripe_mock/request_handlers/tokens.rb'
41
42
  require 'stripe_mock/instance'
42
43
 
@@ -41,6 +41,9 @@ module StripeMock
41
41
  'charge.dispute.created',
42
42
  'charge.dispute.updated',
43
43
  'charge.dispute.closed',
44
+ 'customer.card.created',
45
+ 'customer.card.deleted',
46
+ 'customer.card.updated',
44
47
  'customer.created',
45
48
  'customer.updated',
46
49
  'customer.deleted',
@@ -12,7 +12,6 @@ module StripeMock
12
12
  id: cus_id,
13
13
  livemode: false,
14
14
  delinquent: false,
15
- subscription: nil,
16
15
  discount: nil,
17
16
  account_balance: 0,
18
17
  cards: {
@@ -152,6 +151,7 @@ module StripeMock
152
151
  :current_period_start => 1308595038,
153
152
  :cancel_at_period_end => false,
154
153
  :canceled_at => nil,
154
+ :ended_at => nil,
155
155
  :start => 1308595038,
156
156
  :object => "subscription",
157
157
  :trial_start => 1308595038,
@@ -221,6 +221,15 @@ module StripeMock
221
221
  }
222
222
  end
223
223
 
224
+ def self.mock_subscriptions_array(params={})
225
+ {
226
+ :data => [],
227
+ :count => 0,
228
+ :object => "list",
229
+ :url => '/v1/customers/test_customer/subscriptions',
230
+ }.merge(params)
231
+ end
232
+
224
233
  def self.mock_plan(params={})
225
234
  {
226
235
  interval: "month",
@@ -17,6 +17,7 @@ module StripeMock
17
17
 
18
18
  include StripeMock::RequestHandlers::Charges
19
19
  include StripeMock::RequestHandlers::Cards
20
+ include StripeMock::RequestHandlers::Subscriptions # must be before Customers
20
21
  include StripeMock::RequestHandlers::Customers
21
22
  include StripeMock::RequestHandlers::Events
22
23
  include StripeMock::RequestHandlers::Invoices
@@ -27,7 +28,7 @@ module StripeMock
27
28
 
28
29
 
29
30
  attr_reader :bank_tokens, :charges, :customers, :events,
30
- :invoices, :plans, :recipients
31
+ :invoices, :plans, :recipients, :subscriptions
31
32
 
32
33
  attr_accessor :error_queue, :debug, :strict
33
34
 
@@ -40,6 +41,7 @@ module StripeMock
40
41
  @invoices = {}
41
42
  @plans = {}
42
43
  @recipients = {}
44
+ @subscriptions = {}
43
45
 
44
46
  @debug = false
45
47
  @error_queue = ErrorQueue.new
@@ -128,6 +130,27 @@ module StripeMock
128
130
  card
129
131
  end
130
132
 
133
+ def get_customer_subscription(customer, sub_id)
134
+ customer[:subscriptions][:data].find{|sub| sub[:id] == sub_id }
135
+ end
136
+
137
+ def add_subscription_to_customer(plan, cus)
138
+ params = { id: new_id('su'), plan: plan, customer: cus[:id] }
139
+
140
+ if plan[:trial_period_days].nil?
141
+ params.merge!({status: 'active', trial_start: nil, trial_end: nil})
142
+ else
143
+ params.merge!({status: 'trialing', trial_start: Time.now.to_i, trial_end: (Time.now + plan[:trial_period_days]).to_i })
144
+ end
145
+
146
+ subscription = Data.mock_subscription params
147
+
148
+ cus[:subscriptions] = Data.mock_subscriptions_array(url: "/v1/customers/#{cus[:id]}/subscriptions") unless cus[:subscriptions]
149
+ cus[:subscriptions][:count] = (cus[:subscriptions][:count] ? cus[:subscriptions][:count]+1 : 1 )
150
+ cus[:subscriptions][:data] << subscription
151
+ subscription
152
+ end
153
+
131
154
  private
132
155
 
133
156
  def assert_existance(type, id, obj, message=nil)
@@ -4,8 +4,6 @@ module StripeMock
4
4
 
5
5
  def Customers.included(klass)
6
6
  klass.add_handler 'post /v1/customers', :new_customer
7
- klass.add_handler 'post /v1/customers/(.*)/subscription', :update_subscription
8
- klass.add_handler 'delete /v1/customers/(.*)/subscription', :cancel_subscription
9
7
  klass.add_handler 'post /v1/customers/(.*)', :update_customer
10
8
  klass.add_handler 'get /v1/customers/(.*)', :get_customer
11
9
  klass.add_handler 'delete /v1/customers/(.*)', :delete_customer
@@ -20,6 +18,9 @@ module StripeMock
20
18
  params[:default_card] = cards.first[:id]
21
19
  end
22
20
 
21
+ params[:subscriptions] = Data.mock_subscriptions_array(url: "/v1/customers/#{params[:id]}/subscriptions")
22
+ customers[ params[:id] ] = Data.mock_customer(cards, params)
23
+
23
24
  if params[:plan]
24
25
  plan = plans[ params[:plan] ]
25
26
  assert_existance :plan, params[:plan], plan
@@ -28,63 +29,10 @@ module StripeMock
28
29
  raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
29
30
  end
30
31
 
31
- sub = Data.mock_subscription id: new_id('su'), plan: plan, customer: params[:id]
32
- params[:subscription] = sub
33
- end
34
-
35
- customers[ params[:id] ] = Data.mock_customer(cards, params)
36
- end
37
-
38
- def update_subscription(route, method_url, params, headers)
39
- route =~ method_url
40
-
41
- customer = customers[$1]
42
- assert_existance :customer, $1, customer
43
-
44
- plan_id = params.delete(:plan)
45
- plan = plans[plan_id]
46
- assert_existance :plan, plan_id, plan
47
-
48
- if params[:card]
49
- new_card = get_card_by_token(params.delete(:card))
50
- add_card_to_customer(new_card, customer)
51
- customer[:default_card] = new_card[:id]
52
- end
53
-
54
- # Ensure customer has card to charge if plan has no trial and is not free
55
- if customer[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
56
- raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
57
- end
58
-
59
- # Update plan data
60
- plan.merge!(params)
61
-
62
- sub = Data.mock_subscription id: new_id('su'), plan: plan, customer: $1
63
- customer[:subscription] = sub
64
- end
65
-
66
- def cancel_subscription(route, method_url, params, headers)
67
- route =~ method_url
68
-
69
- customer = customers[$1]
70
- assert_existance :customer, $1, customer
71
-
72
- sub = customer[:subscription]
73
- assert_existance nil, nil, sub, "No active subscription for customer: #{$1}"
74
-
75
- plan = plans[ sub[:plan][:id] ]
76
- assert_existance :plan, params[:plan], plan
77
-
78
- if params[:at_period_end] == true
79
- status = 'active'
80
- cancel_at_period_end = true
81
- else
82
- status = 'canceled'
83
- cancel_at_period_end = false
32
+ add_subscription_to_customer(plan, customers[params[:id]] )
84
33
  end
85
34
 
86
- sub = Data.mock_subscription id: sub[:id], plan: plan, customer: $1, status: status, cancel_at_period_end: cancel_at_period_end
87
- customer[:subscription] = sub
35
+ customers[ params[:id] ]
88
36
  end
89
37
 
90
38
  def update_customer(route, method_url, params, headers)
@@ -0,0 +1,127 @@
1
+ module StripeMock
2
+ module RequestHandlers
3
+ module Subscriptions
4
+
5
+ def Subscriptions.included(klass)
6
+ klass.add_handler 'get /v1/customers/(.*)/subscriptions', :retrieve_subscriptions
7
+ klass.add_handler 'post /v1/customers/(.*)/subscriptions', :create_subscription
8
+ klass.add_handler 'get /v1/customers/(.*)/subscriptions/(.*)', :retrieve_subscription
9
+ klass.add_handler 'post /v1/customers/(.*)/subscriptions/(.*)', :update_subscription
10
+ klass.add_handler 'delete /v1/customers/(.*)/subscriptions/(.*)', :cancel_subscription
11
+ end
12
+
13
+ def create_subscription(route, method_url, params, headers)
14
+ route =~ method_url
15
+
16
+ customer = customers[$1]
17
+ assert_existance :customer, $1, customer
18
+
19
+ plan = plans[params[:plan]]
20
+ assert_existance :plan, params[:plan], plan
21
+
22
+ # Ensure customer has card to charge if plan has no trial and is not free
23
+ verify_card_present(customer, plan)
24
+
25
+ subscription = add_subscription_to_customer(plan, customer)
26
+
27
+ # oddly, subscription returned from 'create_subscription' does not expand plan
28
+ subscription.merge(plan: params[:plan])
29
+ end
30
+
31
+ def retrieve_subscription(route, method_url, params, headers)
32
+ route =~ method_url
33
+
34
+ customer = customers[$1]
35
+ assert_existance :customer, $1, customer
36
+ subscription = get_customer_subscription(customer, $2)
37
+ assert_existance :subscription, $2, subscription
38
+
39
+ subscription
40
+ end
41
+
42
+ def retrieve_subscriptions(route, method_url, params, headers)
43
+ route =~ method_url
44
+
45
+ customer = customers[$1]
46
+ assert_existance :customer, $1, customer
47
+
48
+ subscription_list = Data.mock_subscriptions_array url: "/v1/customers/#{customer[:id]}/subscriptions", count: customer[:subscriptions][:data].length
49
+ customer.subscriptions.each do |subscription|
50
+ subscription_list[:data] << subscription
51
+ end
52
+ subscription_list
53
+ end
54
+
55
+ def update_subscription(route, method_url, params, headers)
56
+ route =~ method_url
57
+
58
+ customer = customers[$1]
59
+ assert_existance :customer, $1, customer
60
+ subscription = get_customer_subscription(customer, $2)
61
+ assert_existance :subscription, $2, subscription
62
+
63
+ if params[:card]
64
+ new_card = get_card_by_token(params.delete(:card))
65
+ add_card_to_customer(new_card, customer)
66
+ customer[:default_card] = new_card[:id]
67
+ end
68
+
69
+ # expand the plan for addition to the customer object
70
+ if params[:plan]
71
+ plan_name = params[:plan]
72
+ plan = plans[plan_name]
73
+ assert_existance :plan, params[:plan], plan
74
+ params[:plan] = plan
75
+ end
76
+
77
+ # Ensure customer has card to charge if plan has no trial and is not free
78
+ verify_card_present(customer, plan)
79
+
80
+ subscription.merge!(params)
81
+
82
+ # delete the old subscription, replace with the new subscription
83
+ customer[:subscriptions][:data].reject! { |sub| sub[:id] == subscription[:id] }
84
+ customer[:subscriptions][:data] << subscription
85
+
86
+ # oddly, subscription returned from 'create_subscription' does not expand plan
87
+ subscription.merge(plan: plan_name)
88
+ end
89
+
90
+ def cancel_subscription(route, method_url, params, headers)
91
+ route =~ method_url
92
+
93
+ customer = customers[$1]
94
+ assert_existance :customer, $1, customer
95
+ subscription = get_customer_subscription(customer, $2)
96
+ assert_existance :subscription, $2, subscription
97
+
98
+ cancel_params = { canceled_at: Time.now.to_i }
99
+ if params[:at_period_end] == true
100
+ cancel_params[:cancel_at_period_end] = true
101
+ else
102
+ cancel_params[:status] = "canceled"
103
+ cancel_params[:cancel_at_period_end] = false
104
+ cancel_params[:ended_at] = Time.now.to_i
105
+ end
106
+
107
+ subscription.merge!(cancel_params)
108
+
109
+ customer[:subscriptions][:data].reject!{|sub|
110
+ sub[:id] == subscription[:id]
111
+ }
112
+
113
+ customer[:subscriptions][:data] << subscription
114
+ subscription
115
+ end
116
+
117
+ private
118
+
119
+ def verify_card_present(customer, plan)
120
+ if customer[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
121
+ raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
122
+ end
123
+ end
124
+
125
+ end
126
+ end
127
+ end
@@ -1,4 +1,4 @@
1
1
  module StripeMock
2
2
  # stripe-ruby-mock version
3
- VERSION = "1.8.7.7"
3
+ VERSION = "1.10.1.0"
4
4
  end
@@ -0,0 +1,30 @@
1
+ {
2
+ "created": 1326853478,
3
+ "livemode": false,
4
+ "id": "evt_00000000000000",
5
+ "type": "customer.card.created",
6
+ "object": "event",
7
+ "data": {
8
+ "object": {
9
+ "id": "card_VALID",
10
+ "object": "card",
11
+ "last4": "4242",
12
+ "type": "Visa",
13
+ "exp_month": 3,
14
+ "exp_year": 2020,
15
+ "fingerprint": "wXWJT135mEK107G8",
16
+ "customer": "cus_VALID",
17
+ "country": "US",
18
+ "name": "Testy Tester",
19
+ "address_line1": null,
20
+ "address_line2": null,
21
+ "address_city": null,
22
+ "address_state": null,
23
+ "address_zip": null,
24
+ "address_country": null,
25
+ "cvc_check": "pass",
26
+ "address_line1_check": null,
27
+ "address_zip_check": null
28
+ }
29
+ }
30
+ }
@@ -0,0 +1,30 @@
1
+ {
2
+ "created": 1326853478,
3
+ "livemode": false,
4
+ "id": "evt_00000000000000",
5
+ "type": "customer.card.deleted",
6
+ "object": "event",
7
+ "data": {
8
+ "object": {
9
+ "id": "card_VALID",
10
+ "object": "card",
11
+ "last4": "4242",
12
+ "type": "Visa",
13
+ "exp_month": 3,
14
+ "exp_year": 2020,
15
+ "fingerprint": "wXWJT135mEK107G8",
16
+ "customer": "cus_VALID",
17
+ "country": "US",
18
+ "name": "Testy Tester",
19
+ "address_line1": null,
20
+ "address_line2": null,
21
+ "address_city": null,
22
+ "address_state": null,
23
+ "address_zip": null,
24
+ "address_country": null,
25
+ "cvc_check": "pass",
26
+ "address_line1_check": null,
27
+ "address_zip_check": null
28
+ }
29
+ }
30
+ }
@@ -0,0 +1,34 @@
1
+ {
2
+ "created": 1326853478,
3
+ "livemode": false,
4
+ "id": "evt_00000000000000",
5
+ "type": "customer.card.updated",
6
+ "object": "event",
7
+ "data": {
8
+ "object": {
9
+ "id": "card_VALID",
10
+ "object": "card",
11
+ "last4": "4242",
12
+ "type": "Visa",
13
+ "exp_month": 3,
14
+ "exp_year": 2020,
15
+ "fingerprint": "wXWJT135mEK107G8",
16
+ "customer": "cus_VALID",
17
+ "country": "US",
18
+ "name": "Testy Tester",
19
+ "address_line1": null,
20
+ "address_line2": null,
21
+ "address_city": null,
22
+ "address_state": null,
23
+ "address_zip": null,
24
+ "address_country": null,
25
+ "cvc_check": "pass",
26
+ "address_line1_check": null,
27
+ "address_zip_check": null
28
+ },
29
+ "previous_attributes":
30
+ {
31
+ "name": "Testy Tester Jr."
32
+ }
33
+ }
34
+ }
@@ -11,7 +11,9 @@
11
11
  "period_start": 1378082075,
12
12
  "period_end": 1380674075,
13
13
  "lines": {
14
- "data": [
14
+ "invoiceitems": [],
15
+ "prorations": [],
16
+ "subscriptions": [
15
17
  {
16
18
  "id": "su_2hksGtIPylSBg2",
17
19
  "object": "line_item",
@@ -38,10 +40,7 @@
38
40
  },
39
41
  "description": null
40
42
  }
41
- ],
42
- "count": 1,
43
- "object": "list",
44
- "url": "/v1/invoices/in_2fzcb1D7guR4IE/lines"
43
+ ]
45
44
  },
46
45
  "subtotal": 1000,
47
46
  "total": 1000,
@@ -11,7 +11,9 @@
11
11
  "period_start": 1378082075,
12
12
  "period_end": 1380674075,
13
13
  "lines": {
14
- "data": [
14
+ "invoiceitems": [],
15
+ "prorations": [],
16
+ "subscriptions": [
15
17
  {
16
18
  "id": "su_2hkslKrnt23NBz",
17
19
  "object": "line_item",
@@ -38,10 +40,7 @@
38
40
  },
39
41
  "description": null
40
42
  }
41
- ],
42
- "count": 1,
43
- "object": "list",
44
- "url": "/v1/invoices/in_2fzcb1D7guR4IE/lines"
43
+ ]
45
44
  },
46
45
  "subtotal": 1000,
47
46
  "total": 1000,
@@ -11,7 +11,9 @@
11
11
  "period_start": 1378082075,
12
12
  "period_end": 1380674075,
13
13
  "lines": {
14
- "data": [
14
+ "invoiceitems": [],
15
+ "prorations": [],
16
+ "subscriptions": [
15
17
  {
16
18
  "id": "su_2hkscimMTuAjFP",
17
19
  "object": "line_item",
@@ -38,10 +40,7 @@
38
40
  },
39
41
  "description": null
40
42
  }
41
- ],
42
- "count": 1,
43
- "object": "list",
44
- "url": "/v1/invoices/in_2fzcb1D7guR4IE/lines"
43
+ ]
45
44
  },
46
45
  "subtotal": 1000,
47
46
  "total": 1000,
@@ -11,7 +11,9 @@
11
11
  "period_start": 1378082075,
12
12
  "period_end": 1380674075,
13
13
  "lines": {
14
- "data": [
14
+ "invoiceitems": [],
15
+ "prorations": [],
16
+ "subscriptions": [
15
17
  {
16
18
  "id": "su_2hkskhhOSfPr47",
17
19
  "object": "line_item",
@@ -38,10 +40,7 @@
38
40
  },
39
41
  "description": null
40
42
  }
41
- ],
42
- "count": 1,
43
- "object": "list",
44
- "url": "/v1/invoices/in_2fzcb1D7guR4IE/lines"
43
+ ]
45
44
  },
46
45
  "subtotal": 1000,
47
46
  "total": 1000,
@@ -62,9 +61,7 @@
62
61
  "application_fee": null
63
62
  },
64
63
  "previous_attributes": {
65
- "lines": [
66
-
67
- ]
64
+ "lines": []
68
65
  }
69
66
  }
70
67
  }
@@ -39,9 +39,12 @@ shared_examples 'Customer API' do
39
39
  customer = Stripe::Customer.create(id: 'test_cus_plan', card: 'tk', :plan => 'silver')
40
40
 
41
41
  customer = Stripe::Customer.retrieve('test_cus_plan')
42
- expect(customer.subscription).to_not be_nil
43
- expect(customer.subscription.plan.id).to eq('silver')
44
- expect(customer.subscription.customer).to eq(customer.id)
42
+ expect(customer.subscriptions.count).to eq(1)
43
+ expect(customer.subscriptions.data.length).to eq(1)
44
+
45
+ expect(customer.subscriptions).to_not be_nil
46
+ expect(customer.subscriptions.first.plan.id).to eq('silver')
47
+ expect(customer.subscriptions.first.customer).to eq(customer.id)
45
48
  end
46
49
 
47
50
  it 'cannot create a customer with a plan that does not exist' do
@@ -90,7 +93,8 @@ shared_examples 'Customer API' do
90
93
  expect(customer.id).to eq(original.id)
91
94
  expect(customer.email).to eq(original.email)
92
95
  expect(customer.default_card).to eq(original.default_card)
93
- expect(customer.subscription).to be_nil
96
+ expect(customer.subscriptions.count).to eq(0)
97
+ expect(customer.subscriptions.data).to be_empty
94
98
  end
95
99
 
96
100
  it "cannot retrieve a customer that doesn't exist" do
@@ -2,130 +2,344 @@ require 'spec_helper'
2
2
 
3
3
  shared_examples 'Customer Subscriptions' do
4
4
 
5
- it "updates a stripe customer's subscription" do
6
- plan = Stripe::Plan.create(id: 'silver')
7
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
8
- sub = customer.update_subscription({ :plan => 'silver' })
5
+ context "creating a new subscription" do
6
+ it "adds a new subscription to customer with none" do
7
+ plan = Stripe::Plan.create(id: 'silver', name: 'Silver Plan', amount: 4999)
8
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
9
9
 
10
- expect(sub.object).to eq('subscription')
11
- expect(sub.plan.id).to eq('silver')
12
- expect(sub.plan.to_hash).to eq(plan.to_hash)
10
+ expect(customer.subscriptions.data).to be_empty
11
+ expect(customer.subscriptions.count).to eq(0)
13
12
 
14
- customer = Stripe::Customer.retrieve('test_customer_sub')
15
- expect(customer.subscription).to_not be_nil
16
- expect(customer.subscription.id).to eq(sub.id)
17
- expect(customer.subscription.plan.id).to eq('silver')
18
- expect(customer.subscription.customer).to eq(customer.id)
19
- end
13
+ sub = customer.subscriptions.create({ :plan => 'silver' })
20
14
 
21
- it "updates plan details" do
22
- plan = Stripe::Plan.create(id: 'copper')
23
- customer = Stripe::Customer.create(id: 'test_customer_sub2', card: 'tk')
24
- sub = customer.update_subscription({ :plan => 'copper', :quantity => 3 })
15
+ expect(sub.object).to eq('subscription')
16
+ expect(sub.plan).to eq('silver')
25
17
 
26
- expect(sub.object).to eq('subscription')
27
- expect(sub.plan.id).to eq('copper')
28
- expect(sub.plan.quantity).to eq(3)
29
- end
18
+ customer = Stripe::Customer.retrieve('test_customer_sub')
19
+ expect(customer.subscriptions.data).to_not be_empty
20
+ expect(customer.subscriptions.count).to eq(1)
21
+ expect(customer.subscriptions.data.length).to eq(1)
30
22
 
31
- it "throws an error when subscribing a customer with no card" do
32
- plan = Stripe::Plan.create(id: 'enterprise', amount: 499)
33
- customer = Stripe::Customer.create(id: 'cardless')
23
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
24
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
25
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
34
26
 
35
- expect { customer.update_subscription({ :plan => 'enterprise' }) }.to raise_error {|e|
36
- expect(e).to be_a Stripe::InvalidRequestError
37
- expect(e.http_status).to eq(400)
38
- expect(e.message).to_not be_nil
39
- }
40
- end
27
+ end
28
+
29
+ it "adds additional subscription to customer with existing subscription" do
30
+ silver = Stripe::Plan.create(id: 'silver')
31
+ gold = Stripe::Plan.create(id: 'gold')
32
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: 'gold')
33
+
34
+ sub = customer.subscriptions.create({ :plan => 'silver' })
35
+
36
+ expect(sub.object).to eq('subscription')
37
+ expect(sub.plan).to eq('silver')
38
+
39
+ customer = Stripe::Customer.retrieve('test_customer_sub')
40
+ expect(customer.subscriptions.data).to_not be_empty
41
+ expect(customer.subscriptions.count).to eq(2)
42
+ expect(customer.subscriptions.data.length).to eq(2)
43
+
44
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(gold.to_hash)
45
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
46
+
47
+ expect(customer.subscriptions.data.last.id).to eq(sub.id)
48
+ expect(customer.subscriptions.data.last.plan.to_hash).to eq(silver.to_hash)
49
+ expect(customer.subscriptions.data.last.customer).to eq(customer.id)
50
+ end
51
+
52
+ it "throws an error when plan does not exist" do
53
+ customer = Stripe::Customer.create(id: 'cardless')
54
+
55
+ expect { customer.subscriptions.create({ :plan => 'gazebo' }) }.to raise_error {|e|
56
+ expect(e).to be_a Stripe::InvalidRequestError
57
+ expect(e.http_status).to eq(404)
58
+ expect(e.message).to_not be_nil
59
+ }
60
+
61
+ expect(customer.subscriptions.data).to be_empty
62
+ expect(customer.subscriptions.count).to eq(0)
63
+ end
64
+
65
+ it "throws an error when subscribing a customer with no card" do
66
+ plan = Stripe::Plan.create(id: 'enterprise', amount: 499)
67
+ customer = Stripe::Customer.create(id: 'cardless')
68
+
69
+ expect { customer.subscriptions.create({ :plan => 'enterprise' }) }.to raise_error {|e|
70
+ expect(e).to be_a Stripe::InvalidRequestError
71
+ expect(e.http_status).to eq(400)
72
+ expect(e.message).to_not be_nil
73
+ }
74
+
75
+ expect(customer.subscriptions.data).to be_empty
76
+ expect(customer.subscriptions.count).to eq(0)
77
+ end
78
+
79
+ it "subscribes a customer with no card to a plan with a free trial" do
80
+ plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
81
+ customer = Stripe::Customer.create(id: 'cardless')
41
82
 
42
- it "subscribes a customer with no card to a free plan" do
43
- plan = Stripe::Plan.create(id: 'free_tier', amount: 0)
44
- customer = Stripe::Customer.create(id: 'cardless')
45
- sub = customer.update_subscription({ :plan => 'free_tier' })
83
+ sub = customer.subscriptions.create({ :plan => 'trial' })
46
84
 
47
- expect(sub.object).to eq('subscription')
48
- expect(sub.plan.id).to eq('free_tier')
49
- expect(sub.plan.to_hash).to eq(plan.to_hash)
85
+ expect(sub.object).to eq('subscription')
86
+ expect(sub.plan).to eq('trial')
50
87
 
51
- customer = Stripe::Customer.retrieve('cardless')
52
- expect(customer.subscription).to_not be_nil
53
- expect(customer.subscription.id).to eq(sub.id)
54
- expect(customer.subscription.plan.id).to eq('free_tier')
55
- expect(customer.subscription.customer).to eq(customer.id)
88
+ customer = Stripe::Customer.retrieve('cardless')
89
+ expect(customer.subscriptions.data).to_not be_empty
90
+ expect(customer.subscriptions.count).to eq(1)
91
+ expect(customer.subscriptions.data.length).to eq(1)
92
+
93
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
94
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
95
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
96
+ end
97
+
98
+ it "subscribes a customer with no card to a free plan" do
99
+ plan = Stripe::Plan.create(id: 'free_tier', amount: 0)
100
+ customer = Stripe::Customer.create(id: 'cardless')
101
+
102
+ sub = customer.subscriptions.create({ :plan => 'free_tier' })
103
+
104
+ expect(sub.object).to eq('subscription')
105
+ expect(sub.plan).to eq('free_tier')
106
+
107
+ customer = Stripe::Customer.retrieve('cardless')
108
+ expect(customer.subscriptions.data).to_not be_empty
109
+ expect(customer.subscriptions.count).to eq(1)
110
+ expect(customer.subscriptions.data.length).to eq(1)
111
+
112
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
113
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
114
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
115
+ end
56
116
  end
57
117
 
58
- it "subscribes a customer with no card to a plan with a free trial" do
59
- plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
60
- customer = Stripe::Customer.create(id: 'cardless')
61
- sub = customer.update_subscription({ :plan => 'trial' })
118
+ context "updating a subscription" do
119
+
120
+ it "updates a stripe customer's existing subscription" do
121
+ silver = Stripe::Plan.create(id: 'silver')
122
+ gold = Stripe::Plan.create(id: 'gold')
123
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: 'silver')
124
+
125
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
126
+ sub.plan = 'gold'
127
+ sub.save
128
+
129
+ expect(sub.object).to eq('subscription')
130
+ expect(sub.plan).to eq('gold')
131
+
132
+ customer = Stripe::Customer.retrieve('test_customer_sub')
133
+ expect(customer.subscriptions.data).to_not be_empty
134
+ expect(customer.subscriptions.count).to eq(1)
135
+ expect(customer.subscriptions.data.length).to eq(1)
136
+
137
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
138
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(gold.to_hash)
139
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
140
+ end
141
+
142
+ it "throws an error when plan does not exist" do
143
+ free = Stripe::Plan.create(id: 'free', amount: 0)
144
+ customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
145
+
146
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
147
+ sub.plan = 'gazebo'
148
+
149
+ expect { sub.save }.to raise_error {|e|
150
+ expect(e).to be_a Stripe::InvalidRequestError
151
+ expect(e.http_status).to eq(404)
152
+ expect(e.message).to_not be_nil
153
+ }
154
+
155
+ customer = Stripe::Customer.retrieve('cardless')
156
+ expect(customer.subscriptions.count).to eq(1)
157
+ expect(customer.subscriptions.data.length).to eq(1)
158
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
159
+ end
160
+
161
+ it "throws an error when subscription does not exist" do
162
+ free = Stripe::Plan.create(id: 'free', amount: 0)
163
+ customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
164
+
165
+ expect { customer.subscriptions.retrieve("gazebo") }.to raise_error {|e|
166
+ expect(e).to be_a Stripe::InvalidRequestError
167
+ expect(e.http_status).to eq(404)
168
+ expect(e.message).to_not be_nil
169
+ }
62
170
 
63
- expect(sub.object).to eq('subscription')
64
- expect(sub.plan.id).to eq('trial')
65
- expect(sub.plan.to_hash).to eq(plan.to_hash)
171
+ customer = Stripe::Customer.retrieve('cardless')
172
+ expect(customer.subscriptions.count).to eq(1)
173
+ expect(customer.subscriptions.data.length).to eq(1)
174
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
175
+ end
66
176
 
67
- customer = Stripe::Customer.retrieve('cardless')
68
- expect(customer.subscription).to_not be_nil
69
- expect(customer.subscription.id).to eq(sub.id)
70
- expect(customer.subscription.plan.id).to eq('trial')
71
- expect(customer.subscription.customer).to eq(customer.id)
177
+ it "throws an error when updating a customer with no card" do
178
+ free = Stripe::Plan.create(id: 'free', amount: 0)
179
+ paid = Stripe::Plan.create(id: 'enterprise', amount: 499)
180
+ customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
181
+
182
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
183
+ sub.plan = 'enterprise'
184
+
185
+ expect { sub.save }.to raise_error {|e|
186
+ expect(e).to be_a Stripe::InvalidRequestError
187
+ expect(e.http_status).to eq(400)
188
+ expect(e.message).to_not be_nil
189
+ }
190
+
191
+ customer = Stripe::Customer.retrieve('cardless')
192
+ expect(customer.subscriptions.count).to eq(1)
193
+ expect(customer.subscriptions.data.length).to eq(1)
194
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
195
+ end
196
+
197
+ it "updates a customer with no card to a plan with a free trial" do
198
+ free = Stripe::Plan.create(id: 'free', amount: 0)
199
+ trial = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
200
+ customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
201
+
202
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
203
+ sub.plan = 'trial'
204
+ sub.save
205
+
206
+ expect(sub.object).to eq('subscription')
207
+ expect(sub.plan).to eq('trial')
208
+
209
+ customer = Stripe::Customer.retrieve('cardless')
210
+ expect(customer.subscriptions.data).to_not be_empty
211
+ expect(customer.subscriptions.count).to eq(1)
212
+ expect(customer.subscriptions.data.length).to eq(1)
213
+
214
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
215
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(trial.to_hash)
216
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
217
+ end
218
+
219
+ it "updates a customer with no card to a free plan" do
220
+ free = Stripe::Plan.create(id: 'free', amount: 0)
221
+ gratis = Stripe::Plan.create(id: 'gratis', amount: 0)
222
+ customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
223
+
224
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
225
+ sub.plan = 'gratis'
226
+ sub.save
227
+
228
+ expect(sub.object).to eq('subscription')
229
+ expect(sub.plan).to eq('gratis')
230
+
231
+ customer = Stripe::Customer.retrieve('cardless')
232
+ expect(customer.subscriptions.data).to_not be_empty
233
+ expect(customer.subscriptions.count).to eq(1)
234
+ expect(customer.subscriptions.data.length).to eq(1)
235
+
236
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
237
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(gratis.to_hash)
238
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
239
+ end
240
+
241
+ it "sets a card when updating a customer's subscription" do
242
+ free = Stripe::Plan.create(id: 'free', amount: 0)
243
+ paid = Stripe::Plan.create(id: 'paid', amount: 499)
244
+ customer = Stripe::Customer.create(id: 'test_customer_sub', plan: 'free')
245
+
246
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
247
+ sub.plan = 'paid'
248
+ sub.card = 'tk'
249
+ sub.save
250
+
251
+ customer = Stripe::Customer.retrieve('test_customer_sub')
252
+
253
+ expect(customer.cards.count).to eq(1)
254
+ expect(customer.cards.data.length).to eq(1)
255
+ expect(customer.default_card).to_not be_nil
256
+ expect(customer.default_card).to eq customer.cards.data.first.id
257
+ end
72
258
  end
73
259
 
74
- it "cancels a stripe customer's subscription" do
75
- Stripe::Plan.create(id: 'the truth')
76
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
77
- sub = customer.update_subscription({ :plan => 'the truth' })
260
+ context "cancelling a subscription" do
78
261
 
79
- result = customer.cancel_subscription
80
- expect(result.status).to eq('canceled')
81
- expect(result.cancel_at_period_end).to be_false
82
- expect(result.id).to eq(sub.id)
262
+ it "cancels a stripe customer's subscription" do
263
+ truth = Stripe::Plan.create(id: 'the truth')
264
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: "the truth")
83
265
 
84
- customer = Stripe::Customer.retrieve('test_customer_sub')
85
- expect(customer.subscription).to_not be_nil
86
- expect(customer.subscription.id).to eq(result.id)
266
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
267
+ result = sub.delete()
268
+
269
+ expect(result.status).to eq('canceled')
270
+ expect(result.cancel_at_period_end).to be_false
271
+ expect(result.id).to eq(sub.id)
272
+
273
+ customer = Stripe::Customer.retrieve('test_customer_sub')
274
+ expect(customer.subscriptions.data).to_not be_empty
275
+ expect(customer.subscriptions.count).to eq(1)
276
+ expect(customer.subscriptions.data.length).to eq(1)
277
+
278
+ expect(customer.subscriptions.data.first.status).to eq('canceled')
279
+ expect(customer.subscriptions.data.first.cancel_at_period_end).to be_false
280
+ expect(customer.subscriptions.data.first.ended_at).to_not be_nil
281
+ expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
282
+ end
283
+
284
+ it "cancels a stripe customer's subscription at period end" do
285
+ truth = Stripe::Plan.create(id: 'the_truth')
286
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: "the_truth")
287
+
288
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
289
+ result = sub.delete(at_period_end: true)
290
+
291
+ expect(result.status).to eq('active')
292
+ expect(result.cancel_at_period_end).to be_true
293
+ expect(result.id).to eq(sub.id)
294
+
295
+ customer = Stripe::Customer.retrieve('test_customer_sub')
296
+ expect(customer.subscriptions.data).to_not be_empty
297
+ expect(customer.subscriptions.count).to eq(1)
298
+ expect(customer.subscriptions.data.length).to eq(1)
299
+
300
+ expect(customer.subscriptions.data.first.status).to eq('active')
301
+ expect(customer.subscriptions.data.first.cancel_at_period_end).to be_true
302
+ expect(customer.subscriptions.data.first.ended_at).to be_nil
303
+ expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
304
+ end
87
305
  end
88
306
 
89
- it "cancels a stripe customer's subscription at period end" do
90
- Stripe::Plan.create(id: 'the truth')
91
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
92
- sub = customer.update_subscription({ :plan => 'the truth' })
307
+ it "doesn't change status of subscription when cancelling at period end" do
308
+ trial = Stripe::Plan.create(id: 'trial', trial_period_days: 14)
309
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: "trial")
310
+
311
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
312
+ result = sub.delete(at_period_end: true)
93
313
 
94
- result = customer.cancel_subscription(at_period_end: true)
95
- expect(result.status).to eq('active')
96
- expect(result.cancel_at_period_end).to be_true
97
- expect(result.id).to eq(sub.id)
314
+ expect(result.status).to eq('trialing')
98
315
 
99
316
  customer = Stripe::Customer.retrieve('test_customer_sub')
100
- expect(customer.subscription).to_not be_nil
101
- expect(customer.subscription.id).to eq(result.id)
102
- end
103
317
 
104
- it "cannot update to a plan that does not exist" do
105
- customer = Stripe::Customer.create(id: 'test_customer_sub')
106
- expect {
107
- customer.update_subscription(plan: 'imagination')
108
- }.to raise_error Stripe::InvalidRequestError
318
+ expect(customer.subscriptions.data.first.status).to eq('trialing')
109
319
  end
110
320
 
111
- it "cannot cancel a plan that does not exist" do
112
- customer = Stripe::Customer.create(id: 'test_customer_sub')
113
- expect {
114
- customer.cancel_subscription(plan: 'imagination')
115
- }.to raise_error Stripe::InvalidRequestError
116
- end
321
+ context "retrieve multiple subscriptions" do
117
322
 
118
- it "sets a card when updating a customer's subscription" do
119
- plan = Stripe::Plan.create(id: 'small')
120
- customer = Stripe::Customer.create(id: 'test_customer_sub')
121
- customer.update_subscription(card: 'tk', :plan => 'small')
323
+ it "retrieves a list of multiple subscriptions" do
324
+ free = Stripe::Plan.create(id: 'free', amount: 0)
325
+ paid = Stripe::Plan.create(id: 'paid', amount: 499)
326
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: "free")
327
+ customer.subscriptions.create({ :plan => 'paid' })
122
328
 
123
- customer = Stripe::Customer.retrieve('test_customer_sub')
329
+ customer = Stripe::Customer.retrieve('test_customer_sub')
330
+
331
+ list = customer.subscriptions
332
+
333
+ expect(list.object).to eq("list")
334
+ expect(list.count).to eq(2)
335
+ expect(list.data.length).to eq(2)
336
+
337
+ expect(list.data.first.object).to eq("subscription")
338
+ expect(list.data.first.plan.to_hash).to eq(free.to_hash)
124
339
 
125
- expect(customer.cards.count).to eq(1)
126
- expect(customer.cards.data.length).to eq(1)
127
- expect(customer.default_card).to_not be_nil
128
- expect(customer.default_card).to eq customer.cards.data.first.id
340
+ expect(list.data.last.object).to eq("subscription")
341
+ expect(list.data.last.plan.to_hash).to eq(paid.to_hash)
342
+ end
129
343
  end
130
344
 
131
345
  end
@@ -81,18 +81,18 @@ shared_examples 'Webhook Events API' do
81
81
  it "takes a hash and deep merges arrays in the data object" do
82
82
  event = StripeMock.mock_webhook_event('invoice.created', {
83
83
  :lines => {
84
- :data => [
84
+ :subscriptions => [
85
85
  { :amount => 555,
86
86
  :plan => { :id => 'wh_test' }
87
87
  }
88
88
  ]
89
89
  }
90
90
  })
91
- expect(event.data.object.lines.data.first.amount).to eq(555)
92
- expect(event.data.object.lines.data.first.plan.id).to eq('wh_test')
91
+ expect(event.data.object.lines.subscriptions.first.amount).to eq(555)
92
+ expect(event.data.object.lines.subscriptions.first.plan.id).to eq('wh_test')
93
93
  # Ensure data from invoice.created.json is still present
94
- expect(event.data.object.lines.data.first.type).to eq('subscription')
95
- expect(event.data.object.lines.data.first.plan.currency).to eq('usd')
94
+ expect(event.data.object.lines.subscriptions.first.type).to eq('subscription')
95
+ expect(event.data.object.lines.subscriptions.first.plan.currency).to eq('usd')
96
96
  end
97
97
 
98
98
  it "can generate all events" do
@@ -20,11 +20,11 @@ def it_behaves_like_stripe(&block)
20
20
  it_behaves_like 'Card API', &block
21
21
  it_behaves_like 'Charge API', &block
22
22
  it_behaves_like 'Customer API', &block
23
- it_behaves_like 'Customer Subscriptions', &block
24
23
  it_behaves_like 'Invoice API', &block
25
24
  it_behaves_like 'Invoice Item API', &block
26
25
  it_behaves_like 'Plan API', &block
27
26
  it_behaves_like 'Recipient API', &block
28
27
  it_behaves_like 'Stripe Error Mocking', &block
28
+ it_behaves_like 'Customer Subscriptions', &block
29
29
  it_behaves_like 'Webhook Events API', &block
30
30
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ['lib']
19
19
 
20
- gem.add_dependency 'stripe', '>= 1.8.4'
20
+ gem.add_dependency 'stripe', '>= 1.10.1'
21
21
  gem.add_dependency 'jimson-temp'
22
22
  gem.add_dependency 'dante', '>= 0.2.0'
23
23
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe-ruby-mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.7.7
4
+ version: 1.10.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-24 00:00:00.000000000 Z
12
+ date: 2014-02-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stripe
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.8.4
21
+ version: 1.10.1
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 1.8.4
29
+ version: 1.10.1
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: jimson-temp
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -151,6 +151,7 @@ files:
151
151
  - lib/stripe_mock/request_handlers/invoices.rb
152
152
  - lib/stripe_mock/request_handlers/plans.rb
153
153
  - lib/stripe_mock/request_handlers/recipients.rb
154
+ - lib/stripe_mock/request_handlers/subscriptions.rb
154
155
  - lib/stripe_mock/request_handlers/tokens.rb
155
156
  - lib/stripe_mock/server.rb
156
157
  - lib/stripe_mock/util.rb
@@ -166,6 +167,9 @@ files:
166
167
  - lib/stripe_mock/webhook_fixtures/charge.succeeded.json
167
168
  - lib/stripe_mock/webhook_fixtures/coupon.created.json
168
169
  - lib/stripe_mock/webhook_fixtures/coupon.deleted.json
170
+ - lib/stripe_mock/webhook_fixtures/customer.card.created.json
171
+ - lib/stripe_mock/webhook_fixtures/customer.card.deleted.json
172
+ - lib/stripe_mock/webhook_fixtures/customer.card.updated.json
169
173
  - lib/stripe_mock/webhook_fixtures/customer.created.json
170
174
  - lib/stripe_mock/webhook_fixtures/customer.deleted.json
171
175
  - lib/stripe_mock/webhook_fixtures/customer.discount.created.json