Skip to content

Commit

Permalink
Ensure only subscriptions with status "canceled" are excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
angusmcleod committed Oct 15, 2024
1 parent 6900db5 commit aab4ada
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/subscription_server/providers/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def subscriptions(product_ids, resource_name)
sub_hash = sub.to_h
price = sub_hash[:items][:data][0][:price]

if product_ids.include?(price[:product]) && sub_hash[:canceled_at].blank?
if product_ids.include?(price[:product]) && sub_hash[:status] == 'active'
product = ::Stripe::Product.retrieve(price[:product])
subscription = SubscriptionServer::Subscription.new(
resource: resource_name,
Expand Down
10 changes: 10 additions & 0 deletions spec/components/subscription_server/user_subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ def response_error(message)
expect(subscription.price_name).to eq(Stripe::PRICE_NAME)
end

context "with a cancelled subscription" do
it "does not load cancelled subscriptions" do
Stripe::Customer.has_subscription = true
Stripe::Customer.subscription_attrs = { "status" => "canceled" }
@instance.load(resources)
expect(@instance.errors).to include(response_error("no subscriptions found for #{resource}"))
expect(@instance.subscriptions).to eq([])
end
end

context "with domain limit" do
before do
Stripe::Customer.has_subscription = true
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/providers/stripe_plugin/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module ::Stripe

class Customer
cattr_accessor :has_subscription
cattr_accessor(:subscription_attrs) { {} }

def self.list(email: nil, expand: nil)
stripe_path = "#{Rails.root}/plugins/discourse-subscription-server/spec/fixtures/providers/stripe"
Expand All @@ -29,6 +30,11 @@ def self.list(email: nil, expand: nil)
subscription["items"]["data"][0]["price"]["product"] = PRODUCT_ID
subscription["items"]["data"][0]["price"]["id"] = PRICE_ID
subscription["items"]["data"][0]["price"]["nickname"] = PRICE_NAME
if subscription_attrs.present?
subscription_attrs.each do |k, v|
subscription[k] = v
end
end
customer_list["data"][0]["subscriptions"]["data"] << subscription
end

Expand Down

0 comments on commit aab4ada

Please sign in to comment.