# Refund a paid payment link

`POST /businesses/{bid}/payment-requests/{id}/refund`

Requires the `documents` scope.

Gives the money of a `paid` link back to the card that paid it, then issues the document that corrects its receipt: a negative קבלה (400) for a קבלה, or a חשבונית זיכוי (330) and the negative קבלה that records the money for a חשבונית מס/קבלה (320). In full by default; `amount` for a part, with a `reason`. Never more than was paid less what already went back. The invoice the receipt had closed is owed again by the refunded amount. **`Idempotency-Key` is required**: the same key is the same refund, wherever it stands. If the document fails after the money moved, the refund is `refunded` with `documentError`, and the same call issues it without asking the card company again. If the card company gives no answer the call is `409` and the refund is `unknown`: look in the terminal, then repeat with `resolve`. `payment_request.refunded` is sent once the document is issued. **A payment on a real terminal answers `422 not_supported` today** (see `refundable`); the sandbox's simulated terminal refunds.

## Path parameters

| Name | Type | | Description |
|---|---|---|---|
| `bid` | string | **required** | Business id, from `POST /businesses`. |
| `id` | string | **required** | Payment request id. |

## Headers

| Name | Type | | Description |
|---|---|---|---|
| `Idempotency-Key` | string |  | Your own stable id for this call. A retry returns the same row with `deduped: true` instead of creating a duplicate. Interchangeable with `externalRef` in the body. |

## Body (optional)

| Field | Type | | Description |
|---|---|---|---|
| `amount` | integer |  | A part of what was paid. Omit for everything not yet refunded. Integer agorot (₪1 = 100). |
| `reason` | string |  | Printed on the correcting document. Required for a part. |
| `resolve` | string |  | Only for a refund that is `unknown`: what the owner found in his terminal. One of: `refunded`, `not_refunded`. |
| `idempotencyKey` | string |  | The same as the `Idempotency-Key` header. |

## Response

`200`: `{ "ok": true, … }`

| Field | Type | | Description |
|---|---|---|---|
| `ok` | boolean | **required** |  |
| `paymentRequest` | PaymentRequest | **required** | A request for a payment and its public link. When the card company confirms the payment, BillOS issues the receipt itself: a קבלה (400) for an invoice it pays, a חשבונית מס/קבלה (320) for a proforma or for its own lines (a 400 for an עוסק פטור), and the invoice it paid is closed. |
| `status` | string |  |  One of: `refunded`, `withdrawn`. |
| `refund` | object |  | This refund, as in `paymentRequest.refunds`. |
| `document` | object |  | The correcting document, null while it is still owed. |
| `document.id` | string |  |  |
| `document.docType` | integer |  |  |
| `document.docNumber` | integer |  |  |
| `deduped` | boolean |  |  |

Errors: `400`, `401`, `403`, `404`, `409`, `422`, `429`. Every failure answers `{ "error", "reason", "hint", "param", "doc_url", "retryable", "request_id" }`: branch on `reason`, and read `hint` for what to change (https://docs.billos.co.il/errors.md).

## Examples

### cURL

```bash
curl -X POST "$BASE/businesses/$BID/payment-requests/$DOC_ID/refund" \
  -H "X-Api-Key: $KEY"
```
### Node

```node
const res = await fetch(`${BASE}/businesses/${BID}/payment-requests/${DOC_ID}/refund`, {
  method: 'POST',
  headers: {
    'X-Api-Key': KEY,
  },
})
const data = await res.json()
if (!data.ok) throw new Error(data.reason)
```
### Python

```python
import requests
res = requests.post(
    f"{BASE}/businesses/{BID}/payment-requests/{DOC_ID}/refund",
    headers={"X-Api-Key": KEY},
)
data = res.json()
```
### PHP

```php
$ch = curl_init("$BASE/businesses/$BID/payment-requests/$DOC_ID/refund");
curl_setopt_array($ch, [
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => ["X-Api-Key: $KEY"],
]);
$data = json_decode(curl_exec($ch), true);
```
### C#

```csharp
using System.Text.Json;

using var http = new HttpClient();
using var req = new HttpRequestMessage(HttpMethod.Post, $"{BASE}/businesses/{BID}/payment-requests/{DOC_ID}/refund");
req.Headers.Add("X-Api-Key", KEY);
using var res = await http.SendAsync(req);
var data = JsonDocument.Parse(await res.Content.ReadAsStringAsync()).RootElement;
```