Add method to locate plans by the stripe price id
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user