Add method to locate plans by the stripe price id

This commit is contained in:
Jorge Manrubia
2025-12-18 10:02:06 +01:00
parent d9ce1577c0
commit b44b7ba992
3 changed files with 12 additions and 1 deletions
@@ -80,6 +80,6 @@ class Stripe::WebhooksController < ApplicationController
def plan_key_for(stripe_subscription)
price_id = stripe_subscription.items.data.first&.price&.id
Plan.all.find { |plan| plan.stripe_price_id == price_id }&.key
Plan.find_by_price_id(price_id)&.key
end
end
+4
View File
@@ -29,6 +29,10 @@ class Plan
@all_by_key[key]
end
def find_by_price_id(price_id)
all.find { |plan| plan.stripe_price_id == price_id }
end
alias [] find
end
+7
View File
@@ -8,4 +8,11 @@ class PlanTest < ActiveSupport::TestCase
test "monthly plan is not free" do
assert_not Plan[:monthly_v1].free?
end
test "find plan by its price id" do
Plan.paid.stubs(:stripe_price_id).returns("price_monthly_v1")
assert_equal Plan.paid, Plan.find_by_price_id("price_monthly_v1")
assert_nil Plan.find_by_price_id("unknown_price_id")
end
end