# Terminal Delivery

Terminal Delivery is an Algerian courier with home delivery, its own stop desks and partner pickup points (shops that hold the parcel for the buyer). The integration needs one value: your store's API token.

## What you need

* One value, labelled **API Token** in the credential modal, issued from your Terminal Delivery account. Terminal authenticates with `Authorization: Token <value>`.

If your account has no token yet, ask Terminal Delivery to enable API access.

## Linking Terminal Delivery in DZBuild

1. Open [`/dashboard/link-shipping`](https://dzbuild.com/dashboard/link-shipping) and find the **Terminal Delivery** tile.
2. Click **Lier / ربط**.
3. Paste your **API Token** in the single credential field.
4. Click **Tester la connexion** (« اختبار الاتصال »). DZBuild calls Terminal live; a wrong token is refused at once.
5. Click **Lier et enregistrer** (« ربط وحفظ »).

Per-wilaya prices are edited on [`/dashboard/shipping`](https://dzbuild.com/dashboard/shipping).

## What gets synced

* The **wilayas** Terminal serves.
* **Communes** per wilaya, with Terminal's own commune ids.
* **Home + desk rates** per wilaya.
* **Stop desks and partner pickup points** for the [Stop Desk picker](https://dzbuild.com/docs/couriers/stop-desk.md).

### Rates

**Sync rates from provider** reads Terminal's delivery options for each wilaya and fills both the home and the desk price. They are the standard (non-express) prices.

Terminal prices per **commune**, so DZBuild reads each wilaya's centre commune. A wilaya whose centre commune has no price for your store stays at 0; set it by hand on [`/dashboard/shipping`](https://dzbuild.com/dashboard/shipping).

### Desks and pickup points

Terminal lists two kinds of locations per commune: its own **stop desks** and **partner pickup points**. DZBuild lists both in the Stop Desk picker. It reads each wilaya's centre commune first; when a wilaya prices desk delivery but its centre commune lists no location, DZBuild checks up to 15 of that wilaya's other communes.

Desk delivery is offered only in the communes where Terminal listed a stop desk or a pickup point. An order sent to a partner pickup point carries the point's id, so the parcel goes to that shop.

## What happens when an order ships

DZBuild posts the order with the customer's name, phone and address, the commune as **Terminal's commune id** (or the commune and wilaya names when the commune is not in the synced list, Terminal matches them in Latin or Arabic), the delivery type (1 home, 2 stop desk, 3 pickup point), the product list as one product line, your order number as `external_id`, and `total_price`. A desk order is sent with the desk's own commune.

Three rules to know:

* **`total_price` includes delivery.** It is the amount the customer pays and it must be at least Terminal's delivery price for that commune, otherwise the order is refused (code 10).
* **Duplicates are refused for 24 hours.** An order with the same customer name and phone as one of your active orders from the last 24 hours is refused (code 55). Correct the phone or wait; do not resend as is.
* **No cancellation through the API.** Cancel a parcel from your Terminal Delivery account.

Terminal answers with an order id (saved as the tracking) and a tracking number printed on the slip. DZBuild fetches the slip (bordereau) right away; **Print label** on the order prints it.

### Status meanings

| Terminal status                                                                                                                          | What it means                                         | DZBuild status            |
| ---------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- | ------------------------- |
| 4 Created, 5 Pickup requested, 6 In process, 8 Waiting transit, 9 In transit, 11 Pending, 12 Out of stock, 15 Ready to ship, 22 Assigned | The parcel is with Terminal, not yet out for delivery | Processing                |
| 31 Shipped, 32 Alerted, 42 Postponed, 51 Ready to return                                                                                 | Out for delivery, or a failed attempt                 | Shipped                   |
| 33 Ready for pickup                                                                                                                      | Waiting for the customer at the desk or pickup point  | Shipped                   |
| 41 Delivered                                                                                                                             | Delivered                                             | Delivered                 |
| 50 Aborted                                                                                                                               | Cancelled by Terminal                                 | Cancelled                 |
| 10 In transit to be returned, 53 Not received                                                                                            | The parcel is coming back                             | Returned (stock waits)    |
| 52 Taken by store                                                                                                                        | The parcel is back with you                           | Returned (stock restored) |

A return that never reaches status 52 gets its stock back after 15 days, or earlier when you mark it received on the order.

## Common errors and fixes

DZBuild shows Terminal's refusals in Arabic. When Terminal sends its own explanation, it follows the message in brackets.

| Symptom                                                                                                                   | Likely cause                                                           | Fix                                                                                                               |
| ------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| « بيانات الاعتماد غير صحيحة لدى Terminal Delivery (HTTP 401) - تأكد من رمز الـ Token »                                    | Wrong or expired token                                                 | Copy the token again from your Terminal account.                                                                  |
| « فشل التحقق: السعر الإجمالي أقل من الحد الأدنى لدى Terminal Delivery، السعر يجب أن يشمل تكلفة التوصيل » (code 10)        | The order total is below Terminal's delivery price for that commune    | Check that the order total includes the delivery price, and raise the shipping price on the order if it does not. |
| « فشل التحقق: لا يوجد سعر توصيل لهذه البلدية في حساب Terminal Delivery » (code 70)                                        | Your Terminal account has no price for that commune                    | Ask Terminal Delivery to open the commune, or ship that order with another courier.                               |
| « لم يتم العثور على البلدية لدى Terminal Delivery أو المنطقة غير مغطاة » (code 40)                                        | Terminal does not know or does not serve the commune                   | Correct the commune, or click **Actualiser** (« تحديث البيانات ») on the tile to re-sync the commune list.        |
| « مكتب الاستلام (Stop Desk) أو نقطة الاستلام غير متوفرة في هذه البلدية لدى Terminal Delivery » (code 45)                  | No stop desk or pickup point in that commune                           | Switch the order to home delivery or pick another desk.                                                           |
| « طلب مكرر لدى Terminal Delivery: يوجد طلب نشط بنفس اسم العميل ورقم الهاتف خلال آخر 24 ساعة، لا تُعِد الإرسال » (code 55) | Same name and phone as an active order of the last 24 hours            | Wait, or correct the phone; do not resend as is.                                                                  |
| « فشل التحقق: المنتج غير موجود أو غير متطابق في كتالوج Terminal Delivery » (code 50)                                      | The product line could not be created or matched in Terminal's catalog | Rename the product in DZBuild, or create it in Terminal with the same name.                                       |
| « فشل التحقق: حساب Terminal Delivery غير مفعّل للإرسال عبر API، تواصل مع Terminal » (code 120)                            | Your Terminal account is not enabled for API orders                    | Ask Terminal Delivery to enable it.                                                                               |
| « ملصق Terminal Delivery غير متوفر لهذا الطلب، اطبعه من لوحة Terminal Delivery »                                          | Terminal did not return the slip when the order was sent               | Print the slip from your Terminal Delivery account.                                                               |

## Tips

* The connection test works on an account with no shipments yet.
* A new product name creates a product in your Terminal catalog; keep product names stable so Terminal's stock view stays readable. When a name already exists there, DZBuild reuses that product.
* Terminal allows 300 API calls per minute per account; DZBuild waits the seconds Terminal asks for (up to 10) before retrying once.
