# Charges (/docs/charges)

> Pre-release: the public API is not live yet and has no base URL. Routes and fields are in the OpenAPI contract, at /openapi.yaml.

A PIX payment to receive: how to create it, follow its state and retry safely.

A charge is a PIX payment to receive: an amount, a PIX code for the payer and an expiration. You create the charge, hand the code to the payer, and follow its state until it is paid or it expires.

## Operations [#operations]

| Operation in the contract | What it does |
|---|---|
| `createCharge` | Creates the charge and its PIX code. Requires `Idempotency-Key` |
| `getCharge` | Returns one charge by its `id` |
| `listCharges` | Returns the charges of the environment, newest first, paginated by cursor |

The route, the fields and the limits of each operation are in the [OpenAPI contract](/openapi.yaml), under these names.

## Creating a charge [#creating-a-charge]

You send the amount, in cents, and in how many seconds the charge expires. The answer is the charge, with the state `pending` and its `br_code`.

`br_code` is the PIX copy-and-paste code, and it is also the content of the QR code. Render it as a QR code for the payer to scan, or show it as text for the payer to paste into their banking app.

The amount and the expiration never change after creation.

## States [#states]

The state is the `status` field.

| State | Meaning |
|---|---|
| `pending` | Waiting for payment |
| `paid` | Paid |
| `expired` | Expired without payment |
| `disputed` | The payer disputed the payment at their bank, and the bank is holding the amount until it decides. See [Disputes](/docs/disputes) |
| `refunded` | The amount went back to the payer, by a refund or by a dispute the bank accepted. Final: it never changes again |

| From | To | When |
|---|---|---|
| `pending` | `paid` | The bank confirms the payment |
| `pending` | `expired` | The expiration passes without payment |
| `expired` | `paid` | The bank confirms a payment made at the last moment, after the charge had already expired on the platform |
| `paid` | `refunded` | A refund of the charge reaches the payer. See [Refunds](/docs/refunds) |
| `paid` | `disputed` | The bank holds the amount because the payer disputed the payment |
| `disputed` | `paid` | The bank refuses the dispute |
| `disputed` | `refunded` | The bank accepts the dispute and sends the amount back to the payer |

Three rules follow from the tables:

- **`expired` is not final.** An `expired` charge can still become `paid`. Handle a payment whenever it arrives.
- **A `paid` charge becomes `refunded` or `disputed`, and only `disputed` comes back to `paid`.** The charge keeps its `paid_at` in the three states. While a refund is in progress the charge is still `paid`.
- **Handle an unknown state without failing.** In the contract `status` is an open string, not an enum, for this reason.

## Listing charges [#listing-charges]

`listCharges` returns the newest charges first. Each page brings `next_cursor`: pass it as `cursor` to read the next page. A `null` `next_cursor` means this is the last page.

## Idempotency [#idempotency]

Creating a charge requires the `Idempotency-Key` header: a value that you choose, unique for each charge you intend to create. A UUID is fine.

| You repeat the request with | Result |
|---|---|
| The same `Idempotency-Key` and the same body | The charge that was already created is returned. No new charge is created |
| The same `Idempotency-Key` and a different body | `422` with the `idempotency_key_reused` code |
| A different `Idempotency-Key` | A new charge |

- The key is unique per environment.
- Two requests are the same when they ask for the same amount and the same expiration. The order of the fields and the whitespace of the JSON do not matter.
- The key never expires. Repeating a request with a key used long ago still returns that charge, as it is now: it may already be `paid` or `expired`.
- Simultaneous requests with the same key create a single charge, and all of them receive it.
- If a request fails with a network error or a timeout, repeat it with the same key. It is safe.

## Charges from an offer [#charges-from-an-offer]

An [offer](/docs/offers) is something for sale at a fixed price, with a checkout page. Each purchase of an offer creates a charge in the environment of the offer. It is a charge like any other: it has the same states, it sends the same webhooks, and `getCharge` and `listCharges` return it.

Two fields of the charge tell where it came from. Both are in the `Charge` schema of the contract, and both are `null` in a charge created by `createCharge`.

- **`offer_id` is the offer that was bought.** The amount of the charge is the price the offer had at the purchase.
- **`customer` is who bought**, with the name and the e-mail address typed at the purchase. The e-mail address was not verified.
- **The customer is not necessarily the payer.** The PIX can leave the bank account of another person.

A webhook carries only the id of the charge. When `charge.paid` arrives, read the charge with `getCharge` to know which offer was bought and by whom.

## Identifiers [#identifiers]

The `id` of a charge follows the [TypeID](https://github.com/jetify-com/typeid) format: the prefix `chg`, an underscore, and 26 characters of base32 that encode a UUIDv7. For example, `chg_01h455vb4pex5vsknk084sn02q`.

- The prefix tells the type. An identifier of another type is refused where a charge is expected.
- The identifier of an offer has the same format, with the prefix `ofr`, and the one of a refund, with the prefix `rfd`.
- The identifier reveals when the charge was created. Treat the part after the underscore as opaque.

## Charges of another environment [#charges-of-another-environment]

Asking for a charge that does not belong to the environment of your key returns `404` with the `charge_not_found` code, exactly as if it did not exist.