@@ -134,35 +134,36 @@ defmodule Algora.Payments do
134
134
|> Repo . all ( )
135
135
end
136
136
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 )
141
142
account -> { :ok , account }
142
143
end
143
144
end
144
145
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
147
148
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 )
149
150
|> Repo . one ( )
150
151
end
151
152
152
- @ spec create_account ( user :: User . t ( ) , attrs :: % { optional ( atom ( ) ) => any ( ) } ) ::
153
+ @ spec create_account ( user :: User . t ( ) , region :: :US | :EU , country :: String . t ( ) ) ::
153
154
{ :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 )
156
157
157
- with { :ok , stripe_account } <- create_stripe_account ( attrs ) do
158
+ with { :ok , stripe_account } <- create_stripe_account ( % { country: country , type: type } ) do
158
159
attrs = % {
159
160
provider: "stripe" ,
160
161
provider_id: stripe_account . id ,
161
162
provider_meta: Util . normalize_struct ( stripe_account ) ,
162
- type: attrs . type ,
163
- region: :US ,
163
+ type: type ,
164
+ region: region ,
164
165
user_id: user . id ,
165
- country: attrs . country
166
+ country: country
166
167
}
167
168
168
169
% Account { }
@@ -171,12 +172,12 @@ defmodule Algora.Payments do
171
172
end
172
173
end
173
174
174
- @ spec create_stripe_account ( attrs :: % { optional ( atom ( ) ) => any ( ) } ) ::
175
+ @ spec create_stripe_account ( attrs :: any ( ) ) ::
175
176
{ :ok , Stripe.Account . t ( ) } | { :error , Stripe.Error . t ( ) }
176
177
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
178
179
{ :ok , account } -> { :ok , account }
179
- { :error , _reason } -> Stripe.Account . create ( % { type: type } )
180
+ { :error , _reason } -> Stripe.Account . create ( % { type: to_string ( type ) } )
180
181
end
181
182
end
182
183
@@ -192,58 +193,60 @@ defmodule Algora.Payments do
192
193
end
193
194
194
195
@ 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 ( ) }
196
197
def create_login_link ( account ) do
197
- Stripe.Account . create_login_link ( account . provider_id , % { } )
198
+ Stripe.LoginLink . create ( account . provider_id , % { } )
198
199
end
199
200
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
204
205
nil ->
205
206
{ :error , :account_not_found }
206
207
207
208
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 }
239
240
end
240
241
end
241
242
end
242
243
244
+ @ spec get_service_agreement ( account :: Stripe.Account . t ( ) ) :: String . t ( )
243
245
defp get_service_agreement ( % { tos_acceptance: % { service_agreement: agreement } } = _account ) when not is_nil ( agreement ) do
244
246
agreement
245
247
end
246
248
249
+ @ spec get_service_agreement ( account :: Stripe.Account . t ( ) ) :: String . t ( )
247
250
defp get_service_agreement ( % { capabilities: capabilities } ) do
248
251
if is_nil ( capabilities [ :card_payments ] ) , do: "recipient" , else: "full"
249
252
end
0 commit comments