# Payments

> All examples are **real output** captured from a running server in test mode (2026-10-02).
> Long values are kept; only the checkout token is shortened (`ct_…`).

A **payment** is one request for money from a customer. The customer picks a network and coin; we lock
a rate and give them a unique exact amount to your wallet address. Money goes straight to your
wallet — we never hold it.

## Create a payment
`POST /v1/payments`

| Param | Type | Required | Notes |
|---|---|---|---|
| `amount` | string | yes | Decimal string > 0. Fiat: max 2 decimals. Coin: max coin decimals |
| `currency` | string | yes | A fiat code from the platform list (USD, INR, EUR, GBP, AED, SGD, AUD, CAD, JPY, … — admin-managed) or a coin symbol (`USDT`, `BNB`, …) |
| `reference_id` | string ≤ 64 | recommended | Your order id (searchable, may repeat) |
| `description` | string ≤ 255 | no | Shown at checkout |
| `customer` | object | no | `name`, `email`, `phone` — never shown on checkout |
| `allowed_chains` | string[] | no | e.g. `["bsc","vdc"]`. Default: every chain where you have an active wallet |
| `allowed_coins` | string[] | no | e.g. `["USDT","USDC"]` |
| `chain` | string | no | When `currency` is a coin on several chains, fix the chain. With `coin`: the network to pay on |
| `coin` | string | no | Needs `chain`. Locks the payment to this coin **and returns `quote` (address, exact amount, `payment_uri`, `qr_url`) in this response** — one call for your own payment page. Checkout then opens straight on the QR |
| `expires_in` | integer seconds | no | Default 1800 (admin-set), min 300, max 604800 |
| `redirect_url` | https URL | no | Customer goes here after payment (signed query, see checkout.md) |
| `cancel_url` | https URL | no | Enables a "Cancel" button on checkout |
| `metadata` | object | no | ≤ 50 string values; key ≤ 40 chars, value ≤ 500 chars |

```bash
curl https://razcryptogateway.com/v1/payments \
  -H "Authorization: Bearer sk_test_…" \
  -H "Idempotency-Key: 7b1f0c2e-6a55-4d1e-9a51-2c4cbf6d3a10" \
  -H "Content-Type: application/json" \
  -d '{"amount":"25.00","currency":"USD","reference_id":"order_1042","description":"Pro plan — October","customer":{"email":"asha@example.com"},"redirect_url":"https://shop.example.com/thanks","metadata":{"plan":"pro"}}'
```
Response `201`:
```json
{
  "id": "pay_ccJHIhKHa4VfNp6HyXMD",
  "object": "payment",
  "livemode": false,
  "status": "pending",
  "flags": [],
  "amount": "25",
  "currency": "USD",
  "reference_id": "order_1042",
  "description": "Pro plan — October",
  "customer": {
    "email": "asha@example.com"
  },
  "metadata": {
    "plan": "pro"
  },
  "allowed_chains": null,
  "allowed_coins": null,
  "quote": null,
  "amount_received": null,
  "transfers": [],
  "checkout_url": "https://razcryptogateway.com/pay_ccJHIhKHa4VfNp6HyXMD?t=ct_…",
  "redirect_url": "https://shop.example.com/thanks",
  "cancel_url": null,
  "payment_link": null,
  "expires_at": "2026-10-02T12:05:04.820Z",
  "paid_at": null,
  "created_at": "2026-10-02T11:35:04.822Z"
}
```
`quote` is `null` because the customer can still choose between several coins. If only one
network + coin is possible (e.g. you pass `allowed_coins: ["USDT"]` and have one wallet), the quote is
created at once and returned here.

### One call for your own payment page

Single-coin shops (for example only USDT on BNB Smart Chain) can send `chain` + `coin`. The create response then already contains everything needed for your own page. No second call is needed.

```bash
curl https://razcryptogateway.com/v1/payments \
  -H "Authorization: Bearer sk_test_…" \
  -H "Idempotency-Key: order_1042" \
  -H "Content-Type: application/json" \
  -d '{"amount":"25.00","currency":"USD","chain":"bsc","coin":"USDT","reference_id":"order_1042"}'
```

```json
{
  "id": "pay_3fK9sQ2mZt7LxR1aPq0B",
  "status": "pending",
  "amount": "25.00",
  "currency": "USD",
  "checkout_url": "https://razcryptogateway.com/pay_3fK9sQ2mZt7LxR1aPq0B?t=ct_…",
  "expires_at": "2026-10-03T10:30:00.000Z",
  "quote": {
    "chain": "bsc",
    "chain_name": "BNB Smart Chain",
    "coin": "USDT",
    "amount": "25.0137",
    "address": "0x52908400098527886E0F7030069857D2E4169EE7",
    "payment_uri": "ethereum:0x55d398326f99059fF775485246999027B3197955@56/transfer?address=0x5290…&uint256=25013700000000000000",
    "qr_url": "https://razcryptogateway.com/checkout/v1/payments/pay_3fK9sQ2mZt7LxR1aPq0B/qr.svg?type=uri&t=ct_…",
    "address_qr_url": "https://razcryptogateway.com/checkout/v1/payments/pay_3fK9sQ2mZt7LxR1aPq0B/qr.svg?type=address&t=ct_…"
  }
}
```

**Building your own page:**
- **Show:**
  - the QR (`qr_url` as an `<img>`, or render `payment_uri` yourself)
  - the **exact** `quote.amount`
  - `quote.address`
  - the network name
  - a timer until `expires_at`
- **Status:** follow it with webhooks (`payment.confirming`, `payment.paid`) or by polling `GET /v1/payments/{id}`. Ship only on `paid`.
- **Unavailable coin:** if the coin is not available (unknown chain or coin, no active wallet on that chain, or the coin is switched off), the API answers right away with an error and no payment is left open. List what you can use with `GET /v1/chains` (all supported networks and coins) — your own enabled wallets decide what is available for your payments.

```json
{
  "error": {
    "type": "invalid_request_error",
    "code": "method_not_available",
    "message": "Chain \"tron\" is not supported. Supported chains: bsc, eth, polygon, solana, vdc",
    "param": "chain",
    "doc_url": "https://razcryptogateway.com/docs/errors#method_not_available"
  }
}
```

  Other answers: a coin that is not on that chain → `method_not_available` with `param: "coin"` and the list of supported coins; no active wallet on that chain → `no_payment_method_available` (`param: "chain"`); the coin switched off for your wallet → `method_not_available`. Coin symbols are not case-sensitive.
- **Hosted checkout or checkout.js:** if you open them for this payment instead, they skip network/coin selection and show the QR directly.

### The payment object
| Field | Meaning |
|---|---|
| `status` | `pending` → `confirming` → `paid`; or `underpaid`, `expired`, `cancelled` |
| `flags` | `late` (paid after expiry, within the grace window), `overpaid`, `manual` |
| `amount`, `currency` | What you charge |
| `quote` | Chosen network + coin, exact amount, address, QR data (below) |
| `amount_received` | Sum of matched on-chain transfers in the quoted coin |
| `transfers` | On-chain transfers matched to this payment |
| `checkout_url` | Hosted checkout page for this payment (contains a private token) |
| `livemode` | `false` for test keys |

**Ship the order only on `paid`.** `confirming` means the transfer is on-chain but not final yet.

## Available networks & coins — `GET /v1/payments/{id}/methods`
```json
{
  "object": "list",
  "has_more": false,
  "data": [
    {
      "chain": "bsc",
      "chain_name": "BNB Smart Chain",
      "coins": [
        {
          "coin": "USDT",
          "name": "Tether USD",
          "decimals": 18,
          "logo_url": null,
          "min_amount": "1"
        },
        {
          "coin": "USDC",
          "name": "USD Coin",
          "decimals": 18,
          "logo_url": null,
          "min_amount": "1"
        },
        {
          "coin": "BNB",
          "name": "BNB",
          "decimals": 18,
          "logo_url": null,
          "min_amount": "0.001"
        }
      ]
    }
  ]
}
```

## Quote — `POST /v1/payments/{id}/quote`
For a custom checkout: lock a network + coin. Allowed while nothing has been received; a new quote
replaces the old one (and frees its reserved amount).
```bash
curl https://razcryptogateway.com/v1/payments/pay_ccJHIhKHa4VfNp6HyXMD/quote \
  -H "Authorization: Bearer sk_test_…" -H "Content-Type: application/json" \
  -d '{"chain":"bsc","coin":"USDT"}'
```
```json
{
  "id": "pay_ccJHIhKHa4VfNp6HyXMD",
  "object": "payment",
  "livemode": false,
  "status": "pending",
  "flags": [],
  "amount": "25",
  "currency": "USD",
  "reference_id": "order_1042",
  "description": "Pro plan — October",
  "customer": {
    "email": "asha@example.com"
  },
  "metadata": {
    "plan": "pro"
  },
  "allowed_chains": null,
  "allowed_coins": null,
  "quote": {
    "chain": "bsc",
    "chain_name": "BNB Smart Chain",
    "coin": "USDT",
    "coin_name": "Tether USD",
    "contract": "0x55d398326f99059ff775485246999027b3197955",
    "decimals": 18,
    "address": "0x52908400098527886E0F7030069857D2E4169EE7",
    "amount": "25.0056",
    "rate": "0.999809565882114",
    "rate_currency": "USD",
    "payment_uri": "ethereum:0x55d398326f99059fF775485246999027B3197955@56/transfer?address=0x52908400098527886E0F7030069857D2E4169EE7&uint256=25005600000000000000",
    "wallet_links": [
      {
        "wallet": "metamask",
        "url": "https://metamask.app.link/send/0x55d398326f99059fF775485246999027B3197955@56/transfer?address=0x52908400098527886E0F7030069857D2E4169EE7&uint256=25005600000000000000"
      },
      {
        "wallet": "trust",
        "url": "https://link.trustwallet.com/send?asset=c20000714_t0x55d398326f99059fF775485246999027B3197955&address=0x52908400098527886E0F7030069857D2E4169EE7&amount=25.0056"
      }
    ],
    "finality": "finalized",
    "confirmations_required": null,
    "solana_reference": null,
    "rate_expires_at": "2026-10-02T11:55:10.138Z",
    "expires_at": "2026-10-02T12:05:04.820Z"
  },
  "amount_received": null,
  "transfers": [],
  "checkout_url": "https://razcryptogateway.com/pay_ccJHIhKHa4VfNp6HyXMD?t=ct_…",
  "redirect_url": "https://shop.example.com/thanks",
  "cancel_url": null,
  "payment_link": null,
  "expires_at": "2026-10-02T12:05:04.820Z",
  "paid_at": null,
  "created_at": "2026-10-02T11:35:04.822Z"
}
```
| `quote` field | Meaning |
|---|---|
| `amount` | **Exact** amount to send — unique per open payment on this address. The extra decimals are worth at most about ₹2 |
| `rate` | Price of 1 coin in `rate_currency`, locked until `rate_expires_at` (null when you priced in the coin) |
| `payment_uri` | EIP-681 / Solana Pay URI for the QR code |
| `wallet_links` | Deep links for MetaMask, Trust Wallet, Phantom |
| `qr_url` / `address_qr_url` | Ready-made QR images (SVG; use `qr.png` for PNG) of the payment URI / the plain address |
| `finality` / `confirmations_required` | How long we wait before `paid` |

Errors: `method_not_available`, `payment_not_quotable`, `amount_out_of_range`, `amount_slots_exhausted`,
`rates_unavailable`.

## Retrieve — `GET /v1/payments/{id}`

The way to **check a payment's status** from your server (for example before you ship, or if a webhook was missed). `status` is one of `pending`, `confirming`, `paid`, `underpaid`, `expired`, `cancelled` — only `paid` means the money arrived.
Returns the payment object (as above). Search by your order id with
`GET /v1/payments?reference_id=order_1042`.

## List — `GET /v1/payments`
Filters: `status`, `reference_id`, `chain`, `coin`, `created[gte]`, `created[lte]`; cursor pagination
with `limit` (1–100) and `starting_after`. Newest first.
```bash
curl "https://razcryptogateway.com/v1/payments?limit=2" -H "Authorization: Bearer sk_test_…"
```
```json
{
  "object": "list",
  "data": [
    {
      "id": "pay_hmF3p1rFRpArfk3iNEnr",
      "object": "payment",
      "status": "cancelled",
      "reference_id": "order_1044",
      "…": "…"
    },
    {
      "id": "pay_QP3ECwWl7qMXI5cz1xP4",
      "object": "payment",
      "status": "pending",
      "reference_id": "order_1043",
      "…": "…"
    }
  ],
  "has_more": true,
  "next_cursor": "pay_QP3ECwWl7qMXI5cz1xP4"
}
```
Next page: `GET /v1/payments?limit=2&starting_after=pay_QP3ECwWl7qMXI5cz1xP4`.

## Cancel — `POST /v1/payments/{id}/cancel`
Only `pending` payments with nothing received. The body may be empty.
```json
{
  "id": "pay_hmF3p1rFRpArfk3iNEnr",
  "object": "payment",
  "status": "cancelled",
  "reference_id": "order_1044",
  "…": "…"
}
```
Cancelling again:
```json
{
  "error": {
    "type": "invalid_request_error",
    "code": "payment_not_cancellable",
    "message": "A cancelled payment cannot be cancelled",
    "request_id": "req_UZuyCVxIFTp9MrOw",
    "doc_url": "https://razcryptogateway.com/docs/errors#payment_not_cancellable"
  }
}
```

## Test mode: simulate an outcome — `POST /v1/payments/{id}/simulate`
Test keys only. `outcome`: `paid`, `overpaid`, `underpaid`, `expired`, `cancelled`. Produces the same
status changes, timeline and webhooks as a real payment — no blockchain or test coins needed.
```bash
curl https://razcryptogateway.com/v1/payments/pay_ccJHIhKHa4VfNp6HyXMD/simulate \
  -H "Authorization: Bearer sk_test_…" -H "Content-Type: application/json" -d '{"outcome":"paid"}'
```
```json
{
  "id": "pay_ccJHIhKHa4VfNp6HyXMD",
  "object": "payment",
  "livemode": false,
  "status": "paid",
  "flags": [],
  "amount": "25",
  "currency": "USD",
  "reference_id": "order_1042",
  "description": "Pro plan — October",
  "customer": {
    "email": "asha@example.com"
  },
  "metadata": {
    "plan": "pro"
  },
  "allowed_chains": null,
  "allowed_coins": null,
  "quote": {
    "chain": "bsc",
    "coin": "USDT",
    "address": "0x52908400098527886E0F7030069857D2E4169EE7",
    "amount": "25.0056",
    "rate": "0.999809565882114",
    "rate_currency": "USD",
    "…": "…"
  },
  "amount_received": "25.0056",
  "transfers": [
    {
      "id": "trf_swmI530u7pnMcsIomPp2",
      "chain": "bsc",
      "coin": "USDT",
      "tx_hash": "sim_29fee5f2dfd6ce3fbe41fc553be8ffa2",
      "log_index": -1,
      "from": "simulated",
      "to": "0x52908400098527886E0F7030069857D2E4169EE7",
      "amount": "25.0056",
      "status": "confirmed",
      "block_number": null,
      "explorer_url": null,
      "detected_at": "2026-10-02T11:35:16.363Z",
      "confirmed_at": "2026-10-02T11:35:16.361Z"
    }
  ],
  "checkout_url": "https://razcryptogateway.com/pay_ccJHIhKHa4VfNp6HyXMD?t=ct_…",
  "redirect_url": "https://shop.example.com/thanks",
  "cancel_url": null,
  "payment_link": null,
  "expires_at": "2026-10-02T12:05:04.820Z",
  "paid_at": "2026-10-02T11:35:16.361Z",
  "created_at": "2026-10-02T11:35:04.822Z"
}
```

## Validation errors (real)
```json
{
  "error": {
    "type": "invalid_request_error",
    "code": "amount_invalid",
    "message": "Fiat amounts can have at most 2 decimals",
    "param": "amount",
    "request_id": "req_1X0ImWkH0AoT4kjY",
    "doc_url": "https://razcryptogateway.com/docs/errors#amount_invalid"
  }
}
```
```json
{
  "error": {
    "type": "invalid_request_error",
    "code": "no_payment_method_available",
    "message": "No active wallet + coin matches this payment. Add a wallet in the dashboard.",
    "request_id": "req_n30k6QcxFnPUD8Uo",
    "doc_url": "https://razcryptogateway.com/docs/errors#no_payment_method_available"
  }
}
```

## How a payment completes (live)
1. Customer sends the exact `quote.amount` to `quote.address` on the chosen network.
2. The detection provider notifies us → transfer stored and matched → `confirming` (`payment.confirming` webhook).
3. When the block is final (`finality`) → `paid` (`payment.paid` webhook). Paid after expiry but within
   the grace window → `paid` + `late`. Too little after the grace window → `underpaid`.
4. A rounded amount still matches when it is the only open payment for that coin on your address and
   within tolerance; otherwise the transfer is kept as *unmatched* for you or support to assign —
   never lost. Details: `docs/PAYMENT-FLOW.md`.
