mirror of
https://github.com/outbackdingo/firezone.git
synced 2026-01-27 10:18:54 +00:00
fix(portal): Persist first user email to Stripe during account sign up (#4408)
This commit is contained in:
@@ -92,9 +92,10 @@ defmodule Domain.Billing do
|
||||
|
||||
def create_customer(%Accounts.Account{} = account) do
|
||||
secret_key = fetch_config!(:secret_key)
|
||||
email = get_customer_email(account)
|
||||
|
||||
with {:ok, %{"id" => customer_id, "email" => customer_email}} <-
|
||||
APIClient.create_customer(secret_key, account.id, account.name, account.slug) do
|
||||
APIClient.create_customer(secret_key, account.id, account.name, account.slug, email) do
|
||||
Accounts.update_account(account, %{
|
||||
metadata: %{stripe: %{customer_id: customer_id, billing_email: customer_email}}
|
||||
})
|
||||
@@ -118,6 +119,9 @@ defmodule Domain.Billing do
|
||||
end
|
||||
end
|
||||
|
||||
defp get_customer_email(%{metadata: %{stripe: %{billing_email: email}}}), do: email
|
||||
defp get_customer_email(_account), do: nil
|
||||
|
||||
def update_customer(%Accounts.Account{} = account) do
|
||||
secret_key = fetch_config!(:secret_key)
|
||||
customer_id = account.metadata.stripe.customer_id
|
||||
|
||||
@@ -25,14 +25,15 @@ defmodule Domain.Billing.Stripe.APIClient do
|
||||
[conn_opts: [transport_opts: transport_opts]]
|
||||
end
|
||||
|
||||
def create_customer(api_token, id, name, slug) do
|
||||
def create_customer(api_token, id, name, slug, email) do
|
||||
body =
|
||||
URI.encode_query(
|
||||
%{
|
||||
"name" => name,
|
||||
"metadata[account_id]" => id,
|
||||
"metadata[account_slug]" => slug
|
||||
},
|
||||
}
|
||||
|> put_if_not_nil("email", email),
|
||||
:www_form
|
||||
)
|
||||
|
||||
@@ -112,6 +113,9 @@ defmodule Domain.Billing.Stripe.APIClient do
|
||||
end
|
||||
end
|
||||
|
||||
defp put_if_not_nil(map, _key, nil), do: map
|
||||
defp put_if_not_nil(map, key, value), do: Map.put(map, key, value)
|
||||
|
||||
defp fetch_config!(key) do
|
||||
Domain.Config.fetch_env!(:domain, __MODULE__)
|
||||
|> Keyword.fetch!(key)
|
||||
|
||||
@@ -10,20 +10,23 @@ defmodule Domain.Mocks.Stripe do
|
||||
def mock_create_customer_endpoint(bypass, account, resp \\ %{}) do
|
||||
customers_endpoint_path = "v1/customers"
|
||||
|
||||
resp =
|
||||
Map.merge(
|
||||
customer_object("cus_NffrFeUfNV2Hib", account.name, "foo@example.com", %{
|
||||
"account_id" => account.id
|
||||
}),
|
||||
resp
|
||||
)
|
||||
|
||||
test_pid = self()
|
||||
|
||||
Bypass.expect(bypass, "POST", customers_endpoint_path, fn conn ->
|
||||
conn = Plug.Conn.fetch_query_params(conn)
|
||||
conn = fetch_request_params(conn)
|
||||
send(test_pid, {:bypass_request, conn})
|
||||
|
||||
email = Map.get(conn.params, "email", "foo@example.com")
|
||||
|
||||
resp =
|
||||
Map.merge(
|
||||
customer_object("cus_NffrFeUfNV2Hib", account.name, email, %{
|
||||
"account_id" => account.id
|
||||
}),
|
||||
resp
|
||||
)
|
||||
|
||||
Plug.Conn.send_resp(conn, 200, Jason.encode!(resp))
|
||||
end)
|
||||
|
||||
|
||||
@@ -379,7 +379,8 @@ defmodule Web.SignUp do
|
||||
:account,
|
||||
fn _repo, _changes ->
|
||||
Accounts.create_account(%{
|
||||
name: registration.account.name
|
||||
name: registration.account.name,
|
||||
metadata: %{stripe: %{billing_email: registration.email}}
|
||||
})
|
||||
end
|
||||
)
|
||||
|
||||
@@ -50,6 +50,7 @@ defmodule Web.Live.SignUpTest do
|
||||
account = Repo.one(Domain.Accounts.Account)
|
||||
assert account.name == account_name
|
||||
assert account.metadata.stripe.customer_id
|
||||
assert account.metadata.stripe.billing_email == email
|
||||
|
||||
group = Repo.one(Domain.Actors.Group)
|
||||
assert group.account_id == account.id
|
||||
|
||||
Reference in New Issue
Block a user