Create free subscriptions

This commit is contained in:
Uku Taht 2019-11-18 16:13:54 +08:00
parent 2d68b6df0f
commit 10840f9e10
6 changed files with 61 additions and 4 deletions

View File

@ -0,0 +1,22 @@
defmodule Mix.Tasks.CreateFreeSubscription do
use Mix.Task
use Plausible.Repo
require Logger
alias Plausible.Billing.Subscription
@doc """
This is scheduled to run every 6 hours.
"""
def run([user_id]) do
Application.ensure_all_started(:plausible)
execute(user_id)
end
def run(_), do: IO.puts("Usage - mix create_free_subscription <user_id>")
def execute(user_id) do
Subscription.free(%{user_id: user_id})
|> Repo.insert!
end
end

View File

@ -27,6 +27,10 @@ defmodule Plausible.Billing.Plans do
def 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) ->

View File

@ -26,4 +26,16 @@ defmodule Plausible.Billing.Subscription do
|> validate_inclusion(:status, @valid_statuses)
|> unique_constraint(:paddle_subscription_id)
end
def free(attrs \\ %{}) do
%__MODULE__{
paddle_plan_id: "free_10k",
status: "active",
next_bill_amount: "0"
}
|> cast(attrs, @required_fields)
|> validate_required([:user_id])
|> validate_inclusion(:status, @valid_statuses)
|> unique_constraint(:paddle_subscription_id)
end
end

View File

@ -18,7 +18,9 @@
<h4 class="font-black">Next bill amount</h4>
<%= if @subscription do %>
<div class="text-xl py-2 font-medium">$<%= @subscription.next_bill_amount %></div>
<%= link("Update billing info", to: @subscription.update_url, class: "text-sm text-indigo font-medium") %>
<%= if @subscription.update_url do %>
<%= link("Update billing info", to: @subscription.update_url, class: "text-sm text-indigo font-medium") %>
<% end %>
<% else %>
<div class="text-xl py-2 font-medium">$0</div>
<% end %>
@ -26,7 +28,7 @@
<div class="text-center bg-grey-lighter py-8 px-6 rounded h-32 my-4" style="width: 11.75rem;">
<h4 class="font-black">Next bill date</h4>
<%= if @subscription do %>
<%= if @subscription && @subscription.next_bill_date do %>
<div class="text-xl py-2 font-medium"><%= Timex.format!(@subscription.next_bill_date, "{Mshort} {D}, {YYYY}") %></div>
<% else %>
<div class="text-xl py-2 font-medium"></div>
@ -41,7 +43,7 @@
pageviews in the last 30 days
</div>
<%= if @subscription do %>
<%= if @subscription && @subscription.cancel_url do %>
<div class="mt-8">
<%= link("Cancel my subscription", to: @subscription.cancel_url, class: "text-indigo text-sm") %>
</div>

View File

@ -8,7 +8,8 @@ defmodule PlausibleWeb.AuthView do
"558746" => "Business",
"558156" => "Personal (T)",
"558199" => "Startup (T)",
"558200" => "Business (T)"
"558200" => "Business (T)",
"free_10k" => "Free 10k"
}
def subscription_name(subscription) do

View File

@ -0,0 +1,16 @@
defmodule Plausible.Repo.Migrations.AllowFreeSubscriptions do
use Ecto.Migration
def up do
execute """
ALTER table subscriptions
ALTER paddle_subscription_id DROP NOT NULL,
ALTER update_url DROP NOT NULL,
ALTER cancel_url DROP NOT NULL,
ALTER next_bill_date DROP NOT NULL
"""
end
def down do
end
end