<!-- Fonte: https://docs.onzepay.com/api/devolucoes -->
# Devoluções

Devolve, total ou parcialmente, um Pix recebido. Não há tarifa. O total devolvido não passa do líquido creditado na cobrança. Webhooks: `pix.refund.completed` quando conclui e `pix.refund.failed` quando o liquidante recusa (o saldo não é debitado e `failureReason` diz o motivo).

## Solicitar uma devolução

`POST https://api.onzepay.com/v1/refunds`

Escopo: `refunds.write` · exige `Idempotency-Key`.

Informe a cobrança por `chargeId` ou pelo `endToEndId` do Pix recebido.

**Corpo**

| Campo | Tipo | Descrição |
|---|---|---|
| `chargeId` | string | Id da cobrança (`chg_…`). |
| `endToEndId` | string | Alternativa ao `chargeId`. |
| `amount` *(obrigatório)* | integer | Valor em centavos. |
| `externalId` | string | Seu identificador. Único por conta. |
| `reason` | string | Motivo (até 240 caracteres). |

:::tabs
```bash title="curl"
curl -X POST "$ONZEPAY_API/v1/refunds" \
  -H "Authorization: Bearer $ONZEPAY_SECRET_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
  "chargeId": "chg_034yDKstyRbto2AiOEPZJP",
  "amount": 3000,
  "externalId": "refund-4821",
  "reason": "Cliente desistiu da compra"
}'
```
```javascript title="Node.js"
const res = await fetch(`${process.env.ONZEPAY_API}/v1/refunds`, {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.ONZEPAY_SECRET_KEY}`,
    'Idempotency-Key': crypto.randomUUID(),
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    "chargeId": "chg_034yDKstyRbto2AiOEPZJP",
    "amount": 3000,
    "externalId": "refund-4821",
    "reason": "Cliente desistiu da compra"
  })
})
const data = await res.json()
if (!res.ok) throw new Error(`${data.code}: ${data.message}`)
```
```python title="Python"
import os, uuid
import requests

res = requests.post(
    f"{os.environ['ONZEPAY_API']}/v1/refunds",
    headers={"Authorization": f"Bearer {os.environ['ONZEPAY_SECRET_KEY']}", "Idempotency-Key": str(uuid.uuid4())},
    json={
        "chargeId": "chg_034yDKstyRbto2AiOEPZJP",
        "amount": 3000,
        "externalId": "refund-4821",
        "reason": "Cliente desistiu da compra",
    },
    timeout=30,
)
res.raise_for_status()
data = res.json()
```
:::

**Resposta `201`**

```json
{
  "id": "rfd_034yDL0aB7cD9eF1gH2iJ3",
  "externalId": "refund-4821",
  "chargeId": "chg_034yDKstyRbto2AiOEPZJP",
  "endToEndId": "D12345678202611161230abcdef987654",
  "amount": 3000,
  "feeAmount": 0,
  "status": "completed",
  "origin": "spontaneous",
  "reason": "Cliente desistiu da compra",
  "failureReason": null,
  "createdAt": "2026-11-16T12:30:00.000Z"
}
```

**Erros**

| HTTP | `code` | Quando |
|---|---|---|
| `400` | `REFUND_TOO_HIGH` | Passa do líquido creditado. |
| `400` | `INVALID_CHARGE` | Cobrança não paga ou não encontrada. |
| `409` | `EXTERNAL_ID_CONFLICT` | Já existe devolução com este `externalId`. |

## Consultar uma devolução

`GET https://api.onzepay.com/v1/refunds/{id}`

Escopo: `refunds.read`.

**Parâmetros de caminho**

| Campo | Tipo | Descrição |
|---|---|---|
| `id` *(obrigatório)* | string | Id da devolução (`rfd_…`). |

:::tabs
```bash title="curl"
curl -X GET "$ONZEPAY_API/v1/refunds/rfd_034yDL0aB7cD9eF1gH2iJ3" \
  -H "Authorization: Bearer $ONZEPAY_SECRET_KEY"
```
```javascript title="Node.js"
const res = await fetch(`${process.env.ONZEPAY_API}/v1/refunds/rfd_034yDL0aB7cD9eF1gH2iJ3`, {
  method: 'GET',
  headers: {
    Authorization: `Bearer ${process.env.ONZEPAY_SECRET_KEY}`
  }
})
const data = await res.json()
if (!res.ok) throw new Error(`${data.code}: ${data.message}`)
```
```python title="Python"
import os
import requests

res = requests.get(
    f"{os.environ['ONZEPAY_API']}/v1/refunds/rfd_034yDL0aB7cD9eF1gH2iJ3",
    headers={"Authorization": f"Bearer {os.environ['ONZEPAY_SECRET_KEY']}"},
    timeout=30,
)
res.raise_for_status()
data = res.json()
```
:::

**Resposta `200`**

```json
{
  "id": "rfd_034yDL0aB7cD9eF1gH2iJ3",
  "externalId": "refund-4821",
  "chargeId": "chg_034yDKstyRbto2AiOEPZJP",
  "endToEndId": "D12345678202611161230abcdef987654",
  "amount": 3000,
  "feeAmount": 0,
  "status": "completed",
  "origin": "spontaneous",
  "reason": "Cliente desistiu da compra",
  "failureReason": null,
  "createdAt": "2026-11-16T12:30:00.000Z"
}
```

## Buscar devoluções

`GET https://api.onzepay.com/v1/refunds`

Escopo: `refunds.read`.

Com `chargeId`, lista as devoluções da cobrança. Com só `externalId`, devolve a devolução direto.

**Query**

| Campo | Tipo | Descrição |
|---|---|---|
| `chargeId` | string | Id da cobrança (`chg_…`). |
| `externalId` | string | Seu identificador. |

:::tabs
```bash title="curl"
curl -X GET "$ONZEPAY_API/v1/refunds?chargeId=chg_034yDKstyRbto2AiOEPZJP" \
  -H "Authorization: Bearer $ONZEPAY_SECRET_KEY"
```
```javascript title="Node.js"
const res = await fetch(`${process.env.ONZEPAY_API}/v1/refunds?chargeId=chg_034yDKstyRbto2AiOEPZJP`, {
  method: 'GET',
  headers: {
    Authorization: `Bearer ${process.env.ONZEPAY_SECRET_KEY}`
  }
})
const data = await res.json()
if (!res.ok) throw new Error(`${data.code}: ${data.message}`)
```
```python title="Python"
import os
import requests

res = requests.get(
    f"{os.environ['ONZEPAY_API']}/v1/refunds?chargeId=chg_034yDKstyRbto2AiOEPZJP",
    headers={"Authorization": f"Bearer {os.environ['ONZEPAY_SECRET_KEY']}"},
    timeout=30,
)
res.raise_for_status()
data = res.json()
```
:::

**Resposta `200`**

```json
{
  "data": [
    {
      "id": "rfd_034yDL0aB7cD9eF1gH2iJ3",
      "externalId": "refund-4821",
      "chargeId": "chg_034yDKstyRbto2AiOEPZJP",
      "endToEndId": "D12345678202611161230abcdef987654",
      "amount": 3000,
      "feeAmount": 0,
      "status": "completed",
      "origin": "spontaneous",
      "reason": "Cliente desistiu da compra",
      "failureReason": null,
      "createdAt": "2026-11-16T12:30:00.000Z"
    }
  ]
}
```
