|
| 1 | +defmodule CodeCorps.StripeService.StripeConnectAccountService do |
| 2 | + alias CodeCorps.{Repo, StripeConnectAccount, StripeExternalAccount} |
| 3 | + alias CodeCorps.StripeService.Adapters.{StripeConnectAccountAdapter} |
| 4 | + alias CodeCorps.StripeService.StripeConnectExternalAccountService |
| 5 | + alias Ecto.Multi |
| 6 | + |
| 7 | + import Ecto.Query, only: [where: 3] |
| 8 | + |
| 9 | + @api Application.get_env(:code_corps, :stripe) |
| 10 | + |
| 11 | + @doc """ |
| 12 | + Used to create a remote `Stripe.Account` record as well as an associated local |
| 13 | + `StripeConnectAccount` record. |
| 14 | + """ |
| 15 | + def create(attributes) do |
| 16 | + with {:ok, from_params} <- StripeConnectAccountAdapter.from_params(attributes), |
| 17 | + {:ok, %Stripe.Account{} = account} <- @api.Account.create(from_params), |
| 18 | + {:ok, params} <- StripeConnectAccountAdapter.to_params(account, attributes) |
| 19 | + do |
| 20 | + %StripeConnectAccount{} |> StripeConnectAccount.create_changeset(params) |> Repo.insert |
| 21 | + else |
| 22 | + failure -> failure |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + @doc """ |
| 27 | + Used to update both the local `StripeConnectAccount` as well as the remote `Stripe.Account`, |
| 28 | + using attributes sent by the client |
| 29 | + """ |
| 30 | + def update(%StripeConnectAccount{id_from_stripe: id_from_stripe} = local_account, %{} = attributes) do |
| 31 | + with {:ok, from_params} <- StripeConnectAccountAdapter.from_params(attributes), |
| 32 | + {:ok, %Stripe.Account{} = api_account} <- @api.Account.update(id_from_stripe, from_params) |
| 33 | + do |
| 34 | + update_local_account(local_account, api_account, attributes) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + @doc """ |
| 39 | + Used to update the local `StripeConnectAccount` record using data retrieved from the Stripe API |
| 40 | + """ |
| 41 | + def update_from_stripe(id_from_stripe) do |
| 42 | + with {:ok, %Stripe.Account{} = api_account} <- @api.Account.retrieve(id_from_stripe), |
| 43 | + %StripeConnectAccount{} = local_account <- Repo.get_by(StripeConnectAccount, id_from_stripe: id_from_stripe) |
| 44 | + do |
| 45 | + update_local_account(local_account, api_account) |
| 46 | + else |
| 47 | + nil -> {:error, :not_found} |
| 48 | + failure -> failure |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + # updates a StripeConnectAccount record with combined information from the provided |
| 53 | + # Stripe.Account record and an optional attributes map |
| 54 | + defp update_local_account( |
| 55 | + %StripeConnectAccount{} = local_account, |
| 56 | + %Stripe.Account{} = api_account, |
| 57 | + attributes \\ %{} |
| 58 | + ) do |
| 59 | + with {:ok, params} <- StripeConnectAccountAdapter.to_params(api_account, attributes) do |
| 60 | + changeset = local_account |> StripeConnectAccount.webhook_update_changeset(params) |
| 61 | + |
| 62 | + multi = Multi.new |
| 63 | + |> Multi.update(:stripe_connect_account, changeset) |
| 64 | + |> Multi.run(:process_external_accounts, &process_external_accounts(&1, api_account)) |
| 65 | + |
| 66 | + case Repo.transaction(multi) do |
| 67 | + {:ok, %{stripe_connect_account: stripe_connect_account, process_external_accounts: _}} -> |
| 68 | + {:ok, stripe_connect_account} |
| 69 | + {:error, :stripe_connect_account, %Ecto.Changeset{} = changeset, %{}} -> |
| 70 | + {:error, changeset} |
| 71 | + {:error, failed_operation, failed_value, _changes_so_far} -> |
| 72 | + {:error, failed_operation, failed_value} |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + # goes through all Stripe.ExternalAccount objects within the retrieved Stripe.Account object, |
| 78 | + # then either retrieves or creates a StripeExternalAccount object for each of them |
| 79 | + defp process_external_accounts(_, %Stripe.Account{external_accounts: %{data: []}}), do: {:ok, []} |
| 80 | + defp process_external_accounts( |
| 81 | + %{stripe_connect_account: %StripeConnectAccount{} = connect_account}, |
| 82 | + %Stripe.Account{external_accounts: %{data: new_external_account_list}} |
| 83 | + ) do |
| 84 | + StripeExternalAccount |
| 85 | + |> where([e], e.stripe_connect_account_id == ^connect_account.id) |
| 86 | + |> Repo.delete_all |
| 87 | + |
| 88 | + new_external_account_list |
| 89 | + |> (fn(list) -> [List.last(list)] end).() # We only support one external account for now |
| 90 | + |> Enum.map(&find_or_create_external_account(&1, connect_account)) |
| 91 | + |> Enum.map(&take_record/1) |
| 92 | + |> aggregate_records |
| 93 | + end |
| 94 | + |
| 95 | + # retrieves or creates a StripeExternalAccount object associated to the provided |
| 96 | + # Stripe.ExternalAccount and StripeConnectAccount objects |
| 97 | + # returns {:ok, list_of_created_external_account_records} |
| 98 | + defp find_or_create_external_account(%Stripe.ExternalAccount{} = api_external_account, connect_account) do |
| 99 | + case Repo.get_by(StripeExternalAccount, id_from_stripe: api_external_account.id) do |
| 100 | + nil -> StripeConnectExternalAccountService.create(api_external_account, connect_account) |
| 101 | + %StripeExternalAccount{} = local_external_account -> {:ok, local_external_account} |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + defp take_record({:ok, %StripeExternalAccount{} = external_account}), do: external_account |
| 106 | + |
| 107 | + defp aggregate_records(results), do: {:ok, results} |
| 108 | +end |
0 commit comments