Change enterprise plans

pull/1775/head
Uku Taht 5 years ago
parent 3c93a2d91b
commit f00457c5d1

@ -1,5 +1,4 @@
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"
@headers [
@ -21,7 +20,13 @@ defmodule Plausible.Billing.PaddleApi do
quantity: 1
}
{:ok, response} = HTTPoison.post(@update_preview_endpoint, Jason.encode!(params), @headers)
{:ok, response} =
HTTPoison.post(
vendors_domain() <> "/api/2.0/subscription/preview_update",
Jason.encode!(params),
@headers
)
body = Jason.decode!(response.body)
if body["success"] do
@ -125,6 +130,13 @@ defmodule Plausible.Billing.PaddleApi do
end
end
def vendors_domain() do
case Application.get_env(:plausible, :environment) do
"dev" -> "https://sandbox-vendors.paddle.com"
_ -> "https://vendors.paddle.com"
end
end
defp get_config() do
Application.get_env(:plausible, :paddle)
end

@ -97,7 +97,10 @@ defmodule Plausible.Billing.Plans do
Map.fetch!(found, :limit)
else
enterprise_plan =
Repo.get_by(Plausible.Billing.EnterprisePlan, user_id: subscription.user_id)
Repo.get_by(Plausible.Billing.EnterprisePlan,
user_id: subscription.user_id,
paddle_plan_id: subscription.paddle_plan_id
)
if enterprise_plan do
enterprise_plan.monthly_pageview_limit
@ -111,6 +114,17 @@ defmodule Plausible.Billing.Plans do
end
end
def get_enterprise_plan(user) do
if user.subscription do
Repo.get_by(Plausible.Billing.EnterprisePlan,
user_id: user.id,
paddle_plan_id: user.subscription.paddle_plan_id
)
else
Repo.get_by(Plausible.Billing.EnterprisePlan, user_id: user.id)
end
end
def suggested_plan(user, usage) do
Enum.find(plans_for(user), fn plan -> usage < plan[:limit] end)
end

@ -30,35 +30,16 @@ defmodule PlausibleWeb.BillingController do
end
def upgrade_enterprise_plan(conn, %{"plan_id" => plan_id}) do
user =
conn.assigns[:current_user]
|> Repo.preload(:enterprise_plan)
if user.enterprise_plan && user.enterprise_plan.id == String.to_integer(plan_id) do
usage = Plausible.Billing.usage(conn.assigns[:current_user])
render(conn, "upgrade_to_plan.html",
usage: usage,
user: user,
layout: {PlausibleWeb.LayoutView, "focus.html"}
)
else
render_error(conn, 404)
end
end
def upgrade_to_plan(conn, %{"plan_id" => plan_id}) do
plan = Plausible.Billing.Plans.for_product_id(plan_id)
user = conn.assigns[:current_user]
plan = Repo.get_by(Plausible.Billing.EnterprisePlan, user_id: user.id, id: plan_id)
if plan do
cycle = if plan[:monthly_product_id] == plan_id, do: "monthly", else: "yearly"
plan = Map.merge(plan, %{cycle: cycle, product_id: plan_id})
usage = Plausible.Billing.usage(conn.assigns[:current_user])
render(conn, "upgrade_to_plan.html",
usage: usage,
user: user,
plan: plan,
user: conn.assigns[:current_user],
layout: {PlausibleWeb.LayoutView, "focus.html"}
)
else
@ -79,8 +60,10 @@ defmodule PlausibleWeb.BillingController do
cond do
subscription && user.enterprise_plan ->
redirect(conn,
to: Routes.billing_path(conn, :change_enterprise_plan, user.enterprise_plan.id)
render(conn, "change_enterprise_plan_contact_us.html",
user: user,
plan: user.enterprise_plan,
layout: {PlausibleWeb.LayoutView, "focus.html"}
)
subscription ->
@ -95,31 +78,30 @@ defmodule PlausibleWeb.BillingController do
end
def change_enterprise_plan(conn, %{"plan_id" => plan_id}) do
user =
conn.assigns[:current_user]
|> Repo.preload(:enterprise_plan)
user = conn.assigns[:current_user]
current_plan =
Repo.get_by(Plausible.Billing.EnterprisePlan,
user_id: user.id,
paddle_plan_id: user.subscription.paddle_plan_id
)
new_plan = Repo.get_by(Plausible.Billing.EnterprisePlan, user_id: user.id, id: plan_id)
cond do
is_nil(user.subscription) ->
redirect(conn, to: "/billing/upgrade")
is_nil(user.enterprise_plan) ->
is_nil(current_plan) ->
render_error(conn, 404)
user.enterprise_plan.id !== String.to_integer(plan_id) ->
is_nil(new_plan) || new_plan.paddle_plan_id == user.subscription.paddle_plan_id ->
render_error(conn, 404)
user.enterprise_plan.paddle_plan_id == user.subscription.paddle_plan_id ->
render(conn, "change_enterprise_plan_contact_us.html",
user: user,
plan: user.enterprise_plan,
layout: {PlausibleWeb.LayoutView, "focus.html"}
)
true ->
render(conn, "change_enterprise_plan.html",
user: user,
plan: user.enterprise_plan,
plan: new_plan,
layout: {PlausibleWeb.LayoutView, "focus.html"}
)
end
@ -127,6 +109,7 @@ defmodule PlausibleWeb.BillingController do
def change_plan_preview(conn, %{"plan_id" => new_plan_id}) do
subscription = Billing.active_subscription_for(conn.assigns[:current_user].id)
IO.inspect(subscription)
if subscription do
{:ok, preview_info} = Billing.change_plan_preview(subscription, new_plan_id)

@ -17,7 +17,7 @@
return currency + this.localizedPlan.price.net
},
fetchPlan() {
fetchJsonp('https://checkout.paddle.com/api/2.0/prices?product_ids=<%= @plan.paddle_plan_id %>')
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]

@ -1,43 +0,0 @@
<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>
<div class="flex flex-col w-full max-w-4xl px-4 mx-auto mt-4 md:flex-row">
<div class="flex-1 px-8 py-4 mt-8 mb-4 bg-white rounded shadow-md dark:bg-gray-800">
<div class="w-full py-4 dark:text-gray-100">
<span>You've used <b><%= PlausibleWeb.AuthView.delimit_integer(@usage) %></b> billable pageviews in the last 30 days</span>
</div>
<div class="w-full py-4 dark:text-gray-100">
<span>With this link you can upgrade to an enterprise plan with <b><%= PlausibleWeb.StatsView.large_number_format(@plan[:limit]) %> monthly pageviews</b></span>, billed on a <%= @plan[:cycle] %> basis.
</div>
<div class="mt-6 text-left">
<span class="inline-flex w-full rounded-md shadow-sm">
<button type="button" data-theme="none" data-product="<%= @plan[:product_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="items-center button paddle_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>
</span>
</div>
</div>
<div class="flex-1 pl-8 pt-14">
<h3 class="text-lg font-medium text-gray-900 leading-6 dark:text-gray-100">
What happens if I go over my page views limit?
</h3>
<div class="mt-2 text-base text-gray-500 leading-6 dark:text-gray-200">
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>
<div class="mt-8 text-center dark:text-gray-100">
Questions? Contact <%= link("support@plausible.io", to: "mailto:support@plausible.io", class: "text-indigo-500") %>
</div>
<%= render("_paddle_script.html") %>

@ -1,37 +1,61 @@
<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>
<div class="flex flex-col w-full max-w-4xl px-4 mx-auto mt-4 md:flex-row">
<div class="flex-1 px-8 py-4 mt-8 mb-4 bg-white rounded shadow-md dark:bg-gray-800">
<div class="w-full py-4 dark:text-gray-100">
<span>You've used <b><%= PlausibleWeb.AuthView.delimit_integer(@usage) %></b> billable pageviews in the last 30 days</span>
</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>
<div class="w-full py-4 dark:text-gray-100">
<span>With this link you can upgrade to an enterprise plan with <b><%= PlausibleWeb.StatsView.large_number_format(@user.enterprise_plan.monthly_pageview_limit) %> monthly pageviews</b></span>, billed on a <%= @user.enterprise_plan.billing_interval %> basis.
</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>
<div class="mt-6 text-left">
<span class="inline-flex w-full rounded-md shadow-sm">
<button type="button" data-theme="none" data-product="<%= @user.enterprise_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="items-center button paddle_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>
</span>
</div>
</div>
<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 class="flex-1 pl-8 pt-14">
<h3 class="text-lg font-medium text-gray-900 leading-6 dark:text-gray-100">
What happens if I go over my page views limit?
</h3>
<div class="mt-2 text-base text-gray-500 leading-6 dark:text-gray-200">
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>
</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>
@ -40,4 +64,4 @@
Questions? Contact <%= link("support@plausible.io", to: "mailto:support@plausible.io", class: "text-indigo-500") %>
</div>
<%= render("_paddle_script.html") %>
<%= render("_paddle_script.html") %>

Loading…
Cancel
Save