@@ -4,28 +4,44 @@ defmodule CodeCorps.StripeService.StripeConnectAccountService do
44
55 @ api Application . get_env ( :code_corps , :stripe )
66
7+ @ doc """
8+ Used to create a remote `Stripe.Account` record as well as an associated local
9+ `StripeConnectAccount` record.
10+ """
711 def create ( attributes ) do
812 with { :ok , from_params } <- StripeConnectAccountAdapter . from_params ( attributes ) ,
913 { :ok , % Stripe.Account { } = account } <- @ api . Account . create ( from_params ) ,
1014 { :ok , params } <- StripeConnectAccountAdapter . to_params ( account , attributes )
1115 do
12- % StripeConnectAccount { }
13- |> StripeConnectAccount . create_changeset ( params )
14- |> Repo . insert
16+ % StripeConnectAccount { } |> StripeConnectAccount . create_changeset ( params ) |> Repo . insert
1517 end
1618 end
1719
20+ @ doc """
21+ Used to update both the local `StripeConnectAccount` as well as the remote `Stripe.Account`,
22+ using attributes sent by the client
23+ """
1824 def update ( % StripeConnectAccount { id_from_stripe: id_from_stripe } = account , % { } = attributes ) do
1925 with { :ok , from_params } <- StripeConnectAccountAdapter . from_params ( attributes ) ,
2026 { :ok , % Stripe.Account { } = stripe_account } <- @ api . Account . update ( id_from_stripe , from_params ) ,
21- { :ok , params } <- StripeConnectAccountAdapter . to_params ( stripe_account , attributes ) ,
22- { :ok , % StripeConnectAccount { } = updated_account } <- account |> StripeConnectAccount . webhook_update_changeset ( params ) |> Repo . update
27+ { :ok , params } <- StripeConnectAccountAdapter . to_params ( stripe_account , attributes )
2328 do
24- { :ok , updated_account }
29+ account |> StripeConnectAccount . webhook_update_changeset ( params ) |> Repo . update
30+ end
31+ end
32+
33+ @ doc """
34+ Used to update the local `StripeConnectAccount` record using data retrieved from the Stripe API
35+ """
36+ def update_from_stripe ( id_from_stripe ) do
37+ with { :ok , % Stripe.Account { } = stripe_account } <- @ api . Account . retrieve ( id_from_stripe ) ,
38+ % StripeConnectAccount { } = local_account <- Repo . get_by ( StripeConnectAccount , id_from_stripe: id_from_stripe ) ,
39+ { :ok , params } <- stripe_account |> StripeConnectAccountAdapter . to_params ( % { } )
40+ do
41+ local_account |> StripeConnectAccount . webhook_update_changeset ( params ) |> Repo . update
2542 else
26- { :error , % Ecto.Changeset { } = changeset } -> { :error , changeset }
27- { :error , % Stripe.APIErrorResponse { } = error } -> { :error , error }
28- _ -> { :error , :unhandled }
43+ # Not found locally
44+ nil -> { :error , :not_found }
2945 end
3046 end
3147end
0 commit comments