Payzum.Webhooks (Payzum v0.1.0)

Copy Markdown View Source

Verification for the three webhook families Payzum sends.

They are NOT interchangeable, and confusing them is the most expensive mistake you can make with this API — 20 of the 21 Payzum cart plugins read x-payzum-signature (the mass-payout header) for a payment IPN. The signature never verifies, deliveries 401, the gateway retries five times and dead-letters, and the order is silently never fulfilled.

  • payment IPN — HMAC-SHA-512, x-nowpayments-sig, key-sorted JSON
  • CoinPayments IPN — HMAC-SHA-512, HMAC, form-urlencoded
  • mass payout — HMAC-SHA-256, X-Payzum-Signature, JSON as sent

Header names are fixed and owned by this module. They are deliberately not parameters: a setting is an invitation to fill it with the wrong value.

Summary

Functions

The deduplication id from the header.

The deduplication id carried INSIDE the signed body.

Case-insensitive header lookup, including the CGI form.

Whether the event is one of the five documented types.

The five event types the payment IPN emits — five, not two.

Build a verifier.

Verify a CoinPayments-shaped IPN and return its form fields.

Verify a mass-payout webhook and return its payload.

Verify a payment IPN and return its decoded payload.

Types

t()

@type t() :: %Payzum.Webhooks{replay_window: pos_integer(), secret: String.t()}

Functions

coinpayments_header()

event_id(headers)

The deduplication id from the header.

The header is NOT covered by the signature, so prefer event_id_from_payload/1.

event_id_from_payload(payload)

The deduplication id carried INSIDE the signed body.

The payment IPN signs event_id, a mass payout signs eventId and a CoinPayments IPN signs ipn_id. Retries reuse the value, so a second delivery with the same id must be a no-op — and because it is inside the signed body, an attacker cannot forge a new one from a captured delivery.

event_id_header()

header_value(headers, name)

Case-insensitive header lookup, including the CGI form.

Frameworks normalise header casing differently — Plug lower-cases and prefixes some of them, Cowboy passes the raw name — and matching case-sensitively would work in development and fail in production.

ipn_event_type?(event)

Whether the event is one of the five documented types.

ipn_event_types()

The five event types the payment IPN emits — five, not two.

An integration that handles only paid/expired silently discards the three others, including the two that matter for security.

mass_payout_header()

new(secret, opts \\ [])

@spec new(String.t(), keyword()) :: {:ok, t()} | {:error, Payzum.Errors.Signature.t()}

Build a verifier.

payment_ipn_header()

verify_coinpayments_ipn(verifier, raw_body, headers)

@spec verify_coinpayments_ipn(t(), binary(), map()) ::
  {:ok, map()} | {:error, Payzum.Errors.Signature.t()}

Verify a CoinPayments-shaped IPN and return its form fields.

There is no freshness check: the payload carries no timestamp, so a replay window is impossible. Deduplicating on ipn_id is the only defence available.

verify_mass_payout(verifier, raw_body, headers, opts \\ [])

@spec verify_mass_payout(t(), binary(), map(), keyword()) ::
  {:ok, map()} | {:error, Payzum.Errors.Signature.t()}

Verify a mass-payout webhook and return its payload.

verify_payment_ipn(verifier, raw_body, headers, opts \\ [])

@spec verify_payment_ipn(t(), binary(), map(), keyword()) ::
  {:ok, map()} | {:error, Payzum.Errors.Signature.t()}

Verify a payment IPN and return its decoded payload.

raw_body must be the bytes exactly as received, before any parsing.