Skip to content

Fix issue with Adapters.StripeEvent.to_params for an event containing a balance object #690

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/code_corps/stripe_service/adapters/stripe_event.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule CodeCorps.StripeService.Adapters.StripeEventAdapter do
@moduledoc """
Handles data transformations between the API `Stripe.Event` struct and maps of
attributes suitable for work with our own `StripeEvent` database objects.
"""
import CodeCorps.MapUtils, only: [keys_to_string: 1]
import CodeCorps.StripeService.Util, only: [transform_map: 2]

Expand All @@ -14,6 +18,7 @@ defmodule CodeCorps.StripeService.Adapters.StripeEventAdapter do
Transforms a `%Stripe.Event{}` and a set of local attributes into a
map of parameters used to create or update a `StripeEvent` record.
"""
@spec to_params(Stripe.Event.t, map) :: {:ok, map}
def to_params(%Stripe.Event{} = stripe_event, %{} = attributes) do
result =
stripe_event
Expand Down Expand Up @@ -45,12 +50,11 @@ defmodule CodeCorps.StripeService.Adapters.StripeEventAdapter do
params |> Map.merge(attributes)
end

defp add_object_type(params, stripe_event) do
object_type = stripe_event.data.object.object
params |> Map.put(:object_type, object_type)
end
# NOTE: unlike object_id, object_type should never be nil
# Due to that, we do not have a catch-all clause for the nil case
# If it ever is nil, it should fail and we should know about it
defp add_object_type(params, %{data: %{object: %{object: object}}}), do: params |> Map.put(:object_type, object)

defp add_object_id(params, stripe_event) do
params |> Map.put(:object_id, stripe_event.data.object.id)
end
defp add_object_id(params, %{data: %{object: %{id: id}}}), do: params |> Map.put(:object_id, id)
defp add_object_id(params, _stripe_event), do: params |> Map.put(:object_id, nil)
end
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,47 @@ defmodule CodeCorps.StripeService.Adapters.StripeEventTest do
"user_id" => "act_123"
}

@stripe_event_for_balance_available %Stripe.Event{
api_version: nil,
created: nil,
data: %{
# NOTE: stripity_stripe does not serialize Balance objects yet.
# Once it does, this map should be replaced with a Stripe.Balance struct
object: %{
available: [%{amount: 0, currency: "usd", source_types: %{card: 0}}],
connect_reserved: [%{amount: 0, currency: "usd"}],
livemode: false,
object: "balance",
pending: [%{amount: 0, currency: "usd", source_types: %{card: 0}}]
}
},
id: "evt_balance",
livemode: false,
object: "event",
pending_webhooks: nil,
request: nil,
type: "balance.available",
user_id: "act_with_balance"
}

@local_map_for_balance_available %{
"endpoint" => "connect",
"id_from_stripe" => "evt_balance",
"object_id" => nil,
"object_type" => "balance",
"type" => "balance.available",
"user_id" => "act_with_balance"
}

describe "to_params/2" do
test "converts from stripe map to local properly" do

{:ok, result} = to_params(@stripe_event, @attributes)
assert result == @local_map
end

test "works with balance.available event" do
{:ok, result} = to_params(@stripe_event_for_balance_available, @attributes)
assert result == @local_map_for_balance_available
end
end
end