Skip to content

Commit fdfaece

Browse files
Merge pull request stripe-ruby-mock#590 from gkemmey/support-cancelling-at-period-end-by-updating-the-subscription
support cancelling at period end by updating the subscription
2 parents 17a0bf8 + 26915c4 commit fdfaece

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ def custom_subscription_params(plans, cus, options = {})
3232
params.merge!({ :plan => (plans.size == 1 ? plans.first : nil) })
3333
keys_to_merge = /application_fee_percent|quantity|metadata|tax_percent|billing|days_until_due/
3434
params.merge! options.select {|k,v| k =~ keys_to_merge}
35+
36+
if options[:cancel_at_period_end] == true
37+
params.merge!(cancel_at_period_end: true, canceled_at: now)
38+
elsif options[:cancel_at_period_end] == false
39+
params.merge!(cancel_at_period_end: false, canceled_at: nil)
40+
end
41+
3542
# TODO: Implement coupon logic
3643

3744
if (((plan && plan[:trial_period_days]) || 0) == 0 && options[:trial_end].nil?) || options[:trial_end] == "now"

spec/shared_stripe_examples/subscription_examples.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,52 @@ def gen_card_tk
971971
end
972972
end
973973

974+
it "supports 'cancelling' by updating cancel_at_period_end" do
975+
truth = stripe_helper.create_plan(id: 'the_truth')
976+
customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
977+
978+
sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
979+
result = Stripe::Subscription.update(sub.id, cancel_at_period_end: true)
980+
981+
expect(result.status).to eq('active')
982+
expect(result.cancel_at_period_end).to eq(true)
983+
expect(result.id).to eq(sub.id)
984+
985+
customer = Stripe::Customer.retrieve('test_customer_sub')
986+
expect(customer.subscriptions.data).to_not be_empty
987+
expect(customer.subscriptions.count).to eq(1)
988+
expect(customer.subscriptions.data.length).to eq(1)
989+
990+
expect(customer.subscriptions.data.first.status).to eq('active')
991+
expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(true)
992+
expect(customer.subscriptions.data.first.ended_at).to be_nil
993+
expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
994+
end
995+
996+
it "resumes a subscription cancelled by updating cancel_at_period_end" do
997+
truth = stripe_helper.create_plan(id: 'the_truth')
998+
customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
999+
1000+
sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
1001+
Stripe::Subscription.update(sub.id, cancel_at_period_end: true)
1002+
1003+
result = Stripe::Subscription.update(sub.id, cancel_at_period_end: false)
1004+
1005+
expect(result.status).to eq('active')
1006+
expect(result.cancel_at_period_end).to eq(false)
1007+
expect(result.id).to eq(sub.id)
1008+
1009+
customer = Stripe::Customer.retrieve('test_customer_sub')
1010+
expect(customer.subscriptions.data).to_not be_empty
1011+
expect(customer.subscriptions.count).to eq(1)
1012+
expect(customer.subscriptions.data.length).to eq(1)
1013+
1014+
expect(customer.subscriptions.data.first.status).to eq('active')
1015+
expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(false)
1016+
expect(customer.subscriptions.data.first.ended_at).to be_nil
1017+
expect(customer.subscriptions.data.first.canceled_at).to be_nil
1018+
end
1019+
9741020
it "doesn't change status of subscription when cancelling at period end" do
9751021
trial = stripe_helper.create_plan(id: 'trial', trial_period_days: 14)
9761022
customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "trial")

0 commit comments

Comments
 (0)