From 4a76b95953bbc844637819a7ac421cbb13c55e42 Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Wed, 15 Nov 2023 08:46:52 -0500 Subject: [PATCH] Subscription expired without any active subscriptions. --- .../models/methods.rb | 32 ++++++++++++------- .../commands/subscription_spec.rb | 14 ++++++++ .../slack-ruby-bot-server-stripe/team_spec.rb | 10 ++++++ 3 files changed, 44 insertions(+), 12 deletions(-) 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