# Provider integration (Pinbot): operations guide

## Status
Implemented and **untested against the real provider**. It is switched **off** by default (`PINBOT_ENABLED=false`): ordering keeps returning
`503 provider_unavailable` and nothing is reserved. Turn it on only after the checklist below.

## What is implemented
- `PinbotClient`: raw API key in `Authorization`, no redirects, timeouts, **no retries**, nothing logged, transport errors = "outcome unknown".
- `PinbotProvider`: `POST /topup` (async, with callback URL) or `POST /topup-instant` (`PINBOT_MODE=instant`). The order is set to
  `processing` and `submitted_at` is stored **before** the request, together with voucher allocation, in one transaction.
- `PinbotResponseParser`: **fail-closed** reader. Per-item boolean `ok` decides outcomes; anything unclear = unknown (never success).
- Callback `POST|GET /webhooks/pinbot/{provider_order_id}/{token}`: per-order HMAC token, replay-deduplicated, payload stored scrubbed.
  A callback **never settles money**; it only triggers an authenticated `GET /search` lookup.
- Reconciliation (`trevo:reconcile-orders`, every 5 min): looks up `processing`/`manual_review` orders; re-queues orders that were **never** sent.
  No definitive answer within `PINBOT_STUCK_AFTER_MINUTES` => `manual_review` (never auto-failed).
- Voucher inventory: encrypted at rest, one voucher per order item, `used` on success, `review` on rejection.
- Ops commands: `trevo:vouchers:import {product} {file.csv}`, `trevo:provider:status`, `trevo:reconcile-orders`.

## Go-live checklist (all owner inputs; none are guessed in code)
1. **Real Pinbot response samples** for: topup (async) accepted/rejected, topup-instant (success / partial / failed), `/search` (found / pending / not found), and the callback payload.
   The parser's assumptions (see class docblock) must be checked against them and the tests updated with the real bodies.
2. Confirm **request encoding** (`PINBOT_BODY_FORMAT=json|form`) and that **callbacks are accepted on an HTTPS URL** (the brief documents `http://` for the API itself).
3. Confirm whether Pinbot can **sign callbacks**. Until then callbacks are only triggers (safe by design).
4. Voucher stock: import it (`trevo:vouchers:import`) from your own secure source; delete the CSV afterwards.
5. Shell: set `PINBOT_SHELL_USERNAME/PASSWORD/AUTOCODE` in the server environment (or a secret manager) and set each Shell product's package code in `products.provider_meta` as `{"package":"<code>"}`. No Shell products exist yet.
6. Set sell prices and activate services/products (admin UI is Phase 6; SQL until then).
7. Run `php artisan trevo:provider:status`, place a **small real order** from a test reseller, watch it end-to-end, then enable for others.
8. Run a queue worker and the scheduler (`schedule:work` or cron `schedule:run`).

## Runbook
- **`manual_review` order**: funds are still reserved. Check the provider panel for `TREVO-<order id>` (`provider_order_id`). The scheduler keeps looking it up (up to `PINBOT_MAX_RECONCILE_ATTEMPTS`).
  If it never resolves, decide manually (Phase 6 admin tools will provide settle/refund buttons; until then use `OrderSettlementService::settle()` from `tinker`).
- **Voucher in `review`**: the provider rejected it; it may or may not still be valid. Check with the provider before reusing or voiding it.
- **Callbacks not arriving**: harmless; reconciliation covers it. Check `webhook_events` for what did arrive.

## Security notes
- Secrets exist only in environment variables; they are never stored in the database, logs, API responses or webhook payload copies (`Redactor`).
- Voucher codes are encrypted with `APP_KEY`: **back the key up** and never rotate it without re-encrypting stock.
- Webhook tokens are derived from `APP_KEY`; rotating it invalidates callback URLs of in-flight orders (reconciliation still resolves them).
