-
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.
Implement find_or_create behavior for external accounts, fix existing…
… tests, add external account created event to explicit ignore
- Loading branch information
Showing
9 changed files
with
88 additions
and
68 deletions.
There are no files selected for viewing
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 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
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
22 changes: 6 additions & 16 deletions
22
test/lib/code_corps/stripe_service/stripe_connect_external_account_service_test.exs
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,29 +1,19 @@ | ||
defmodule CodeCorps.StripeService.StripeConnectExternalAccountServiceTest do | ||
use ExUnit.Case, async: true | ||
|
||
use CodeCorps.ModelCase | ||
|
||
alias CodeCorps.StripeService.StripeConnectExternalAccountService | ||
|
||
describe "create" do | ||
test "creates a StripeExternalAccount" do | ||
id_from_stripe = "ba_testing123" | ||
account_id_from_stripe = "acct_123" | ||
|
||
connect_account = insert(:stripe_connect_account, id_from_stripe: account_id_from_stripe) | ||
api_external_account = %Stripe.ExternalAccount{id: "bnk_123"} | ||
local_connect_account = insert(:stripe_connect_account) | ||
|
||
{:ok, %CodeCorps.StripeExternalAccount{} = external_account} = | ||
StripeConnectExternalAccountService.create(id_from_stripe, account_id_from_stripe) | ||
|
||
assert external_account.id_from_stripe == id_from_stripe | ||
assert external_account.stripe_connect_account_id == connect_account.id | ||
end | ||
|
||
test "returns {:error, :not_found} if there's no associated stripe connect account" do | ||
id_from_stripe = "ba_testing123" | ||
account_id_from_stripe = "acct_123" | ||
StripeConnectExternalAccountService.create(api_external_account, local_connect_account) | ||
|
||
assert {:error, :not_found} == StripeConnectExternalAccountService.create(id_from_stripe, account_id_from_stripe) | ||
assert external_account.id_from_stripe == "bnk_123" | ||
assert external_account.stripe_connect_account_id == local_connect_account.id | ||
assert external_account.account_id_from_stripe == local_connect_account.id_from_stripe | ||
end | ||
end | ||
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