Skip to content

Commit

Permalink
Moved code from handler for "account.updared" into StripeConnectAccou…
Browse files Browse the repository at this point in the history
…ntService
  • Loading branch information
begedin authored and joshsmith committed Jan 12, 2017
1 parent 1465efb commit cf719d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
22 changes: 1 addition & 21 deletions lib/code_corps/stripe_service/events/account_updated.ex
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
defmodule CodeCorps.StripeService.Events.AccountUpdated do
alias CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter
alias CodeCorps.StripeConnectAccount
alias CodeCorps.Repo

@api Application.get_env(:code_corps, :stripe)

def handle(%{"data" => %{"object" => %{"id" => id_from_stripe}}}) do
with {:ok, %Stripe.Account{} = stripe_account} <-
@api.Account.retrieve(id_from_stripe),
%StripeConnectAccount{} = local_account <-
Repo.get_by(StripeConnectAccount, id_from_stripe: id_from_stripe),
{:ok, params} <-
stripe_account |> StripeConnectAccountAdapter.to_params(%{})
do
local_account
|> StripeConnectAccount.webhook_update_changeset(params)
|> Repo.update
else
{:error, %Stripe.APIErrorResponse{}} -> {:error, :stripe_error}
nil -> {:error, :not_found}
_ -> {:error, :unexpected}
end
CodeCorps.StripeService.StripeConnectAccountService.update_from_stripe(id_from_stripe)
end
end
34 changes: 25 additions & 9 deletions lib/code_corps/stripe_service/stripe_connect_account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,44 @@ defmodule CodeCorps.StripeService.StripeConnectAccountService do

@api Application.get_env(:code_corps, :stripe)

@doc """
Used to create a remote `Stripe.Account` record as well as an associated local
`StripeConnectAccount` record.
"""
def create(attributes) do
with {:ok, from_params} <- StripeConnectAccountAdapter.from_params(attributes),
{:ok, %Stripe.Account{} = account} <- @api.Account.create(from_params),
{:ok, params} <- StripeConnectAccountAdapter.to_params(account, attributes)
do
%StripeConnectAccount{}
|> StripeConnectAccount.create_changeset(params)
|> Repo.insert
%StripeConnectAccount{} |> StripeConnectAccount.create_changeset(params) |> Repo.insert
end
end

@doc """
Used to update both the local `StripeConnectAccount` as well as the remote `Stripe.Account`,
using attributes sent by the client
"""
def update(%StripeConnectAccount{id_from_stripe: id_from_stripe} = account, %{} = attributes) do
with {:ok, from_params} <- StripeConnectAccountAdapter.from_params(attributes),
{:ok, %Stripe.Account{} = stripe_account} <- @api.Account.update(id_from_stripe, from_params),
{:ok, params} <- StripeConnectAccountAdapter.to_params(stripe_account, attributes),
{:ok, %StripeConnectAccount{} = updated_account} <- account |> StripeConnectAccount.webhook_update_changeset(params) |> Repo.update
{:ok, params} <- StripeConnectAccountAdapter.to_params(stripe_account, attributes)
do
{:ok, updated_account}
account |> StripeConnectAccount.webhook_update_changeset(params) |> Repo.update
end
end

@doc """
Used to update the local `StripeConnectAccount` record using data retrieved from the Stripe API
"""
def update_from_stripe(id_from_stripe) do
with {:ok, %Stripe.Account{} = stripe_account} <- @api.Account.retrieve(id_from_stripe),
%StripeConnectAccount{} = local_account <- Repo.get_by(StripeConnectAccount, id_from_stripe: id_from_stripe),
{:ok, params} <- stripe_account |> StripeConnectAccountAdapter.to_params(%{})
do
local_account |> StripeConnectAccount.webhook_update_changeset(params) |> Repo.update
else
{:error, %Ecto.Changeset{} = changeset} -> {:error, changeset}
{:error, %Stripe.APIErrorResponse{} = error} -> {:error, error}
_ -> {:error, :unhandled}
# Not found locally
nil -> {:error, :not_found}
end
end
end

0 comments on commit cf719d9

Please sign in to comment.