Skip to content

Commit 5ff690f

Browse files
authored
Merge pull request #639 from code-corps/634-explicitly-ignore-events
Explicitly ignore events listed in #609
2 parents 51d3426 + 7711fe6 commit 5ff690f

22 files changed

Lines changed: 546 additions & 545 deletions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule CodeCorps.StripeService.Events.AccountUpdated do
2-
def handle(%{"data" => %{"object" => %{"id" => id_from_stripe}}}) do
2+
def handle(%{data: %{object: %{id: id_from_stripe}}}) do
33
CodeCorps.StripeService.StripeConnectAccountService.update_from_stripe(id_from_stripe)
44
end
55
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule CodeCorps.StripeService.Events.ConnectExternalAccountCreated do
2-
def handle(%{"data" => %{"object" => %{"account" => account_id_from_stripe, "id" => id_from_stripe}}}) do
2+
def handle(%{data: %{object: %{account: account_id_from_stripe, id: id_from_stripe}}}) do
33
CodeCorps.StripeService.StripeConnectExternalAccountService.create(id_from_stripe, account_id_from_stripe)
44
end
55
end
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule CodeCorps.StripeService.Events.CustomerSourceUpdated do
2-
def handle(%{"data" => %{"object" => %{"id" => card_id, "object" => "card"}}}) do
2+
def handle(%{data: %{object: %Stripe.Card{id: card_id}}}) do
33
CodeCorps.StripeService.StripePlatformCardService.update_from_stripe(card_id)
44
end
55

6-
def handle(%{"data" => %{"object" => %{"id" => _, "object" => _}}}), do: {:error, :unsupported_object}
6+
def handle(_data), do: {:error, :unsupported_object}
77
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule CodeCorps.StripeService.Events.CustomerSubscriptionDeleted do
2-
def handle(%{"data" => %{"object" => %{"id" => stripe_sub_id, "customer" => connect_customer_id}}}) do
2+
def handle(%{data: %{object: %{id: stripe_sub_id, customer: connect_customer_id}}}) do
33
CodeCorps.StripeService.StripeConnectSubscriptionService.update_from_stripe(stripe_sub_id, connect_customer_id)
44
end
55
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule CodeCorps.StripeService.Events.CustomerSubscriptionUpdated do
2-
def handle(%{"data" => %{"object" => %{"id" => stripe_sub_id, "customer" => connect_customer_id}}}) do
2+
def handle(%{data: %{object: %{id: stripe_sub_id, customer: connect_customer_id}}}) do
33
CodeCorps.StripeService.StripeConnectSubscriptionService.update_from_stripe(stripe_sub_id, connect_customer_id)
44
end
55
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule CodeCorps.StripeService.Events.CustomerUpdated do
2-
def handle(%{"data" => %{"object" => %{"id" => id_from_stripe}}}) do
2+
def handle(%{data: %{object: %{id: id_from_stripe}}}) do
33
CodeCorps.StripeService.StripePlatformCustomerService.update_from_stripe(id_from_stripe)
44
end
55
end
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
defmodule CodeCorps.StripeService.Events.InvoicePaymentSucceeded do
2-
def handle(%{"data" => %{"object" => %{"id" => id_from_stripe, "customer" => customer_id_from_stripe}}}) do
2+
def handle(%{data: %{object:
3+
%{id: id_from_stripe, customer: customer_id_from_stripe}
4+
}}) do
35
CodeCorps.StripeService.StripeInvoiceService.create(id_from_stripe, customer_id_from_stripe)
46
end
57
end

lib/code_corps/stripe_service/webhook_processing/connect_event_handler.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule CodeCorps.StripeService.WebhookProcessing.ConnectEventHandler do
1313
in which the first member is `:ok`, followed by one or more other elements, usually modified records.
1414
* `{:ok, :unhandled_event}` if the specific event is not supported yet or at all
1515
"""
16-
def handle_event(%{"type" => type} = attributes), do: do_handle(type, attributes)
16+
def handle_event(%{type: type} = attributes), do: do_handle(type, attributes)
1717

1818
defp do_handle("account.updated", attributes), do: Events.AccountUpdated.handle(attributes)
1919
defp do_handle("account.external_account.created", attributes), do: Events.ConnectExternalAccountCreated.handle(attributes)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule CodeCorps.StripeService.WebhookProcessing.EnvironmentFilter do
2+
@moduledoc """
3+
Used to filter events based on environment
4+
"""
5+
6+
@doc """
7+
Returns true if the livemode attribute of the event matches
8+
the current environment of the Phoenix application.
9+
10+
- livemode events are processed only in production.
11+
- non-livemode events are processed in all other environments
12+
"""
13+
def environment_matches?(%{"livemode" => livemode}) do
14+
case Application.get_env(:code_corps, :stripe_env) do
15+
:prod -> livemode
16+
_ -> !livemode
17+
end
18+
end
19+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
defmodule CodeCorps.StripeService.WebhookProcessing.EventHandler do
2+
alias CodeCorps.{StripeEvent, Repo}
3+
alias CodeCorps.StripeService.Adapters.StripeEventAdapter
4+
alias CodeCorps.StripeService.WebhookProcessing.{
5+
ConnectEventHandler, IgnoredEventHandler, PlatformEventHandler
6+
}
7+
8+
def handle(%Stripe.Event{type: type} = api_event, handler) do
9+
with {:ok, endpoint} <- infer_endpoint_from_handler(handler),
10+
{:ok, %StripeEvent{} = local_event} <- find_or_create_event(api_event, endpoint)
11+
do
12+
case IgnoredEventHandler.should_handle?(type) do
13+
true -> call_ignored_handler(local_event)
14+
false -> call_handler(api_event, local_event, handler)
15+
end
16+
else
17+
failure -> failure
18+
end
19+
end
20+
21+
defp infer_endpoint_from_handler(ConnectEventHandler), do: {:ok, "connect"}
22+
defp infer_endpoint_from_handler(PlatformEventHandler), do: {:ok, "platform"}
23+
24+
defp find_or_create_event(%Stripe.Event{} = api_event, endpoint) do
25+
case find_event(api_event.id) do
26+
%StripeEvent{status: "processing"} -> {:error, :already_processing}
27+
%StripeEvent{} = local_event -> {:ok, local_event}
28+
nil -> create_event(api_event, endpoint)
29+
end
30+
end
31+
32+
defp find_event(id_from_stripe) do
33+
Repo.get_by(StripeEvent, id_from_stripe: id_from_stripe)
34+
end
35+
36+
defp create_event(%Stripe.Event{} = api_event, endpoint) do
37+
with {:ok, params} <- StripeEventAdapter.to_params(api_event, %{"endpoint" => endpoint}) do
38+
%StripeEvent{} |> StripeEvent.create_changeset(params) |> Repo.insert
39+
end
40+
end
41+
42+
defp call_ignored_handler(local_event), do: IgnoredEventHandler.handle(local_event)
43+
44+
defp call_handler(api_event, local_event, handler) do
45+
# results are multiple, so we convert the tuple to list for easier matching
46+
case api_event |> handler.handle_event |> Tuple.to_list do
47+
[:ok, :unhandled_event] -> local_event |> set_unhandled
48+
[:ok | _results] -> local_event |> set_processed
49+
[:error | _error] -> local_event |> set_errored
50+
end
51+
end
52+
53+
defp set_errored(%StripeEvent{} = local_event) do
54+
local_event |> StripeEvent.update_changeset(%{status: "errored"}) |> Repo.update
55+
end
56+
57+
defp set_processed(%StripeEvent{} = local_event) do
58+
local_event |> StripeEvent.update_changeset(%{status: "processed"}) |> Repo.update
59+
end
60+
61+
defp set_unhandled(%StripeEvent{} = local_event) do
62+
local_event |> StripeEvent.update_changeset(%{status: "unhandled"}) |> Repo.update
63+
end
64+
end

0 commit comments

Comments
 (0)