Skip to content

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

added invoices section (#1511)

RobertJoonas9022234aa654
diff --git a/config/.env.dev b/config/.env.dev
index 82fc0e4c7e..7b40e3ed12 100644
--- a/config/.env.dev
+++ b/config/.env.dev
@@ -10,3 +10,5 @@ DISABLE_CRON=true
ADMIN_USER_IDS=1
IP_GEOLOCATION_DB=/home/uku/plausible/analytics/city_database.mmdb
SHOW_CITIES=true
+PADDLE_VENDOR_AUTH_CODE=895e20d4efaec0575bb857f44b183217b332d9592e76e69b8a
+PADDLE_VENDOR_ID=3942
diff --git a/config/config.exs b/config/config.exs
index f57503a5cb..62c3134b80 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -28,8 +28,6 @@ config :plausible,
session_timeout: 1000 * 60 * 30,
session_length_minutes: 30
-config :plausible, :paddle, vendor_id: "49430"
-
config :plausible, Plausible.ClickhouseRepo, loggers: [Ecto.LogEntry]
config :plausible, Plausible.Repo,
diff --git a/config/runtime.exs b/config/runtime.exs
index 7bc22a736c..0ed92b272c 100644
--- a/config/runtime.exs
+++ b/config/runtime.exs
@@ -86,6 +86,7 @@ sentry_dsn = get_var_from_path_or_env(config_dir, "SENTRY_DSN")
honeycomb_api_key = get_var_from_path_or_env(config_dir, "HONEYCOMB_API_KEY")
honeycomb_dataset = get_var_from_path_or_env(config_dir, "HONEYCOMB_DATASET")
paddle_auth_code = get_var_from_path_or_env(config_dir, "PADDLE_VENDOR_AUTH_CODE")
+paddle_vendor_id = get_var_from_path_or_env(config_dir, "PADDLE_VENDOR_ID")
google_cid = get_var_from_path_or_env(config_dir, "GOOGLE_CLIENT_ID")
google_secret = get_var_from_path_or_env(config_dir, "GOOGLE_CLIENT_SECRET")
slack_hook_url = get_var_from_path_or_env(config_dir, "SLACK_WEBHOOK")
@@ -220,7 +221,9 @@ config :sentry,
enable_source_code_context: true,
root_source_code_path: [File.cwd!()]
-config :plausible, :paddle, vendor_auth_code: paddle_auth_code
+config :plausible, :paddle,
+ vendor_auth_code: paddle_auth_code,
+ vendor_id: paddle_vendor_id
config :plausible, :google,
client_id: google_cid,
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index bd7854f791..e8a5ffc589 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -280,5 +280,5 @@ defmodule Plausible.Billing do
defp maybe_adjust_api_key_limits(err), do: err
- defp paddle_api(), do: Application.fetch_env!(:plausible, :paddle_api)
+ def paddle_api(), do: Application.fetch_env!(:plausible, :paddle_api)
end
diff --git a/lib/plausible/billing/paddle_api.ex b/lib/plausible/billing/paddle_api.ex
index b834b95828..42a2742faa 100644
--- a/lib/plausible/billing/paddle_api.ex
+++ b/lib/plausible/billing/paddle_api.ex
@@ -75,6 +75,56 @@ defmodule Plausible.Billing.PaddleApi do
end
end
+ def get_invoices(nil), do: {:error, :no_subscription}
+
+ def get_invoices(subscription) do
+ config = get_config()
+
+ params = %{
+ vendor_id: config[:vendor_id],
+ vendor_auth_code: config[:vendor_auth_code],
+ subscription_id: subscription.paddle_subscription_id,
+ is_paid: 1,
+ from: Timex.shift(Timex.today(), years: -5) |> Timex.format!("{YYYY}-{0M}-{0D}"),
+ to: Timex.shift(Timex.today(), days: 1) |> Timex.format!("{YYYY}-{0M}-{0D}")
+ }
+
+ case HTTPoison.post(invoices_endpoint(), Jason.encode!(params), @headers) do
+ {:ok, response} ->
+ body = Jason.decode!(response.body)
+
+ if body["success"] && body["response"] != [] do
+ body["response"] |> last_12_invoices()
+ else
+ {:error, :request_failed}
+ end
+
+ {:error, _reason} ->
+ {:error, :request_failed}
+ end
+ end
+
+ defp invoices_endpoint() do
+ case Application.get_env(:plausible, :environment) do
+ "dev" -> "https://sandbox-vendors.paddle.com/api/2.0/subscription/payments"
+ _ -> "https://vendors.paddle.com/api/2.0/subscription/payments"
+ end
+ end
+
+ defp last_12_invoices(invoice_list) do
+ Enum.sort(invoice_list, fn %{"payout_date" => d1}, %{"payout_date" => d2} ->
+ Date.compare(Date.from_iso8601!(d1), Date.from_iso8601!(d2)) == :gt
+ end)
+ |> Enum.take(12)
+ end
+
+ def checkout_domain() do
+ case Application.get_env(:plausible, :environment) do
+ "dev" -> "https://sandbox-checkout.paddle.com"
+ _ -> "https://checkout.paddle.com"
+ end
+ end
+
defp get_config() do
Application.get_env(:plausible, :paddle)
end
diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex
index 7de5c317a2..a508425461 100644
--- a/lib/plausible/billing/plans.ex
+++ b/lib/plausible/billing/plans.ex
@@ -9,7 +9,36 @@ defmodule Plausible.Billing.Plans do
%{limit: 10_000_000, monthly_product_id: "655350", yearly_cost: "$250"}
]
+ @sandbox_plans [
+ %{
+ limit: 10_000,
+ monthly_product_id: "19878",
+ yearly_product_id: "20127",
+ monthly_cost: "$6",
+ yearly_cost: "$60"
+ },
+ %{
+ limit: 100_000,
+ monthly_product_id: "20657",
+ yearly_product_id: "20658",
+ monthly_cost: "$12.34",
+ yearly_cost: "$120.34"
+ }
+ ]
+
def plans_for(user) do
+ case Application.get_env(:plausible, :environment) do
+ "dev" ->
+ Enum.map(@sandbox_plans, fn plan ->
+ Map.put(plan, :volume, number_format(plan[:limit]))
+ end)
+
+ _ ->
+ real_plans_for(user)
+ end
+ end
+
+ def real_plans_for(user) do
user = Repo.preload(user, :subscription)
v1_plans = plans_v1()
@@ -82,7 +111,10 @@ defmodule Plausible.Billing.Plans do
end
defp all_plans() do
- plans_v1() ++ @unlisted_plans_v1 ++ plans_v2() ++ @unlisted_plans_v2
+ case Application.get_env(:plausible, :environment) do
+ "dev" -> @sandbox_plans
+ _ -> plans_v1() ++ @unlisted_plans_v1 ++ plans_v2() ++ @unlisted_plans_v2
+ end
end
defp plans_v1() do
diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex
index 8583d02af6..3920fbe16f 100644
--- a/lib/plausible_web/controllers/auth_controller.ex
+++ b/lib/plausible_web/controllers/auth_controller.ex
@@ -437,16 +437,17 @@ defmodule PlausibleWeb.AuthController do
end
def user_settings(conn, _params) do
- changeset = Auth.User.changeset(conn.assigns[:current_user])
+ user = conn.assigns[:current_user]
+ changeset = Auth.User.changeset(user)
- {usage_pageviews, usage_custom_events} =
- Plausible.Billing.usage_breakdown(conn.assigns[:current_user])
+ {usage_pageviews, usage_custom_events} = Plausible.Billing.usage_breakdown(user)
render(conn, "user_settings.html",
- user: conn.assigns[:current_user] |> Repo.preload(:api_keys),
+ user: user |> Repo.preload(:api_keys),
changeset: changeset,
- subscription: conn.assigns[:current_user].subscription,
- theme: conn.assigns[:current_user].theme || "system",
+ subscription: user.subscription,
+ invoices: Plausible.Billing.paddle_api().get_invoices(user.subscription),
+ theme: user.theme || "system",
usage_pageviews: usage_pageviews,
usage_custom_events: usage_custom_events
)
diff --git a/lib/plausible_web/templates/auth/user_settings.html.eex b/lib/plausible_web/templates/auth/user_settings.html.eex
index 97eb93ea61..8c4e94e400 100644
--- a/lib/plausible_web/templates/auth/user_settings.html.eex
+++ b/lib/plausible_web/templates/auth/user_settings.html.eex
@@ -123,6 +123,56 @@
</div>
<% end %>
</div>
+
+<%= case @invoices do %>
+ <% {:error, :no_subscription} -> %>
+
+ <% {:error, :request_failed} -> %>
+ <div class="max-w-2xl px-8 pt-6 pb-8 mx-auto mt-16 bg-white border-t-2 border-orange-200 rounded rounded-t-none shadow-md dark:bg-gray-800">
+ <h2 class="text-xl font-black dark:text-gray-100">Invoices</h2>
+ <div class="my-4 border-b border-gray-300 dark:border-gray-500"></div>
+ <p class="text-center text-black dark:text-gray-100 m-2">
+ Something went wrong
+ </p>
+ </div>
+
+ <% invoice_list -> %>
+ <div class="max-w-2xl px-8 pt-6 pb-8 mx-auto mt-16 bg-white border-t-2 border-orange-200 rounded rounded-t-none shadow-md dark:bg-gray-800">
+ <h2 class="text-xl font-black dark:text-gray-100">Invoices</h2>
+ <div class="my-4 border-b border-gray-300 dark:border-gray-500"></div>
+ <table class="min-w-full divide-y divide-gray-200">
+ <thead>
+ <tr>
+ <th scope="col" class="py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
+ Date
+ </th>
+ <th scope="col" class="py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
+ Amount
+ </th>
+ <th scope="col" class="py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
+ Invoice
+ </th>
+ </tr>
+ </thead>
+ <%= for invoice <- format_invoices(invoice_list) do %>
+ <tbody class="divide-y divide-gray-200">
+ <tr>
+ <td class="py-4 text-sm text-gray-800 dark:text-gray-200 font-medium">
+ <%= invoice.date %>
+ </td>
+ <td class="py-4 text-sm text-gray-800 dark:text-gray-200">
+ <%= invoice.currency <> invoice.amount %>
+ </td>
+ <td class="py-4 text-sm text-indigo-500">
+ <%= link("Link", to: invoice.url, target: "_blank" ) %>
+ </td>
+ </tr>
+ </tbody>
+ <% end %>
+ </table>
+ </div>
+ <% end %>
+
<% end %>
<div class="max-w-2xl px-8 pt-6 pb-8 mx-auto mt-16 bg-white border-t-2 border-green-500 rounded rounded-t-none shadow-md dark:bg-gray-800">
diff --git a/lib/plausible_web/templates/billing/_paddle_script.html.eex b/lib/plausible_web/templates/billing/_paddle_script.html.eex
new file mode 100644
index 0000000000..da12a8d489
--- /dev/null
+++ b/lib/plausible_web/templates/billing/_paddle_script.html.eex
@@ -0,0 +1,7 @@
+<script type="text/javascript" src="https://cdn.paddle.com/paddle/paddle.js"></script>
+<script>
+ <%= if Application.get_env(:plausible, :environment) == "dev" do %>
+ Paddle.Environment.set('sandbox');
+ <% end %>
+ Paddle.Setup({vendor: <%= Application.get_env(:plausible, :paddle) |> Keyword.fetch!(:vendor_id) %> });
+</script>
\ No newline at end of file
diff --git a/lib/plausible_web/templates/billing/change_enterprise_plan.html.eex b/lib/plausible_web/templates/billing/change_enterprise_plan.html.eex
index 81318c05b4..fb7910ba56 100644
--- a/lib/plausible_web/templates/billing/change_enterprise_plan.html.eex
+++ b/lib/plausible_web/templates/billing/change_enterprise_plan.html.eex
@@ -64,5 +64,4 @@
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>Paddle.Setup({vendor: 49430})</script>
+<%= render("_paddle_script.html") %>
diff --git a/lib/plausible_web/templates/billing/change_plan.html.eex b/lib/plausible_web/templates/billing/change_plan.html.eex
index 40097ba69b..67e7cb797a 100644
--- a/lib/plausible_web/templates/billing/change_plan.html.eex
+++ b/lib/plausible_web/templates/billing/change_plan.html.eex
@@ -119,5 +119,4 @@
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>Paddle.Setup({vendor: 49430})</script>
+<%= render("_paddle_script.html") %>
diff --git a/lib/plausible_web/templates/billing/change_plan_preview.html.eex b/lib/plausible_web/templates/billing/change_plan_preview.html.eex
index 98cf7e215c..0835a0c5ee 100644
--- a/lib/plausible_web/templates/billing/change_plan_preview.html.eex
+++ b/lib/plausible_web/templates/billing/change_plan_preview.html.eex
@@ -81,5 +81,4 @@
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>Paddle.Setup({vendor: 49430})</script>
+<%= render("_paddle_script.html") %>
diff --git a/lib/plausible_web/templates/billing/upgrade.html.eex b/lib/plausible_web/templates/billing/upgrade.html.eex
index c2d9c4e827..ac24ff7306 100644
--- a/lib/plausible_web/templates/billing/upgrade.html.eex
+++ b/lib/plausible_web/templates/billing/upgrade.html.eex
@@ -34,8 +34,9 @@
productIds.push(plan.monthly_product_id)
productIds.push(plan.yearly_product_id)
})
-
- fetchJsonp('https://checkout.paddle.com/api/2.0/prices?product_ids=' + productIds.join(','))
+
+ var checkoutDomain = '<%= Plausible.Billing.PaddleApi.checkout_domain() %>'
+ fetchJsonp(checkoutDomain + '/api/2.0/prices?product_ids=' + productIds.join(','))
.then((res) => res.json())
.then((data) => {
this.localizedPlans = data.response.products
@@ -124,5 +125,4 @@
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>Paddle.Setup({vendor: 49430})</script>
+<%= render("_paddle_script.html") %>
diff --git a/lib/plausible_web/templates/billing/upgrade_enterprise_plan.html.eex b/lib/plausible_web/templates/billing/upgrade_enterprise_plan.html.eex
index 7167f01a17..a888b63dd4 100644
--- a/lib/plausible_web/templates/billing/upgrade_enterprise_plan.html.eex
+++ b/lib/plausible_web/templates/billing/upgrade_enterprise_plan.html.eex
@@ -40,5 +40,4 @@
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>Paddle.Setup({vendor: 49430})</script>
+<%= render("_paddle_script.html") %>
diff --git a/lib/plausible_web/templates/billing/upgrade_to_plan.html.eex b/lib/plausible_web/templates/billing/upgrade_to_plan.html.eex
index f0340668ba..33e6ab5297 100644
--- a/lib/plausible_web/templates/billing/upgrade_to_plan.html.eex
+++ b/lib/plausible_web/templates/billing/upgrade_to_plan.html.eex
@@ -40,5 +40,4 @@
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>Paddle.Setup({vendor: 49430})</script>
+<%= render("_paddle_script.html") %>
\ No newline at end of file
diff --git a/lib/plausible_web/views/auth_view.ex b/lib/plausible_web/views/auth_view.ex
index dc442e2df2..32976e2c51 100644
--- a/lib/plausible_web/views/auth_view.ex
+++ b/lib/plausible_web/views/auth_view.ex
@@ -22,6 +22,18 @@ defmodule PlausibleWeb.AuthView do
Plans.subscription_interval(subscription)
end
+ def format_invoices(invoice_list) do
+ Enum.map(invoice_list, fn invoice ->
+ %{
+ date:
+ invoice["payout_date"] |> Date.from_iso8601!() |> Timex.format!("{Mshort} {D}, {YYYY}"),
+ amount: (invoice["amount"] / 1) |> :erlang.float_to_binary(decimals: 2),
+ currency: invoice["currency"] |> PlausibleWeb.BillingView.present_currency(),
+ url: invoice["receipt_url"]
+ }
+ end)
+ end
+
def delimit_integer(number) do
Integer.to_charlist(number)
|> :lists.reverse()
diff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exs
index aa88bc012c..f3c5ce34e8 100644
--- a/test/plausible_web/controllers/auth_controller_test.exs
+++ b/test/plausible_web/controllers/auth_controller_test.exs
@@ -488,6 +488,37 @@ defmodule PlausibleWeb.AuthControllerTest do
assert html_response(conn, 200) =~ "10k pageviews"
assert html_response(conn, 200) =~ "N/A billing"
end
+
+ test "shows invoices for subscribed user", %{conn: conn, user: user} do
+ insert(:subscription,
+ paddle_plan_id: "558018",
+ paddle_subscription_id: "redundant",
+ user: user
+ )
+
+ conn = get(conn, "/settings")
+ assert html_response(conn, 200) =~ "Dec 24, 2020"
+ assert html_response(conn, 200) =~ "€11.11"
+ assert html_response(conn, 200) =~ "Nov 24, 2020"
+ assert html_response(conn, 200) =~ "$22.00"
+ end
+
+ test "shows 'something went wrong' on failed invoice request'", %{conn: conn, user: user} do
+ insert(:subscription,
+ paddle_plan_id: "558018",
+ paddle_subscription_id: "invalid_subscription_id",
+ user: user
+ )
+
+ conn = get(conn, "/settings")
+ assert html_response(conn, 200) =~ "Invoices"
+ assert html_response(conn, 200) =~ "Something went wrong"
+ end
+
+ test "does not show invoice section for a user with no subscription", %{conn: conn} do
+ conn = get(conn, "/settings")
+ assert !(html_response(conn, 200) =~ "Invoices")
+ end
end
describe "PUT /settings" do
diff --git a/test/support/paddle_api_mock.ex b/test/support/paddle_api_mock.ex
index c212b4272a..42ad51a375 100644
--- a/test/support/paddle_api_mock.ex
+++ b/test/support/paddle_api_mock.ex
@@ -25,4 +25,29 @@ defmodule Plausible.PaddleApi.Mock do
}
}}
end
+
+ def get_invoices(nil), do: {:error, :no_subscription}
+
+ def get_invoices(subscription) do
+ case subscription.paddle_subscription_id do
+ "invalid_subscription_id" ->
+ {:error, :request_failed}
+
+ _ ->
+ [
+ %{
+ "amount" => 11.11,
+ "currency" => "EUR",
+ "payout_date" => "2020-12-24",
+ "receipt_url" => "https://some-receipt-url.com"
+ },
+ %{
+ "amount" => 22,
+ "currency" => "USD",
+ "payout_date" => "2020-11-24",
+ "receipt_url" => "https://other-receipt-url.com"
+ }
+ ]
+ end
+ end
end