Skip to content

Commit b44b7ba

Browse files
committed
Add method to locate plans by the stripe price id
1 parent d9ce157 commit b44b7ba

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

saas/app/controllers/stripe/webhooks_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ def next_amount_due_for(stripe_subscription)
8080

8181
def plan_key_for(stripe_subscription)
8282
price_id = stripe_subscription.items.data.first&.price&.id
83-
Plan.all.find { |plan| plan.stripe_price_id == price_id }&.key
83+
Plan.find_by_price_id(price_id)&.key
8484
end
8585
end

saas/app/models/plan.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def find(key)
2929
@all_by_key[key]
3030
end
3131

32+
def find_by_price_id(price_id)
33+
all.find { |plan| plan.stripe_price_id == price_id }
34+
end
35+
3236
alias [] find
3337
end
3438

saas/test/models/plan_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ class PlanTest < ActiveSupport::TestCase
88
test "monthly plan is not free" do
99
assert_not Plan[:monthly_v1].free?
1010
end
11+
12+
test "find plan by its price id" do
13+
Plan.paid.stubs(:stripe_price_id).returns("price_monthly_v1")
14+
15+
assert_equal Plan.paid, Plan.find_by_price_id("price_monthly_v1")
16+
assert_nil Plan.find_by_price_id("unknown_price_id")
17+
end
1118
end

0 commit comments

Comments
 (0)