Skip to content

Commit f753cd9

Browse files
authored
Merge pull request #690 from code-corps/689-fix-event-aving-on-webhook-for-balance-object
Fix issue with Adapters.StripeEvent.to_params for an event containing a balance object
2 parents c409862 + 7712a34 commit f753cd9

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

lib/code_corps/stripe_service/adapters/stripe_event.ex

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
defmodule CodeCorps.StripeService.Adapters.StripeEventAdapter do
2+
@moduledoc """
3+
Handles data transformations between the API `Stripe.Event` struct and maps of
4+
attributes suitable for work with our own `StripeEvent` database objects.
5+
"""
26
import CodeCorps.MapUtils, only: [keys_to_string: 1]
37
import CodeCorps.StripeService.Util, only: [transform_map: 2]
48

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

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

53-
defp add_object_id(params, stripe_event) do
54-
params |> Map.put(:object_id, stripe_event.data.object.id)
55-
end
58+
defp add_object_id(params, %{data: %{object: %{id: id}}}), do: params |> Map.put(:object_id, id)
59+
defp add_object_id(params, _stripe_event), do: params |> Map.put(:object_id, nil)
5660
end

test/lib/code_corps/stripe_service/adapters/stripe_event_test.exs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,47 @@ defmodule CodeCorps.StripeService.Adapters.StripeEventTest do
3434
"user_id" => "act_123"
3535
}
3636

37+
@stripe_event_for_balance_available %Stripe.Event{
38+
api_version: nil,
39+
created: nil,
40+
data: %{
41+
# NOTE: stripity_stripe does not serialize Balance objects yet.
42+
# Once it does, this map should be replaced with a Stripe.Balance struct
43+
object: %{
44+
available: [%{amount: 0, currency: "usd", source_types: %{card: 0}}],
45+
connect_reserved: [%{amount: 0, currency: "usd"}],
46+
livemode: false,
47+
object: "balance",
48+
pending: [%{amount: 0, currency: "usd", source_types: %{card: 0}}]
49+
}
50+
},
51+
id: "evt_balance",
52+
livemode: false,
53+
object: "event",
54+
pending_webhooks: nil,
55+
request: nil,
56+
type: "balance.available",
57+
user_id: "act_with_balance"
58+
}
59+
60+
@local_map_for_balance_available %{
61+
"endpoint" => "connect",
62+
"id_from_stripe" => "evt_balance",
63+
"object_id" => nil,
64+
"object_type" => "balance",
65+
"type" => "balance.available",
66+
"user_id" => "act_with_balance"
67+
}
68+
3769
describe "to_params/2" do
3870
test "converts from stripe map to local properly" do
39-
4071
{:ok, result} = to_params(@stripe_event, @attributes)
4172
assert result == @local_map
4273
end
74+
75+
test "works with balance.available event" do
76+
{:ok, result} = to_params(@stripe_event_for_balance_available, @attributes)
77+
assert result == @local_map_for_balance_available
78+
end
4379
end
4480
end

0 commit comments

Comments
 (0)