# Test a late fee formula

Source: https://api-docs.magicdoor.com/api-reference/late-fee-settings/test-late-fee-formula

Evaluates a late fee formula against sample numbers and returns what it would charge, without reading or writing any stored setting. Use it to check a formula before saving it as the `latefees` settings type. Requires the `settings:read` permission.

`POST https://services.magicdoor.com/settings/company-portal/settings/latefees/test/formula`

**Authentication:** bearer token in the `Authorization` header, as `Bearer <token>`.

## Body

Content type: `application/json`

- `formula` (string, required) — The expression to evaluate. Must contain at least one non-whitespace character. Example: `Min(due * 0.2, 500) + Max(due - 1000, 0) * 0.05`.
- `due` (number) — The amount currently overdue on the bill. Zero or greater. Default: `100`.
- `total` (number) — The full amount of the bill, paid or not. Zero or greater. Default: `1000`.
- `lateBills` (integer) — How many bills are already late. Zero or greater. Default: `1`.
- `lateDays` (integer) — How many days the bill has been late. Zero or greater. Default: `5`.
- `gracePeriod` (integer) — How many days of grace the company allows before a fee applies. Zero or greater. Default: `5`.
- `graceBalance` (number) — The balance below which no late fee is charged. Zero or greater. Default: `0`.

```json
{
  "formula": "Min(due * 0.2, 500) + Max(due - 1000, 0) * 0.05",
  "due": 1850,
  "total": 1850,
  "lateBills": 1,
  "lateDays": 5,
  "gracePeriod": 5,
  "graceBalance": 0
}
```

## Example request

```bash
curl --request POST \
  --url 'https://services.magicdoor.com/settings/company-portal/settings/latefees/test/formula' \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '{
  "formula": "Min(due * 0.2, 500) + Max(due - 1000, 0) * 0.05",
  "due": 1850,
  "total": 1850,
  "lateBills": 1,
  "lateDays": 5,
  "gracePeriod": 5,
  "graceBalance": 0
}'
```

## Responses

### 200 — What the formula evaluates to.

- `result` (number, required) — The fee the formula produced, rounded to two decimal places.

```json
{
  "result": 412.5
}
```

### 400 — `formula` is missing or blank, or one of the sample numbers is out of range.

### 401 — The access token is missing, expired, or invalid.

### 403 — The caller does not hold the `settings:read` permission.

### 422 — The formula could not be parsed, or it did not evaluate to a number.

## Variables a formula can use

| Variable | Comes from |
| --- | --- |
| `due` | The `due` value in the request body |
| `total` | The `total` value in the request body |
| `lateBills` | The `lateBills` value in the request body |
| `lateDays` | The `lateDays` value in the request body |
| `gracePeriod` | The `gracePeriod` value in the request body |
| `graceBalance` | The `graceBalance` value in the request body |

Formulas are arithmetic expressions with functions such as `Min` and `Max`. The result is rounded to two decimal places, halves away from zero. A formula that does not parse, or that evaluates to something other than a number, comes back as a `422`.
