Coffrify.Phoenix.WebhookController behaviour (Coffrify v0.9.0)

View Source

Drop-in Phoenix controller mixin for handling Coffrify webhooks.

Setup

In your router.ex:

pipeline :coffrify_webhook do
  plug :accepts, ["json"]
  plug Coffrify.Plug.VerifyWebhook,
    secret: {System, :fetch_env!, ["COFFRIFY_WEBHOOK_SECRET"]}
end

scope "/integrations" do
  pipe_through :coffrify_webhook
  post "/coffrify", MyAppWeb.CoffrifyWebhookController, :handle
end

In your controller:

defmodule MyAppWeb.CoffrifyWebhookController do
  use Coffrify.Phoenix.WebhookController

  @impl Coffrify.Phoenix.WebhookController
  def handle_event(%{"type" => "transfer.created"} = event, _conn) do
    MyApp.Analytics.log_transfer(event)
    :ok
  end

  def handle_event(%{"type" => "ping"}, _conn), do: :ok

  def handle_event(_event, _conn), do: :ignore
end

The handle_event/2 callback is required. Return :ok (or {:ok, anything}), :ignore, or {:error, reason} — the controller turns those into the right HTTP status (2xx for ok/ignore, 5xx for errors so Coffrify retries).

When the :replay_store option is passed to use, the controller deduplicates events via the configured Coffrify.Runtime.WebhookReplay adapter before invoking handle_event/2.

Summary

Callbacks

handle_event(event, conn)

@callback handle_event(event :: map(), conn :: Plug.Conn.t()) ::
  :ok | {:ok, term()} | :ignore | {:error, term()}