# Open a standing order on a card

`POST /businesses/{bid}/standing-orders`

Requires the `documents` scope.

A monthly charge of the customer's card on the business's own terminal. **Nothing is charged by this call**: it answers a `url` for the customer, who types his own card there and ticks a consent stating the amount, the day, the end and how to cancel. The business never types a card. `chargeAtConsent` takes the first charge when he agrees; otherwise the first charge is on the next `dayOfMonth`. It runs until cancelled unless `maxCharges` or `endDate` is given (one of them, not both). A refused charge is retried 1, 3 and 7 days after its day, then the order is `failed` (webhook `standing_order.updated`; every refusal is `standing_order.charge_failed`). The customer can cancel from his link at any time. Send an `Idempotency-Key` so a retried call is the same order. ⚠️ `422 not_supported` where standing orders are not open: today they run only on a simulated terminal (sandbox keys), where a card ending 0002 is refused at every charge.

## Path parameters

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

## 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

| Field | Type | | Description |
|---|---|---|---|
| `amount` | integer | **required** | What every charge takes, VAT included. Integer agorot (₪1 = 100). |
| `description` | string | **required** | What the customer is charged for. |
| `dayOfMonth` | integer | **required** | 1 to 28. |
| `party` | object | **required** | Who is charged: a saved customer (`partyId`) or a name. |
| `party.partyId` | string |  |  |
| `party.name` | string |  |  |
| `party.phone` | string |  |  |
| `party.email` | string |  | Where every receipt is mailed, with the link to cancel. |
| `party.taxId` | string |  |  |
| `chargeAtConsent` | boolean |  | Default false. |
| `maxCharges` | integer |  | End after this many charges. |
| `endDate` | string |  | No charge after this day. |
| `externalRef` | string |  | The same as the `Idempotency-Key` header. |

## Response

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

| Field | Type | | Description |
|---|---|---|---|
| `ok` | boolean | **required** |  |
| `standingOrder` | StandingOrder | **required** | A standing order on a card (הוראת קבע בכרטיס אשראי): the customer is charged `amount` every month on `dayOfMonth`, on the business's own terminal. Opening one charges nothing: the customer opens `url`, types his own card and agrees to the terms himself, and the consent is recorded as he gave it. Every charge is a payment request (`GET .../standing-orders/{id}/charges`) that issues its receipt like a paid link. The card is kept as the card company's token, sealed, and is shown by its last four digits only. |
| `deduped` | boolean |  |  |

Errors: `400`, `401`, `403`, `404`, `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/standing-orders" \
  -H "X-Api-Key: $KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": 120000,
    "description": "איפור כלה",
    "dayOfMonth": 0,
    "party": {
      "name": "דנה לוי",
      "phone": "0521111111",
      "email": "dana@example.com",
      "taxId": "123456782"
    },
    "externalRef": "order-8812"
  }'
```
### Node

```node
const res = await fetch(`${BASE}/businesses/${BID}/standing-orders`, {
  method: 'POST',
  headers: {
    'X-Api-Key': KEY,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "amount": 120000,
    "description": "איפור כלה",
    "dayOfMonth": 0,
    "party": {
      "name": "דנה לוי",
      "phone": "0521111111",
      "email": "dana@example.com",
      "taxId": "123456782"
    },
    "externalRef": "order-8812"
  }),
})
const data = await res.json()
if (!data.ok) throw new Error(data.reason)
```
### Python

```python
import requests
res = requests.post(
    f"{BASE}/businesses/{BID}/standing-orders",
    headers={"X-Api-Key": KEY},
    json={
      "amount": 120000,
      "description": "איפור כלה",
      "dayOfMonth": 0,
      "party": {
        "name": "דנה לוי",
        "phone": "0521111111",
        "email": "dana@example.com",
        "taxId": "123456782"
      },
      "externalRef": "order-8812"
    },
)
data = res.json()
```
### PHP

```php
$ch = curl_init("$BASE/businesses/$BID/standing-orders");
curl_setopt_array($ch, [
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => ["X-Api-Key: $KEY", 'Content-Type: application/json'],
  CURLOPT_POSTFIELDS => json_encode([
    'amount' => 120000,
    'description' => 'איפור כלה',
    'dayOfMonth' => 0,
    'party' => [
      'name' => 'דנה לוי',
      'phone' => '0521111111',
      'email' => 'dana@example.com',
      'taxId' => '123456782',
    ],
    'externalRef' => 'order-8812',
  ]),
]);
$data = json_decode(curl_exec($ch), true);
```
### C#

```csharp
using System.Text;
using System.Text.Json;

using var http = new HttpClient();
using var req = new HttpRequestMessage(HttpMethod.Post, $"{BASE}/businesses/{BID}/standing-orders");
req.Headers.Add("X-Api-Key", KEY);
req.Content = new StringContent("""
    {
      "amount": 120000,
      "description": "איפור כלה",
      "dayOfMonth": 0,
      "party": {
        "name": "דנה לוי",
        "phone": "0521111111",
        "email": "dana@example.com",
        "taxId": "123456782"
      },
      "externalRef": "order-8812"
    }
    """, Encoding.UTF8, "application/json");
using var res = await http.SendAsync(req);
var data = JsonDocument.Parse(await res.Content.ReadAsStringAsync()).RootElement;
```