Solaris.Webhooks.Plug (Solaris v1.0.0)

Copy Markdown View Source

A Plug for handling Solaris webhooks in Phoenix or any Plug-based app.

Handles signature verification, JSON parsing, event dispatch, and appropriate HTTP responses in one place.

Requirements

plug and plug_crypto must be added to your deps when using this module:

{:plug, "~> 1.15"},
{:plug_crypto, "~> 2.0"}

Setup

1. Preserve raw body before JSON parsing

In your endpoint.ex:

plug Plug.Parsers,
  parsers: [:urlencoded, :multipart, :json],
  pass: ["*/*"],
  json_decoder: Jason,
  body_reader: {Solaris.Webhooks.Plug.BodyReader, :read_body, []}

2. Add to router

# router.ex
post "/webhooks/solaris",
  Solaris.Webhooks.Plug,
  handler: MyApp.SolarisHandler,
  secret: System.get_env("SOLARIS_WEBHOOK_SECRET")

3. Implement your handler

defmodule MyApp.SolarisHandler do
  @behaviour Solaris.Webhooks.Handler

  @impl true
  def handle_event("BOOKING", payload, _event) do
    MyApp.Ledger.record(payload["booking"])
    :ok
  end

  # Always include a catch-all for forward compatibility
  def handle_event(_type, _payload, _event), do: :ok
end

Options

OptionRequiredDescription
:handleryesModule implementing Solaris.Webhooks.Handler
:secretyes*Webhook signing secret (* or set :solaris, :webhook_secret)
:signature_headernoHeader name (default: "solaris-webhook-signature")