Raxol.Payments.Protocol behaviour (Raxol Payments v0.2.0)

Copy Markdown View Source

Behaviour for payment protocol implementations.

Each protocol handles a specific 402 payment flow. The auto-pay plugin tries each configured protocol's detect?/2 to find one that matches the server's 402 response, then delegates signing to that protocol.

Implementations

Summary

Callbacks

Extract the payment amount from a parsed challenge.

Build authorization headers for the payment.

Check if this protocol can handle the given 402 response.

Human-readable protocol name.

Parse the 402 response headers into a challenge map.

Parse the payment receipt from a successful response.

Functions

Resolve a protocol atom to its implementation module.

Types

challenge()

@type challenge() :: map()

headers()

@type headers() :: [{String.t(), String.t()}]

receipt()

@type receipt() :: map()

Callbacks

amount(challenge)

@callback amount(challenge :: challenge()) :: Decimal.t()

Extract the payment amount from a parsed challenge.

Returns the amount in human-decimal units (e.g. 1.00 means $1.00 USDC, not 1_000_000 atomic units). Implementations are expected to normalize via Raxol.Payments.Assets.to_human/2 using the challenge's asset metadata. The SpendingPolicy caps are written in the same units.

build_payment(challenge, wallet)

@callback build_payment(challenge :: challenge(), wallet :: module()) ::
  {:ok, headers()} | {:error, term()}

Build authorization headers for the payment.

Signs the challenge using the provided wallet module and returns headers to attach to the retry request.

detect?(status, headers)

@callback detect?(status :: integer(), headers :: headers()) :: boolean()

Check if this protocol can handle the given 402 response.

Inspects the response status and headers to determine if this protocol's payment flow applies.

name()

@callback name() :: String.t()

Human-readable protocol name.

parse_challenge(headers)

@callback parse_challenge(headers :: headers()) :: {:ok, challenge()} | {:error, term()}

Parse the 402 response headers into a challenge map.

Extracts payment requirements (price, currency, recipient, network) from the protocol-specific headers.

parse_receipt(headers)

@callback parse_receipt(headers :: headers()) :: {:ok, receipt()} | {:error, term()}

Parse the payment receipt from a successful response.

Extracts confirmation details (tx hash, amount settled, etc.) from the response headers after a successful paid request.

Functions

resolve(module)

@spec resolve(atom()) :: module()

Resolve a protocol atom to its implementation module.