Skip to content
Snippets Groups Projects
Commit 0bf1da09 authored by Uku Taht's avatar Uku Taht
Browse files

New plans

parent 6951e49c
No related branches found
No related tags found
No related merge requests found
...@@ -27,31 +27,30 @@ defmodule Plausible.Billing.Plans do ...@@ -27,31 +27,30 @@ defmodule Plausible.Billing.Plans do
] ]
def plans_for(user) do def plans_for(user) do
case Application.get_env(:plausible, :environment) do
"dev" ->
Enum.map(@sandbox_plans, fn plan ->
Map.put(plan, :volume, number_format(plan[:limit]))
end)
_ ->
real_plans_for(user)
end
end
def real_plans_for(user) do
user = Repo.preload(user, :subscription) user = Repo.preload(user, :subscription)
sandbox_plans = plans_sandbox()
v1_plans = plans_v1() v1_plans = plans_v1()
v2_plans = plans_v2()
v1_plan_ids = v3_plans = plans_v3()
v1_plans
|> Enum.map(fn plan -> [plan[:monthly_product_id], plan[:yearly_product_id]] end)
|> List.flatten()
raw_plans = raw_plans =
if user.subscription && user.subscription.paddle_plan_id in v1_plan_ids do cond do
contains?(v1_plans, user.subscription) ->
v1_plans v1_plans
else
plans_v2() contains?(v2_plans, user.subscription) ->
v2_plans
contains?(v3_plans, user.subscription) ->
v3_plans
contains?(sandbox_plans, user.subscription) ->
sandbox_plans
true ->
case Application.get_env(:plausible, :environment) do
_ -> v3_plans
end
end end
Enum.map(raw_plans, fn plan -> Map.put(plan, :volume, number_format(plan[:limit])) end) Enum.map(raw_plans, fn plan -> Map.put(plan, :volume, number_format(plan[:limit])) end)
...@@ -106,15 +105,21 @@ defmodule Plausible.Billing.Plans do ...@@ -106,15 +105,21 @@ defmodule Plausible.Billing.Plans do
Enum.find(plans_for(user), fn plan -> usage < plan[:limit] end) Enum.find(plans_for(user), fn plan -> usage < plan[:limit] end)
end end
defp contains?(_plans, nil), do: false
defp contains?(plans, subscription) do
Enum.any?(plans, fn plan ->
plan[:monthly_product_id] == subscription.paddle_plan_id ||
plan[:yearly_product_id] == subscription.paddle_plan_id
end)
end
defp number_format(num) do defp number_format(num) do
PlausibleWeb.StatsView.large_number_format(num) PlausibleWeb.StatsView.large_number_format(num)
end end
defp all_plans() do defp all_plans() do
case Application.get_env(:plausible, :environment) do plans_v1() ++ @unlisted_plans_v1 ++ plans_v2() ++ @unlisted_plans_v2 ++ plans_sandbox()
"dev" -> @sandbox_plans
_ -> plans_v1() ++ @unlisted_plans_v1 ++ plans_v2() ++ @unlisted_plans_v2
end
end end
defp plans_v1() do defp plans_v1() do
...@@ -126,4 +131,16 @@ defmodule Plausible.Billing.Plans do ...@@ -126,4 +131,16 @@ defmodule Plausible.Billing.Plans do
File.read!(Application.app_dir(:plausible) <> "/priv/plans_v2.json") File.read!(Application.app_dir(:plausible) <> "/priv/plans_v2.json")
|> Jason.decode!(keys: :atoms) |> Jason.decode!(keys: :atoms)
end end
defp plans_v3() do
File.read!(Application.app_dir(:plausible) <> "/priv/plans_v3.json")
|> Jason.decode!(keys: :atoms)
end
defp plans_sandbox() do
case Application.get_env(:plausible, :environment) do
"dev" -> @sandbox_plans
_ -> []
end
end
end end
[
{
"limit":10000,
"monthly_cost":"$9",
"monthly_product_id":"749342",
"yearly_cost":"$90",
"yearly_product_id":"749343"
},
{
"limit":100000,
"monthly_cost":"$19",
"monthly_product_id":"749344",
"yearly_cost":"$190",
"yearly_product_id":"749345"
},
{
"limit":200000,
"monthly_cost":"$29",
"monthly_product_id":"749346",
"yearly_cost":"$290",
"yearly_product_id":"749347"
},
{
"limit":500000,
"monthly_cost":"$49",
"monthly_product_id":"749348",
"yearly_cost":"$490",
"yearly_product_id":"749349"
},
{
"limit":1000000,
"monthly_cost":"$69",
"monthly_product_id":"749350",
"yearly_cost":"$690",
"yearly_product_id":"749352"
},
{
"limit":2000000,
"monthly_cost":"$89",
"monthly_product_id":"749353",
"yearly_cost":"$890",
"yearly_product_id":"749355"
},
{
"limit":5000000,
"monthly_cost":"$129",
"monthly_product_id":"749356",
"yearly_cost":"$1290",
"yearly_product_id":"749357"
},
{
"limit":10000000,
"monthly_cost":"$169",
"monthly_product_id":"749358",
"yearly_cost":"$1690",
"yearly_product_id":"749359"
}
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment