@@ -134,24 +134,23 @@ defmodule Algora.Payments do
134134 |> Repo . all ( )
135135 end
136136
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 )
142- account -> { :ok , account }
137+ @ spec fetch_or_create_account ( user :: User . t ( ) , region :: :US | :EU , country :: String . t ( ) ) ::
138+ { :ok , Account . t ( ) } | { :error , Ecto.Changeset . t ( ) }
139+ def fetch_or_create_account ( user , region , country ) do
140+ case fetch_account ( user , region ) do
141+ { :ok , account } -> { :ok , account }
142+ { :error , :not_found } -> create_account ( user , region , country )
143143 end
144144 end
145145
146- @ spec get_account ( user :: User . t ( ) , region :: :US | :EU ) :: Account . t ( ) | nil
147- def get_account ( user , region ) do
148- Account
149- |> where ( [ a ] , a . user_id == ^ user . id and a . region == ^ region )
150- |> Repo . one ( )
146+ @ spec fetch_account ( user :: User . t ( ) , region :: :US | :EU ) ::
147+ { :ok , Account . t ( ) } | { :error , :not_found }
148+ def fetch_account ( user , region ) do
149+ Repo . fetch_by ( Account , user_id: user . id , region: region )
151150 end
152151
153152 @ spec create_account ( user :: User . t ( ) , region :: :US | :EU , country :: String . t ( ) ) ::
154- { :ok , Account . t ( ) } | { :error , any ( ) }
153+ { :ok , Account . t ( ) } | { :error , Ecto.Changeset . t ( ) }
155154 def create_account ( user , region , country ) do
156155 type = ConnectCountries . account_type ( country )
157156
@@ -172,7 +171,7 @@ defmodule Algora.Payments do
172171 end
173172 end
174173
175- @ spec create_stripe_account ( attrs :: any ( ) ) ::
174+ @ spec create_stripe_account ( attrs :: map ( ) ) ::
176175 { :ok , Stripe.Account . t ( ) } | { :error , Stripe.Error . t ( ) }
177176 defp create_stripe_account ( % { country: country , type: type } ) do
178177 case Stripe.Account . create ( % { country: country , type: to_string ( type ) } ) do
@@ -198,46 +197,39 @@ defmodule Algora.Payments do
198197 Stripe.LoginLink . create ( account . provider_id , % { } )
199198 end
200199
200+ @ spec update_account ( account :: Account . t ( ) , stripe_account :: Stripe.Account . t ( ) ) ::
201+ { :ok , Account . t ( ) } | { :error , Ecto.Changeset . t ( ) }
202+ def update_account ( account , stripe_account ) do
203+ account
204+ |> Account . changeset ( % {
205+ provider: "stripe" ,
206+ provider_id: stripe_account . id ,
207+ provider_meta: Util . normalize_struct ( stripe_account ) ,
208+ charges_enabled: stripe_account . charges_enabled ,
209+ payouts_enabled: stripe_account . payouts_enabled ,
210+ payout_interval: stripe_account . settings . payouts . schedule . interval ,
211+ payout_speed: stripe_account . settings . payouts . schedule . delay_days ,
212+ default_currency: stripe_account . default_currency ,
213+ details_submitted: stripe_account . details_submitted ,
214+ country: stripe_account . country ,
215+ service_agreement: get_service_agreement ( stripe_account )
216+ } )
217+ |> Repo . update ( )
218+ end
219+
201220 @ spec refresh_stripe_account ( user :: User . t ( ) ) ::
202- { :ok , Account . t ( ) } | { :error , any ( ) }
221+ { :ok , Account . t ( ) } | { :error , Ecto.Changeset . t ( ) } | { :error , :not_found } | { :error , Stripe.Error . t ( ) }
203222 def refresh_stripe_account ( user ) do
204- case get_account ( user , :US ) do
205- nil ->
206- { :error , :account_not_found }
207-
208- account ->
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
223+ with { :ok , account } <- fetch_account ( user , :US ) ,
224+ { :ok , stripe_account } <- Stripe.Account . retrieve ( account . provider_id ) ,
225+ { :ok , updated_account } <- update_account ( account , stripe_account ) do
226+ user = Accounts . get_user ( account . user_id )
235227
236- res
228+ if user && stripe_account . charges_enabled do
229+ Accounts . update_settings ( user , % { country: stripe_account . country } )
230+ end
237231
238- { :error , error } ->
239- { :error , error }
240- end
232+ { :ok , updated_account }
241233 end
242234 end
243235
@@ -251,7 +243,7 @@ defmodule Algora.Payments do
251243 if is_nil ( capabilities [ :card_payments ] ) , do: "recipient" , else: "full"
252244 end
253245
254- @ spec delete_account ( account :: Account . t ( ) ) :: { :ok , Account . t ( ) } | { :error , any ( ) }
246+ @ spec delete_account ( account :: Account . t ( ) ) :: { :ok , Account . t ( ) } | { :error , Ecto.Changeset . t ( ) }
255247 def delete_account ( account ) do
256248 with { :ok , _stripe_account } <- Stripe.Account . delete ( account . provider_id ) do
257249 Repo . delete ( account )
0 commit comments