## Sales integrations

A sales integration connects Reklamator to your shop, ERP or other sales system through its API (REST, JSON). With it:

- an operator on the issue card **imports an order** — number, purchase date, customer and products — instead of retyping them,
- a customer in a [remote form](/config/forms) **looks up their order** and picks the items the issue is about.

Integrations are configured under **System → Sales integrations**.

{% callout title="Requirements" %}
Sales integrations are available in the **Business** and **Professional** plans. The account administrator grants permissions from the *Sales integrations* group:

- **Sales integration settings** — adding, editing and testing integrations and operations,
- **Use sales integrations** — importing an order on the issue card (issue edit permission is also required).
{% /callout %}

## How it works

Configuration has two levels:

1. **Integration** — which system you connect to: API address, authentication, headers sent with every request.
2. **Operation** — what exactly you fetch, e.g. "order by number": path, parameters, mapping of the response onto an order, and verification rules. One integration can have many operations.

The API response is converted into Reklamator's uniform **order** (number, date, customer, items), so the next steps — import and the form — work the same for every system.

## Adding an integration

1. Go to **System → Sales integrations** and click **New integration**.
2. Enter a **Name** and **Code** (lowercase letters, digits and hyphens; the code cannot be changed later — it becomes part of the identifiers of imported products and customers).
3. Enter the **API address** — the common start of all addresses, e.g. `https://shop.example.com/api`. Operation paths are appended to it.
4. Set **Timeout (s)** — how long to wait for a response (1–30 s, default 10).
5. Choose the **Authentication type** and fill in the details (table below).
6. Optionally add **Default headers**, e.g. `Accept-Language`.
7. Turn on **Active** when the integration is ready — an inactive one does not appear in import or in forms.

### Authentication

| Type | What Reklamator sends |
|---|---|
| **None** | No authentication |
| **Basic** | `Authorization: Basic …` with the given username and password |
| **Bearer** | `Authorization: Bearer <token>` |
| **API key** | A key with the given name in a header or in the query string |
| **OAuth2 (client credentials)** | Fetches a token from the **Token URL** (client ID, client secret, optional scope and audience) and sends it as `Bearer`; the token is kept until it expires and fetched again automatically |

Passwords, tokens and keys are stored encrypted and are not shown after saving. When editing, leave a secret field empty to keep it unchanged.

### API address and security

- An **`https`** address is required.
- Internal addresses (`localhost`, private networks, link-local addresses) are rejected — the API must be reachable from the internet. This also applies to IP addresses entered directly.
- Reklamator does not follow redirects, and the response may be at most 5 MB.

#### Unencrypted connection (HTTP)

If the other system does not support HTTPS (e.g. an ERP under test exposed at `http://` and a public IP address), turn on **Allow unencrypted connection (HTTP)**. Without it, an `http://` address is rejected already in the form.

{% callout type="warning" title="Data travels in plain text" %}
Over HTTP, order and customer data as well as passwords, tokens and API keys are sent unencrypted and can be intercepted on the way. Use it only for testing or when there is no other option — and turn it off once the system moves to HTTPS.
{% /callout %}

The switch applies to this one integration — both the **API address** and the OAuth2 **Token URL**. Integrations with HTTP enabled show an **HTTP** label next to the address in the list. The switch cannot be turned off while either address starts with `http://` — change the address to `https://` first.

Blocking of internal addresses still applies over HTTP.

## Operations

Open an integration's operations from the row menu (**Operations**) and add one with **New operation**.

### Request

| Field | Meaning |
|---|---|
| **Code**, **Name** | Identifier and the name shown in import and in the form |
| **Kind** | *Order lookup* |
| **Method** | `GET` or `POST` |
| **Path** | Appended to the API address; may contain input values in curly braces, e.g. `/orders/{order_number}` |
| **Request mode** | Where parameters go: *Query string*, *JSON body*, *Form body* or *No parameters* |
| **Parameters** | Parameter name and its source: **Input** (a value entered by the operator or customer, e.g. `order_number`, `email`, `phone`) or **Constant** (a value set in the configuration) |

### Response and mapping

The **Response root** is a JSONPath to the order data, e.g. `$.data` (empty = the whole response).

**Mapping** assigns parts of the response (JSONPath) to order fields:

| Order field | Example |
|---|---|
| `order_number`, `order_date`, `status`, `currency`, `total` | `$.number`, `$.created_at` |
| `customer.name`, `customer.email`, `customer.phone`, `customer.street`, `customer.house_number`, `customer.zip_code`, `customer.city`, `customer.country`, `customer.tax_number`, `customer.external_id` | `$.billing.email` |
| `items` (a **List** row) with item mapping: `sku`, `name`, `quantity`, `price`, `serial_number`, `purchase_date`, `external_id` | `$.lines` → `$.sku` |

A field outside this list goes into the order's extra data (`extra.<key>`) — it can then be written into the issue's [custom field](/config/appfields) with the same code.

Each row can have a **transform**: *Date*, *Number*, *Phone*, *Lowercase*, *Uppercase*, *Trim spaces*.

### Steps

When one request is not enough (e.g. the order list returns only an ID and the details live at another address), add **Steps** — up to 5 extra requests run after the base operation. **Exports** pull values from a response (name → JSONPath) and make them available to later steps just like input values, e.g. `/orders/{order_id}/lines`.

### Verification

**Verification** prevents looking up other people's orders — especially in a remote form. Each rule compares a value given by the customer (an input key, e.g. `email`) with a field of the found order (e.g. `customer.email`) in the chosen mode: *Exact*, *Email* (case-insensitive) or *Phone* (compares the trailing digits, ignoring spaces and the country prefix).

If the value does not match, the result looks exactly like a missing order — it does not reveal that an order with that number exists. A rule whose input value is empty is skipped.

### Test

The **Test** button runs the operation with the given values and shows: the request sent, the status, the raw response, the mapped order and **mapping warnings** (e.g. a path returned nothing). The test uses the **saved** operation configuration — save your changes before testing.

## Importing an order into an issue

On the issue card click **Import order**:

1. Choose the **Integration** and **Operation**, enter the values (e.g. order number, email) and click **Search**.
2. Check the order found and tick what to transfer: **Order number**, **Purchase date**, **Customer**, **Custom fields** and **Items**.
3. Click **Import**.

The ticked issue fields are **overwritten**. Selected items are added to the issue's products (with quantity and serial number). The import is recorded in the issue history.

**Customers and products.** Reklamator first looks for existing records — a product by its integration identifier, then by code (SKU); a customer by integration identifier, then by email. Missing ones are created. Existing ones are not overwritten — only an empty integration link is filled in.

## Remote form

In the [remote form](/config/forms) settings, in the **Sales integration (order lookup)** section, choose the integration and the **Lookup operation**. The customer first looks up their order and then picks the items the issue is about.

For forms, set up **verification** in the operation (e.g. order number + email) — otherwise anyone who guesses a number will see the order details.

## Common errors

| Message | Cause |
|---|---|
| *This address is blocked by the security policy.* | The address is not `https` (and HTTP is not allowed), points to an internal network, or the host name does not resolve |
| *An http address requires “Allow unencrypted connection (HTTP)” to be on* | The address starts with `http://` and the switch is off |
| *The external system is not responding.* | No connection, timeout exceeded, or the OAuth2 token could not be fetched |
| *The external system returned an error.* | The API responded with an error code — check the status and raw response in the test |
| *The response is not valid JSON.* | The API returned HTML or other non-JSON text, or a response larger than 5 MB |
| *Order not found.* | No such order, or verification did not match |
| *No organization for the customer* | Customer import without an organization — set the issue organization first |
