Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Yearly billing (#58)
Uku Taht9476ccf95cb5
diff --git a/config/config.exs b/config/config.exsindex ac51e693d8..a8695a37e0 100644--- a/config/config.exs+++ b/config/config.exs@@ -48,6 +48,10 @@ config :plausible,config :plausible,session_timeout: 1000 * 60 * 30 # 30 minutes+config :plausible, :paddle,+ vendor_id: "49430",+ vendor_auth_code: System.get_env("PADDLE_VENDOR_AUTH_CODE")+# Import environment specific config. This must remain at the bottom# of this file so it overrides the configuration defined above.import_config "#{Mix.env()}.exs"diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.exindex 2487826deb..b9c01cf702 100644--- a/lib/plausible/billing/billing.ex+++ b/lib/plausible/billing/billing.ex@@ -1,6 +1,6 @@defmodule Plausible.Billing douse Plausible.Repo- alias Plausible.Billing.{Subscription, Plans, PaddleApi}+ alias Plausible.Billing.{Subscription, PaddleApi}@paddle_api Application.fetch_env!(:plausible, :paddle_api)def active_subscription_for(user_id) do@@ -59,22 +59,30 @@ defmodule Plausible.Billing doendend- def change_plan(user, new_plan) do+ def change_plan(user, new_plan_id) dosubscription = active_subscription_for(user.id)- res = PaddleApi.update_subscription(subscription.paddle_subscription_id, %{- plan_id: Plans.paddle_id_for_plan(new_plan)+ res = @paddle_api.update_subscription(subscription.paddle_subscription_id, %{+ plan_id: new_plan_id})case res do{:ok, response} ->+ amount = :erlang.float_to_binary(response["next_payment"]["amount"] / 1, decimals: 2)+Subscription.changeset(subscription, %{- paddle_plan_id: Integer.to_string(response["plan_id"])+ paddle_plan_id: Integer.to_string(response["plan_id"]),+ next_bill_amount: amount,+ next_bill_date: response["next_payment"]["date"],}) |> Repo.updatee -> eendend+ def change_plan_preview(subscription, new_plan_id) do+ PaddleApi.update_subscription_preview(subscription.paddle_subscription_id, new_plan_id)+ end+def needs_to_upgrade?(user) doif Timex.before?(user.trial_expiry_date, Timex.today()) do!active_subscription_for(user.id)diff --git a/lib/plausible/billing/paddle_api.ex b/lib/plausible/billing/paddle_api.exindex a619ef5485..941c3f16e3 100644--- a/lib/plausible/billing/paddle_api.ex+++ b/lib/plausible/billing/paddle_api.ex@@ -1,13 +1,36 @@defmodule Plausible.Billing.PaddleApi do+ @update_preview_endpoint "https://vendors.paddle.com/api/2.0/subscription/preview_update"@update_endpoint "https://vendors.paddle.com/api/2.0/subscription/users/update"@get_endpoint "https://vendors.paddle.com/api/2.0/subscription/users"- @vendor_id "49430"- @vendor_auth_code System.get_env("PADDLE_VENDOR_AUTH_CODE")+ @vendor_id Keyword.fetch!(Application.get_env(:plausible, :paddle), :vendor_id)+ @vendor_auth_code Keyword.fetch!(Application.get_env(:plausible, :paddle), :vendor_auth_code)@headers [{"Content-type", "application/json"},{"Accept", "application/json"}]+ def update_subscription_preview(paddle_subscription_id, new_plan_id) do+ params = %{+ vendor_id: @vendor_id,+ vendor_auth_code: @vendor_auth_code,+ subscription_id: paddle_subscription_id,+ plan_id: new_plan_id,+ prorate: true,+ keep_modifiers: true,+ bill_immediately: true,+ quantity: 1+ }++ {:ok, response} = HTTPoison.post(@update_preview_endpoint, Poison.encode!(params), @headers)+ body = Poison.decode!(response.body)++ if body["success"] do+ {:ok, body["response"]}+ else+ {:error, body["error"]}+ end+ end+def update_subscription(paddle_subscription_id, params) doparams = Map.merge(params, %{vendor_id: @vendor_id,diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.exindex 50589a399f..9e10dd3854 100644--- a/lib/plausible/billing/plans.ex+++ b/lib/plausible/billing/plans.ex@@ -1,44 +1,32 @@defmodule Plausible.Billing.Plans do- @app_env System.get_env("APP_ENV") || "dev"-- @real_plans %{- personal: "558018",- startup: "558745",- business: "558746"- }-- @test_plans %{- personal: "558156",- startup: "558199",- business: "558200"+ @plans %{+ monthly: %{+ "10k": %{product_id: "558018", due_now: "$6"},+ "100k": %{product_id: "558745", due_now: "$12"},+ "1m": %{product_id: "558746", due_now: "$36"},+ },+ yearly: %{+ "10k": %{product_id: "572810", due_now: "$48"},+ "100k": %{product_id: "590752", due_now: "$96"},+ "1m": %{product_id: "590753", due_now: "$288"}+ },}- def paddle_id_for_plan(plan) do- if @app_env == "prod" do- @real_plans[plan]- else- @test_plans[plan]- end- end-- def is?(subscription, plan) do- paddle_id_for_plan(plan) == subscription.paddle_plan_id+ def plans do+ @plansenddef allowance(subscription) do- cond do- subscription.paddle_plan_id == "572810" -> # Personal annual- 10_000- subscription.paddle_plan_id == "free_10k" ->- 10_000- is?(subscription, :personal) ->- 10_000- is?(subscription, :startup) ->- 100_000- is?(subscription, :business) ->- 1_000_000- true ->- raise "Subscription not found for #{subscription.paddle_plan_id}"- end+ allowed_volume = %{+ "free_10k" => 10_000,+ "558018" => 10_000,+ "572810" => 10_000,+ "558745" => 100_000,+ "590752" => 100_000,+ "558746" => 1_000_000,+ "590753" => 1_000_000,+ }++ allowed_volume[subscription.paddle_plan_id]endenddiff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.exindex e93408f06f..a29d30482d 100644--- a/lib/plausible_web/controllers/billing_controller.ex+++ b/lib/plausible_web/controllers/billing_controller.ex@@ -8,6 +8,7 @@ defmodule PlausibleWeb.BillingController dodef change_plan_form(conn, _params) dosubscription = Billing.active_subscription_for(conn.assigns[:current_user].id)+if subscription dorender(conn, "change_plan.html", subscription: subscription, layout: {PlausibleWeb.LayoutView, "focus.html"})else@@ -15,16 +16,25 @@ defmodule PlausibleWeb.BillingController doendend- def change_plan(conn, %{"plan_name" => plan}) when plan in ["personal", "startup", "business"] do- new_plan = String.to_existing_atom(plan)+ def change_plan_preview(conn, %{"plan_id" => new_plan_id}) do+ subscription = Billing.active_subscription_for(conn.assigns[:current_user].id)++ if subscription do+ {:ok, preview_info} = Billing.change_plan_preview(subscription, new_plan_id)+ render(conn, "change_plan_preview.html", subscription: subscription, preview_info: preview_info, layout: {PlausibleWeb.LayoutView, "focus.html"})+ else+ redirect(conn, to: "/billing/upgrade")+ end+ end- case Billing.change_plan(conn.assigns[:current_user], new_plan) do+ def change_plan(conn, %{"new_plan_id" => new_plan_id}) do+ case Billing.change_plan(conn.assigns[:current_user], new_plan_id) do{:ok, _subscription} ->conn|> put_flash(:success, "Plan changed successfully")|> redirect(to: "/settings"){:error, e} ->- Sentry.capture_message("Error changing plans", extra: %{errors: inspect(e), new_plan: new_plan, user_id: conn.assigns[:current_user].id})+ Sentry.capture_message("Error changing plans", extra: %{errors: inspect(e), new_plan_id: new_plan_id, user_id: conn.assigns[:current_user].id})conn|> put_flash(:error, "Something went wrong. Please try again or contact support at [email protected]")|> redirect(to: "/settings")diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.exindex 710a5b0f18..efa3edbef4 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -98,7 +98,8 @@ defmodule PlausibleWeb.Router dopost "/contact", PageController, :submit_contact_formget "/billing/change-plan", BillingController, :change_plan_form- post "/billing/change-plan/:plan_name", BillingController, :change_plan+ get "/billing/change-plan/preview/:plan_id", BillingController, :change_plan_preview+ post "/billing/change-plan/:new_plan_id", BillingController, :change_planget "/billing/upgrade", BillingController, :upgradeget "/billing/success", BillingController, :successdiff --git a/lib/plausible_web/templates/billing/_checkout_button.html.eex b/lib/plausible_web/templates/billing/_checkout_button.html.eexdeleted file mode 100644index 3865b025bc..0000000000--- a/lib/plausible_web/templates/billing/_checkout_button.html.eex+++ /dev/null@@ -1 +0,0 @@-<button class="paddle_button button button-sm" data-theme="none" data-product="<%= Plausible.Billing.Plans.paddle_id_for_plan(@plan) %>" data-email="<%= @conn.assigns[:current_user].email %>" data-disable-logout="true" data-passthrough="<%= @conn.assigns[:current_user].id %>" data-success="/billing/success">Select</button>diff --git a/lib/plausible_web/templates/billing/change_plan.html.eex b/lib/plausible_web/templates/billing/change_plan.html.eexindex 0c131755ec..a4abc18b99 100644--- a/lib/plausible_web/templates/billing/change_plan.html.eex+++ b/lib/plausible_web/templates/billing/change_plan.html.eex@@ -1,56 +1,100 @@-<div class="max-w-md w-full mx-auto bg-white shadow-md rounded px-8 py-6 mb-4 mt-8">- <h2 class="text-xl font-black">Change subscription plan</h2>+<script>+ window.plans = <%= raw Jason.encode!(Plausible.Billing.Plans.plans()) %>+</script>- <div class="py-4">- If you choose to change your plan, your current subscription will- be prorated.- </div>+<div class="mx-auto mt-6 text-center">+ <h1 class="text-3xl font-black">Change subscription plan</h1>+</div>- <div class="w-full flex justify-between items-center py-4 border-t">- <span><b>10,000</b> / mo</span>- <b class="bg-orange-200 text-orange-900 p-1 rounded-sm">$6 / mo</b>- <%= if Plausible.Billing.Plans.is?(@subscription, :personal) do %>- <div>- <svg class="feather text-green-500"><use xlink:href="#feather-check-circle" /></svg>- Selected- </div>- <% else %>- <%= button("Select", class: "button button-sm", method: :post, to: "/billing/change-plan/personal") %>- <% end %>- </div>- <div class="w-full flex justify-between items-center py-4 border-t">- <span><b>100,000</b> / mo</span>- <b class="bg-orange-200 text-orange-900 p-1 rounded-sm">$12 / mo</b>- <%= if Plausible.Billing.Plans.is?(@subscription, :startup) do %>- <div>- <svg class="feather text-green-500"><use xlink:href="#feather-check-circle" /></svg>- Selected- </div>- <% else %>- <%= button("Select", class: "button button-sm", method: :post, to: "/billing/change-plan/startup") %>- <% end %>- </div>- <div class="w-full flex justify-between items-center py-4 border-t">- <span><b>1,000,000</b> / mo</span>- <b class="bg-orange-200 text-orange-900 p-1 rounded-sm">$36 / mo</b>- <%= if Plausible.Billing.Plans.is?(@subscription, :business) do %>- <div>- <svg class="feather text-green-500"><use xlink:href="#feather-check-circle" /></svg>- Selected+<div class="w-full max-w-lg px-4 mt-4 mx-auto">+ <div x-data="{volume: '10k', billingCycle: 'monthly'}" class="flex-1 bg-white shadow-md rounded px-8 py-4 mb-4 mt-8">+ <div class="w-full pt-2 text-xl font-bold">+ Select your new plan+ </div>++ <div class="w-full text-gray-500 text-sm">+ Depending on which plan you choose, your card might be charged immediately and your+ next payment date could change. You can preview these changes before committing.+ </div>++ <div class="pt-8"></div>++ <span class="relative z-0 inline-flex shadow-sm w-full">+ <button type="button" @click="billingCycle = 'monthly'" :class="{'bg-indigo-600 text-white border-indigo-600': billingCycle === 'monthly', 'bg-white text-gray-700 hover:text-gray-500 border-gray-300': billingCycle === 'yearly'}" class="relative w-full text-center px-4 py-2 rounded-l-md border text-sm leading-5 font-medium focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150">+ Monthly billing+ </button>+ <button type="button" @click="billingCycle = 'yearly'" :class="{'bg-indigo-600 text-white border-indigo-600': billingCycle === 'yearly', 'bg-white text-gray-700 hover:text-gray-500 border-gray-300': billingCycle === 'monthly'}" class="-ml-px relative w-full text-center px-4 py-2 rounded-r-md border text-sm leading-5 font-medium focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150">+ Yearly billing+ <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium leading-4 bg-yellow-100 text-yellow-800">+ -33%+ </span>+ </button>+ </span>++ <div class="pt-6"></div>++ <div class="flex flex-col">+ <div class="-my-2 py-2 overflow-x-auto sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8">+ <div class="align-middle inline-block min-w-full shadow overflow-hidden sm:rounded-lg border-b border-gray-200">+ <table class="min-w-full">+ <thead>+ <tr>+ <th class="px-6 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">+ Monthly pageviews+ </th>+ <th class="px-6 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">+ Price per month+ </th>+ </tr>+ </thead>+ <tbody class="bg-white">+ <tr @click="volume = '10k'" :class="{'border-2 border-green-300 bg-green-50': volume === '10k', 'border-b border-gray-200 cursor-pointer': volume !== '10k'}">+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': volume === '10k'}">10k</td>+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': volume === '10k'}">+ <span x-show="billingCycle === 'monthly'">$6</span>+ <span x-show="billingCycle === 'yearly'">$4</span>+ </td>+ </tr>+ <tr @click="volume = '100k'" :class="{'border-2 border-green-300 bg-green-50': volume === '100k', 'border-b border-gray-200 cursor-pointer': volume !== '100k'}">+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': volume === '100k'}">100k</td>+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': volume === '100k'}">+ <span x-show="billingCycle === 'monthly'">$12</span>+ <span x-show="billingCycle === 'yearly'">$8</span>+ </td>+ </tr>+ <tr @click="volume = '1m'" :class="{'border-2 border-green-300 bg-green-50': volume === '1m', 'border-b border-gray-200 cursor-pointer': volume !== '1m'}">+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': volume === '1m'}">1m</td>+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': volume === '1m'}">+ <span x-show="billingCycle === 'monthly'">$36</span>+ <span x-show="billingCycle === 'yearly'">$24</span>+ </td>+ </tr>+ </tbody>+ </table>+ </div></div>- <% else %>- <%= button("Select", class: "button button-sm", method: :post, to: "/billing/change-plan/business") %>- <% end %>- </div>+ </div>- <script type="text/javascript" src="https://cdn.paddle.com/paddle/paddle.js"></script>- <script type="text/javascript" src="<%= Routes.static_path(@conn, "/js/checkout.js") %>"></script>- <script type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>- <script>- window.Checkout.init(- <%= @conn.assigns[:current_user].id %>,- '<%= @conn.assigns[:current_user].email %>'- )- </script>+ <div class="text-right mt-6">+ <span class="inline-flex rounded-md shadow-sm">+ <a x-show="window.plans[billingCycle][volume].product_id !== '<%= @subscription.paddle_plan_id %>'" style="display: none;" :href="'/billing/change-plan/preview/' + window.plans[billingCycle][volume].product_id" type="button" class="inline-flex items-center px-4 py-2 border border-transparent text-sm leading-5 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:shadow-outline-indigo active:bg-indigo-700 transition ease-in-out duration-150">+ <svg fill="currentColor" viewBox="0 0 20 20" class="w-4 h-4 inline 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+ </a>+ <a x-show="window.plans[billingCycle][volume].product_id === '<%= @subscription.paddle_plan_id %>'" style="display: none;" tooltip="Select a different plan to continue" type="button" class="inline-flex items-center px-4 py-2 border border-transparent text-sm leading-5 font-medium rounded-md text-white bg-gray-400">+ <svg fill="currentColor" viewBox="0 0 20 20" class="w-4 h-4 inline 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+ </a>+ </span>+ </div>+ </div>+</div>++<div class="text-center mt-8">+ Questions? Contact <%= link("[email protected]", to: "mailto: [email protected]", class: "text-indigo-500") %></div>++<script type="text/javascript" src="https://cdn.paddle.com/paddle/paddle.js"></script>+<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>+<script>Paddle.Setup({vendor: 49430})</script>diff --git a/lib/plausible_web/templates/billing/change_plan_preview.html.eex b/lib/plausible_web/templates/billing/change_plan_preview.html.eexnew file mode 100644index 0000000000..3bd61818d1--- /dev/null+++ b/lib/plausible_web/templates/billing/change_plan_preview.html.eex@@ -0,0 +1,86 @@+<div class="mx-auto mt-6 text-center">+ <h1 class="text-3xl font-black">Confirm new subscription plan</h1>+</div>++<div class="w-full max-w-lg px-4 mt-4 mx-auto">+ <div class="flex-1 bg-white shadow-md rounded px-8 py-4 mb-4 mt-8">+ <div class="text-lg font-bold">Due now</div>+ <div class="block text-gray-500 text-sm">+ Your card will be charged a pro-rated amount for the current billing period+ </div>++ <div class="flex flex-col mt-4">+ <div class="-my-2 py-2 overflow-x-auto sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8">+ <div class="align-middle inline-block min-w-full shadow overflow-hidden sm:rounded-lg border-b border-gray-200">+ <table class="min-w-full">+ <thead>+ <tr>+ <th class="px-6 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">+ Amount+ </th>+ <th class="px-6 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">+ Date+ </th>+ </tr>+ </thead>+ <tbody class="bg-white">+ <tr class="border-b border-gray-200">+ <td class="px-6 py-4 text-sm leading-5 font-bold">$<%= @preview_info["immediate_payment"]["amount"] %></td>+ <td class="px-6 py-4 text-sm leading-5"><%= present_date(@preview_info["immediate_payment"]["date"]) %></td>+ </tr>+ </tbody>+ </table>+ </div>+ </div>+ </div>++ <div class="pt-6"></div>++ <div class="py-2 text-lg font-bold">Next payment</div>+++ <div class="flex flex-col">+ <div class="-my-2 py-2 overflow-x-auto sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8">+ <div class="align-middle inline-block min-w-full shadow overflow-hidden sm:rounded-lg border-b border-gray-200">+ <table class="min-w-full">+ <thead>+ <tr>+ <th class="px-6 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">+ Amount+ </th>+ <th class="px-6 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">+ Date+ </th>+ </tr>+ </thead>+ <tbody class="bg-white">+ <tr class="border-b border-gray-200">+ <td class="px-6 py-4 text-sm leading-5 font-bold">$<%= @preview_info["next_payment"]["amount"] %></td>+ <td class="px-6 py-4 text-sm leading-5"><%= present_date(@preview_info["next_payment"]["date"]) %></td>+ </tr>+ </tbody>+ </table>+ </div>+ </div>+ </div>++ <div class="flex items-center justify-between mt-10">+ <span class="flex rounded-md shadow-sm">+ <a href="/billing/change-plan" type="button" class="inline-flex items-center px-4 py-2 border border-gray-300 text-sm leading-5 font-medium rounded-md text-gray-700 bg-white hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue active:text-gray-800 active:bg-gray-50 transition ease-in-out duration-150">+ Back+ </a>+ </span>+ <span class="flex space-betwee rounded-md shadow-sm">+ <%= button("Confirm plan change", to: "/billing/change-plan/#{@preview_info["plan_id"]}", method: :post, class: "inline-flex items-center px-4 py-2 border border-transparent text-sm leading-5 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:shadow-outline-indigo active:bg-indigo-700 transition ease-in-out duration-150") %>+ </span>+ </div>+ </div>+</div>++<div class="text-center mt-8">+ Questions? Contact <%= link("[email protected]", to: "mailto: [email protected]", class: "text-indigo-500") %>+</div>++<script type="text/javascript" src="https://cdn.paddle.com/paddle/paddle.js"></script>+<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>+<script>Paddle.Setup({vendor: 49430})</script>diff --git a/lib/plausible_web/templates/billing/upgrade.html.eex b/lib/plausible_web/templates/billing/upgrade.html.eexindex daade17187..6806b20b31 100644--- a/lib/plausible_web/templates/billing/upgrade.html.eex+++ b/lib/plausible_web/templates/billing/upgrade.html.eex@@ -1,56 +1,102 @@-<div class="max-w-lg w-full mx-auto bg-white shadow-md rounded px-8 py-6 mb-4 mt-8">- <h2 class="text-xl font-black">Upgrade your free trial</h2>-- <div class="mt-2">- <%= cond do %>- <% Timex.equal?(@user.trial_expiry_date, @today) -> %>- Today is the last day of your trial. Please select a plan and enter- your billing info to access your stats after the trial ends.- <% Timex.after?(@user.trial_expiry_date, @today) -> %>- <p class="py-2">- Your free trial ends on <b><%= Timex.format!(@user.trial_expiry_date, "{WDshort} {D} {Mshort}") %></b>. Please select a plan and enter- your billing info to access your stats after the trial ends.- </p>- <% Timex.before?(@user.trial_expiry_date, @today) -> %>- <p class="py-2">- Your free trial ended on <%= Timex.format!(@user.trial_expiry_date, "{Mshort} {D}")%>. To access your stats going forward, please select a plan and enter your billing info.- </p>- <% end %>-- <p class="py-2">- If you go over your limit in the future, we will contact you about upgrading.- Don't worry, we will never bill you unexpectedly or stop recording without telling you first.- </p>- <p class="py-2">- All plans are on a monthly basis, cancel any time.- </p>- </div>- <div class="pt-6"></div>+<script>+ window.plans = <%= raw Jason.encode!(Plausible.Billing.Plans.plans()) %>+</script>- <div class="w-full flex justify-between items-center py-4 border-t">- <span><b>10,000</b> / mo</span>- <b class="bg-orange-200 text-orange-900 p-1 rounded-sm">$6 / mo</b>- <%= render("_checkout_button.html", conn: @conn, plan: :personal) %>- </div>- <div class="w-full flex justify-between items-center py-4 border-t">- <span><b>100,000</b> / mo</span>- <b class="bg-orange-200 text-orange-900 p-1 rounded-sm">$12 / mo</b>- <%= render("_checkout_button.html", conn: @conn, plan: :startup) %>- </div>- <div class="w-full flex justify-between items-center py-4 border-t">- <span><b>1,000,000</b> / mo</span>- <b class="bg-orange-200 text-orange-900 p-1 rounded-sm">$36 / mo</b>- <%= render("_checkout_button.html", conn: @conn, plan: :business) %>- </div>- <div class="w-full flex justify-between items-center py-4 border-t">- <span>Your usage: <b><%= PlausibleWeb.AuthView.delimit_integer(@usage) %></b> pageviews in the last 30 days</span>- </div>+<div class="mx-auto mt-6 text-center">+ <h1 class="text-3xl font-black">Upgrade your free trial</h1>+</div>++<div class="w-full max-w-4xl px-4 mt-4 mx-auto flex flex-col md:flex-row">+ <div x-data="{planSize: '10k', billingCycle: 'monthly'}" class="flex-1 bg-white shadow-md rounded px-8 py-4 mb-4 mt-8">+ <div class="w-full py-4">+ <span>You've used <b><%= PlausibleWeb.AuthView.delimit_integer(@usage) %></b> pageviews in the last 30 days</span>+ </div>++ <div class="pt-2"></div>- <div class="text-center mt-8">- Questions? Contact <%= link("[email protected]", to: "mailto: [email protected]", class: "text-indigo-500") %>+ <span class="relative z-0 inline-flex shadow-sm w-full">+ <button type="button" @click="billingCycle = 'monthly'" :class="{'bg-indigo-600 text-white border-indigo-600': billingCycle === 'monthly', 'bg-white text-gray-700 hover:text-gray-500 border-gray-300': billingCycle === 'yearly'}" class="relative w-full text-center px-4 py-2 rounded-l-md border text-sm leading-5 font-medium focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150">+ Monthly billing+ </button>+ <button type="button" @click="billingCycle = 'yearly'" :class="{'bg-indigo-600 text-white border-indigo-600': billingCycle === 'yearly', 'bg-white text-gray-700 hover:text-gray-500 border-gray-300': billingCycle === 'monthly'}" class="-ml-px relative w-full text-center px-4 py-2 rounded-r-md border text-sm leading-5 font-medium focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150">+ Yearly billing+ <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium leading-4 bg-yellow-100 text-yellow-800">+ -33%+ </span>+ </button>+ </span>++ <div class="pt-6"></div>++ <div class="flex flex-col">+ <div class="-my-2 py-2 overflow-x-auto sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8">+ <div class="align-middle inline-block min-w-full shadow overflow-hidden sm:rounded-lg border-b border-gray-200">+ <table class="min-w-full">+ <thead>+ <tr>+ <th class="px-6 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">+ Monthly pageviews+ </th>+ <th class="px-6 py-3 border-b border-gray-200 bg-gray-100 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">+ Price per month+ </th>+ </tr>+ </thead>+ <tbody class="bg-white">+ <tr @click="planSize = '10k'" :class="{'border-2 border-green-300': planSize === '10k', 'border-b border-gray-200 cursor-pointer': planSize !== '10k'}">+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': planSize === '10k'}">10k</td>+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': planSize === '10k'}">+ <span x-show="billingCycle === 'monthly'">$6</span>+ <span x-show="billingCycle === 'yearly'">$4</span>+ </td>+ </tr>+ <tr @click="planSize = '100k'" :class="{'border-2 border-green-300': planSize === '100k', 'border-b border-gray-200 cursor-pointer': planSize !== '100k'}">+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': planSize === '100k'}">100k</td>+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': planSize === '100k'}">+ <span x-show="billingCycle === 'monthly'">$12</span>+ <span x-show="billingCycle === 'yearly'">$8</span>+ </td>+ </tr>+ <tr @click="planSize = '1m'" :class="{'border-2 border-green-300': planSize === '1m', 'border-b border-gray-200 cursor-pointer': planSize !== '1m'}">+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': planSize === '1m'}">1m</td>+ <td class="px-6 py-4 text-sm leading-5" :class="{'font-bold': planSize === '1m'}">+ <span x-show="billingCycle === 'monthly'">$36</span>+ <span x-show="billingCycle === 'yearly'">$24</span>+ </td>+ </tr>+ </tbody>+ </table>+ </div>+ </div>+ </div>++ <div class="text-right mt-6">+ <div class="mb-4 text-sm font-medium">Due today: <b x-text="window.plans[billingCycle][planSize].due_now">$6</b></div>+ <span class="inline-flex rounded-md shadow-sm">+ <button type="button" data-theme="none" :data-product="window.plans[billingCycle][planSize].product_id" data-email="<%= @conn.assigns[:current_user].email %>" data-disable-logout="true" data-passthrough="<%= @conn.assigns[:current_user].id %>" data-success="/billing/success" class="paddle_button inline-flex items-center px-4 py-2 border border-transparent text-sm leading-5 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:shadow-outline-indigo active:bg-indigo-700 transition ease-in-out duration-150">+ <svg fill="currentColor" viewBox="0 0 20 20" class="w-4 h-4 inline 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>+ </span>+ </div></div>- <script type="text/javascript" src="https://cdn.paddle.com/paddle/paddle.js"></script>- <script>Paddle.Setup({vendor: 49430})</script>+ <div class="flex-1 pt-14 pl-8">+ <h3 class="text-lg leading-6 font-medium text-gray-900">+ What happens if I go over my page views limit?+ </h3>+ <div class="mt-2 text-base leading-6 text-gray-500">+ You will never be charged extra for an occasional traffic spike. There are no surprise fees and your card will never be charged unexpectedly.<br /><br />+ If your page views exceed your plan for two consecutive months, we will contact you to upgrade to a higher plan for the following month. You will have two weeks to make a decision. You can decide to continue with a higher plan or to cancel your account at that point.+ </div>+ </div></div>++<div class="text-center mt-8">+ Questions? Contact <%= link("[email protected]", to: "mailto: [email protected]", class: "text-indigo-500") %>+</div>++<script type="text/javascript" src="https://cdn.paddle.com/paddle/paddle.js"></script>+<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>+<script>Paddle.Setup({vendor: 49430})</script>diff --git a/lib/plausible_web/templates/email/trial_upgrade_email.html.eex b/lib/plausible_web/templates/email/trial_upgrade_email.html.eexindex fc0e60d636..12154fbbd1 100644--- a/lib/plausible_web/templates/email/trial_upgrade_email.html.eex+++ b/lib/plausible_web/templates/email/trial_upgrade_email.html.eex@@ -4,6 +4,7 @@ Thanks for exploring Plausible, a simple and privacy-friendly alternative to Goo<br /><br />In the last month, your account has used <%= PlausibleWeb.AuthView.delimit_integer(@pageviews) %> pageviews.Based on that we recommend you select the <%= suggested_plan_name(@pageviews) %> plan which runs at <%= suggested_plan_cost(@pageviews) %>.+You can also go with yealy billing to get 33% off on your plan.<br /><br /><%= link("Upgrade now", to: "https://plausible.io/billing/upgrade") %><br /><br />diff --git a/lib/plausible_web/views/auth_view.ex b/lib/plausible_web/views/auth_view.exindex 51c82ee0d8..2c1d95dd6a 100644--- a/lib/plausible_web/views/auth_view.ex+++ b/lib/plausible_web/views/auth_view.ex@@ -2,14 +2,13 @@ defmodule PlausibleWeb.AuthView douse PlausibleWeb, :view@subscription_names %{- "558018" => "Personal",- "572810" => "Personal (A)",- "558745" => "Startup",- "558746" => "Business",- "558156" => "Personal (T)",- "558199" => "Startup (T)",- "558200" => "Business (T)",- "free_10k" => "Free 10k"+ "558018" => "10k / monthly",+ "558745" => "100k / monthly",+ "558746" => "1m / monthly",+ "572810" => "10k / yearly",+ "590752" => "100k / yearly",+ "590753" => "1m / yearly",+ "free_10k" => "10k / free"}def subscription_name(subscription) dodiff --git a/lib/plausible_web/views/billing_view.ex b/lib/plausible_web/views/billing_view.exindex d3fe4a2547..353f9b9aba 100644--- a/lib/plausible_web/views/billing_view.ex+++ b/lib/plausible_web/views/billing_view.ex@@ -1,6 +1,11 @@defmodule PlausibleWeb.BillingView douse PlausibleWeb, :view+ def present_date(date) do+ Date.from_iso8601!(date)+ |> Timex.format!("{D} {Mshort} {YYYY}")+ end+def reccommended_plan(usage) docond dousage < 9000 ->diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exsindex f09309a16e..b990dff3b3 100644--- a/test/plausible/billing/billing_test.exs+++ b/test/plausible/billing/billing_test.exs@@ -198,4 +198,18 @@ defmodule Plausible.BillingTest doassert res == {:ok, nil}endend++ describe "change_plan" do+ test "sets the next bill amount and date" do+ user = insert(:user)+ insert(:subscription, user: user)++ Billing.change_plan(user, "123123")++ subscription = 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+ endenddiff --git a/test/plausible_web/controllers/billing_controller_test.exs b/test/plausible_web/controllers/billing_controller_test.exsindex e5cfddf6d4..1376187c2c 100644--- a/test/plausible_web/controllers/billing_controller_test.exs+++ b/test/plausible_web/controllers/billing_controller_test.exs@@ -22,8 +22,15 @@ defmodule PlausibleWeb.BillingControllerTest dodescribe "POST /change-plan" dosetup [:create_user, :log_in]- test "calls Paddle API to update subscription" do+ test "calls Paddle API to update subscription", %{conn: conn, user: user} do+ insert(:subscription, user: user)++ post(conn, "/billing/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"endendenddiff --git a/test/support/paddle_api_mock.ex b/test/support/paddle_api_mock.exindex 3aa0189b8e..3789438fde 100644--- a/test/support/paddle_api_mock.ex+++ b/test/support/paddle_api_mock.ex@@ -7,4 +7,16 @@ defmodule Plausible.PaddleApi.Mock do}}}end++ def update_subscription(_, %{plan_id: new_plan_id}) do+ new_plan_id = String.to_integer(new_plan_id)++ {:ok, %{+ "plan_id" => new_plan_id,+ "next_payment" => %{+ "date" => "2019-07-10",+ "amount" => 6+ }+ }}+ endend