11defmodule 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
0 commit comments