-
Notifications
You must be signed in to change notification settings - Fork 84
Store events to avoid duplication. Move event handling to async tasks. #544
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
lib/code_corps/stripe_service/webhook_processing/connect_event_handler.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| defmodule CodeCorps.StripeService.WebhookProcessing.ConnectEventHandler do | ||
| @moduledoc """ | ||
| In charge of handling Stripe Connect webhooks | ||
| """ | ||
|
|
||
| alias CodeCorps.StripeService.Events | ||
|
|
||
| @doc """ | ||
| Handles Stripe Connect webhooks | ||
|
|
||
| ## Returns | ||
| * The result of calling the specific handlers `handle/1` function. This result ought ot be a tupple, | ||
| in which the first member is `:ok`, followed by one or more other elements, usually modified records. | ||
| * `{:ok, :unhandled_event}` if the specific event is not supported yet or at all | ||
| """ | ||
| def handle_event(%{"type" => type} = attributes), do: do_handle(type, attributes) | ||
|
|
||
| defp do_handle("account.updated", attributes), do: Events.AccountUpdated.handle(attributes) | ||
| defp do_handle("customer.subscription.deleted", attributes), do: Events.CustomerSubscriptionDeleted.handle(attributes) | ||
| defp do_handle("customer.subscription.updated", attributes), do: Events.CustomerSubscriptionUpdated.handle(attributes) | ||
| defp do_handle(_, _), do: {:ok, :unhandled_event} | ||
| end |
21 changes: 21 additions & 0 deletions
21
lib/code_corps/stripe_service/webhook_processing/platform_event_handler.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| defmodule CodeCorps.StripeService.WebhookProcessing.PlatformEventHandler do | ||
| @moduledoc """ | ||
| In charge of handling Stripe Platform webhooks | ||
| """ | ||
|
|
||
| alias CodeCorps.StripeService.Events | ||
|
|
||
| @doc """ | ||
| Handles Stripe Platform webhooks | ||
|
|
||
| ## Returns | ||
| * The result of calling the specific handlers `handle/1` function. This result ought ot be a tupple, | ||
| in which the first member is `:ok`, followed by one or more other elements, usually modified records. | ||
| * `{:ok, :unhandled_event}` if the specific event is not supported yet or at all | ||
| """ | ||
| def handle_event(%{"type" => type} = attributes), do: do_handle(type, attributes) | ||
|
|
||
| defp do_handle("customer.updated", attributes), do: Events.CustomerUpdated.handle(attributes) | ||
| defp do_handle("customer.source.updated", attributes), do: Events.CustomerSourceUpdated.handle(attributes) | ||
| defp do_handle(_, _), do: {:ok, :unhandled_event} | ||
| end |
84 changes: 84 additions & 0 deletions
84
lib/code_corps/stripe_service/webhook_processing/webhook_processor.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| defmodule CodeCorps.StripeService.WebhookProcessing.WebhookProcessor do | ||
| @moduledoc """ | ||
| Used to process a Stripe webhook request. | ||
| """ | ||
|
|
||
| alias CodeCorps.StripeEvent | ||
| alias CodeCorps.Repo | ||
|
|
||
| @doc """ | ||
| Used to process a Stripe webhook event. | ||
|
|
||
| Receives the event json as the first parameter. | ||
| Since a webhook can be a platform or a connect webhook, | ||
| the function requires the handler module as the second parameter. | ||
|
|
||
| ## Returns | ||
|
|
||
| * `{:ok, :ignored_by_environment}` if the event was ignored due to environment mismatch | ||
| * `{:ok, :enqueued}` if the event will be handled | ||
|
|
||
| ## Note | ||
|
|
||
| Stripe events can have their `livemode` property set to `true` or `false`. | ||
| A livemode event should be handled by the production environment, while all other environments | ||
| handle non-livemode events. | ||
| """ | ||
| def process_async(%{} = json, handler) do | ||
| case event_matches_environment?(json) do | ||
| true -> do_process_async(json, handler) | ||
| false -> {:ok, :ignored_by_environment} | ||
| end | ||
| end | ||
|
|
||
| defp do_process_async(json, handler) do | ||
| Task.Supervisor.start_child(:webhook_processor, fn -> do_process(json, handler) end) | ||
| end | ||
|
|
||
| defp event_matches_environment?(%{"livemode" => livemode}) do | ||
| case Application.get_env(:code_corps, :stripe_env) do | ||
| :prod -> livemode | ||
| _ -> !livemode | ||
| end | ||
| end | ||
|
|
||
| defp do_process(%{"id" => event_id, "type" => event_type} = json, handler) do | ||
| with {:ok, %StripeEvent{} = event} <- find_or_create_event(event_id, event_type) do | ||
| case handler.handle_event(json) |> Tuple.to_list do | ||
| [:ok, :unhandled_event] -> event |> set_unhandled | ||
| [:ok | _results] -> event |> set_processed | ||
| [:error | _error] -> event |> set_errored | ||
| end | ||
| else | ||
| {:error, :already_processing} -> nil | ||
| end | ||
| end | ||
|
|
||
| defp find_or_create_event(id_from_stripe, type) do | ||
| case find_event(id_from_stripe) do | ||
| %StripeEvent{status: "processing"} -> {:error, :already_processing} | ||
| %StripeEvent{} = event -> {:ok, event} | ||
| nil -> create_event(id_from_stripe, type) | ||
| end | ||
| end | ||
|
|
||
| defp find_event(id_from_stripe) do | ||
| Repo.get_by(StripeEvent, id_from_stripe: id_from_stripe) | ||
| end | ||
|
|
||
| defp create_event(id_from_stripe, type) do | ||
| %StripeEvent{} |> StripeEvent.create_changeset(%{id_from_stripe: id_from_stripe, type: type}) |> Repo.insert | ||
| end | ||
|
|
||
| defp set_processed(%StripeEvent{} = event) do | ||
| event |> StripeEvent.update_changeset(%{status: "processed"}) |> Repo.update | ||
| end | ||
|
|
||
| defp set_errored(%StripeEvent{} = event) do | ||
| event |> StripeEvent.update_changeset(%{status: "errored"}) |> Repo.update | ||
| end | ||
|
|
||
| defp set_unhandled(%StripeEvent{} = event) do | ||
| event |> StripeEvent.update_changeset(%{status: "unhandled"}) |> Repo.update | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| defmodule CodeCorps.Repo.Migrations.AddStripeEvents do | ||
| use Ecto.Migration | ||
|
|
||
| def change do | ||
| create table(:stripe_events) do | ||
| add :id_from_stripe, :string, null: false | ||
| add :status, :string, default: "unprocessed" | ||
| add :type, :string, null: false | ||
|
|
||
| timestamps() | ||
| end | ||
|
|
||
| create unique_index(:stripe_events, [:id_from_stripe]) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_bymight return anil. We weren't restricting the type.