# Signed payment receipts

Availability is gated. This feature does not send funds or sign wallet transactions.

Use a merchant secret API key to GET `/v1/payments/{paymentId}/receipt`.
Restricted agent keys and publishable keys cannot use this route. The response's
`data.status` is `issued` with `data.jws`, `pending` when no completed verification
snapshot exists, or `unavailable` when issuance is disabled. Retry pending reads;
never interpret pending/unavailable as a payment failure. Merchant dashboard
users with payment read permission can read receipts; the agent reporting view
requires export permission (owner, admin, finance).

Receipts are immutable historical evidence for one payment, not fulfillment
instructions or proof of the invoice's current state. They include invoice and
payment IDs, exact decimal customer credit and merchant receipt, settlement
chain, token contract and six-decimal scale, transaction hash and recipient.
`collectionStateAtVerification`, invoice credit, remaining amount and overpayment
are captured only after invoice aggregation and the payment's checkout completion.
A partial receipt remains partial after later payments. `paymentStatus=confirmed`
by itself does not mean the invoice was fully collected. Overpayment is distinct
from credit and from the merchant amount received. Use verified webhooks and
current invoice reads for fulfillment. A signature is Payclave's attestation;
it is not an additional blockchain confirmation or a guarantee of future finality.

`evidence=test_simulation` is explicitly simulated, even if a test flow names a
transaction hash. Live receipts use `independent_onchain_verification`. Payer is
omitted unless independently proven in live mode; a provider-reported address is
never promoted. No customer name, email, provider payload, or credential appears.
Payments finalized before this feature captured evidence can remain pending;
there is no unsafe automatic retrospective attestation.

## Verify with the server SDK

The source SDK adds `getPaymentReceipt` and `verifyPaymentReceipt` (publication
is a separate release gate). Obtain trusted keys from your configured API
origin's `/v1/receipt-keys` response (`data.keys`); do not trust a receipt's URLs,
embedded keys, or an issuer supplied by an untrusted payer.

```ts
import { verifyPaymentReceipt } from "@payclave/sdk-server"
const receipt = verifyPaymentReceipt(jws, trustedKeys, {
  issuer: "https://api.payclave.com",
  mode: "live",
  paymentId: expectedPaymentId,
  invoiceId: expectedInvoiceId,
})
```

The verifier pins algorithm, type, schema, domain, issuer, mode and resource IDs.
Keys use JWK `kty=OKP`, `crv=Ed25519`, `alg=EdDSA`, `use=sig`, and a unique `kid`.
See [RFC 8037](https://www.rfc-editor.org/rfc/rfc8037) and
[RFC 7515](https://www.rfc-editor.org/rfc/rfc7515).

Compact JWS has three unpadded base64url segments. Verify Ed25519 over ASCII
`protected.payload`, using the exact received segments; never parse and
re-serialize JSON before verifying. Protected header contains only `alg`, `kid`,
`typ=payclave-payment-receipt+jws`. Payload uses
`schema=payclave.payment-receipt.v1`, `domain=payclave.payment-receipt`, `iss`, and
explicit `mode`. Issuance serializes JSON once, signs its bytes, and persists the
entire compact JWS. No JSON canonicalization algorithm is needed by verifiers;
the stored bytes are authoritative. Amounts are decimal strings, never floats.
Retain old verification keys and verify old receipts using their `kid`.

## Agent reporting and exports

`GET /v1/agent-payments?from=...&to=...` is secret-key-only and gated separately.
The UTC window uses payment creation time: inclusive `from`, exclusive `to`, at
most 366 days and 10,000 observed x402 attempts. Narrow a larger window. Pending,
failed and unaggregated confirmed records count as attempts but add no received
money. `verified` counts completed verification snapshots, including partial and
overpaid payments; in test mode this means simulated evidence. Groups keep
settlement chains and token contracts separate. Fee collected and fee waived
are separate exact decimal totals. There is no conversion to dollars or implied
merchant balance. Pages contain 50 records; use the opaque `nextCursor` as
`startingAfter` with the same window and mode. Totals cover the full window,
not just the page. This is a live report; rerun it after pending work completes.

POST `/v1/reconciliation-exports` with `paymentMethod: "x402"`,
`dateBasis: "payment_created"`, the same from/to, `timezone: "UTC"`, and
`format: "csv"` or `"json"`. Additional status, token and exception filters are
not accepted for agent exports. The existing durable archive lifecycle, expiry,
checksum, download access and PII restrictions apply. Payment rows contain only
matching x402 attempts in the window; transaction rows match their payment IDs.
Invoice rows and the ordinary reconciliation summary remain whole-invoice
context, potentially including other payments. Use `manifest.json.agentPayments`
for agent-only denominators, received money and collected/waived fees. Those
groups use the same cutoff and bounded query as reporting; they are additional
manifest metadata, not changes to the v1 tabular columns. Webhooks and adjustments
are omitted from this filtered export because they cannot safely be attributed
to one payment method. An export's cutoff is historical; a later live report can
change as processing completes.

Agent traffic currently means payment records tagged x402 by the server. Challenges and unsettled authorizations without a payment record are excluded from the denominator. The protocol does not prove that an AI agent paid. An ordinary hosted
checkout is not presumed to be agent traffic. Unsigned payer instructions are
being developed separately; no instruction markers or agent attribution for
those flows are assumed here.

Agent exports freeze membership, rows and report totals when requested; later
invoice or payment updates do not change the archive. Agent export requests are
limited to 10,000 total rows and 8 MiB of frozen data. Narrow the date window if
needed. Historical attempts without finalized cutoff evidence remain counted;
late-updated states can be `unknown_at_cutoff`. Only receipt evidence captured
and verified by the cutoff contributes verified received-money totals. A row
with `verification_evidence_unavailable` has no verified credit at that cutoff.
