Refactor enterprise plan upgrade and change-plan actions (#3397)
* rename enterprise?/1 function * change link text to Upgrade when subscription deleted * extract paddle_button and paddle_script components * create a new upgrade-to-enterprise-plan page * extract upgrade_link component * rename function * link to enterprise plan upgrade page from settings ...if the user has an enterprise plan configured * fetch enterprise plan price on the new page * add change_enterprise_plan functionality on the new page * render existing change_enterprise_plan_contact_us.html ...when subscribed to latest configured enterprise plan * rename vars and extract resumable? fn * remove dead billing route * small test refactor: extract convenience fn * add tests for... ...restricting paused and past_due subscription access to the new enterprise plan page. 1. redirect to /settings from the controller action 2. hiding the change-plan link from the user settings * implement redirect to /settings * hide the enterprise upgrade/change-plan link * add tests for a deleted enterprise subscription * plug in the new controller action and delete dead code * optimize for dark mode * fix compile warning * credo fix * display N/A instead of crash when price nil * change subscription.status type to Ecto.Enum Also, create a new `Subscription.Status` module that exposes macros to return the used atom values (prevent typos at compiletime). * fix bug (@conn not available anymore) * use Routes.billing_path where applicable * add a status() type * silence credo * refactor suggestion from review Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Remove the __using__ macro from Subscription.Status ... instead be explicit about requires and aliases and also order the use, import, require, and alias clauses according to https://github.com/christopheradams/elixir_style_guide#module-attribute-ordering * drop the virtual Enteprise 'price_per_interval' field * apply review suggestion to make the code more DRY * use dot syntax to fetch current user in new controller actions * fix formatting --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>pull/3406/head r2023.10.10-206
parent
dec193e904
commit
3d2f356ba7
@ -0,0 +1,45 @@
|
||||
defmodule Plausible.Billing.Subscription.Status do
|
||||
@moduledoc """
|
||||
The subscription statuses are stored in Paddle. They can only be changed
|
||||
through Paddle webhooks, which always send the current subscription status
|
||||
via the payload.
|
||||
|
||||
* `active` - All good with the payments. Can access stats.
|
||||
|
||||
* `past_due` - The payment has failed, but we're trying to charge the customer
|
||||
again. Access to stats is still granted. There will be three retries - after
|
||||
3, 5, and 7 days have passed from the first failure. After a failure on the
|
||||
final retry, the subscription status will change to `paused`. As soon as the
|
||||
customer updates their billing details, Paddle will charge them again, and
|
||||
after a successful payment, the subscription will become `active` again.
|
||||
|
||||
* `paused` - we've tried to charge the customer but all the retries have failed.
|
||||
Stats access restricted. As soon as the customer updates their billing details,
|
||||
Paddle will charge them again, and after a successful payment, the subscription
|
||||
will become `active` again.
|
||||
|
||||
* `deleted` - The customer has triggered the cancel subscription action. Access
|
||||
to stats should be granted for the time the customer has already paid for. If
|
||||
they want to upgrade again, new billing details have to be provided.
|
||||
|
||||
Paddle documentation links for reference:
|
||||
|
||||
* Subscription statuses -
|
||||
https://developer.paddle.com/classic/reference/zg9joji1mzu0mdi2-subscription-status-reference
|
||||
|
||||
* Payment failures -
|
||||
https://developer.paddle.com/classic/guides/zg9joji1mzu0mduy-payment-failures
|
||||
"""
|
||||
|
||||
@statuses [:active, :past_due, :paused, :deleted]
|
||||
|
||||
@type status() :: unquote(Enum.reduce(@statuses, &{:|, [], [&1, &2]}))
|
||||
|
||||
for status <- @statuses do
|
||||
defmacro unquote(status)(), do: unquote(status)
|
||||
end
|
||||
|
||||
def valid_statuses() do
|
||||
@statuses
|
||||
end
|
||||
end
|
||||
@ -1,68 +0,0 @@
|
||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch-jsonp/1.1.3/fetch-jsonp.min.js"></script>
|
||||
<div class="mx-auto mt-6 text-center">
|
||||
<h1 class="text-3xl font-black dark:text-gray-100">Change subscription plan</h1>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
plan = function() {
|
||||
return {
|
||||
localizedPlan: null,
|
||||
price() {
|
||||
var currency = {
|
||||
'USD': '$',
|
||||
'EUR': '€',
|
||||
'GBP': '£'
|
||||
}[this.localizedPlan.currency]
|
||||
|
||||
return currency + this.localizedPlan.price.net
|
||||
},
|
||||
fetchPlan() {
|
||||
fetchJsonp('<%= Plausible.Billing.PaddleApi.checkout_domain() %>/api/2.0/prices?product_ids=<%= @plan.paddle_plan_id %>')
|
||||
.then(res => res.json())
|
||||
.then((data) => {
|
||||
this.localizedPlan = data.response.products[0]
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="w-full max-w-lg px-4 mx-auto mt-4">
|
||||
<div x-init="fetchPlan()" x-data="window.plan()" class="flex-1 p-8 mt-8 bg-white rounded shadow-md dark:bg-gray-800">
|
||||
<div x-show="!localizedPlan" class="mx-auto my-40 loading sm"><div></div></div>
|
||||
<template x-if="localizedPlan">
|
||||
<div>
|
||||
<div class="w-full pb-4 dark:text-gray-100">
|
||||
<span>We've prepared your account for an upgrade to custom limits outside the listed plans:</span>
|
||||
</div>
|
||||
|
||||
<ul class="w-full py-4 dark:text-gray-100">
|
||||
<li>Up to <b><%= PlausibleWeb.StatsView.large_number_format(@plan.monthly_pageview_limit) %></b> monthly pageviews</li>
|
||||
<li>Up to <b><%= PlausibleWeb.StatsView.large_number_format(@plan.site_limit) %></b> sites</li>
|
||||
<li>Up to <b><%= PlausibleWeb.StatsView.large_number_format(@plan.hourly_api_request_limit) %></b> hourly api requests</li>
|
||||
</ul>
|
||||
|
||||
<ul class="w-full py-4 dark:text-gray-100">
|
||||
<span>The plan is priced at</span>
|
||||
<template x-if="localizedPlan"><b x-text="price()"></b> </template>
|
||||
<span>per <%= if @plan.billing_interval == :yearly, do: "year", else: "month" %>. On the next page, our payment provider will calculate the prorated amount that your card will be charged if you decide to upgrade now.</span>
|
||||
</ul>
|
||||
|
||||
<div class="mt-6 text-left">
|
||||
<span class="inline-flex w-full rounded-md shadow-sm">
|
||||
<%= link(to: Routes.billing_path(@conn, :change_plan_preview, @plan.paddle_plan_id), class: "inline-flex items-center px-4 py-2 text-sm font-medium text-white bg-indigo-600 border border-transparent leading-5 rounded-md hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:ring active:bg-indigo-700 transition ease-in-out duration-150 ") do %>
|
||||
<svg fill="currentColor" viewBox="0 0 20 20" class="inline w-4 h-4 mr-2"><path d="M10 12a2 2 0 100-4 2 2 0 000 4z"></path><path fill-rule="evenodd" d="M.458 10C1.732 5.943 5.522 3 10 3s8.268 2.943 9.542 7c-1.274 4.057-5.064 7-9.542 7S1.732 14.057.458 10zM14 10a4 4 0 11-8 0 4 4 0 018 0z" clip-rule="evenodd"></path></svg>
|
||||
Preview changes
|
||||
<% end %>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-8 text-center dark:text-gray-100">
|
||||
Questions? <%= link("Contact us", to: "https://plausible.io/contact", class: "text-indigo-500") %>
|
||||
</div>
|
||||
|
||||
<%= render("_paddle_script.html") %>
|
||||
@ -0,0 +1,91 @@
|
||||
<div class="mx-auto mt-6 text-center">
|
||||
<h1 class="text-3xl font-black text-black dark:text-gray-100">
|
||||
<%= if @subscription_resumable,
|
||||
do: "Change subscription plan",
|
||||
else: "Upgrade to Enterprise" %>
|
||||
</h1>
|
||||
</div>
|
||||
<div class="w-full max-w-lg px-4 mx-auto mt-4 text-gray-900 dark:text-gray-100">
|
||||
<div class="flex-1 p-8 mt-8 rounded bg-white shadow-md dark:bg-gray-800 dark:shadow-none">
|
||||
<div class="w-full pb-4">
|
||||
<span>
|
||||
<%= if @subscription_resumable,
|
||||
do:
|
||||
"We've prepared your account for an upgrade to custom limits outside the listed plans:",
|
||||
else:
|
||||
"We've prepared a custom enterprise plan for your account with the following limits:" %>
|
||||
</span>
|
||||
</div>
|
||||
<PlausibleWeb.Components.Billing.present_enterprise_plan plan={@latest_enterprise_plan} />
|
||||
<ul class="w-full py-4">
|
||||
<span>
|
||||
The plan is priced at
|
||||
<b>
|
||||
<%= case @price do
|
||||
%Money{} = money ->
|
||||
PlausibleWeb.Components.Billing.format_price(money, format: :standard)
|
||||
|
||||
nil ->
|
||||
"N/A"
|
||||
end %>
|
||||
</b>
|
||||
</span>
|
||||
<span>
|
||||
per <%= if @latest_enterprise_plan.billing_interval == :yearly,
|
||||
do: "year",
|
||||
else: "month" %> + VAT if applicable. <%= if @subscription_resumable,
|
||||
do:
|
||||
"On the next page, our payment provider will calculate the prorated amount that your card will be charged if you decide to upgrade now.",
|
||||
else: "Click the button below to upgrade." %>
|
||||
</span>
|
||||
</ul>
|
||||
<div class="w-max">
|
||||
<%= if @subscription_resumable do %>
|
||||
<span class="inline-flex w-full rounded-md shadow-sm">
|
||||
<.link
|
||||
id="preview-changes"
|
||||
href={
|
||||
Routes.billing_path(
|
||||
@conn,
|
||||
:change_plan_preview,
|
||||
@latest_enterprise_plan.paddle_plan_id
|
||||
)
|
||||
}
|
||||
class="inline-flex items-center px-4 py-2 text-sm font-medium text-white bg-indigo-600 border border-transparent leading-5 rounded-md hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:ring active:bg-indigo-700 transition ease-in-out duration-150"
|
||||
>
|
||||
<svg fill="currentColor" viewBox="0 0 20 20" class="inline w-4 h-4 mr-2">
|
||||
<path d="M10 12a2 2 0 100-4 2 2 0 000 4z"></path>
|
||||
<path
|
||||
fill-rule="evenodd"
|
||||
d="M.458 10C1.732 5.943 5.522 3 10 3s8.268 2.943 9.542 7c-1.274 4.057-5.064 7-9.542 7S1.732 14.057.458 10zM14 10a4 4 0 11-8 0 4 4 0 018 0z"
|
||||
clip-rule="evenodd"
|
||||
>
|
||||
</path>
|
||||
</svg>
|
||||
Preview changes
|
||||
</.link>
|
||||
</span>
|
||||
<% else %>
|
||||
<PlausibleWeb.Components.Billing.paddle_button
|
||||
id="paddle-button"
|
||||
paddle_product_id={@latest_enterprise_plan.paddle_plan_id}
|
||||
user={@user}
|
||||
>
|
||||
<svg fill="currentColor" viewBox="0 0 20 20" class="inline w-4 h-4 mr-2">
|
||||
<path
|
||||
fill-rule="evenodd"
|
||||
d="M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z"
|
||||
clip-rule="evenodd"
|
||||
>
|
||||
</path>
|
||||
</svg>
|
||||
Pay securely via Paddle
|
||||
</PlausibleWeb.Components.Billing.paddle_button>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-8 text-center text-gray-900 dark:text-gray-100">
|
||||
Questions? <a class="text-indigo-600" href={@contact_link}>Contact us</a>
|
||||
</div>
|
||||
<PlausibleWeb.Components.Billing.paddle_script />
|
||||
@ -1,67 +0,0 @@
|
||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch-jsonp/1.1.3/fetch-jsonp.min.js"></script>
|
||||
<script>
|
||||
plan = function() {
|
||||
return {
|
||||
localizedPlan: null,
|
||||
price() {
|
||||
var currency = {
|
||||
'USD': '$',
|
||||
'EUR': '€',
|
||||
'GBP': '£'
|
||||
}[this.localizedPlan.currency]
|
||||
|
||||
return currency + this.localizedPlan.price.net
|
||||
},
|
||||
fetchPlan() {
|
||||
fetchJsonp('<%= Plausible.Billing.PaddleApi.checkout_domain() %>/api/2.0/prices?product_ids=<%= @plan.paddle_plan_id %>')
|
||||
.then(res => res.json())
|
||||
.then((data) => {
|
||||
this.localizedPlan = data.response.products[0]
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="mx-auto mt-6 text-center">
|
||||
<h1 class="text-3xl font-black dark:text-gray-100">Upgrade your free trial</h1>
|
||||
</div>
|
||||
|
||||
<div class="w-full max-w-lg px-4 mx-auto mt-4">
|
||||
<div x-init="fetchPlan()" x-data="window.plan()" class="flex-1 p-8 mt-8 bg-white rounded shadow-md dark:bg-gray-800">
|
||||
<div x-show="!localizedPlan" class="mx-auto my-32 loading sm"><div></div></div>
|
||||
<template x-if="localizedPlan">
|
||||
<div>
|
||||
<div class="w-full pb-4 dark:text-gray-100">
|
||||
<span>We've prepared a custom enterprise plan for your account with the following limits:</span>
|
||||
</div>
|
||||
|
||||
<ul class="w-full py-4 dark:text-gray-100">
|
||||
<li>Up to <b><%= PlausibleWeb.StatsView.large_number_format(@plan.monthly_pageview_limit) %></b> monthly pageviews</li>
|
||||
<li>Up to <b><%= PlausibleWeb.StatsView.large_number_format(@plan.site_limit) %></b> sites</li>
|
||||
<li>Up to <b><%= PlausibleWeb.StatsView.large_number_format(@plan.hourly_api_request_limit) %></b> hourly api requests</li>
|
||||
</ul>
|
||||
|
||||
<ul class="w-full py-4 dark:text-gray-100">
|
||||
<span>The plan is priced at</span>
|
||||
<template x-if="localizedPlan"><b x-text="price()"></b> </template>
|
||||
<span>per <%= if @plan.billing_interval == :yearly, do: "year", else: "month" %> + VAT if applicable. Click the button below to upgrade.
|
||||
</ul>
|
||||
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="mt-6">
|
||||
<button type="button" data-theme="none" data-product="<%= @plan.paddle_plan_id %>" data-email="<%= @conn.assigns[:current_user].email %>" data-disable-logout="true" data-passthrough="<%= @conn.assigns[:current_user].id %>" data-success="/billing/upgrade-success" class="paddle_button items-center button">
|
||||
<svg fill="currentColor" viewBox="0 0 20 20" class="inline w-4 h-4 mr-2"><path fill-rule="evenodd" d="M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z" clip-rule="evenodd"></path></svg>
|
||||
Pay securely via Paddle
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-8 text-center dark:text-gray-100">
|
||||
Questions? <%= link("Contact us", to: "https://plausible.io/contact", class: "text-indigo-500") %>
|
||||
</div>
|
||||
|
||||
<%= render("_paddle_script.html") %>
|
||||
@ -1,148 +1,328 @@
|
||||
defmodule PlausibleWeb.BillingControllerTest do
|
||||
use PlausibleWeb.ConnCase, async: true
|
||||
import Plausible.Test.Support.HTML
|
||||
require Plausible.Billing.Subscription.Status
|
||||
alias Plausible.Billing.Subscription
|
||||
|
||||
describe "GET /upgrade" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "shows upgrade page when user does not have a subcription already", %{conn: conn} do
|
||||
conn = get(conn, "/billing/upgrade")
|
||||
conn = get(conn, Routes.billing_path(conn, :upgrade))
|
||||
|
||||
assert html_response(conn, 200) =~ "Upgrade your free trial"
|
||||
end
|
||||
|
||||
test "redirects user to change plan if they already have a plan", %{conn: conn, user: user} do
|
||||
insert(:subscription, user: user)
|
||||
conn = get(conn, "/billing/upgrade")
|
||||
conn = get(conn, Routes.billing_path(conn, :upgrade))
|
||||
|
||||
assert redirected_to(conn) == "/billing/change-plan"
|
||||
assert redirected_to(conn) == Routes.billing_path(conn, :change_plan_form)
|
||||
end
|
||||
|
||||
test "redirects user to enteprise plan page if they are configured with one", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
plan = insert(:enterprise_plan, user: user)
|
||||
conn = get(conn, "/billing/upgrade")
|
||||
insert(:enterprise_plan, user: user)
|
||||
conn = get(conn, Routes.billing_path(conn, :upgrade))
|
||||
assert redirected_to(conn) == Routes.billing_path(conn, :upgrade_to_enterprise_plan)
|
||||
end
|
||||
end
|
||||
|
||||
assert redirected_to(conn) == "/billing/upgrade/enterprise/#{plan.id}"
|
||||
describe "GET /upgrade/enterprise/:plan_id (deprecated)" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "redirects to the new :upgrade_to_enterprise_plan action", %{conn: conn} do
|
||||
conn = get(conn, Routes.billing_path(conn, :upgrade_enterprise_plan, "123"))
|
||||
assert redirected_to(conn) == Routes.billing_path(conn, :upgrade_to_enterprise_plan)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /upgrade/enterprise/:plan_id" do
|
||||
describe "GET /change-plan" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "renders enteprise plan upgrade page", %{conn: conn, user: user} do
|
||||
plan = insert(:enterprise_plan, user: user)
|
||||
test "shows change plan page if user has subsription", %{conn: conn, user: user} do
|
||||
insert(:subscription, user: user)
|
||||
conn = get(conn, Routes.billing_path(conn, :change_plan_form))
|
||||
|
||||
conn = get(conn, "/billing/upgrade/enterprise/#{plan.id}")
|
||||
assert html_response(conn, 200) =~ "Change subscription plan"
|
||||
end
|
||||
|
||||
assert html_response(conn, 200) =~ "Upgrade your free trial"
|
||||
assert html_response(conn, 200) =~ "enterprise plan"
|
||||
test "redirects to /upgrade if user does not have a subscription", %{conn: conn} do
|
||||
conn = get(conn, Routes.billing_path(conn, :change_plan_form))
|
||||
|
||||
assert redirected_to(conn) == Routes.billing_path(conn, :upgrade)
|
||||
end
|
||||
|
||||
test "redirects to change-plan page if user is already subscribed to the given enterprise plan",
|
||||
test "redirects to enterprise upgrade page if user has an enterprise plan configured",
|
||||
%{conn: conn, user: user} do
|
||||
plan = insert(:enterprise_plan, user: user)
|
||||
insert(:subscription, paddle_plan_id: plan.paddle_plan_id, user: user)
|
||||
insert(:enterprise_plan, user: user, paddle_plan_id: "123")
|
||||
conn = get(conn, Routes.billing_path(conn, :change_plan_form))
|
||||
assert redirected_to(conn) == Routes.billing_path(conn, :upgrade_to_enterprise_plan)
|
||||
end
|
||||
end
|
||||
|
||||
conn = get(conn, "/billing/upgrade/enterprise/#{plan.id}")
|
||||
describe "GET /change-plan/enterprise/:plan_id (deprecated)" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
assert redirected_to(conn) == "/billing/change-plan"
|
||||
test "redirects to the new :upgrade_to_enterprise_plan action", %{conn: conn} do
|
||||
conn = get(conn, Routes.billing_path(conn, :change_enterprise_plan, "123"))
|
||||
assert redirected_to(conn) == Routes.billing_path(conn, :upgrade_to_enterprise_plan)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /change-plan" do
|
||||
describe "POST /change-plan" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "shows change plan page if user has subsription", %{conn: conn, user: user} do
|
||||
test "calls Paddle API to update subscription", %{conn: conn, user: user} do
|
||||
insert(:subscription, user: user)
|
||||
conn = get(conn, "/billing/change-plan")
|
||||
|
||||
assert html_response(conn, 200) =~ "Change subscription plan"
|
||||
post(conn, Routes.billing_path(conn, :change_plan, "123123"))
|
||||
|
||||
subscription = Plausible.Repo.get_by(Plausible.Billing.Subscription, user_id: user.id)
|
||||
assert subscription.paddle_plan_id == "123123"
|
||||
assert subscription.next_bill_date == ~D[2019-07-10]
|
||||
assert subscription.next_bill_amount == "6.00"
|
||||
end
|
||||
end
|
||||
|
||||
test "redirects to /upgrade if user does not have a subscription", %{conn: conn} do
|
||||
conn = get(conn, "/billing/change-plan")
|
||||
describe "GET /billing/upgrade-success" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
assert redirected_to(conn) == "/billing/upgrade"
|
||||
test "shows success page after user subscribes", %{conn: conn} do
|
||||
conn = get(conn, Routes.billing_path(conn, :upgrade_success))
|
||||
|
||||
assert html_response(conn, 200) =~ "Your account is being upgraded"
|
||||
end
|
||||
end
|
||||
|
||||
test "redirects to enterprise upgrade page if user is due for an enteprise plan upgrade",
|
||||
%{conn: conn, user: user} do
|
||||
insert(:subscription, user: user, paddle_plan_id: "standard-plan-id")
|
||||
enterprise_plan = insert(:enterprise_plan, user: user, paddle_plan_id: "new-custom-id")
|
||||
@configured_enterprise_plan_paddle_plan_id "123"
|
||||
|
||||
conn = get(conn, "/billing/change-plan")
|
||||
describe "GET /upgrade-to-enterprise-plan (no existing subscription)" do
|
||||
setup [:create_user, :log_in, :configure_enterprise_plan]
|
||||
|
||||
assert redirected_to(conn) == "/billing/change-plan/enterprise/#{enterprise_plan.id}"
|
||||
test "displays basic page content", %{conn: conn} do
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
|> html_response(200)
|
||||
|
||||
assert doc =~ "Upgrade to Enterprise"
|
||||
assert doc =~ "prepared a custom enterprise plan for your account with the following limits"
|
||||
assert doc =~ "Questions?"
|
||||
assert doc =~ "Contact us"
|
||||
assert doc =~ "+ VAT if applicable"
|
||||
assert doc =~ "Click the button below to upgrade"
|
||||
assert doc =~ "Pay securely via Paddle"
|
||||
end
|
||||
|
||||
test "displays info about the enterprise plan to upgrade to", %{conn: conn} do
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
|> html_response(200)
|
||||
|
||||
assert doc =~ ~r/Up to\s*<b>\s*50M\s*<\/b>\s*monthly pageviews/
|
||||
assert doc =~ ~r/Up to\s*<b>\s*20k\s*<\/b>\s*sites/
|
||||
assert doc =~ ~r/Up to\s*<b>\s*5k\s*<\/b>\s*hourly api requests/
|
||||
assert doc =~ ~r/The plan is priced at\s*<b>\s*€10\s*<\/b>\s*/
|
||||
assert doc =~ "per year"
|
||||
end
|
||||
|
||||
test "prompts to contact us if user has enterprise plan and existing subscription",
|
||||
test "data-product attribute on the checkout link is the paddle_plan_id of the enterprise plan",
|
||||
%{conn: conn, user: user} do
|
||||
insert(:subscription, user: user, paddle_plan_id: "enterprise-plan-id")
|
||||
insert(:enterprise_plan, user: user, paddle_plan_id: "enterprise-plan-id")
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
|> html_response(200)
|
||||
|
||||
assert %{
|
||||
"disableLogout" => true,
|
||||
"email" => user.email,
|
||||
"passthrough" => user.id,
|
||||
"product" => @configured_enterprise_plan_paddle_plan_id,
|
||||
"success" => Routes.billing_path(PlausibleWeb.Endpoint, :upgrade_success),
|
||||
"theme" => "none"
|
||||
} == get_paddle_checkout_params(find(doc, "#paddle-button"))
|
||||
end
|
||||
end
|
||||
|
||||
conn = get(conn, "/billing/change-plan")
|
||||
describe "GET /upgrade-to-enterprise-plan (active subscription, new enterprise plan configured)" do
|
||||
setup [:create_user, :log_in, :subscribe_enterprise, :configure_enterprise_plan]
|
||||
|
||||
test "displays basic page content", %{conn: conn} do
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
|> html_response(200)
|
||||
|
||||
assert doc =~ "Change subscription plan"
|
||||
assert doc =~ "prepared your account for an upgrade to custom limits"
|
||||
assert doc =~ "+ VAT if applicable"
|
||||
assert doc =~ "calculate the prorated amount that your card will be charged"
|
||||
assert doc =~ "Preview changes"
|
||||
assert doc =~ "Questions?"
|
||||
assert doc =~ "Contact us"
|
||||
end
|
||||
|
||||
assert html_response(conn, 200) =~ "please contact us"
|
||||
test "displays info about the enterprise plan to upgrade to", %{conn: conn} do
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
|> html_response(200)
|
||||
|
||||
assert doc =~ ~r/Up to\s*<b>\s*50M\s*<\/b>\s*monthly pageviews/
|
||||
assert doc =~ ~r/Up to\s*<b>\s*20k\s*<\/b>\s*sites/
|
||||
assert doc =~ ~r/Up to\s*<b>\s*5k\s*<\/b>\s*hourly api requests/
|
||||
assert doc =~ ~r/The plan is priced at\s*<b>\s*€10\s*<\/b>\s*/
|
||||
assert doc =~ "per year"
|
||||
end
|
||||
|
||||
test "preview changes links to :change_plan_preview action", %{conn: conn} do
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
|> html_response(200)
|
||||
|
||||
preview_changes_link = find(doc, "#preview-changes")
|
||||
assert text(preview_changes_link) == "Preview changes"
|
||||
|
||||
assert text_of_attr(preview_changes_link, "href") ==
|
||||
Routes.billing_path(
|
||||
PlausibleWeb.Endpoint,
|
||||
:change_plan_preview,
|
||||
@configured_enterprise_plan_paddle_plan_id
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /change-plan/enterprise/:plan_id" do
|
||||
setup [:create_user, :log_in]
|
||||
@enterprise_contact_link "enterprise@plausible.io"
|
||||
|
||||
test "shows change plan page if user has subsription and enterprise plan", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
insert(:subscription, user: user)
|
||||
describe "GET /upgrade-to-enterprise-plan (already subscribed to latest enterprise plan)" do
|
||||
setup [:create_user, :log_in, :configure_enterprise_plan]
|
||||
|
||||
plan =
|
||||
insert(:enterprise_plan,
|
||||
user: user,
|
||||
monthly_pageview_limit: 1000,
|
||||
hourly_api_request_limit: 500,
|
||||
site_limit: 100
|
||||
)
|
||||
setup context do
|
||||
subscribe_enterprise(context, paddle_plan_id: @configured_enterprise_plan_paddle_plan_id)
|
||||
end
|
||||
|
||||
conn = get(conn, "/billing/change-plan/enterprise/#{plan.id}")
|
||||
test "renders contact note", %{conn: conn} do
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
|> html_response(200)
|
||||
|
||||
assert html_response(conn, 200) =~ "Change subscription plan"
|
||||
assert html_response(conn, 200) =~ "Up to <b>1k</b> monthly pageviews"
|
||||
assert html_response(conn, 200) =~ "Up to <b>500</b> hourly api requests"
|
||||
assert html_response(conn, 200) =~ "Up to <b>100</b> sites"
|
||||
assert doc =~ "Need to change your limits?"
|
||||
assert doc =~ "Your account is on an enterprise plan"
|
||||
assert doc =~ "contact us at #{@enterprise_contact_link}"
|
||||
end
|
||||
end
|
||||
|
||||
test "renders 404 is user does not have enterprise plan", %{conn: conn, user: user} do
|
||||
insert(:subscription, user: user)
|
||||
conn = get(conn, "/billing/change-plan/enterprise/123")
|
||||
describe "GET /upgrade-to-enterprise-plan (subscription past_due or paused)" do
|
||||
setup [:create_user, :log_in, :configure_enterprise_plan]
|
||||
|
||||
test "redirects to /settings when past_due", %{conn: conn} = context do
|
||||
subscribe_enterprise(context, status: Subscription.Status.past_due())
|
||||
conn = get(conn, Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
assert redirected_to(conn) == "/settings"
|
||||
end
|
||||
|
||||
assert conn.status == 404
|
||||
test "redirects to /settings when paused", %{conn: conn} = context do
|
||||
subscribe_enterprise(context, status: Subscription.Status.paused())
|
||||
conn = get(conn, Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
assert redirected_to(conn) == "/settings"
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /change-plan" do
|
||||
setup [:create_user, :log_in]
|
||||
describe "GET /upgrade-to-enterprise-plan (deleted enterprise subscription)" do
|
||||
setup [:create_user, :log_in, :configure_enterprise_plan]
|
||||
|
||||
test "calls Paddle API to update subscription", %{conn: conn, user: user} do
|
||||
insert(:subscription, user: user)
|
||||
setup context do
|
||||
subscribe_enterprise(context,
|
||||
paddle_plan_id: @configured_enterprise_plan_paddle_plan_id,
|
||||
status: Subscription.Status.deleted()
|
||||
)
|
||||
|
||||
post(conn, "/billing/change-plan/123123")
|
||||
context
|
||||
end
|
||||
|
||||
subscription = Plausible.Repo.get_by(Plausible.Billing.Subscription, user_id: user.id)
|
||||
assert subscription.paddle_plan_id == "123123"
|
||||
assert subscription.next_bill_date == ~D[2019-07-10]
|
||||
assert subscription.next_bill_amount == "6.00"
|
||||
test "displays the same content as for a user without a subscription", %{conn: conn} do
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
|> html_response(200)
|
||||
|
||||
assert doc =~ "Upgrade to Enterprise"
|
||||
assert doc =~ "prepared a custom enterprise plan for your account with the following limits"
|
||||
assert doc =~ "Questions?"
|
||||
assert doc =~ "Contact us"
|
||||
assert doc =~ "+ VAT if applicable"
|
||||
assert doc =~ "Click the button below to upgrade"
|
||||
assert doc =~ "Pay securely via Paddle"
|
||||
end
|
||||
|
||||
test "still allows to subscribe back to the same plan", %{conn: conn} do
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
|> html_response(200)
|
||||
|
||||
assert doc =~ ~r/Up to\s*<b>\s*50M\s*<\/b>\s*monthly pageviews/
|
||||
assert doc =~ ~r/Up to\s*<b>\s*20k\s*<\/b>\s*sites/
|
||||
assert doc =~ ~r/Up to\s*<b>\s*5k\s*<\/b>\s*hourly api requests/
|
||||
assert doc =~ ~r/The plan is priced at\s*<b>\s*€10\s*<\/b>\s*/
|
||||
assert doc =~ "per year"
|
||||
end
|
||||
|
||||
test "renders paddle button with the correct checkout params",
|
||||
%{conn: conn, user: user} do
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|
||||
|> html_response(200)
|
||||
|
||||
assert %{
|
||||
"disableLogout" => true,
|
||||
"email" => user.email,
|
||||
"passthrough" => user.id,
|
||||
"product" => @configured_enterprise_plan_paddle_plan_id,
|
||||
"success" => Routes.billing_path(PlausibleWeb.Endpoint, :upgrade_success),
|
||||
"theme" => "none"
|
||||
} == get_paddle_checkout_params(find(doc, "#paddle-button"))
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /billing/upgrade-success" do
|
||||
setup [:create_user, :log_in]
|
||||
defp configure_enterprise_plan(%{user: user}) do
|
||||
insert(:enterprise_plan,
|
||||
user_id: user.id,
|
||||
paddle_plan_id: "123",
|
||||
billing_interval: :yearly,
|
||||
monthly_pageview_limit: 50_000_000,
|
||||
site_limit: 20_000,
|
||||
hourly_api_request_limit: 5000,
|
||||
inserted_at: Timex.now() |> Timex.shift(hours: 1)
|
||||
)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
test "shows success page after user subscribes", %{conn: conn} do
|
||||
conn = get(conn, "/billing/upgrade-success")
|
||||
defp subscribe_enterprise(%{user: user}, opts \\ []) do
|
||||
opts =
|
||||
opts
|
||||
|> Keyword.put(:user, user)
|
||||
|> Keyword.put_new(:paddle_plan_id, "321")
|
||||
|> Keyword.put_new(:status, Subscription.Status.active())
|
||||
|
||||
assert html_response(conn, 200) =~ "Your account is being upgraded"
|
||||
insert(:subscription, opts)
|
||||
|
||||
{:ok, user: Plausible.Users.with_subscription(user)}
|
||||
end
|
||||
|
||||
defp get_paddle_checkout_params(element) do
|
||||
with onclick <- text_of_attr(element, "onclick"),
|
||||
[[_, checkout_params_str]] <- Regex.scan(~r/Paddle\.Checkout\.open\((.*?)\)/, onclick),
|
||||
{:ok, checkout_params} <- Jason.decode(checkout_params_str) do
|
||||
checkout_params
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue