Daraja.Callback.Guard (daraja v0.1.0)

Copy Markdown View Source

Optional idempotency guard for inbound M-PESA callbacks.

Safaricom may deliver the same callback more than once. Track seen transaction identifiers (CheckoutRequestID, TransID, OriginatorConversationID, etc.) before fulfilling orders or updating balances.

Add to your supervision tree when you want in-memory deduplication:

children = [
  {Daraja.Callback.Guard, []}
]

Entries expire after :ttl seconds (default 86_400) so the table does not grow without bound.

Summary

Functions

Records id if it has not been seen within the TTL window.

Functions

ensure_fresh(id, opts \\ [])

@spec ensure_fresh(
  term(),
  keyword()
) :: :ok | {:error, :duplicate}

Records id if it has not been seen within the TTL window.

Returns :ok on first sighting or {:error, :duplicate} when the same id was recorded recently.

server defaults to Daraja.Callback.Guard. Pass a custom atom when running multiple named guards.

start_link(opts \\ [])