-
Notifications
You must be signed in to change notification settings - Fork 86
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This call will return |
||
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 |
There was a problem hiding this comment.
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.