X402.Extensions.PaymentIdentifier (X402 v0.3.3)

Copy Markdown View Source

Encodes and decodes x402 payment identifier payloads.

The payment identifier extension payload is Base64-encoded JSON with a required "paymentId" field.

Summary

Types

Payment identifier value used for idempotency.

Functions

Decodes a Base64 JSON payload and returns the payment identifier.

Encodes a payment identifier to a Base64 JSON payload.

Extracts and validates "paymentId" from a decoded payload map.

Types

decode_error()

@type decode_error() ::
  :invalid_base64 | :invalid_json | :missing_payment_id | :invalid_payment_id

encode_error()

@type encode_error() :: :invalid_payment_id | :invalid_json

payment_id()

@type payment_id() :: String.t()

Payment identifier value used for idempotency.

Functions

decode(value)

(since 0.1.0)
@spec decode(String.t()) :: {:ok, payment_id()} | {:error, decode_error()}

Decodes a Base64 JSON payload and returns the payment identifier.

Examples

iex> {:ok, encoded} = X402.Extensions.PaymentIdentifier.encode("payment-123")
iex> X402.Extensions.PaymentIdentifier.decode(encoded)
{:ok, "payment-123"}

iex> X402.Extensions.PaymentIdentifier.decode("not-base64")
{:error, :invalid_base64}

encode(payment_id)

(since 0.1.0)
@spec encode(payment_id()) :: {:ok, String.t()} | {:error, encode_error()}

Encodes a payment identifier to a Base64 JSON payload.

Examples

iex> {:ok, encoded} = X402.Extensions.PaymentIdentifier.encode("payment-123")
iex> {:ok, "payment-123"} = X402.Extensions.PaymentIdentifier.decode(encoded)

fetch_payment_id(payload)

(since 0.1.0)
@spec fetch_payment_id(term()) ::
  {:ok, payment_id()} | {:error, :missing_payment_id | :invalid_payment_id}

Extracts and validates "paymentId" from a decoded payload map.

Examples

iex> X402.Extensions.PaymentIdentifier.fetch_payment_id(%{"paymentId" => "pay-1"})
{:ok, "pay-1"}

iex> X402.Extensions.PaymentIdentifier.fetch_payment_id(%{})
{:error, :missing_payment_id}