-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extedn StripeConnectAccountService.update_from_stripe to also create …
…associated external account record
- Loading branch information
Showing
17 changed files
with
393 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 17 additions & 7 deletions
24
lib/code_corps/stripe_service/adapters/stripe_external_account.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
defmodule CodeCorps.StripeService.Adapters.StripeExternalAccountAdapter do | ||
import CodeCorps.MapUtils, only: [rename: 3] | ||
|
||
alias CodeCorps.MapUtils | ||
alias CodeCorps.StripeConnectAccount | ||
|
||
@stripe_attributes [ | ||
:account, :account_holder_name, :account_holder_type, :bank_name, :country, | ||
:account_holder_name, :account_holder_type, :bank_name, :country, | ||
:currency, :default_for_currency, :fingerprint, :id, :last4, | ||
:routing_number, :status | ||
] | ||
|
||
def to_params(%Stripe.ExternalAccount{} = bank_account, stripe_connect_account_id \\ nil) do | ||
def to_params(%Stripe.ExternalAccount{} = external_account, %StripeConnectAccount{} = connect_account) do | ||
params = | ||
bank_account | ||
external_account | ||
|> Map.from_struct | ||
|> Map.take(@stripe_attributes) | ||
|> rename(:id, :id_from_stripe) | ||
|> rename(:account, :account_id_from_stripe) | ||
|> Map.put(:stripe_connect_account_id, stripe_connect_account_id) | ||
|> MapUtils.rename(:id, :id_from_stripe) | ||
|> add_association_attributes(connect_account) | ||
|
||
{:ok, params} | ||
end | ||
|
||
defp add_association_attributes(attributes, %StripeConnectAccount{} = connect_account) do | ||
association_attributes = build_association_attributes(connect_account) | ||
attributes |> Map.merge(association_attributes) | ||
end | ||
|
||
defp build_association_attributes(%StripeConnectAccount{id: id, id_from_stripe: id_from_stripe}) do | ||
%{account_id_from_stripe: id_from_stripe, stripe_connect_account_id: id} | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
lib/code_corps/stripe_service/stripe_connect_account_service.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
defmodule CodeCorps.StripeService.StripeConnectAccountService do | ||
alias CodeCorps.{Repo, StripeConnectAccount, StripeExternalAccount} | ||
alias CodeCorps.StripeService.Adapters.{StripeConnectAccountAdapter} | ||
alias CodeCorps.StripeService.StripeConnectExternalAccountService | ||
alias Ecto.Multi | ||
|
||
@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 | ||
else | ||
failure -> failure | ||
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} = local_account, %{} = attributes) do | ||
with {:ok, from_params} <- StripeConnectAccountAdapter.from_params(attributes), | ||
{:ok, %Stripe.Account{} = api_account} <- @api.Account.update(id_from_stripe, from_params) | ||
do | ||
update_local_account(local_account, api_account, attributes) | ||
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{} = api_account} <- @api.Account.retrieve(id_from_stripe), | ||
%StripeConnectAccount{} = local_account <- Repo.get_by(StripeConnectAccount, id_from_stripe: id_from_stripe) | ||
do | ||
update_local_account(local_account, api_account) | ||
else | ||
nil -> {:error, :not_found} | ||
failure -> failure | ||
end | ||
end | ||
|
||
# updates a StripeConnectAccount record with combined information from the provided | ||
# Stripe.Account record and an optional attributes map | ||
defp update_local_account( | ||
%StripeConnectAccount{} = local_account, | ||
%Stripe.Account{} = api_account, | ||
attributes \\ %{} | ||
) do | ||
with {:ok, params} <- StripeConnectAccountAdapter.to_params(api_account, attributes) do | ||
changeset = local_account |> StripeConnectAccount.webhook_update_changeset(params) | ||
|
||
multi = Multi.new | ||
|> Multi.update(:stripe_connect_account, changeset) | ||
|> Multi.run(:process_external_accounts, &process_external_accounts(&1, api_account)) | ||
|
||
case Repo.transaction(multi) do | ||
{:ok, %{stripe_connect_account: stripe_connect_account, process_external_accounts: _}} -> | ||
{:ok, stripe_connect_account} | ||
{:error, :stripe_connect_account, %Ecto.Changeset{} = changeset, %{}} -> | ||
{:error, changeset} | ||
{:error, failed_operation, failed_value, _changes_so_far} -> | ||
{:error, failed_operation, failed_value} | ||
end | ||
end | ||
end | ||
|
||
# goes through all Stripe.ExternalAccount objects within the retrieved Stripe.Account object, | ||
# then either retrieves or creates a StripeExternalAccount object for each of them | ||
defp process_external_accounts(_, %Stripe.Account{external_accounts: %{data: []}}), do: {:ok, []} | ||
defp process_external_accounts( | ||
%{stripe_connect_account: %StripeConnectAccount{} = connect_account}, | ||
%Stripe.Account{external_accounts: %{data: external_account_list}} | ||
) do | ||
external_account_list | ||
|> (fn(list) -> [List.last(list)] end).() # We only support one external account for now | ||
|> Enum.map(&find_or_create_external_account(&1, connect_account)) | ||
|> Enum.map(&take_record/1) | ||
|> aggregate_records | ||
end | ||
|
||
# retrieves or creates a StripeExternalAccount object associated to the provided | ||
# Stripe.ExternalAccount and StripeConnectAccount objects | ||
# returns {:ok, list_of_created_external_account_records} | ||
defp find_or_create_external_account(%Stripe.ExternalAccount{} = api_external_account, connect_account) do | ||
case Repo.get_by(StripeExternalAccount, id_from_stripe: api_external_account.id) do | ||
nil -> StripeConnectExternalAccountService.create(api_external_account, connect_account) | ||
%StripeExternalAccount{} = local_external_account -> {:ok, local_external_account} | ||
end | ||
end | ||
|
||
defp take_record({:ok, %StripeExternalAccount{} = external_account}), do: external_account | ||
|
||
defp aggregate_records(results), do: {:ok, results} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.