Repository navigation
Explicitly ignore events listed in #609 #639
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| defmodule CodeCorps.StripeService.Events.AccountUpdated do | ||
| def handle(%{"data" => %{"object" => %{"id" => id_from_stripe}}}) do | ||
| def handle(%{data: %{object: %{id: id_from_stripe}}}) do | ||
| CodeCorps.StripeService.StripeConnectAccountService.update_from_stripe(id_from_stripe) | ||
| end | ||
| end | ||
2 changes: 1 addition & 1 deletion
2
lib/code_corps/stripe_service/events/connect_external_account_created.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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| defmodule CodeCorps.StripeService.Events.ConnectExternalAccountCreated do | ||
| def handle(%{"data" => %{"object" => %{"account" => account_id_from_stripe, "id" => id_from_stripe}}}) do | ||
| def handle(%{data: %{object: %{account: account_id_from_stripe, id: id_from_stripe}}}) do | ||
| CodeCorps.StripeService.StripeConnectExternalAccountService.create(id_from_stripe, account_id_from_stripe) | ||
| end | ||
| end |
4 changes: 2 additions & 2 deletions
4
lib/code_corps/stripe_service/events/customer_source_updated.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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| defmodule CodeCorps.StripeService.Events.CustomerSourceUpdated do | ||
| def handle(%{"data" => %{"object" => %{"id" => card_id, "object" => "card"}}}) do | ||
| def handle(%{data: %{object: %Stripe.Card{id: card_id}}}) do | ||
| CodeCorps.StripeService.StripePlatformCardService.update_from_stripe(card_id) | ||
| end | ||
|
|
||
| def handle(%{"data" => %{"object" => %{"id" => _, "object" => _}}}), do: {:error, :unsupported_object} | ||
| def handle(_data), do: {:error, :unsupported_object} | ||
| end |
2 changes: 1 addition & 1 deletion
2
lib/code_corps/stripe_service/events/customer_subscription_deleted.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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| defmodule CodeCorps.StripeService.Events.CustomerSubscriptionDeleted do | ||
| def handle(%{"data" => %{"object" => %{"id" => stripe_sub_id, "customer" => connect_customer_id}}}) do | ||
| def handle(%{data: %{object: %{id: stripe_sub_id, customer: connect_customer_id}}}) do | ||
| CodeCorps.StripeService.StripeConnectSubscriptionService.update_from_stripe(stripe_sub_id, connect_customer_id) | ||
| end | ||
| end |
2 changes: 1 addition & 1 deletion
2
lib/code_corps/stripe_service/events/customer_subscription_updated.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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| defmodule CodeCorps.StripeService.Events.CustomerSubscriptionUpdated do | ||
| def handle(%{"data" => %{"object" => %{"id" => stripe_sub_id, "customer" => connect_customer_id}}}) do | ||
| def handle(%{data: %{object: %{id: stripe_sub_id, customer: connect_customer_id}}}) do | ||
| CodeCorps.StripeService.StripeConnectSubscriptionService.update_from_stripe(stripe_sub_id, connect_customer_id) | ||
| 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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| defmodule CodeCorps.StripeService.Events.CustomerUpdated do | ||
| def handle(%{"data" => %{"object" => %{"id" => id_from_stripe}}}) do | ||
| def handle(%{data: %{object: %{id: id_from_stripe}}}) do | ||
| CodeCorps.StripeService.StripePlatformCustomerService.update_from_stripe(id_from_stripe) | ||
| end | ||
| end |
4 changes: 3 additions & 1 deletion
4
lib/code_corps/stripe_service/events/invoice_payment_succeeded.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 |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| defmodule CodeCorps.StripeService.Events.InvoicePaymentSucceeded do | ||
| def handle(%{"data" => %{"object" => %{"id" => id_from_stripe, "customer" => customer_id_from_stripe}}}) do | ||
| def handle(%{data: %{object: | ||
| %{id: id_from_stripe, customer: customer_id_from_stripe} | ||
| }}) do | ||
| CodeCorps.StripeService.StripeInvoiceService.create(id_from_stripe, customer_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
19 changes: 19 additions & 0 deletions
19
lib/code_corps/stripe_service/webhook_processing/environment_filter.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,19 @@ | ||
| defmodule CodeCorps.StripeService.WebhookProcessing.EnvironmentFilter do | ||
| @moduledoc """ | ||
| Used to filter events based on environment | ||
| """ | ||
|
|
||
| @doc """ | ||
| Returns true if the livemode attribute of the event matches | ||
| the current environment of the Phoenix application. | ||
|
|
||
| - livemode events are processed only in production. | ||
| - non-livemode events are processed in all other environments | ||
| """ | ||
| def environment_matches?(%{"livemode" => livemode}) do | ||
| case Application.get_env(:code_corps, :stripe_env) do | ||
| :prod -> livemode | ||
| _ -> !livemode | ||
| end | ||
| end | ||
| end |
64 changes: 64 additions & 0 deletions
64
lib/code_corps/stripe_service/webhook_processing/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,64 @@ | ||
| defmodule CodeCorps.StripeService.WebhookProcessing.EventHandler do | ||
| alias CodeCorps.{StripeEvent, Repo} | ||
| alias CodeCorps.StripeService.Adapters.StripeEventAdapter | ||
| alias CodeCorps.StripeService.WebhookProcessing.{ | ||
| ConnectEventHandler, IgnoredEventHandler, PlatformEventHandler | ||
| } | ||
|
|
||
| def handle(%Stripe.Event{type: type} = api_event, handler) do | ||
| with {:ok, endpoint} <- infer_endpoint_from_handler(handler), | ||
| {:ok, %StripeEvent{} = local_event} <- find_or_create_event(api_event, endpoint) | ||
| do | ||
| case IgnoredEventHandler.should_handle?(type) do | ||
| true -> call_ignored_handler(local_event) | ||
| false -> call_handler(api_event, local_event, handler) | ||
| end | ||
| else | ||
| failure -> failure | ||
| end | ||
| end | ||
|
|
||
| defp infer_endpoint_from_handler(ConnectEventHandler), do: {:ok, "connect"} | ||
| defp infer_endpoint_from_handler(PlatformEventHandler), do: {:ok, "platform"} | ||
|
|
||
| defp find_or_create_event(%Stripe.Event{} = api_event, endpoint) do | ||
| case find_event(api_event.id) do | ||
| %StripeEvent{status: "processing"} -> {:error, :already_processing} | ||
| %StripeEvent{} = local_event -> {:ok, local_event} | ||
| nil -> create_event(api_event, endpoint) | ||
| end | ||
| end | ||
|
|
||
| defp find_event(id_from_stripe) do | ||
| Repo.get_by(StripeEvent, id_from_stripe: id_from_stripe) | ||
| end | ||
|
|
||
| defp create_event(%Stripe.Event{} = api_event, endpoint) do | ||
| with {:ok, params} <- StripeEventAdapter.to_params(api_event, %{"endpoint" => endpoint}) do | ||
| %StripeEvent{} |> StripeEvent.create_changeset(params) |> Repo.insert | ||
| end | ||
| end | ||
|
|
||
| defp call_ignored_handler(local_event), do: IgnoredEventHandler.handle(local_event) | ||
|
|
||
| defp call_handler(api_event, local_event, handler) do | ||
| # results are multiple, so we convert the tuple to list for easier matching | ||
| case api_event |> handler.handle_event |> Tuple.to_list do | ||
| [:ok, :unhandled_event] -> local_event |> set_unhandled | ||
| [:ok | _results] -> local_event |> set_processed | ||
| [:error | _error] -> local_event |> set_errored | ||
| end | ||
| end | ||
|
|
||
| defp set_errored(%StripeEvent{} = local_event) do | ||
| local_event |> StripeEvent.update_changeset(%{status: "errored"}) |> Repo.update | ||
| end | ||
|
|
||
| defp set_processed(%StripeEvent{} = local_event) do | ||
| local_event |> StripeEvent.update_changeset(%{status: "processed"}) |> Repo.update | ||
| end | ||
|
|
||
| defp set_unhandled(%StripeEvent{} = local_event) do | ||
| local_event |> StripeEvent.update_changeset(%{status: "unhandled"}) |> Repo.update | ||
| end | ||
| end |
55 changes: 55 additions & 0 deletions
55
lib/code_corps/stripe_service/webhook_processing/ignored_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,55 @@ | ||
| defmodule CodeCorps.StripeService.WebhookProcessing.IgnoredEventHandler do | ||
| alias CodeCorps.{StripeEvent, Repo} | ||
|
|
||
| @ignored_event_types [ | ||
| "application_fee.created", | ||
| "customer.created", | ||
| "customer.source.created", | ||
| "customer.subscription.created", | ||
| "invoice.created", | ||
| "plan.created" | ||
| ] | ||
|
|
||
| @doc """ | ||
| Determines if an event type should be handled by __MODULE__ | ||
|
|
||
| Returns true or false depending on specified type | ||
| """ | ||
| @spec should_handle?(String.t) :: boolean | ||
| def should_handle?(type), do: Enum.member?(ignored_event_types, type) | ||
|
|
||
| @doc """ | ||
| Returns a list of event types which are being explicitly ignored by the application. | ||
| """ | ||
| @spec ignored_event_types :: list | ||
| def ignored_event_types, do: @ignored_event_types | ||
|
|
||
| @doc """ | ||
| Takes in a `CodeCorps.StripeEvent` to be processed as "ignored". | ||
| Determines the reason for ignoring the event, then updates the record to | ||
| `status: "ignored"` and `ignored_reason: inferred_message` | ||
|
|
||
| Returns `{:ok, %CodeCorps.StripeEvent{}} | ||
| """ | ||
| @spec handle(StripeEvent.t) :: {:ok, StripeEvent.t} | ||
| def handle(%StripeEvent{type: type} = local_event) do | ||
| with ignored_reason <- get_reason(type) do | ||
| local_event |> set_ignored(ignored_reason) | ||
| end | ||
| end | ||
|
|
||
| @spec get_reason(String.t) :: String.t | ||
| defp get_reason("application_fee.created"), do: "We don't make use of the application fee object." | ||
| defp get_reason("customer.created"), do: "Customers are only created from the client." | ||
| defp get_reason("customer.source.created"), do: "Cards are only created from the client. No need to handle" | ||
| defp get_reason("customer.subscription.created"), do: "Subscriptions are only created from the client." | ||
| defp get_reason("invoice.created"), do: "We prefer to handle other lifecycle events for invoices, like payment_succeeded." | ||
| defp get_reason("plan.created"), do: "Plans are only created from the client." | ||
|
|
||
| @spec set_ignored(StripeEvent.t, String.t) :: {:ok, StripeEvent.t} | ||
| defp set_ignored(%StripeEvent{} = local_event, ignored_reason) do | ||
| local_event | ||
| |> StripeEvent.update_changeset(%{status: "ignored", ignored_reason: ignored_reason}) | ||
| |> 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
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
9 changes: 9 additions & 0 deletions
9
priv/repo/migrations/20170115230549_add_ignored_reason_to_stripe_events.exs
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,9 @@ | ||
| defmodule CodeCorps.Repo.Migrations.AddIgnoredReasonToStripeEvents do | ||
| use Ecto.Migration | ||
|
|
||
| def change do | ||
| alter table(:stripe_events) do | ||
| add :ignored_reason, :string | ||
| end | ||
| 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.
One of the changes involved passing in the fetched
Stripe.Eventinto the handlers, instead of the raw json.