From b44b7ba992a59b7e3f5ec2ddf3f17ecc976eadb8 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Thu, 18 Dec 2025 10:02:06 +0100 Subject: [PATCH] Add method to locate plans by the stripe price id --- saas/app/controllers/stripe/webhooks_controller.rb | 2 +- saas/app/models/plan.rb | 4 ++++ saas/test/models/plan_test.rb | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/saas/app/controllers/stripe/webhooks_controller.rb b/saas/app/controllers/stripe/webhooks_controller.rb index 74627203b..cdfb6bcec 100644 --- a/saas/app/controllers/stripe/webhooks_controller.rb +++ b/saas/app/controllers/stripe/webhooks_controller.rb @@ -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 diff --git a/saas/app/models/plan.rb b/saas/app/models/plan.rb index a15ecab54..e77ea81e8 100644 --- a/saas/app/models/plan.rb +++ b/saas/app/models/plan.rb @@ -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 diff --git a/saas/test/models/plan_test.rb b/saas/test/models/plan_test.rb index 6e0d70b5c..54ed279d6 100644 --- a/saas/test/models/plan_test.rb +++ b/saas/test/models/plan_test.rb @@ -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