Skip to content

Commit 339e94d

Browse files
authored
Merge pull request #647 from code-corps/645-split-up-event-handler
Separate IgnoredEventHandler behavior for connect and platform events.
2 parents e8a931c + deb8f5b commit 339e94d

File tree

3 files changed

+79
-32
lines changed

3 files changed

+79
-32
lines changed

lib/code_corps/stripe_service/webhook_processing/event_handler.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ defmodule CodeCorps.StripeService.WebhookProcessing.EventHandler do
99
with {:ok, endpoint} <- infer_endpoint_from_handler(handler),
1010
{:ok, %StripeEvent{} = local_event} <- find_or_create_event(api_event, endpoint)
1111
do
12-
case IgnoredEventHandler.should_handle?(type) do
13-
true -> call_ignored_handler(local_event)
12+
case IgnoredEventHandler.should_handle?(type, handler) do
13+
true -> call_ignored_handler(local_event, handler)
1414
false -> call_handler(api_event, local_event, handler)
1515
end
1616
else
@@ -39,7 +39,7 @@ defmodule CodeCorps.StripeService.WebhookProcessing.EventHandler do
3939
end
4040
end
4141

42-
defp call_ignored_handler(local_event), do: IgnoredEventHandler.handle(local_event)
42+
defp call_ignored_handler(local_event, handler), do: IgnoredEventHandler.handle(local_event, handler)
4343

4444
defp call_handler(api_event, local_event, handler) do
4545
# results are multiple, so we convert the tuple to list for easier matching

lib/code_corps/stripe_service/webhook_processing/ignored_event_handler.ex

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
defmodule CodeCorps.StripeService.WebhookProcessing.IgnoredEventHandler do
22
alias CodeCorps.{StripeEvent, Repo}
3+
alias CodeCorps.StripeService.WebhookProcessing.{
4+
ConnectEventHandler, PlatformEventHandler
5+
}
36

4-
@ignored_event_types [
7+
@ignored_platform_event_types [
58
"account.external_account.created",
69
"application_fee.created",
710
"customer.created",
@@ -11,19 +14,31 @@ defmodule CodeCorps.StripeService.WebhookProcessing.IgnoredEventHandler do
1114
"plan.created"
1215
]
1316

17+
@ignored_connect_event_types [
18+
"account.external_account.created",
19+
"application_fee.created",
20+
"customer.created",
21+
"customer.updated",
22+
"customer.source.created",
23+
"customer.subscription.created",
24+
"invoice.created",
25+
"plan.created"
26+
]
27+
1428
@doc """
1529
Determines if an event type should be handled by __MODULE__
1630
1731
Returns true or false depending on specified type
1832
"""
19-
@spec should_handle?(String.t) :: boolean
20-
def should_handle?(type), do: Enum.member?(ignored_event_types, type)
33+
@spec should_handle?(String.t, Module.t) :: boolean
34+
def should_handle?(type, handler), do: handler |> ignored_event_types |> Enum.member?(type)
2135

2236
@doc """
2337
Returns a list of event types which are being explicitly ignored by the application.
2438
"""
25-
@spec ignored_event_types :: list
26-
def ignored_event_types, do: @ignored_event_types
39+
@spec ignored_event_types(Module.t) :: list
40+
def ignored_event_types(ConnectEventHandler), do: @ignored_connect_event_types
41+
def ignored_event_types(PlatformEventHandler), do: @ignored_platform_event_types
2742

2843
@doc """
2944
Takes in a `CodeCorps.StripeEvent` to be processed as "ignored".
@@ -32,21 +47,36 @@ defmodule CodeCorps.StripeService.WebhookProcessing.IgnoredEventHandler do
3247
3348
Returns `{:ok, %CodeCorps.StripeEvent{}}
3449
"""
35-
@spec handle(StripeEvent.t) :: {:ok, StripeEvent.t}
36-
def handle(%StripeEvent{type: type} = local_event) do
37-
with ignored_reason <- get_reason(type) do
50+
@spec handle(StripeEvent.t, Module.t) :: {:ok, StripeEvent.t}
51+
def handle(%StripeEvent{type: type} = local_event, handler) do
52+
with ignored_reason <- get_reason(type, handler) do
3853
local_event |> set_ignored(ignored_reason)
3954
end
4055
end
4156

42-
@spec get_reason(String.t) :: String.t
43-
defp get_reason("account.external_account.created"), do: "External accounts are stored locally upon updating a connect account."
44-
defp get_reason("application_fee.created"), do: "We don't make use of the application fee object."
45-
defp get_reason("customer.created"), do: "Customers are only created from the client."
46-
defp get_reason("customer.source.created"), do: "Cards are only created from the client. No need to handle"
47-
defp get_reason("customer.subscription.created"), do: "Subscriptions are only created from the client."
48-
defp get_reason("invoice.created"), do: "We prefer to handle other lifecycle events for invoices, like payment_succeeded."
49-
defp get_reason("plan.created"), do: "Plans are only created from the client."
57+
@spec get_reason(String.t, Module.t) :: String.t
58+
defp get_reason(type, ConnectEventHandler), do: get_connect_reason(type)
59+
defp get_reason(type, PlatformEventHandler), do: get_platform_reason(type)
60+
61+
@spec get_connect_reason(String.t) :: String.t
62+
defp get_connect_reason("account.external_account.created"), do: "External accounts are stored locally upon updating a connect account."
63+
defp get_connect_reason("application_fee.created"), do: "We don't make use of the application fee object."
64+
defp get_connect_reason("customer.created"), do: "Customers are only created from the client."
65+
defp get_connect_reason("customer.updated"), do: "We already propagate connect customer updates when a platform customer update is handled."
66+
defp get_connect_reason("customer.source.created"), do: "Cards are only created from the client. No need to handle"
67+
defp get_connect_reason("customer.subscription.created"), do: "Subscriptions are only created from the client."
68+
defp get_connect_reason("invoice.created"), do: "We prefer to handle other lifecycle events for invoices, like payment_succeeded."
69+
defp get_connect_reason("plan.created"), do: "Plans are only created from the client."
70+
71+
@spec get_platform_reason(String.t) :: String.t
72+
defp get_platform_reason("account.external_account.created"), do: "External accounts are stored locally upon updating a connect account."
73+
defp get_platform_reason("application_fee.created"), do: "We don't make use of the application fee object."
74+
defp get_platform_reason("customer.created"), do: "Customers are only created from the client."
75+
defp get_platform_reason("customer.source.created"), do: "Cards are only created from the client. No need to handle"
76+
defp get_platform_reason("customer.subscription.created"), do: "Subscriptions are only created from the client."
77+
defp get_platform_reason("invoice.created"), do: "We prefer to handle other lifecycle events for invoices, like payment_succeeded."
78+
defp get_platform_reason("plan.created"), do: "Plans are only created from the client."
79+
5080

5181
@spec set_ignored(StripeEvent.t, String.t) :: {:ok, StripeEvent.t}
5282
defp set_ignored(%StripeEvent{} = local_event, ignored_reason) do
Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
11
defmodule CodeCorps.StripeService.WebhookProcessing.IgnoredEventHandlerTest do
22
use CodeCorps.ModelCase
33

4-
alias CodeCorps.StripeService.WebhookProcessing.IgnoredEventHandler
4+
alias CodeCorps.StripeService.WebhookProcessing.{
5+
ConnectEventHandler, IgnoredEventHandler, PlatformEventHandler
6+
}
57

6-
defp ignored?(type) do
8+
@spec ignored?(String.t, Module.t) :: boolean
9+
defp ignored?(type, handler) do
710
event = insert(:stripe_event, type: type)
8-
{:ok, event} = IgnoredEventHandler.handle(event)
11+
{:ok, event} = IgnoredEventHandler.handle(event, handler)
912

1013
event.ignored_reason && event.status == "ignored"
1114
end
1215

13-
describe "handle/1" do
14-
test "ignores events from the ignored events list" do
15-
IgnoredEventHandler.ignored_event_types
16-
|> Enum.each(fn(type) -> assert ignored?(type) end)
16+
describe "handle/2" do
17+
test "ignores events from the ignored platform events list" do
18+
IgnoredEventHandler.ignored_event_types(PlatformEventHandler)
19+
|> Enum.each(fn(type) -> assert ignored?(type, PlatformEventHandler) end)
1720

18-
assert_raise(FunctionClauseError, fn -> ignored?("some.other.type") end)
21+
assert_raise(FunctionClauseError, fn -> ignored?("some.other.type", PlatformEventHandler) end)
22+
end
23+
24+
test "ignores events from the ignored connect events list" do
25+
IgnoredEventHandler.ignored_event_types(ConnectEventHandler)
26+
|> Enum.each(fn(type) -> assert ignored?(type, ConnectEventHandler) end)
27+
28+
assert_raise(FunctionClauseError, fn -> ignored?("some.other.type", ConnectEventHandler) end)
1929
end
2030
end
2131

22-
describe "should_handle?/1" do
23-
test "returns true for types from the ignored list" do
24-
IgnoredEventHandler.ignored_event_types
25-
|> Enum.each(fn(type) -> assert IgnoredEventHandler.should_handle?(type) end)
32+
describe "should_handle?/2" do
33+
test "returns true for types from the ignored platform events list" do
34+
IgnoredEventHandler.ignored_event_types(PlatformEventHandler)
35+
|> Enum.each(fn(type) -> assert IgnoredEventHandler.should_handle?(type, PlatformEventHandler) end)
36+
37+
refute IgnoredEventHandler.should_handle?("some.other.type", PlatformEventHandler)
38+
end
39+
40+
test "returns true for types from the ignored connect events list" do
41+
IgnoredEventHandler.ignored_event_types(ConnectEventHandler)
42+
|> Enum.each(fn(type) -> assert IgnoredEventHandler.should_handle?(type, ConnectEventHandler) end)
2643

27-
refute IgnoredEventHandler.should_handle?("some.other.type")
44+
refute IgnoredEventHandler.should_handle?("some.other.type", ConnectEventHandler)
2845
end
2946
end
3047
end

0 commit comments

Comments
 (0)