Skip to content

Commit 6d29b42

Browse files
authored
Merge pull request #627 from code-corps/consolidate-stripe-connect-account-crud-code
Moved code from handler for "account.updated" into StripeConnectAccountService
2 parents 1465efb + cf719d9 commit 6d29b42

2 files changed

Lines changed: 26 additions & 30 deletions

File tree

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
defmodule CodeCorps.StripeService.Events.AccountUpdated do
2-
alias CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter
3-
alias CodeCorps.StripeConnectAccount
4-
alias CodeCorps.Repo
5-
6-
@api Application.get_env(:code_corps, :stripe)
7-
82
def handle(%{"data" => %{"object" => %{"id" => id_from_stripe}}}) do
9-
with {:ok, %Stripe.Account{} = stripe_account} <-
10-
@api.Account.retrieve(id_from_stripe),
11-
%StripeConnectAccount{} = local_account <-
12-
Repo.get_by(StripeConnectAccount, id_from_stripe: id_from_stripe),
13-
{:ok, params} <-
14-
stripe_account |> StripeConnectAccountAdapter.to_params(%{})
15-
do
16-
local_account
17-
|> StripeConnectAccount.webhook_update_changeset(params)
18-
|> Repo.update
19-
else
20-
{:error, %Stripe.APIErrorResponse{}} -> {:error, :stripe_error}
21-
nil -> {:error, :not_found}
22-
_ -> {:error, :unexpected}
23-
end
3+
CodeCorps.StripeService.StripeConnectAccountService.update_from_stripe(id_from_stripe)
244
end
255
end

lib/code_corps/stripe_service/stripe_connect_account.ex

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3147
end

0 commit comments

Comments
 (0)