Skip to content

Commit 44ffc48

Browse files
authored
Merge pull request #1226 from code-corps/update-stripity-stripe
Update stripity stripe
2 parents dc7f094 + f360934 commit 44ffc48

39 files changed

Lines changed: 394 additions & 283 deletions

blueprint/api.apib

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,13 +2744,13 @@ This endpoint allows you to check whether a username is valid (by running a vali
27442744

27452745
Code Corps responds to [Stripe webhooks](https://stripe.com/docs/webhooks).
27462746

2747-
Since Code Corps is a platform, we have two separate webhook endpoints for dealing with both Stripe Connect (the managed accounts) and the platform (shared data across customers).
2747+
Since Code Corps is a platform, we have two separate webhook endpoints for dealing with both Stripe Connect (the custom accounts) and the platform (shared data across customers).
27482748

27492749
## Stripe Connect Webhook [/webhooks/stripe/connect]
27502750

2751-
Handles Stripe Connect events for managed accounts for projects on the Code Corps platform.
2751+
Handles Stripe Connect events for custom accounts for projects on the Code Corps platform.
27522752

2753-
This endpoint provides information about the actual payments and subscriptions that occur on the managed accounts.
2753+
This endpoint provides information about the actual payments and subscriptions that occur on the custom accounts.
27542754

27552755
### Stripe Connect Webhook [POST]
27562756

@@ -2772,7 +2772,7 @@ This endpoint provides information about the actual payments and subscriptions t
27722772

27732773
Handles Stripe events for the Code Corps platform itself.
27742774

2775-
The platform stores Stripe customers and cards so they can be reused across different projects, and fan out updates to all the managed Stripe accounts.
2775+
The platform stores Stripe customers and cards so they can be reused across different projects, and fan out updates to all the managed custom Stripe accounts.
27762776

27772777
### Stripe Platform Webhook [POST]
27782778

@@ -3169,7 +3169,7 @@ The platform stores Stripe customers and cards so they can be reused across diff
31693169

31703170
## Project Attributes (object)
31713171
+ approved: `true` (boolean) - Has the project been approved by Code Corps?
3172-
+ `can-activate-donations`: `true` (boolean) - Can the project turn on donations? True only if the project has at least one Donation Goal and the Organization has a Stripe Connect Account where `charges-enabled` is `true` and `transfers-enabled` is `true`.
3172+
+ `can-activate-donations`: `true` (boolean) - Can the project turn on donations? True only if the project has at least one Donation Goal and the Organization has a Stripe Connect Account where `charges-enabled` is `true` and `payouts-enabled` is `true`.
31733173
+ `cloudinary-public-id`: `pp0md2banaw7k6oa1ew9` (string)
31743174
+ description: 'Help build and fund public software projects for social good.' (string) - A short plain text description for the project
31753175
+ `icon-large-url`: ``//res.cloudinary.com/dlfnmtoq1/image/upload/c_fill,h_500,w_500/pp0md2banaw7k6oa1ew9` (string) - A url pointing to a large icon for the project
@@ -3448,16 +3448,16 @@ The platform stores Stripe customers and cards so they can be reused across diff
34483448
+ `charges-enabled`: false (boolean) - Whether or not the account can create live charges
34493449
+ country: `US` (string)
34503450
+ `default-currency`: `usd` (string)
3451-
+ `details-submitted`: false (boolean) - Whether or not account details have been submitted yet. Standalone accounts cannot receive transfers before this is true.
3451+
+ `details-submitted`: false (boolean) - Whether or not account details have been submitted yet. Standalone accounts cannot receive payouts before this is true.
34523452
+ `display-name`: `Stripe.com` (string) - The display name for this account. This is used on the Stripe dashboard to help differentiate between accounts.
34533453
+ email: `site@stripe.com` (string) - The primary user’s email address
34543454
+ `id-from-stripe`: `acct_1032D82eZvKYlo2C` (string)
34553455
+ `inserted-at`: `2016-07-08T03:03:51.967Z` (string)
3456-
+ managed: false (boolean) - Whether or not the account is managed by your platform.
34573456
+ `support-email`: null (string) - A publicly shareable email address that can be reached for support for this account
34583457
+ `support-phone`: null (string) - The publicly visible support phone number for the business
34593458
+ `support-url`: null (string) - A publicly shareable URL that can be reached for support for this account
3460-
+ `transfers-enabled`: false (boolean) - Whether or not Stripe will send automatic transfers for this account. This is only false when Stripe is waiting for additional information from the account holder.
3459+
+ `payouts-enabled`: false (boolean) - Whether or not Stripe will send automatic payouts for this account. This is only false when Stripe is waiting for additional information from the account holder.
3460+
+ type: `custom` (string) - The account type, as specified by Stripe. Can be `standard`, `express`, or `custom`. CodeCorps only deals with custom accounts.
34613461
+ `updated-at`: `2014-07-07T18:01:26.000Z` (string)
34623462

34633463
## Stripe Connect Account Resource (object)

lib/code_corps/model/stripe_connect_account.ex

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ defmodule CodeCorps.StripeConnectAccount do
1616
field :details_submitted, :boolean
1717
field :display_name, :string
1818
field :email, :string
19-
2019
field :external_account, :string
21-
2220
field :legal_entity_address_city, :string
2321
field :legal_entity_address_country, :string
2422
field :legal_entity_address_line1, :string
@@ -49,30 +47,25 @@ defmodule CodeCorps.StripeConnectAccount do
4947
field :legal_entity_ssn_last_4, :string, virtual: true
5048
field :legal_entity_ssn_last_4_provided, :boolean, default: false
5149
field :legal_entity_type, :string
52-
5350
field :legal_entity_verification_details, :string
5451
field :legal_entity_verification_details_code, :string
5552
field :legal_entity_verification_document, :string
5653
field :legal_entity_verification_status, :string
57-
5854
field :id_from_stripe, :string, null: false
59-
60-
field :managed, :boolean, default: true
61-
55+
field :payouts_enabled, :boolean
6256
field :support_email, :string
6357
field :support_phone, :string
6458
field :support_url, :string
65-
6659
field :tos_acceptance_date, :integer
6760
field :tos_acceptance_ip, :string
6861
field :tos_acceptance_user_agent, :string
69-
field :transfers_enabled, :boolean
70-
62+
field :type, :string, default: "custom", null: false
7163
field :verification_disabled_reason, :string
7264
field :verification_due_by, :integer
7365
field :verification_fields_needed, {:array, :string}, default: []
7466

7567
belongs_to :organization, CodeCorps.Organization
68+
7669
has_one :stripe_external_account, CodeCorps.StripeExternalAccount
7770

7871
timestamps()
@@ -122,27 +115,26 @@ defmodule CodeCorps.StripeConnectAccount do
122115
:legal_entity_verification_details_code,
123116
:legal_entity_verification_document,
124117
:legal_entity_verification_status,
125-
:managed,
118+
:payouts_enabled,
126119
:support_email,
127120
:support_phone,
128121
:support_url,
129122
:tos_acceptance_date,
130123
:tos_acceptance_ip,
131124
:tos_acceptance_user_agent,
132-
:transfers_enabled,
125+
:type,
133126
:verification_disabled_reason,
134127
:verification_due_by,
135128
:verification_fields_needed
136129
]
137130

138131
def create_changeset(struct, params \\ %{}) do
139132
valid_params = Enum.concat(@insert_params, @stripe_params)
140-
changeset = struct
133+
134+
struct
141135
|> cast(params, valid_params)
142136
|> validate_required([:id_from_stripe, :organization_id, :tos_acceptance_date])
143137
|> assoc_constraint(:organization)
144-
145-
changeset
146138
end
147139

148140
def webhook_update_changeset(struct, params \\ %{}) do

lib/code_corps/stripe_service/adapters/stripe_connect_account.ex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ defmodule CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter do
4949
{:legal_entity_verification_details_code, [:legal_entity, :verification, :details_code]},
5050
{:legal_entity_verification_document, [:legal_entity, :verification, :document]},
5151
{:legal_entity_verification_status, [:legal_entity, :verification, :status]},
52-
{:managed, [:managed]},
52+
{:payouts_enabled, [:payouts_enabled]},
5353
{:support_email, [:support_email]},
5454
{:support_phone, [:support_phone]},
5555
{:support_url, [:support_url]},
5656
{:tos_acceptance_date, [:tos_acceptance, :date]},
5757
{:tos_acceptance_ip, [:tos_acceptance, :ip]},
5858
{:tos_acceptance_user_agent, [:tos_acceptance, :user_agent]},
59-
{:transfers_enabled, [:transfers_enabled]},
59+
{:type, [:type]},
6060
{:verification_disabled_reason, [:verification, :disabled_reason]},
6161
{:verification_due_by, [:verification, :due_by]},
6262
{:verification_fields_needed, [:verification, :fields_needed]}
@@ -76,6 +76,17 @@ defmodule CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter do
7676
{:ok, result}
7777
end
7878

79+
def from_params_update(%{} = attributes) do
80+
result =
81+
attributes
82+
|> remove_attributes()
83+
|> MapUtils.keys_to_atom()
84+
|> MapTransformer.transform_inverse(@stripe_mapping)
85+
|> Map.drop([:type])
86+
87+
{:ok, result}
88+
end
89+
7990
@doc """
8091
Transforms a `%Stripe.Account{}` and a set of local attributes into a
8192
map of parameters used to create or update a `StripeConnectAccount` record.
@@ -120,7 +131,7 @@ defmodule CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter do
120131
end
121132

122133
defp do_add_external_account(map, nil), do: map
123-
defp do_add_external_account(map, %Stripe.ExternalAccount{id: id}) do
134+
defp do_add_external_account(map, %Stripe.BankAccount{id: id}) do
124135
map |> Map.put(:external_account, id)
125136
end
126137

lib/code_corps/stripe_service/adapters/stripe_external_account.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule CodeCorps.StripeService.Adapters.StripeExternalAccountAdapter do
99
:routing_number, :status
1010
]
1111

12-
def to_params(%Stripe.ExternalAccount{} = external_account, %StripeConnectAccount{} = connect_account) do
12+
def to_params(%Stripe.BankAccount{} = external_account, %StripeConnectAccount{} = connect_account) do
1313
params =
1414
external_account
1515
|> Map.from_struct

lib/code_corps/stripe_service/stripe_connect_account_service.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule CodeCorps.StripeService.StripeConnectAccountService do
3535
using attributes sent by the client
3636
"""
3737
def update(%StripeConnectAccount{id_from_stripe: id_from_stripe} = local_account, %{} = attributes) do
38-
with {:ok, from_params} <- StripeConnectAccountAdapter.from_params(attributes),
38+
with {:ok, from_params} <- StripeConnectAccountAdapter.from_params_update(attributes),
3939
{:ok, %Stripe.Account{} = api_account} <- @api.Account.update(id_from_stripe, from_params)
4040
do
4141
update_local_account(local_account, api_account, attributes)
@@ -81,7 +81,7 @@ defmodule CodeCorps.StripeService.StripeConnectAccountService do
8181
end
8282
end
8383

84-
# goes through all Stripe.ExternalAccount objects within the retrieved Stripe.Account object,
84+
# goes through all Stripe.BankAccount objects within the retrieved Stripe.Account object,
8585
# then either retrieves or creates a StripeExternalAccount object for each of them
8686
defp process_external_accounts(_, %Stripe.Account{external_accounts: %{data: []}}), do: {:ok, []}
8787
defp process_external_accounts(
@@ -100,9 +100,9 @@ defmodule CodeCorps.StripeService.StripeConnectAccountService do
100100
end
101101

102102
# retrieves or creates a StripeExternalAccount object associated to the provided
103-
# Stripe.ExternalAccount and StripeConnectAccount objects
103+
# Stripe.BankAccount and StripeConnectAccount objects
104104
# returns {:ok, list_of_created_external_account_records}
105-
defp find_or_create_external_account(%Stripe.ExternalAccount{} = api_external_account, connect_account) do
105+
defp find_or_create_external_account(%Stripe.BankAccount{} = api_external_account, connect_account) do
106106
case Repo.get_by(StripeExternalAccount, id_from_stripe: api_external_account.id) do
107107
nil -> StripeConnectExternalAccountService.create(api_external_account, connect_account)
108108
%StripeExternalAccount{} = local_external_account -> {:ok, local_external_account}

lib/code_corps/stripe_service/stripe_connect_card.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ defmodule CodeCorps.StripeService.StripeConnectCardService do
3535
|> create_non_stripe_attributes(connect_account)
3636

3737
with {:ok, %Stripe.Token{} = connect_token} <-
38-
@api.Token.create_on_connect_account(platform_customer_id, platform_card_id, connect_account: connect_account_id),
38+
@api.Token.create(%{customer: platform_customer_id, card: platform_card_id}, connect_account: connect_account_id),
3939
{:ok, %Stripe.Card{} = connect_card} <-
40-
@api.Card.create(:customer, connect_customer_id, connect_token.id, connect_account: connect_account_id),
40+
@api.Card.create(%{customer: connect_customer_id, source: connect_token.id}, connect_account: connect_account_id),
4141
{:ok, params} <-
4242
StripeConnectCardAdapter.to_params(connect_card, attributes)
4343
do
@@ -64,11 +64,13 @@ defmodule CodeCorps.StripeService.StripeConnectCardService do
6464
end
6565

6666
defp update_on_stripe(%StripeConnectCard{} = record, attributes) do
67+
params =
68+
attributes
69+
|> Map.put(:customer, record.stripe_platform_card.customer_id_from_stripe)
70+
6771
@api.Card.update(
68-
:customer,
69-
record.stripe_platform_card.customer_id_from_stripe,
7072
record.id_from_stripe,
71-
attributes,
73+
params,
7274
connect_account: record.stripe_connect_account.id_from_stripe)
7375
end
7476
end

lib/code_corps/stripe_service/stripe_connect_external_account_service.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
defmodule CodeCorps.StripeService.StripeConnectExternalAccountService do
22
@moduledoc """
33
Used to perform actions on a `StripeConnectExternalAccount` record while
4-
propagating to and from the associated `Stripe.ExternalAccount` record.
4+
propagating to and from the associated `Stripe.BankAccount` record.
55
"""
66
alias CodeCorps.{Repo, StripeConnectAccount, StripeExternalAccount}
77
alias CodeCorps.StripeService.Adapters.StripeExternalAccountAdapter
88

9-
@spec create(Stripe.ExternalAccount.t, StripeConnectAccount.t) :: {:ok, StripeExternalAccount.t}
10-
def create(%Stripe.ExternalAccount{} = external_account, %StripeConnectAccount{} = connect_account) do
9+
@spec create(Stripe.BankAccount.t, StripeConnectAccount.t) :: {:ok, StripeExternalAccount.t}
10+
def create(%Stripe.BankAccount{} = external_account, %StripeConnectAccount{} = connect_account) do
1111
with {:ok, params} <- StripeExternalAccountAdapter.to_params(external_account, connect_account) do
1212
%StripeExternalAccount{} |> StripeExternalAccount.changeset(params) |> Repo.insert
1313
end

lib/code_corps/stripe_service/stripe_connect_plan.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do
1717
"""
1818
@spec create(map) :: {:ok, StripeConnectPlan.t} |
1919
{:error, Ecto.Changeset.t} |
20-
{:error, Stripe.APIErrorResponse.t} |
20+
{:error, Stripe.Error.t} |
2121
{:error, :project_not_ready} |
2222
{:error, :not_found}
2323
def create(%{"project_id" => project_id} = attributes) do

lib/code_corps/stripe_service/stripe_connect_subscription_service.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule CodeCorps.StripeService.StripeConnectSubscriptionService do
2626
"""
2727
@spec find_or_create(map) :: {:ok, StripeConnectSubscription.t} |
2828
{:error, Ecto.Changeset.t} |
29-
{:error, Stripe.APIErrorResponse.t} |
29+
{:error, Stripe.Error.t} |
3030
{:error, :project_not_found} |
3131
{:error, :project_not_ready} |
3232
{:error, :user_not_found} |
@@ -52,7 +52,7 @@ defmodule CodeCorps.StripeService.StripeConnectSubscriptionService do
5252
a `Stripe.Subscription` record and using that data as update parameters
5353
"""
5454
@spec update_from_stripe(String.t, String.t) :: {:ok, StripeConnectSubscription.t} |
55-
{:error, Stripe.APIErrorResponse.t} |
55+
{:error, Stripe.Error.t} |
5656
{:error, :not_found}
5757
def update_from_stripe(stripe_id, connect_customer_id) do
5858
with {:ok, %StripeConnectAccount{} = connect_account} <- retrieve_connect_account(connect_customer_id),

lib/code_corps/stripe_service/stripe_platform_card.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule CodeCorps.StripeService.StripePlatformCardService do
2121

2222
def create(%{"stripe_token" => stripe_token, "user_id" => user_id} = attributes) do
2323
with %StripePlatformCustomer{} = customer <- StripePlatformCustomer |> CodeCorps.Repo.get_by(user_id: user_id),
24-
{:ok, %Stripe.Card{} = card} <- @api.Card.create(:customer, customer.id_from_stripe, stripe_token),
24+
{:ok, %Stripe.Card{} = card} <- @api.Card.create(%{customer: customer.id_from_stripe, source: stripe_token}),
2525
{:ok, params} <- StripePlatformCardAdapter.to_params(card, attributes)
2626
do
2727
%StripePlatformCard{} |> StripePlatformCard.create_changeset(params) |> Repo.insert
@@ -33,7 +33,7 @@ defmodule CodeCorps.StripeService.StripePlatformCardService do
3333

3434
def update_from_stripe(card_id) do
3535
with %StripePlatformCard{} = record <- Repo.get_by(StripePlatformCard, id_from_stripe: card_id),
36-
{:ok, %Stripe.Card{} = stripe_card} <- @api.Card.retrieve(:customer, record.customer_id_from_stripe, card_id),
36+
{:ok, %Stripe.Card{} = stripe_card} <- @api.Card.retrieve(card_id, %{customer: record.customer_id_from_stripe}),
3737
{:ok, params} <- StripePlatformCardAdapter.to_params(stripe_card, %{})
3838
do
3939
perform_update(record, params)

0 commit comments

Comments
 (0)