diff --git a/elixir/apps/domain/lib/domain/billing.ex b/elixir/apps/domain/lib/domain/billing.ex index bf34bc4a7..636a96c47 100644 --- a/elixir/apps/domain/lib/domain/billing.ex +++ b/elixir/apps/domain/lib/domain/billing.ex @@ -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 diff --git a/elixir/apps/domain/lib/domain/billing/stripe/api_client.ex b/elixir/apps/domain/lib/domain/billing/stripe/api_client.ex index 2a0e4d6ce..9ec7d0650 100644 --- a/elixir/apps/domain/lib/domain/billing/stripe/api_client.ex +++ b/elixir/apps/domain/lib/domain/billing/stripe/api_client.ex @@ -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) diff --git a/elixir/apps/domain/test/support/mocks/stripe.ex b/elixir/apps/domain/test/support/mocks/stripe.ex index 8ce91e7a1..a6803fdea 100644 --- a/elixir/apps/domain/test/support/mocks/stripe.ex +++ b/elixir/apps/domain/test/support/mocks/stripe.ex @@ -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) diff --git a/elixir/apps/web/lib/web/live/sign_up.ex b/elixir/apps/web/lib/web/live/sign_up.ex index 5662f9b0b..ce0f4d8cb 100644 --- a/elixir/apps/web/lib/web/live/sign_up.ex +++ b/elixir/apps/web/lib/web/live/sign_up.ex @@ -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 ) diff --git a/elixir/apps/web/test/web/live/sign_up_test.exs b/elixir/apps/web/test/web/live/sign_up_test.exs index de5efb9e3..f0e2c99fc 100644 --- a/elixir/apps/web/test/web/live/sign_up_test.exs +++ b/elixir/apps/web/test/web/live/sign_up_test.exs @@ -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