Skip to content

Commit 56cdacf

Browse files
committed
misc improvements
1 parent b76a81a commit 56cdacf

File tree

3 files changed

+65
-57
lines changed

3 files changed

+65
-57
lines changed

lib/algora/payments/payments.ex

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -134,35 +134,36 @@ defmodule Algora.Payments do
134134
|> Repo.all()
135135
end
136136

137-
@spec get_or_create_account(user_id :: binary(), region :: :US | :EU) :: Account.t()
138-
def get_or_create_account(user_id, region) do
139-
case get_account(user_id, region) do
140-
nil -> create_account(user_id, region)
137+
@spec get_or_create_account(user :: User.t(), region :: :US | :EU, country :: String.t()) ::
138+
{:ok, Account.t()} | {:error, any()}
139+
def get_or_create_account(user, region, country) do
140+
case get_account(user, region) do
141+
nil -> create_account(user, region, country)
141142
account -> {:ok, account}
142143
end
143144
end
144145

145-
@spec get_account(user_id :: binary(), region :: :US | :EU) :: Account.t() | nil
146-
def get_account(user_id, region) do
146+
@spec get_account(user :: User.t(), region :: :US | :EU) :: Account.t() | nil
147+
def get_account(user, region) do
147148
Account
148-
|> where([a], a.user_id == ^user_id and a.region == ^region)
149+
|> where([a], a.user_id == ^user.id and a.region == ^region)
149150
|> Repo.one()
150151
end
151152

152-
@spec create_account(user :: User.t(), attrs :: %{optional(atom()) => any()}) ::
153+
@spec create_account(user :: User.t(), region :: :US | :EU, country :: String.t()) ::
153154
{:ok, Account.t()} | {:error, any()}
154-
def create_account(user, attrs) do
155-
attrs = Map.put(attrs, :type, ConnectCountries.account_type(attrs.country))
155+
def create_account(user, region, country) do
156+
type = ConnectCountries.account_type(country)
156157

157-
with {:ok, stripe_account} <- create_stripe_account(attrs) do
158+
with {:ok, stripe_account} <- create_stripe_account(%{country: country, type: type}) do
158159
attrs = %{
159160
provider: "stripe",
160161
provider_id: stripe_account.id,
161162
provider_meta: Util.normalize_struct(stripe_account),
162-
type: attrs.type,
163-
region: :US,
163+
type: type,
164+
region: region,
164165
user_id: user.id,
165-
country: attrs.country
166+
country: country
166167
}
167168

168169
%Account{}
@@ -171,12 +172,12 @@ defmodule Algora.Payments do
171172
end
172173
end
173174

174-
@spec create_stripe_account(attrs :: %{optional(atom()) => any()}) ::
175+
@spec create_stripe_account(attrs :: any()) ::
175176
{:ok, Stripe.Account.t()} | {:error, Stripe.Error.t()}
176177
defp create_stripe_account(%{country: country, type: type}) do
177-
case Stripe.Account.create(%{country: country, type: type}) do
178+
case Stripe.Account.create(%{country: country, type: to_string(type)}) do
178179
{:ok, account} -> {:ok, account}
179-
{:error, _reason} -> Stripe.Account.create(%{type: type})
180+
{:error, _reason} -> Stripe.Account.create(%{type: to_string(type)})
180181
end
181182
end
182183

@@ -192,58 +193,60 @@ defmodule Algora.Payments do
192193
end
193194

194195
@spec create_login_link(account :: Account.t()) ::
195-
{:ok, Stripe.Account.t()} | {:error, Stripe.Error.t()}
196+
{:ok, Stripe.LoginLink.t()} | {:error, Stripe.Error.t()}
196197
def create_login_link(account) do
197-
Stripe.Account.create_login_link(account.provider_id, %{})
198+
Stripe.LoginLink.create(account.provider_id, %{})
198199
end
199200

200-
@spec refresh_stripe_account(user_id :: binary()) ::
201-
{:ok, Account.t()} | {:error, :account_not_found} | {:error, any()}
202-
def refresh_stripe_account(user_id) do
203-
case get_account(user_id, :US) do
201+
@spec refresh_stripe_account(user :: User.t()) ::
202+
{:ok, Account.t()} | {:error, any()}
203+
def refresh_stripe_account(user) do
204+
case get_account(user, :US) do
204205
nil ->
205206
{:error, :account_not_found}
206207

207208
account ->
208-
with {:ok, stripe_account} <- Stripe.Account.retrieve(account.provider_id) do
209-
attrs = %{
210-
charges_enabled: stripe_account.charges_enabled,
211-
payouts_enabled: stripe_account.payouts_enabled,
212-
payout_interval: stripe_account.settings.payouts.schedule.interval,
213-
payout_speed: stripe_account.settings.payouts.schedule.delay_days,
214-
default_currency: stripe_account.default_currency,
215-
details_submitted: stripe_account.details_submitted,
216-
country: stripe_account.country,
217-
service_agreement: get_service_agreement(stripe_account),
218-
provider_meta: Util.normalize_struct(stripe_account)
219-
}
220-
221-
account
222-
|> Account.changeset(attrs)
223-
|> Repo.update()
224-
|> case do
225-
{:ok, updated_account} ->
226-
if stripe_account.charges_enabled do
227-
account.user_id
228-
|> Accounts.get_user!()
229-
|> Accounts.update_settings(%{country: stripe_account.country})
230-
end
231-
232-
# TODO: enqueue pending transfers
233-
234-
{:ok, updated_account}
235-
236-
error ->
237-
error
238-
end
209+
case Stripe.Account.retrieve(account.provider_id) do
210+
{:ok, stripe_account} ->
211+
attrs = %{
212+
provider: "stripe",
213+
provider_id: stripe_account.id,
214+
provider_meta: Util.normalize_struct(stripe_account),
215+
charges_enabled: stripe_account.charges_enabled,
216+
payouts_enabled: stripe_account.payouts_enabled,
217+
payout_interval: stripe_account.settings.payouts.schedule.interval,
218+
payout_speed: stripe_account.settings.payouts.schedule.delay_days,
219+
default_currency: stripe_account.default_currency,
220+
details_submitted: stripe_account.details_submitted,
221+
country: stripe_account.country,
222+
service_agreement: get_service_agreement(stripe_account)
223+
}
224+
225+
res =
226+
account
227+
|> Account.changeset(attrs)
228+
|> Repo.update()
229+
230+
user = Accounts.get_user(account.user_id)
231+
232+
if user && stripe_account.charges_enabled do
233+
Accounts.update_settings(user, %{country: stripe_account.country})
234+
end
235+
236+
res
237+
238+
{:error, error} ->
239+
{:error, error}
239240
end
240241
end
241242
end
242243

244+
@spec get_service_agreement(account :: Stripe.Account.t()) :: String.t()
243245
defp get_service_agreement(%{tos_acceptance: %{service_agreement: agreement}} = _account) when not is_nil(agreement) do
244246
agreement
245247
end
246248

249+
@spec get_service_agreement(account :: Stripe.Account.t()) :: String.t()
247250
defp get_service_agreement(%{capabilities: capabilities}) do
248251
if is_nil(capabilities[:card_payments]), do: "recipient", else: "full"
249252
end

lib/algora_web/controllers/stripe_callback_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule AlgoraWeb.StripeCallbackController do
1414
|> halt()
1515

1616
current_user ->
17-
case Payments.refresh_stripe_account(current_user.id) do
17+
case Payments.refresh_stripe_account(current_user) do
1818
{:ok, _account} ->
1919
redirect(conn, to: ~p"/user/transactions")
2020

lib/algora_web/live/user/transactions_live.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule AlgoraWeb.User.TransactionsLive do
33
use AlgoraWeb, :live_view
44
use LiveSvelte.Components
55

6+
import Ecto.Changeset
7+
68
alias Algora.Accounts.User
79
alias Algora.Payments
810
alias Algora.Stripe.ConnectCountries
@@ -35,7 +37,7 @@ defmodule AlgoraWeb.User.TransactionsLive do
3537
Payments.subscribe()
3638
end
3739

38-
account = Payments.get_account(socket.assigns.current_user.id, :US)
40+
account = Payments.get_account(socket.assigns.current_user, :US)
3941

4042
dbg(account)
4143

@@ -86,8 +88,11 @@ defmodule AlgoraWeb.User.TransactionsLive do
8688
|> PayoutAccountForm.changeset(params)
8789
|> Map.put(:action, :validate)
8890

91+
country = get_change(changeset, :country)
92+
8993
if changeset.valid? do
90-
with {:ok, account} <- Payments.get_or_create_account(socket.assigns.current_user.id, :US),
94+
with {:ok, account} <-
95+
Payments.get_or_create_account(socket.assigns.current_user, :US, country),
9196
{:ok, %{url: url}} <- Payments.create_account_link(account, AlgoraWeb.Endpoint.url()) do
9297
{:noreply, redirect(socket, external: url)}
9398
else

0 commit comments

Comments
 (0)