Skip to content

Commit f773c1f

Browse files
committed
Merge pull request stripe-ruby-mock#45 from skalb/cancel-sub
Set status and cancel_at_period_end instead of deleting a subscription.
2 parents 5e6749b + 0b9dd20 commit f773c1f

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

lib/stripe_mock/request_handlers/customers.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,19 @@ def cancel_subscription(route, method_url, params, headers)
6868
sub = customer[:subscription]
6969
assert_existance nil, nil, sub, "No active subscription for customer: #{$1}"
7070

71-
customer[:subscription] = nil
72-
7371
plan = plans[ sub[:plan][:id] ]
7472
assert_existance :plan, params[:plan], plan
7573

76-
Data.mock_delete_subscription(id: sub[:id])
74+
if params[:at_period_end] == true
75+
status = 'active'
76+
cancel_at_period_end = true
77+
else
78+
status = 'canceled'
79+
cancel_at_period_end = false
80+
end
81+
82+
sub = Data.mock_subscription id: sub[:id], plan: plan, customer: $1, status: status, cancel_at_period_end: cancel_at_period_end
83+
customer[:subscription] = sub
7784
end
7885

7986
def update_customer(route, method_url, params, headers)

spec/shared_stripe_examples/customer_examples.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,33 @@
201201
end
202202

203203
it "cancels a stripe customer's subscription" do
204-
plan = Stripe::Plan.create(id: 'the truth')
204+
Stripe::Plan.create(id: 'the truth')
205205
customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
206206
sub = customer.update_subscription({ :plan => 'the truth' })
207207

208208
result = customer.cancel_subscription
209-
expect(result.deleted).to eq(true)
209+
expect(result.status).to eq('canceled')
210+
expect(result.cancel_at_period_end).to be_false
210211
expect(result.id).to eq(sub.id)
212+
211213
customer = Stripe::Customer.retrieve('test_customer_sub')
212-
expect(customer.subscription).to be_nil
214+
expect(customer.subscription).to_not be_nil
215+
expect(customer.subscription.id).to eq(result.id)
216+
end
217+
218+
it "cancels a stripe customer's subscription at period end" do
219+
Stripe::Plan.create(id: 'the truth')
220+
customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
221+
sub = customer.update_subscription({ :plan => 'the truth' })
222+
223+
result = customer.cancel_subscription(at_period_end: true)
224+
expect(result.status).to eq('active')
225+
expect(result.cancel_at_period_end).to be_true
226+
expect(result.id).to eq(sub.id)
227+
228+
customer = Stripe::Customer.retrieve('test_customer_sub')
229+
expect(customer.subscription).to_not be_nil
230+
expect(customer.subscription.id).to eq(result.id)
213231
end
214232

215233
it "cannot update to a plan that does not exist" do

0 commit comments

Comments
 (0)