diff --git a/CHANGELOG.md b/CHANGELOG.md index b22a5bd..1cd4fce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### 0.2.0 (Next) +* [#16](https://github.com/slack-ruby/slack-ruby-bot-server-stripe/pull/16): Subscription expired without any active subscriptions - [@dblock](https://github.com/dblock). * [#15](https://github.com/slack-ruby/slack-ruby-bot-server-stripe/pull/15): Fix: display past due subscriptions - [@dblock](https://github.com/dblock). * [#14](https://github.com/slack-ruby/slack-ruby-bot-server-stripe/pull/14): Migrate to GHA - [@duffn](https://github.com/duffn). * Your contribution here. diff --git a/lib/slack-ruby-bot-server-stripe/models/methods.rb b/lib/slack-ruby-bot-server-stripe/models/methods.rb index 77de844..ccc5217 100644 --- a/lib/slack-ruby-bot-server-stripe/models/methods.rb +++ b/lib/slack-ruby-bot-server-stripe/models/methods.rb @@ -37,13 +37,17 @@ def trial_expired? def subscription_text(options = { include_admin_info: false }) subscription_text = [] - if stripe_subscriptions&.any? + if stripe_customer subscription_text << stripe_customer_text - subscription_text.concat(stripe_customer_subscriptions_info) - if options[:include_admin_info] - subscription_text.concat(stripe_customer_invoices_info) - subscription_text.concat(stripe_customer_sources_info) - subscription_text << update_cc_text + if stripe_subscriptions&.any? + subscription_text.concat(stripe_customer_subscriptions_info) + if options[:include_admin_info] + subscription_text.concat(stripe_customer_invoices_info) + subscription_text.concat(stripe_customer_sources_info) + subscription_text << update_cc_text + end + else + subscription_text << 'No active subscriptions.' end elsif subscribed && subscribed_at subscription_text << subscriber_text @@ -184,12 +188,16 @@ def check_subscription! raise Errors::NotSubscribedError unless subscribed? raise Errors::MissingStripeCustomerError unless stripe_customer - stripe_customer.subscriptions.each do |subscription| - case subscription.status - when 'past_due' - subscription_past_due! - when 'canceled', 'unpaid' - subscription_expired! + if stripe_customer.subscriptions.none? + subscription_expired! + else + stripe_customer.subscriptions.each do |subscription| + case subscription.status + when 'past_due' + subscription_past_due! + when 'canceled', 'unpaid' + subscription_expired! + end end end end diff --git a/spec/slack-ruby-bot-server-stripe/commands/subscription_spec.rb b/spec/slack-ruby-bot-server-stripe/commands/subscription_spec.rb index c9fc19d..54ba44b 100644 --- a/spec/slack-ruby-bot-server-stripe/commands/subscription_spec.rb +++ b/spec/slack-ruby-bot-server-stripe/commands/subscription_spec.rb @@ -62,6 +62,20 @@ expect(message: "#{SlackRubyBot.config.user} subscription", user: 'U007').to respond_with_slack_message subscription_text end end + context 'no active subscription' do + before do + customer.subscriptions.data = [] + allow(Stripe::Customer).to receive(:retrieve).and_return(customer) + end + it 'displays subscription info' do + customer_since = Time.at(customer.created).strftime('%B %d, %Y') + subscription_text = [ + "Customer since #{customer_since}.", + 'No active subscriptions.' + ].join("\n") + expect(message: "#{SlackRubyBot.config.user} subscription", user: 'U007').to respond_with_slack_message subscription_text + end + end end end end diff --git a/spec/slack-ruby-bot-server-stripe/team_spec.rb b/spec/slack-ruby-bot-server-stripe/team_spec.rb index ff64063..6ea5078 100644 --- a/spec/slack-ruby-bot-server-stripe/team_spec.rb +++ b/spec/slack-ruby-bot-server-stripe/team_spec.rb @@ -342,6 +342,16 @@ team.check_subscription! end end + context 'with no active subscription' do + before do + team.send(:stripe_customer).subscriptions = [] + end + it 'invokes' do + expect(team).to_not receive(:subscription_past_due!) + expect(team).to receive(:subscription_expired!).and_call_original + team.check_subscription! + end + end end end context 'recommended text' do