Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved code from handler for "account.updated" into StripeConnectAccountService #627

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this one means the call in the do block will just do the same thing automatically.

{: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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one serves no function. It passes out the error response. Not having this line leaves behavior exactly the same.

do
Copy link
Contributor Author

@begedin begedin Jan 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is outright error hiding and should go away, so I removed it.

{:ok, updated_account}
account |> StripeConnectAccount.webhook_update_changeset(params) |> Repo.update
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call will return {:ok, updated_account} anyway. No point in first calling it in the with block, then just passing it through in the do block.

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