-
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 StripeConnectAccount update endpoint for assigning an external ac…
…count
- Loading branch information
Showing
11 changed files
with
203 additions
and
38 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
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,17 +1,26 @@ | ||
defmodule CodeCorps.StripeService.StripeConnectAccountService do | ||
alias CodeCorps.{Repo, StripeConnectAccount} | ||
alias CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter | ||
|
||
@api Application.get_env(:code_corps, :stripe) | ||
|
||
# TODO: Replace with code that implements issue #564 | ||
|
||
def create(%{"country" => country_code, "organization_id" => organization_id} = attributes) do | ||
def create(%{"country" => country_code, "organization_id" => _} = attributes) do | ||
with {:ok, %Stripe.Account{} = account} <- @api.Account.create(%{country: country_code, managed: true}), | ||
{:ok, params} <- StripeConnectAccountAdapter.to_params(account, attributes) | ||
do | ||
%CodeCorps.StripeConnectAccount{} | ||
|> CodeCorps.StripeConnectAccount.create_changeset(params) | ||
|> CodeCorps.Repo.insert | ||
%StripeConnectAccount{} | ||
|> StripeConnectAccount.create_changeset(params) | ||
|> Repo.insert | ||
end | ||
end | ||
|
||
def add_external_account(%StripeConnectAccount{id_from_stripe: stripe_id} = record, external_account) do | ||
with {:ok, %Stripe.Account{} = stripe_account} <- @api.Account.update(stripe_id, %{external_account: external_account}), | ||
{:ok, params} <- StripeConnectAccountAdapter.to_params(stripe_account, %{}) | ||
do | ||
record | ||
|> StripeConnectAccount.stripe_update_changeset(params) | ||
|> Repo.update | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,83 @@ | ||
defmodule CodeCorps.StripeTesting.Account do | ||
def create(_map) do | ||
{:ok, do_create} | ||
end | ||
|
||
def retrieve(_id) do | ||
{:ok, do_create} | ||
end | ||
|
||
defp do_create do | ||
%Stripe.Account{ | ||
business_name: "Code Corps PBC", | ||
business_primary_color: nil, | ||
business_url: "codecorps.org", | ||
charges_enabled: true, | ||
country: "US", | ||
default_currency: "usd", | ||
details_submitted: true, | ||
display_name: "Code Corps Customer", | ||
email: "volunteers@codecorps.org", | ||
id: "acct_123", | ||
managed: true, | ||
metadata: %{}, | ||
statement_descriptor: "CODECORPS.ORG", | ||
support_email: nil, | ||
support_phone: "1234567890", | ||
support_url: nil, | ||
timezone: "America/Los_Angeles", | ||
transfers_enabled: true | ||
{:ok, create_stripe_record(%{})} | ||
end | ||
|
||
def retrieve(id) do | ||
{:ok, create_stripe_record(%{"id" => id})} | ||
end | ||
|
||
def update(id, attributes) do | ||
attributes = | ||
attributes | ||
|> CodeCorps.MapUtils.keys_to_string | ||
|> Map.merge(%{"id" => id}) | ||
|
||
{:ok, create_stripe_record(attributes)} | ||
end | ||
|
||
defp create_stripe_record(attributes) do | ||
with attributes <- account_fixture |> Map.merge(attributes) |> add_nestings | ||
do | ||
Stripe.Account |> Stripe.Converter.stripe_map_to_struct(attributes) | ||
end | ||
end | ||
|
||
defp account_fixture do | ||
%{ | ||
"business_name" => "Code Corps PBC", | ||
"business_primary_color" => nil, | ||
"business_url" => "codecorps.org", | ||
"charges_enabled" => true, | ||
"country" => "US", | ||
"default_currency" => "usd", | ||
"details_submitted" => true, | ||
"display_name" => "Code Corps Customer", | ||
"email" => "volunteers@codecorps.org", | ||
"external_accounts" => %{ | ||
"object" => "list", | ||
"data" => [], | ||
"has_more" => false, | ||
"total_count" => 0, | ||
"url" => "/v1/accounts/acct_123/external_accounts" | ||
}, | ||
"id" => "acct_123", | ||
"managed" => true, | ||
"metadata" => %{}, | ||
"statement_descriptor" => "CODECORPS.ORG", | ||
"support_email" => nil, | ||
"support_phone" => "1234567890", | ||
"support_url" => nil, | ||
"timezone" => "America/Los_Angeles", | ||
"transfers_enabled" => true | ||
} | ||
end | ||
|
||
defp add_nestings(map) do | ||
map | ||
|> add_external_account | ||
end | ||
|
||
defp add_external_account(%{"id" => account_id, "external_account" => external_account_id} = map) do | ||
external_accounts_map = %{ | ||
"object" => "list", | ||
"data" => [%{"id" => external_account_id}], | ||
"has_more" => false, | ||
"total_count" => 1, | ||
"url" => "/v1/accounts/#{account_id}/external_accounts" | ||
} | ||
|
||
Map.put(map, "external_accounts", external_accounts_map) | ||
end | ||
defp add_external_account(%{"id" => account_id} = map) do | ||
external_accounts_map = %{ | ||
"object" => "list", | ||
"data" => [], | ||
"has_more" => false, | ||
"total_count" => 1, | ||
"url" => "/v1/accounts/#{account_id}/external_accounts" | ||
} | ||
|
||
Map.put(map, "external_accounts", external_accounts_map) | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
priv/repo/migrations/20161219163909_add_external_account_to_stripe_connect_accounts.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,9 @@ | ||
defmodule CodeCorps.Repo.Migrations.AddExternalAccountToStripeConnectAccounts do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:stripe_connect_accounts) do | ||
add :external_account, :string | ||
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
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