# Magazynowanie i wydanie

Przyjmij towar do magazynowania, a następnie wydaj przechowywane pozycje: konfiguracja → wycena → utworzenie magazynowania → płatność → lista pozycji nadal na stanie → wycena wydania → utworzenie wydania → płatność → śledzenie → anulowanie.

Ten przewodnik dokumentuje wyłącznie poniższe operacje. Wykonaj je w kolejności. Uwierzytelnianie używa konta **klienta**.

Zamień `YOUR_HOST` i `ACCESS_TOKEN` na wartości ze swojego środowiska.

## 1. Uwierzytelnienie (klient)

```bash
curl -X POST https://YOUR_HOST/api/v1/user/customer/login \
  -H "Content-Type: application/json" \
  -d '{"email":"you@example.com","password":"your_password"}'
```

```
Authorization: Bearer ACCESS_TOKEN
```

GraphQL używa tego samego nagłówka na `POST /api/graphql`.

**Weryfikacja:** logowanie zwraca `access_token`. Późniejsze wywołanie bez niego zwraca `401`.

## 2. Konfiguracja magazynowania

**REST:** `GET /api/v1/customer/storage-orders/config` — [Podręcznik REST](/api/documentation#/paths/v1-customer-storage-orders-config/get)

```bash
curl https://YOUR_HOST/api/v1/customer/storage-orders/config \
  -H "Authorization: Bearer ACCESS_TOKEN"
```

**GraphQL:** `customerStorageOrderConfig` ([Podręcznik GraphQL](/api/graphql/documentation#/customer/customerStorageOrderConfig))

**Weryfikacja:** zapisałeś `id` magazynu i, jeśli katalog nie jest pusty, `id` opakowania.

## 3. Wycena magazynowania

**REST:** `POST /api/v1/customer/storage-orders/calculate-price`

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/storage-orders/calculate-price \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "warehouse_id": 7,
    "start_date": "2026-09-10",
    "end_date": "2026-10-10",
    "items": [{
      "qty": 1,
      "length": 30,
      "width": 20,
      "height": 15,
      "dimension_unit": 2,
      "weight": 2,
      "weight_unit": 2
    }]
  }'
```

**Weryfikacja:** `success` lub `result` jest true i masz cenę. Brak `warehouse_id` / dat to `400`.

## 4. Utworzenie zamówienia magazynowego

**REST:** `POST /api/v1/customer/storage-orders`

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/storage-orders \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: dev-store-001" \
  -d '{
    "warehouse_id": 7,
    "start_date": "2026-09-10",
    "end_date": "2026-10-10",
    "items": [{
      "description": "Box A",
      "qty": 1,
      "length": 30,
      "width": 20,
      "height": 15,
      "dimension_unit": 2,
      "weight": 2,
      "weight_unit": 2,
      "value": 100
    }]
  }'
```

**Weryfikacja:** odpowiedź ma `data.id`. Zapisz to id zamówienia magazynowego.

## 5. Płatność magazynowania

**REST:** `POST /api/v1/customer/storage-orders/{id}/pay`

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/storage-orders/1024/pay \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
```

Opcjonalnie: najpierw `GET /api/v1/customer/storage-orders/{id}/payment-info`.

**REST:** `GET /api/v1/customer/storage-orders/{id}` — [Podręcznik REST](/api/documentation#/paths/v1-customer-storage-orders-id/get)

**GraphQL:** `customerStorageOrderShow` ([Podręcznik GraphQL](/api/graphql/documentation#/customer/customerStorageOrderShow))

**Weryfikacja:** zamówienie magazynowe jest opłacone / potwierdzone. `402` oznacza doładowanie portfela, potem ponów.

Wydanie poniżej działa dopiero po **przyjęciu** paczek w magazynie. Na test poczekaj, aż personel (lub testowe przyjęcie) oznaczy je jako przyjęte, potem kontynuuj.

## 6. Lista pozycji nadal na stanie

**REST:** `GET /api/v1/customer/shipout-orders/available-items` — [Podręcznik REST](/api/documentation#/paths/v1-customer-shipout-orders-available-items/get)

```bash
curl "https://YOUR_HOST/api/v1/customer/shipout-orders/available-items?warehouse_id=7" \
  -H "Authorization: Bearer ACCESS_TOKEN"
```

**GraphQL:** `customerShipoutAvailableItems` ([Podręcznik GraphQL](/api/graphql/documentation#/customer/customerShipoutAvailableItems))

**Weryfikacja:** zapisałeś jedno lub więcej `storage_package_ids` (przykład `5001`). Pusta lista oznacza, że nic jeszcze nie przyjęto — nie twórz wydania. `403` oznacza, że wydanie jest wyłączone dla tego klienta.

## 7. Wycena i utworzenie wydania

**REST:** `GET /api/v1/customer/shipout-orders/services?warehouse_id=7` — [Podręcznik REST](/api/documentation#/paths/v1-customer-shipout-orders-services/get)

Zapisz `service_code`.

**REST:** `POST /api/v1/customer/shipout-orders/services/{serviceCode}/estimate` — [Podręcznik REST](/api/documentation#/paths/v1-customer-shipout-orders-services-serviceCode--estimate/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/shipout-orders/services/intl_express/estimate \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "warehouse_id": 7,
    "delivery_postcode": "M5V2H1",
    "delivery_country": "CA",
    "packages": [{
      "weight": 2.5,
      "length": 30,
      "width": 20,
      "height": 15,
      "weight_unit": 2,
      "dimension_unit": 2
    }]
  }'
```

**REST:** `POST /api/v1/customer/shipout-orders/services/{serviceCode}/orders` — [Podręcznik REST](/api/documentation#/paths/v1-customer-shipout-orders-services-serviceCode--orders/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/shipout-orders/services/intl_express/orders \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: dev-shipout-001" \
  -d '{
    "warehouse_id": 7,
    "storage_package_ids": [5001],
    "delivery_name": "Jane Recipient",
    "delivery_telephone": "5555555555",
    "delivery_email": "jane@example.com",
    "delivery_address_1": "123 King St W",
    "delivery_city": "Toronto",
    "delivery_province": "ON",
    "delivery_country": "CA",
    "delivery_postcode": "M5V2H1"
  }'
```

**GraphQL:** `customerCreateShipoutOrder` ([Podręcznik GraphQL](/api/graphql/documentation#/customer/customerCreateShipoutOrder))

**Weryfikacja:** odpowiedź ma `id` wydania. Wybrane paczki magazynowe są zablokowane na to żądanie.

## 8. Płatność wydania, śledzenie, zdarzenia

**REST:** `POST /api/v1/customer/shipout-orders/{id}/pay` — [Podręcznik REST](/api/documentation#/paths/v1-customer-shipout-orders-id--pay/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/shipout-orders/8001/pay \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
```

**GraphQL:** `customerPayShipout` ([Podręcznik GraphQL](/api/graphql/documentation#/customer/customerPayShipout))

**REST:** `GET /api/v1/customer/shipout-orders/{id}` — [Podręcznik REST](/api/documentation#/paths/v1-customer-shipout-orders-id/get)

Gdy numer śledzenia istnieje:

```bash
curl https://YOUR_HOST/api/v1/tracking/SR123456789012
```

[Podręcznik REST](/api/documentation#/operations/getPublicTracking) · [Podręcznik GraphQL](/api/graphql/documentation#/tracking/trackingPublic)

**REST:** `PUT /api/v1/webhook-settings` — [Podręcznik REST](/api/documentation#/paths/v1-webhook-settings/put)

**GraphQL:** `webhookSettingsUpdate` ([Podręcznik GraphQL](/api/graphql/documentation#/webhooks/webhookSettingsUpdate)).

Weryfikuj **v2**: `HMAC_SHA256(timestamp + "." + raw_body, secret)` wobec `X-Webhook-Signature-V2`. Deduplikuj po `X-Webhook-Event-Id`. Odpowiedz **2xx w mniej niż 3 sekundy**.

```php
$raw = file_get_contents('php://input');
$ts  = $_SERVER['HTTP_X_WEBHOOK_TIMESTAMP'] ?? '';
$sig = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE_V2'] ?? '';
$expected = hash_hmac('sha256', $ts . '.' . $raw, $sharedSecret);
if (!hash_equals($expected, $sig)) {
    http_response_code(401);
    exit;
}
```

**Weryfikacja:** płatność zapisuje kwotę (albo `402` / `422` z jasnym powodem). Publiczne śledzenie znajduje przesyłkę, gdy numer już istnieje.

## 9. Anulowanie testowego wydania

**REST:** `POST /api/v1/customer/shipout-orders/{id}/cancel` — [Podręcznik REST](/api/documentation#/paths/v1-customer-shipout-orders-id--cancel/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/shipout-orders/8001/cancel \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
```

**GraphQL:** `customerCancelShipout` ([Podręcznik GraphQL](/api/graphql/documentation#/customer/customerCancelShipout))

To zwalnia blokadę paczek magazynowych. Samo magazynowanie anuluje się przez `POST /api/v1/customer/storage-orders/{id}/cancel`, dopóki jest jeszcze dozwolone.

**Weryfikacja:** `422` oznacza, że tego statusu nie można anulować. Po udanym anulowaniu wydania krok 6 znowu pokazuje paczki.

## Lista testów

- [ ] Konfiguracja magazynowania zwraca `id` magazynu.
- [ ] Wycena magazynowania zwraca cenę.
- [ ] Utworzenie magazynowania zwraca `data.id`.
- [ ] Płatność magazynowania się udaje, **albo** potwierdziłeś, że portfel trzeba doładować.
- [ ] Lista dostępnych pozycji pokazuje przyjęte paczki (`storage_package_ids`).
- [ ] Wycena wydania zwraca cenę lub `has_items_needing_quote`.
- [ ] Utworzenie wydania zwraca `id` i blokuje te paczki.
- [ ] Płatność wydania się udaje (albo `402` / `422` jest zrozumiałe).
- [ ] Publiczne śledzenie znajduje przesyłkę, gdy numer śledzenia już istnieje.
- [ ] Anulowanie wydania zwalnia paczki, **albo** tego statusu nie można anulować.
