# Partner API (BDTickets aggregator integration)

Provider-side implementation of `the package API spec` — the
`reserve → confirm → cancel → status` aggregator contract. This is the **Let's Go** side: BDTickets is the client, we (this TMS) are the API provider.

Base path: `/partner/v1` (mounted in `bootstrap/app.php` via `routes/partner.php`).

## Endpoints (spec §15 checklist)

| # | Method | Path | Auth scope | Idempotent |
|---|--------|------|------------|------------|
| 1 | POST | `/oauth/token` | — | — |
| 2 | GET | `/destinations` | packages.read | — |
| 3 | GET | `/taxonomies` | packages.read | — |
| 4 | GET | `/packages/search` | packages.read | — |
| 5 | GET | `/packages/{packageId}` | packages.read | — |
| 6 | GET | `/packages/{packageId}/availability` | packages.read | — |
| 7 | POST | `/reservations` | reservations.write | ✅ |
| 8 | GET | `/reservations/{reservationId}` | packages.read | — |
| 9 | POST | `/reservations/{reservationId}/confirm` | reservations.write | ✅ |
| 10 | POST | `/reservations/{reservationId}/cancel` | reservations.write | ✅ |
| 11 | POST | `/reservations/{reservationId}/extend` | reservations.write | ✅ |
| 12 | POST | `/bookings/{bookingId}/cancel` | reservations.write | ✅ |
| 13 | GET | `/bookings/{bookingId}` | packages.read | — |
| 14 | GET | `/bookings?bdticketsOrderRef=…` | packages.read | — |
| 15 | outbound webhook `BOOKING_STATUS_CHANGED` / `RESERVATION_EXPIRED` (§11) |

## Auth (§4)

- **Layer 1:** OAuth 2.0 client-credentials → short-lived bearer token
  (`config('partner.token_ttl')`, default 1h). `X-API-Key` is also accepted for the
  sandbox. Secret rotation supports an overlap window (`client_secret_previous`).
- **Layer 2:** optional per-client IP allow-list (CIDR/range aware, NAT-friendly).
  Empty allow-list = allow any.
- Middleware: `partner.auth:<scope>` enforces the token/key + scope + IP rules.

## Idempotency (§4.3)

`partner.idempotent` middleware requires an `Idempotency-Key` header on all write POSTs.
Same key + same body → replays the stored response; in-flight → `409 IN_PROGRESS`;
same key + different body → `400 VALIDATION_ERROR`.

## Money & conventions (§3)

- Amounts are decimal strings with 2 places (`App\Support\Partner\Money`, bcmath).
- Every response uses the `{ success, data }` / `{ success, error{code,message,details} }`
  envelope. Errors map to stable `error.code`s via `App\Support\Partner\PartnerException`
  and the renderer in `bootstrap/app.php`.
- `X-Request-Id` is echoed back by `partner.correlation`.

## Domain mapping

| Spec concept | TMS model |
|---|---|
| package | `TourPackage` |
| slot / departure date | `TourDeparture` (`slotId = SLOT_<id>`) |
| destination | `Destination` (+ `slug`, `requires_passport`) |
| reservation (hold) | `PartnerReservation` |
| confirmed booking | `PartnerBooking` (+ a materialised core `Booking`) |

`seatsLeft = departure.capacity − confirmed pax − live-held pax`. On confirm we create a
core `Booking` (+ `BookingPassenger`s and a `Customer`) so the TMS owns fulfilment;
the partner ledger remains the source of truth for the hold/sale lifecycle.

## Setup

```bash
php artisan migrate
php artisan db:seed --class="Database\\Seeders\\PartnerApiSeeder"
# sandbox client: client_id=bdtickets, secret=bdtickets-sandbox-secret
```

New config: `config/partner.php` (token/hold TTLs, taxonomies, default cancellation
policy, domestic country codes, commission defaults on the client record).

## Tests

`tests/Feature/Partner/PartnerApiFlowTest.php` covers the happy path
(search → detail → reserve → confirm → status → lookup → cancel), idempotent reserve &
confirm replays, `RESERVATION_EXPIRED`, `PRICE_CHANGED`, `SOLD_OUT`, missing
idempotency key, and unauthenticated access.

```bash
php artisan test --filter PartnerApiFlowTest
```

## Open items deferred to Let's Go ops (spec §14)

- Real media hosting / CDN sizes (§12) — presenter returns absolute URLs from the
  `public` disk; `width`/`height` are null until dimensions are captured on upload.
- Webhook delivery is best-effort/synchronous (`WebhookService`); move to a queued job
  with retry+backoff before scale.
- HMAC request-signing verification (§4.2) — `hmac_secret` column is provisioned but
  inbound verification is not yet enforced.
