-
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.
Add event hook handling for "account.external_account.created"
- Loading branch information
1 parent
e2aca28
commit 77a10fc
Showing
12 changed files
with
172 additions
and
6 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule CodeCorps.StripeService.Adapters.StripeExternalAccountAdapter do | ||
import CodeCorps.MapUtils, only: [rename: 3] | ||
|
||
@stripe_attributes [ | ||
:account, :account_holder_name, :account_holder_type, :bank_name, :country, | ||
:currency, :default_for_currency, :fingerprint, :id, :last4, | ||
:routing_number, :status | ||
] | ||
|
||
def to_params(%Stripe.BankAccount{} = bank_account) do | ||
params = | ||
bank_account | ||
|> Map.from_struct | ||
|> Map.take(@stripe_attributes) | ||
|> rename(:id, :id_from_stripe) | ||
|> rename(:account, :account_id_from_stripe) | ||
|
||
{:ok, params} | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
lib/code_corps/stripe_service/events/external_account_created.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,5 @@ | ||
defmodule CodeCorps.StripeService.Events.ExternalAccountCreated do | ||
def handle(%{"data" => %{"object" => %{"id" => id_from_stripe}}}) do | ||
CodeCorps.StripeService.StripeExternalAccountService.create(id_from_stripe) | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
lib/code_corps/stripe_service/stripe_external_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,16 @@ | ||
defmodule CodeCorps.StripeService.StripeExternalAccountService do | ||
alias CodeCorps.{Repo, StripeExternalAccount} | ||
alias CodeCorps.StripeService.Adapters.StripeExternalAccountAdapter | ||
|
||
@api Application.get_env(:code_corps, :stripe) | ||
|
||
def create(id_from_stripe) do | ||
with {:ok, %Stripe.BankAccount{} = bank_account} <- @api.BankAccount.retrieve(id_from_stripe), | ||
{:ok, params} <- StripeExternalAccountAdapter.to_params(bank_account) | ||
do | ||
%StripeExternalAccount{} | ||
|> StripeExternalAccount.changeset(params) | ||
|> Repo.insert | ||
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
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,24 @@ | ||
defmodule CodeCorps.StripeTesting.BankAccount do | ||
def retrieve(id) do | ||
{:ok, bank_account(id)} | ||
end | ||
|
||
defp bank_account(id) do | ||
%Stripe.BankAccount{ | ||
id: id, | ||
object: "bank_account", | ||
account: "acct_1032D82eZvKYlo2C", | ||
account_holder_name: "Jane Austen", | ||
account_holder_type: "individual", | ||
bank_name: "STRIPE TEST BANK", | ||
country: "US", | ||
currency: "usd", | ||
default_for_currency: false, | ||
fingerprint: "1JWtPxqbdX5Gamtc", | ||
last4: "6789", | ||
metadata: {}, | ||
routing_number: "110000000", | ||
status: "new" | ||
} | ||
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
10 changes: 10 additions & 0 deletions
10
...epo/migrations/20161219160401_change_external_account_default_for_currency_to_boolean.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule CodeCorps.Repo.Migrations.ChangeExternalAccountDefaultForCurrencyToBoolean do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:stripe_external_accounts) do | ||
remove :default_for_currency | ||
add :default_for_currency, :boolean | ||
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
44 changes: 44 additions & 0 deletions
44
test/lib/code_corps/stripe_service/adapters/stripe_external_account_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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule CodeCorps.StripeService.Adapters.StripeExternalAccountTestAdapter do | ||
use ExUnit.Case, async: true | ||
|
||
import CodeCorps.StripeService.Adapters.StripeExternalAccountAdapter, only: [to_params: 1] | ||
|
||
@stripe_connect_account %Stripe.BankAccount{ | ||
id: "ba_19SSZG2eZvKYlo2CXnmzYU5H", | ||
object: "bank_account", | ||
account: "acct_1032D82eZvKYlo2C", | ||
account_holder_name: "Jane Austen", | ||
account_holder_type: "individual", | ||
bank_name: "STRIPE TEST BANK", | ||
country: "US", | ||
currency: "usd", | ||
default_for_currency: false, | ||
fingerprint: "1JWtPxqbdX5Gamtc", | ||
last4: "6789", | ||
metadata: {}, | ||
routing_number: "110000000", | ||
status: "new" | ||
} | ||
|
||
@local_map %{ | ||
id_from_stripe: "ba_19SSZG2eZvKYlo2CXnmzYU5H", | ||
account_id_from_stripe: "acct_1032D82eZvKYlo2C", | ||
account_holder_name: "Jane Austen", | ||
account_holder_type: "individual", | ||
bank_name: "STRIPE TEST BANK", | ||
country: "US", | ||
currency: "usd", | ||
default_for_currency: false, | ||
fingerprint: "1JWtPxqbdX5Gamtc", | ||
last4: "6789", | ||
routing_number: "110000000", | ||
status: "new" | ||
} | ||
|
||
describe "to_params/2" do | ||
test "converts from stripe map to local properly" do | ||
{:ok, result} = to_params(@stripe_connect_account) | ||
assert result == @local_map | ||
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
18 changes: 18 additions & 0 deletions
18
test/lib/code_corps/stripe_service/stripe_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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule CodeCorps.StripeService.StripeExternalAccountServiceTest do | ||
use ExUnit.Case, async: true | ||
|
||
use CodeCorps.ModelCase | ||
|
||
alias CodeCorps.StripeService.StripeExternalAccountService | ||
|
||
describe "create" do | ||
test "creates a StripeExternalAccount" do | ||
id_from_stripe = "ba_testing123" | ||
|
||
{:ok, %CodeCorps.StripeExternalAccount{} = bank_account} = | ||
StripeExternalAccountService.create(id_from_stripe) | ||
|
||
assert bank_account.id_from_stripe == 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