diff --git a/lib/oli/delivery/paywall.ex b/lib/oli/delivery/paywall.ex index 0306519f4a..8b0df09d79 100644 --- a/lib/oli/delivery/paywall.ex +++ b/lib/oli/delivery/paywall.ex @@ -32,7 +32,7 @@ defmodule Oli.Delivery.Paywall do def summarize_access( %User{id: id} = user, - %Section{slug: slug, requires_payment: true} = section + %Section{slug: slug, requires_payment: true, amount: amount} = section ) do if Sections.is_instructor?(user, slug) or Sections.is_admin?(user, slug) do AccessSummary.instructor() @@ -45,19 +45,19 @@ defmodule Oli.Delivery.Paywall do if section.pay_by_institution do AccessSummary.pay_by_institution() else - case has_paid?(enrollment) do - true -> + if has_zero_cost?(amount) do + AccessSummary.has_zero_cost() + else + if has_paid?(enrollment) do AccessSummary.paid() - - _ -> - case within_grace_period?(enrollment, section) do - true -> - grace_period_seconds_remaining(enrollment, section) - |> AccessSummary.within_grace() - - _ -> - AccessSummary.not_paid() + else + if within_grace_period?(enrollment, section) do + grace_period_seconds_remaining(enrollment, section) + |> AccessSummary.within_grace() + else + AccessSummary.not_paid() end + end end end end @@ -69,7 +69,7 @@ defmodule Oli.Delivery.Paywall do def summarize_access( %User{} = user, - %Section{slug: slug, requires_payment: true} = section, + %Section{slug: slug, requires_payment: true, amount: amount} = section, user_role_id, enrollment, payment @@ -90,13 +90,15 @@ defmodule Oli.Delivery.Paywall do AccessSummary.paid() _ -> - case within_grace_period?(enrollment, section) do - true -> + if has_zero_cost?(amount) do + AccessSummary.has_zero_cost() + else + if within_grace_period?(enrollment, section) do grace_period_seconds_remaining(enrollment, section) |> AccessSummary.within_grace() - - _ -> + else AccessSummary.not_paid() + end end end end @@ -120,6 +122,10 @@ defmodule Oli.Delivery.Paywall do end end + defp has_zero_cost?(nil), do: true + + defp has_zero_cost?(amount), do: Money.zero?(amount) + defp within_grace_period?(nil, _), do: false defp within_grace_period?(_, %Section{has_grace_period: false}), do: false diff --git a/lib/oli/delivery/paywall/access_summary.ex b/lib/oli/delivery/paywall/access_summary.ex index 9eae169ac0..493e50f6e2 100644 --- a/lib/oli/delivery/paywall/access_summary.ex +++ b/lib/oli/delivery/paywall/access_summary.ex @@ -69,4 +69,12 @@ defmodule Oli.Delivery.Paywall.AccessSummary do grace_period_remaining: nil } end + + def has_zero_cost() do + %AccessSummary{ + available: true, + reason: :has_zero_cost, + grace_period_remaining: nil + } + end end