-
Notifications
You must be signed in to change notification settings - Fork 86
Fix StripeConnectAccount #596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
AWS_ACCESS_KEY_ID= | ||
AWS_SECRET_ACCESS_KEY= | ||
CLOUDFRONT_DOMAIN= | ||
S3_BUCKET= | ||
SEGMENT_WRITE_KEY= | ||
SENTRY_DSN= | ||
STRIPE_SECRET_KEY= | ||
STRIPE_PLATFORM_CLIENT_ID= | ||
export AWS_ACCESS_KEY_ID= | ||
export AWS_SECRET_ACCESS_KEY= | ||
export CLOUDFRONT_DOMAIN= | ||
export S3_BUCKET= | ||
export SEGMENT_WRITE_KEY= | ||
export SENTRY_DSN= | ||
export STRIPE_SECRET_KEY= | ||
export STRIPE_PLATFORM_CLIENT_ID= |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do | |
|
||
def create(%{"project_id" => project_id} = attributes) do | ||
with {:ok, %Project{} = project} <- get_project(project_id) |> ProjectCanEnableDonations.validate, | ||
%{} = create_attributes <- get_create_attributes(), | ||
%{} = create_attributes <- get_create_attributes(project_id), | ||
connect_account_id <- project.organization.stripe_connect_account.id_from_stripe, | ||
{:ok, plan} <- @api.Plan.create(create_attributes, connect_account: connect_account_id), | ||
{:ok, params} <- StripeConnectPlanAdapter.to_params(plan, attributes) | ||
|
@@ -22,11 +22,12 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do | |
end | ||
end | ||
|
||
defp get_create_attributes do | ||
@spec get_create_attributes(binary) :: map | ||
defp get_create_attributes(project_id) do | ||
%{ | ||
amount: 1, # in cents | ||
currency: "usd", | ||
id: "month", | ||
id: "month_project_" <> to_string(project_id), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @begedin project is intentional since plans are per-project. |
||
interval: "month", | ||
name: "Monthly donation", | ||
statement_descriptor: "CODECORPS.ORG Donation" # No more than 22 chars | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,53 @@ defmodule CodeCorps.StripeService.Util do | |
""" | ||
def transform_map(api_map, mapping), do: mapping |> Enum.reduce(%{}, &map_field(&1, &2, api_map)) | ||
|
||
def transform_attributes(attributes, mapping) do | ||
attributes_map = map_keys_to_atoms(attributes) | ||
mapping |> Enum.reduce(%{}, &map_attribute(&1, &2, attributes_map)) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might not be a bad Idea to have a unit test for this specifically. Separate issue might be good enough for now, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 for separate issue. |
||
|
||
defp map_keys_to_atoms(m) do | ||
result = Enum.map(m, fn | ||
{k, v} when is_binary(k) -> | ||
a = String.to_existing_atom(k) | ||
{a, v} | ||
entry -> | ||
entry | ||
end) | ||
result |> Enum.into(%{}) | ||
end | ||
|
||
defp map_attribute({source_field, target_path}, target_map, source_map) do | ||
value = source_map |> Map.get(source_field) | ||
list = target_path |> Enum.reverse | ||
result = put_value(list, value, %{}) | ||
deep_merge(target_map, result) | ||
end | ||
|
||
defp put_value(_, value, map) when is_nil(value), do: map | ||
defp put_value([head | tail], value, map) do | ||
new_value = Map.put(%{}, head, value) | ||
put_value(tail, new_value, map) | ||
end | ||
defp put_value([], new_value, _map), do: new_value | ||
|
||
defp deep_merge(left, right) do | ||
Map.merge(left, right, &deep_resolve/3) | ||
end | ||
|
||
# Key exists in both maps, and both values are maps as well. | ||
# These can be merged recursively. | ||
defp deep_resolve(_key, left = %{}, right = %{}) do | ||
deep_merge(left, right) | ||
end | ||
|
||
# Key exists in both maps, but at least one of the values is | ||
# NOT a map. We fall back to standard merge behavior, preferring | ||
# the value on the right. | ||
defp deep_resolve(_key, _left, right) do | ||
right | ||
end | ||
|
||
# Takes a tuple which contains a target field and a source path, | ||
# then puts value on the source path from the source map | ||
# into to target map under the target field name. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will likely want to sweep through the code at some point and remove these
_ -> unhandled
cases. They basically swallow any unexpected errors in the process. We should explicitly list any failures we support and handle. If anything unexpected fails, we should treat it as a bug.This is basically equivalent with us wrapping a huge chunk of code into a
try-catch
that catches any sort of exception.