@@ -134,24 +134,23 @@ defmodule Algora.Payments do
134
134
|> Repo . all ( )
135
135
end
136
136
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 )
143
143
end
144
144
end
145
145
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 )
151
150
end
152
151
153
152
@ 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 ( ) }
155
154
def create_account ( user , region , country ) do
156
155
type = ConnectCountries . account_type ( country )
157
156
@@ -172,7 +171,7 @@ defmodule Algora.Payments do
172
171
end
173
172
end
174
173
175
- @ spec create_stripe_account ( attrs :: any ( ) ) ::
174
+ @ spec create_stripe_account ( attrs :: map ( ) ) ::
176
175
{ :ok , Stripe.Account . t ( ) } | { :error , Stripe.Error . t ( ) }
177
176
defp create_stripe_account ( % { country: country , type: type } ) do
178
177
case Stripe.Account . create ( % { country: country , type: to_string ( type ) } ) do
@@ -198,46 +197,39 @@ defmodule Algora.Payments do
198
197
Stripe.LoginLink . create ( account . provider_id , % { } )
199
198
end
200
199
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
+
201
220
@ 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 ( ) }
203
222
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 )
235
227
236
- res
228
+ if user && stripe_account . charges_enabled do
229
+ Accounts . update_settings ( user , % { country: stripe_account . country } )
230
+ end
237
231
238
- { :error , error } ->
239
- { :error , error }
240
- end
232
+ { :ok , updated_account }
241
233
end
242
234
end
243
235
@@ -251,7 +243,7 @@ defmodule Algora.Payments do
251
243
if is_nil ( capabilities [ :card_payments ] ) , do: "recipient" , else: "full"
252
244
end
253
245
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 ( ) }
255
247
def delete_account ( account ) do
256
248
with { :ok , _stripe_account } <- Stripe.Account . delete ( account . provider_id ) do
257
249
Repo . delete ( account )
0 commit comments