# Paying for a top-up as an agent

An agent adds funds to an account at one address, with no sign-in, no cookie and no credential. It pays with x402 (USDC) or with MPP and the Stripe method that `link-cli` uses.

Live payments are switched on by the operator. Until then this address answers `503` with the code `payments_unavailable`, nothing is offered and nothing is charged.

## The request

```
POST https://coronium.ai/v1/topup
Content-Type: application/json

{"accountId": "<32 hex digits>", "usdMicros": "5000000"}
```

- `accountId` is the 32 lowercase hex digits that start the account's proxy username. It is not a secret.
- `usdMicros` is the amount in micro-USD, as a string of digits: a whole number of cents from `1000000` (1 USD) to `1000000000` (1,000 USD). `5000000` is 5 USD.
- The request takes no query, no other field and no session. Repeat it unchanged when you pay.

## The 402

Without a payment the answer is `402 Payment Required`. It offers every method the operator switched on, together:

- `PAYMENT-REQUIRED` holds base64 JSON of an x402 version 2 offer. `accepts` has one entry per network (Base `eip155:8453`, Solana `solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp`): scheme `exact`, the USDC `asset`, `amount` in atomic units (one unit is one micro-USD), `payTo`, `maxTimeoutSeconds` 300, and `extra.paymentFlow` `upfront`, so the payment settles before the balance is credited.
- `WWW-Authenticate: Payment` holds an MPP challenge with `method="stripe"` and `intent="charge"`. Its `request` names the amount in cents and the Stripe profile in `methodDetails.networkId`. It is bound to this account and this amount, and lasts five minutes.
- The body is the refusal `payment_required`, with `cause`, `fix`, `methods` and `guide`.

The 402 is the same for every well-formed id and stores nothing. A payment for an id that has no account is refused with `unknown_account` before any money moves.

## Pay with x402

1. Send the request without a payment and read `PAYMENT-REQUIRED`.
2. Let your x402 version 2 client sign one entry of `accepts` exactly as offered. On Base the authorization must be valid now and for no longer than `maxTimeoutSeconds` (`validBefore` at most 300 seconds ahead). On Solana the transaction is version 0, with the offered `feePayer` first and one token instruction, a `TransferChecked` of the USDC amount. Send no `extensions`, and `resource` only as the 402 offered it.
3. Send the same request again with the `PAYMENT-SIGNATURE` header the client made.

The service settles the payment once at its facilitator and credits it once, when its chain shows the transfer: on Base 10 blocks deep, on Solana `finalized`. Until then the answer is `202 payment_pending`. An entry that was changed in any field is refused with `offer_not_issued`, before anything settles.

## Pay with link-cli

```
link-cli mpp pay https://coronium.ai/v1/topup \
  --method POST \
  --header "Content-Type: application/json" \
  --data '{"accountId":"<32 hex digits>","usdMicros":"5000000"}' \
  --context "<at least 100 characters: what this top-up buys and why, which the person reads when approving>"
```

link-cli sends the request, reads the challenge, creates a spend request of credential type `shared_payment_token` for the challenge's amount and network id, waits for the person to approve it in the Link app, and repeats the request with `Authorization: Payment`. With `--spend-request-id` for an approved spend request it skips the creation and `--context` is not needed. `link-cli mpp decode --challenge '<the WWW-Authenticate value>'` prints the network id. Link takes at most 500 USD per spend request.

## The receipt

A credited payment answers `200`:

```json
{"quoteId": "...", "provider": "x402", "state": "credited", "usdMicros": "5000000",
 "reference": "eip155:8453/0x...", "balance": {"usdMicros": "5000000", "n": "..."}}
```

With x402, `PAYMENT-RESPONSE` holds base64 JSON `{"success": true, "transaction", "network", "payer"}`. With MPP, `Payment-Receipt` holds base64url JSON `{"status": "success", "method": "stripe", "reference": "pi_...", "timestamp"}`.

Sending the same payment again credits nothing and answers `200` and `credited` again, without the balance: a payment on a chain is public, so only the request that first claimed it is told the balance.

A payment is not bound to an account until it is claimed: whoever sends it first is credited. Keep a signed payment private until you send it, as you would cash.

## When the outcome is not known yet

If the settlement is not confirmed in time the answer is `202` with the code `payment_pending`. Do not pay again. Send the same request with the same payment a minute later to read where it stands, even after its challenge or its authorization has expired. A pending payment is credited once when it settles; the service never asks for a second payment while one is pending.

## Refusals

| Code | Status | What to do |
| --- | --- | --- |
| `payments_unavailable` | 503 | No method is switched on. Ask the operator for a credit, or top up by card while signed in |
| `payment_required` | 402 | Pay one of the offers and repeat the request |
| `invalid` | 400 | Correct the body or the payment header the `cause` names |
| `not_whole_cents` | 400 | Send `usdMicros` as a multiple of `10000` |
| `amount_out_of_range` | 400 | Send an amount between `minUsdMicros` and `maxUsdMicros` |
| `offer_not_issued` | 400 | Sign an entry of `accepts` unchanged, from a 402 for the same body |
| `challenge_invalid` | 402 | Pay the fresh challenge of this answer with the body it was issued for |
| `challenge_expired` | 402 | Pay the fresh challenge of this answer within five minutes |
| `payment_pending` | 202 | Wait, then repeat the same request with the same payment |
| `payment_failed` | 402 | The facilitator or Stripe refused that payment and it is never credited. Pay again with a new one |
| `too_many_payments` | 429 | The account had 10 payments in the last hour that did not pay. Wait, or top up by card while signed in |
| `payment_claimed` | 409 | That payment was presented for another account or amount |
| `topups_closed` | 409 | Nothing settles while `GET /app/api/buy` answers `charging` false. Nothing was charged. Check `charging` before paying |
| `unknown_account` | 404 | Use the id that starts the account's proxy username |
| `provider_not_configured` | 503 | Pay with a method this 402 offers |
| `method_not_allowed` | 405 | Send POST |
