Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

CRM: Refund lock (#5480)

hq1bdc44d1d33ee
diff --git a/extra/lib/plausible_web/live/customer_support/live/team.ex b/extra/lib/plausible_web/live/customer_support/live/team.ex
index 2ab8de6156..aa04a3dff0 100644
--- a/extra/lib/plausible_web/live/customer_support/live/team.ex
+++ b/extra/lib/plausible_web/live/customer_support/live/team.ex
@@ -9,6 +9,8 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
alias PlausibleWeb.Router.Helpers, as: Routes
+ require Plausible.Billing.Subscription.Status
+
alias Plausible.Repo
import Ecto.Query
@@ -170,6 +172,28 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
<div class="px-6 py-5 text-center text-sm font-medium">
<span>
<strong>Subscription status</strong> <br />{subscription_status(@team)}
+ <div :if={
+ @team.subscription && @team.subscription.status == Subscription.Status.deleted() &&
+ }>
+ <span class="flex items-center gap-x-8 justify-center mt-1">
+ <div :if={not Teams.locked?(@team)}>
+ <Heroicons.lock_open solid class="inline stroke-2 w-4 h-4 text-red-400 mr-1" />
+ <.styled_link
+ phx-click="refund-lock"
+ phx-target={@myself}
+ data-confirm="Are you sure you want to lock? The only way to unlock, is for the user to resubscribe."
+ >
+ Refund Lock
+ </.styled_link>
+ </div>
+
+ <div :if={Teams.locked?(@team)}>
+ <Heroicons.lock_closed solid class="inline stroke-2 w-4 h-4 text-red-400 mr-1" />
+ Locked
+ </div>
+ </span>
+ </div>
</span>
</div>
<div class="px-6 py-5 text-center text-sm font-medium">
@@ -649,6 +673,20 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
{:noreply, lock_team(socket)}
end
+ def handle_event("refund-lock", _, socket) do
+ team = socket.assigns.team
+
+ {:ok, team} =
+ Repo.transaction(fn ->
+ yesterday = Date.shift(Date.utc_today(), day: -1)
+ Plausible.Billing.SiteLocker.set_lock_status_for(team, true)
+ Repo.update!(Subscription.changeset(team.subscription, %{next_bill_date: yesterday}))
+ Resource.Team.get(team.id)
+ end)
+
+ {:noreply, assign(socket, team: team)}
+ end
+
def team_bg(term) do
list = [
"bg-blue-500",
diff --git a/lib/plausible/teams.ex b/lib/plausible/teams.ex
index a7023051fe..ea20294e08 100644
--- a/lib/plausible/teams.ex
+++ b/lib/plausible/teams.ex
@@ -59,8 +59,7 @@ defmodule Plausible.Teams do
def on_trial?(team) do
team = with_subscription(team)
- not Plausible.Billing.Subscriptions.active?(team.subscription) &&
- trial_days_left(team) >= 0
+ is_nil(team.subscription) and trial_days_left(team) >= 0
end
else
def on_trial?(_), do: always(true)
diff --git a/lib/plausible_web/components/billing/notice.ex b/lib/plausible_web/components/billing/notice.ex
index a94d841c59..28ff6d42cb 100644
--- a/lib/plausible_web/components/billing/notice.ex
+++ b/lib/plausible_web/components/billing/notice.ex
@@ -114,7 +114,11 @@ defmodule PlausibleWeb.Components.Billing.Notice do
} = assigns
) do
~H"""
- <aside id="global-subscription-cancelled-notice" class="container">
+ <aside
+ :if={not Subscriptions.expired?(@subscription)}
+ id="global-subscription-cancelled-notice"
+ class="container"
+ >
<.notice
dismissable_id={Plausible.Billing.cancelled_subscription_notice_dismiss_id(@subscription.id)}
title={Plausible.Billing.subscription_cancelled_notice_title()}
@@ -136,7 +140,7 @@ defmodule PlausibleWeb.Components.Billing.Notice do
assigns = assign(assigns, :container_id, "local-subscription-cancelled-notice")
~H"""
- <aside id={@container_id} class="hidden">
+ <aside :if={not Subscriptions.expired?(@subscription)} id={@container_id} class="hidden">
<.notice
title={Plausible.Billing.subscription_cancelled_notice_title()}
theme={:red}
@@ -262,31 +266,19 @@ defmodule PlausibleWeb.Components.Billing.Notice do
end
defp subscription_cancelled_notice_body(assigns) do
- if Subscriptions.expired?(assigns.subscription) do
- ~H"""
+ ~H"""
+ <p>
+ You have access to your stats until <span class="font-semibold inline"><%= Calendar.strftime(@subscription.next_bill_date, "%b %-d, %Y") %></span>.
<.link
class="underline inline-block"
href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)}
>
Upgrade your subscription
</.link>
- to get access to your stats again.
- """
- else
- ~H"""
- <p>
- You have access to your stats until <span class="font-semibold inline"><%= Calendar.strftime(@subscription.next_bill_date, "%b %-d, %Y") %></span>.
- <.link
- class="underline inline-block"
- href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)}
- >
- Upgrade your subscription
- </.link>
- to make sure you don't lose access.
- </p>
- <.lose_grandfathering_warning subscription={@subscription} />
- """
- end
+ to make sure you don't lose access.
+ </p>
+ <.lose_grandfathering_warning subscription={@subscription} />
+ """
end
defp lose_grandfathering_warning(%{subscription: subscription} = assigns) do
diff --git a/test/plausible_web/controllers/settings_controller_test.exs b/test/plausible_web/controllers/settings_controller_test.exs
index 5451927812..fb62a4bff8 100644
--- a/test/plausible_web/controllers/settings_controller_test.exs
+++ b/test/plausible_web/controllers/settings_controller_test.exs
@@ -205,8 +205,7 @@ defmodule PlausibleWeb.SettingsControllerTest do
|> html_response(200)
|> text_of_element("#global-subscription-cancelled-notice")
- assert notice_text =~ "Subscription cancelled"
- assert notice_text =~ "Upgrade your subscription to get access to your stats again"
+ refute notice_text =~ Plausible.Billing.subscription_cancelled_notice_title()
end
@tag :ee_only
diff --git a/test/plausible_web/live/customer_support/teams_test.exs b/test/plausible_web/live/customer_support/teams_test.exs
index f6a3ccd803..ee8babe720 100644
--- a/test/plausible_web/live/customer_support/teams_test.exs
+++ b/test/plausible_web/live/customer_support/teams_test.exs
@@ -8,6 +8,8 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest do
import Phoenix.LiveViewTest
import Plausible.Test.Support.HTML
+ require Plausible.Billing.Subscription.Status
+
defp open_team(id, qs \\ []) do
Routes.customer_support_resource_path(
PlausibleWeb.Endpoint,
@@ -60,6 +62,38 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest do
refute team.grace_period
end
+ test "refund lock handling", %{conn: conn, user: user} do
+ team = team_of(user)
+ {:ok, _lv, html} = live(conn, open_team(team.id))
+ refute text(html) =~ "Refund Lock"
+ refute text(html) =~ "Locked"
+
+ subscribe_to_growth_plan(user,
+ status: Plausible.Billing.Subscription.Status.deleted()
+ )
+
+ {:ok, lv, html} = live(conn, open_team(team.id))
+
+ assert text(html) =~ "Refund Lock"
+ lv |> element(~s|a[phx-click="refund-lock"]|) |> render_click()
+
+ assert text(render(lv)) =~ "Locked"
+
+ team = Plausible.Repo.reload!(team)
+ assert team.locked
+ refute team.grace_period
+
+ assert Date.diff(
+ Plausible.Teams.with_subscription(team).subscription.next_bill_date,
+ Date.utc_today()
+ ) == -1
+
+ # make sure this team doesn't unlock automatically
+ Plausible.Workers.LockSites.perform(nil)
+ team = Plausible.Repo.reload!(team)
+ assert team.locked
+ end
+
test "404", %{conn: conn} do
assert_raise Ecto.NoResultsError, fn ->
{:ok, _lv, _html} = live(conn, open_team(9999))