# Payzum — official Elixir SDK

Non-custodial crypto and stablecoin payments for Elixir applications. The SDK
owns the four things that break payment integrations: webhook signature
verification, exact decimal amounts, the retry policy, and the status vocabulary.

**Docs** · https://merchant.payzum.com/docs  
**Site** · https://payzum.com  
**Agent index** · https://merchant.payzum.com/llms.txt

```elixir
# mix.exs
{:payzum, "~> 0.1"}
```

Elixir 1.15 or newer. **No Hex dependencies at all**: HTTP is `:httpc`, HMAC is
`:crypto`, and the JSON parser ships with the SDK — because Elixir's standard
library has no JSON decoder, and the alternative would drag a library into a
merchant's tree just to avoid writing one.

## Charge a customer

```elixir
{:ok, client} = Payzum.new(System.fetch_env!("PAYZUM_API_KEY"))

{:ok, invoice} =
  Payzum.Payments.create(client, %{
    price_amount: "49.99",     # string: nothing is rounded on the way in
    price_currency: "usd",
    pay_currency: "all",       # the buyer picks the coin on the hosted checkout
    order_id: "ORDER-12345",
    ipn_callback_url: "https://shop.example/webhooks/payzum",
    success_url: "https://shop.example/thanks",
    cancel_url: "https://shop.example/cart",
    idempotency_key: "ORDER-12345"
  })

redirect(conn, external: invoice["invoice_url"])
```

Always send an `idempotency_key`: without one the SDK refuses to retry a create,
because a blind retry can mint a second real invoice and charge twice.

A 201 with no checkout URL returns `{:error, %Payzum.Errors.MissingInvoiceUrl{}}`
carrying the `payment_id`, so the draft can still be read.

## Get notified when it's paid

```elixir
def payzum_webhook(conn, _params) do
  # Cap the unauthenticated body before reading it.
  {:ok, raw, conn} = Plug.Conn.read_body(conn, length: 1_048_576, read_length: 65_535)

  {:ok, verifier} = Payzum.Webhooks.new(System.fetch_env!("PAYZUM_WEBHOOK_SECRET"))

  case Payzum.Webhooks.verify_payment_ipn(verifier, raw, conn.req_headers) do
    {:ok, payload} ->
      # Deduplicate on the id INSIDE the signed body, never on a header: the
      # header is not covered by the HMAC.
      event_id = Payzum.Webhooks.event_id_from_payload(payload)

      unless already_applied?(event_id) do
        {:ok, status} = Payzum.Status.from_merchant(payload["payment_status"])
        if Payzum.Status.paid?(status), do: fulfil(payload["order_id"])
      end

      send_resp(conn, 200, "ok")

    {:error, %Payzum.Errors.Signature{reason: "malformed_payload"}} ->
      send_resp(conn, 400, "malformed")

    {:error, %Payzum.Errors.Signature{}} ->
      send_resp(conn, 401, "bad signature")
  end
end
```

The order is fulfilled **from the signed notification, never from the buyer's
return**. A closed tab must not lose a paid order.

## Statuses

| Merchant (`payment_status`) | Buyer (`status`) |
|---|---|
| `waiting` | `pending` |
| `partially_paid` | `partial` |
| `finished` | `paid`, **`overpaid`** |
| `expired` | `expired` |
| `failed` | `cancelled` |

`Payzum.Status.from_merchant/1` and `from_buyer/1` return
`{:error, %Payzum.Errors.UnknownStatus{}}` for anything else instead of
guessing — a new value from the API must never be treated as paid.

Five IPN event types exist, two of them security-relevant:
`wrong_token_received` and `suspicious_token_received`.

## Errors

Every function returns `{:ok, value}` or `{:error, exception}`. Branch on the
code, never on the message.

```elixir
case Payzum.Payments.get(client, "pzi_…") do
  {:ok, invoice} ->
    invoice

  {:error, %Payzum.Errors.Api{code: "PAYMENT_NOT_FOUND"}} ->
    not_found()

  {:error, %Payzum.Errors.Api{} = error} ->
    if Payzum.Errors.Api.retryable?(error), do: retry_later(), else: raise(error)

  {:error, %Payzum.Errors.Transport{}} ->
    # No HTTP response at all: DNS, TLS, timeout, reset. The server's state is
    # unknown, so a create is not retried after one unless it carried a key.
    unavailable()
end
```

Exactly three of the sixteen codes are retryable, and `QUOTA_EXCEEDED` — which
arrives as a 429 — is deliberately not one of them.

## Amounts are exact

Every monetary field decodes to its exact source text, never a float:

```elixir
{:ok, invoice} = Payzum.Payments.get(client, "pzi_…")
invoice["price_amount"]   # "0.123456789012345678"
```

The parser keeps each number as the literal text between separators, and the
encoder validates the literal against a strict grammar before emitting it
unquoted, so `NaN`, `Infinity`, `1_000` and hex floats are rejected before
anything is sent.

The buyer surface is the exact source for a settled amount:

```elixir
{:ok, invoice} = Payzum.Invoices.status(client, "pzi_…")
invoice["payAmount"]
invoice["amountReceived"]
```

The ceiling worth knowing: the gateway casts to double before serialising, so the
merchant surface may already have lost digits upstream.

## Testing your integration

- `Payzum.sandbox/1` points at `https://staging.payzum.com`, which has isolated
  data and **separate API keys**.
- Unit tests: pass `transport: fn request -> … end` and a `sleeper:` to
  `Payzum.new/2`, so backoff is asserted instead of slept. A transport is a
  function, so a test can script the queue without a process.
- `Payzum.HttpcTransport.transport(ca_file: path)` trusts a custom CA bundle — a
  corporate proxy or a test harness. Verification is never disabled.
- Contract tests: `payzum-dev/payzum-conformance` runs this SDK against a real
  HTTPS fake API and compares it with the other languages.

MIT licensed. See `llms.txt` for the one-page summary agents read first.
