View Source Ibanity.WebhookPlug (ibanity v1.1.0)
Helper Plug
to process webhook events and send them to a custom handler.
Installation
To handle webhook events, you must first configure your application's endpoint.
Add the following to endpoint.ex
, before Plug.Parsers
is loaded.
plug Ibanity.WebhookPlug,
path: "/webhook/ibanity",
handler: MyAppWeb.IbanityHandler
Supported options
path
: The URL path where your application should listen for Ibanity webhooks.handler
: Custom event handler module that accepts Ibanity event structs and processes them within your application. You must create this module.tolerance
: Maximum drift (in seconds) allowed for the webhook event timestamps. SeeIbanity.Webhook.construct_event/4
for more information.application
: Application configuration which should be used to fetch the webhook signing keys and compare to the webhook audience. SeeIbanity.Webhook.construct_event/4
for more information.
Handling events
You will need to create a custom event handler module to handle events.
Your event handler module should implement the Ibanity.WebhookHandler
behavior, defining a handle_event/1
function which takes an Ibanity event
struct and returns either {:ok, term}
or :ok
.
Example
# lib/myapp_web/ibanity_handler.ex
defmodule MyAppWeb.IbanityHandler do
@behaviour Ibanity.WebhookHandler
alias Ibanity.Webhooks.Xs2a.Synchronization
@impl true
def handle_event(%Synchronization.DetailsUpdated{} = event) do
# TODO: handle the xs2a.synchronization.detailsUpdated event
end
@impl true
def handle_event(%Synchronization.Failed{} = event) do
# TODO: handle the xs2a.synchronization.failed event
end
# Return HTTP 200 for unhandled events
@impl true
def handle_event(_event), do: :ok
end