# Architecture

How the Parcel2Go Shipping plugin and React app fit together.

## Bootstrap

1. **Plugin entry:** `parcel2go-shipping.php` runs on WordPress load. It defines `PARCEL2GO_SHIPPING_MAIN_PLUGIN_FILE`, loads Composer autoload (`vendor/autoload_packages.php`), and hooks `plugins_loaded` to `parcel2go_shipping_init`.
2. **Init:** After checking WooCommerce is active and loading the text domain, `parcel2go_shipping_init` calls `Parcel2GoShipping::instance()`, adds the "Settings" link on the Plugins screen, and registers REST routes for all API controllers (Orders, Quotes, Countries, Dropshop, Checkout, Payment, Settings, Shipments).
3. **Parcel2GoShipping:** The main class constructor only runs when `is_admin()`; it then instantiates `Parcel2GoShipping\Admin\Setup`.
4. **Setup:** Registers and enqueues the built script (`build/index.js`) and style (`build/index.css`) on WooCommerce Admin pages, and localizes `parcel2goShipping` (REST base URL, Sentry DSN/environment, asset URLs, etc.) for the frontend.

## Admin UI

- **Page registration:** Done in two places:
  - **PHP:** `includes/Admin/Setup.php` uses `wc_admin_register_page()` to register the WC Admin pages: landing (`/parcel2go-shipping`), shipments (`/parcel2go-shipping/shipments`), and settings (`/parcel2go-shipping/settings`).
  - **React:** `src/index.tsx` uses `addFilter('woocommerce_admin_pages_list', ...)` to provide the actual containers and breadcrumbs for those paths.
- **Script/style:** Enqueued only when `PageController::is_admin_or_embed_page()` is true, so the app loads only in the WC Admin context.
- **Localized config:** `parcel2goShipping` includes `apiUrl` (REST base), `dsn` / `environment` (Sentry), `flagsBaseUrl`, `couriersBaseUrl`, `editOrderUrlTemplate`, `storeAddressUrl`, and similar.

## Frontend

- **Entry:** Single React app in `src/index.tsx`. Sentry is initialized first; then the app registers with the `woocommerce_admin_pages_list` filter.
- **Routing:** The main component uses `useNavigation()` and `selectedOrderId`. If `selectedOrderId !== null`, it renders `OrderShipPage`; otherwise it renders `LandingPage`. Settings and Previous Shipments are lazy-loaded and rendered via the WC Admin page list (containers for `/parcel2go-shipping/settings` and `/parcel2go-shipping/shipments`).
- **Flow:** User starts on the landing page, picks an order to ship ("Ship now"), then goes through Order Ship (quotes, service selection, booking, payment). Settings and Shipments are separate flows from the sidebar/menu.

## High-level data flow

```mermaid
flowchart LR
	Landing[LandingPage]
	OrderShip[OrderShipPage]
	Quotes[Quotes]
	Booking[Booking]
	Payment[Payment]
	Complete[Complete]
	Settings[Settings]
	Shipments[Shipments]

	Landing -->|"Ship now"| OrderShip
	OrderShip --> Quotes
	Quotes --> Booking
	Booking --> Payment
	Payment --> Complete
	Settings --> Landing
	Shipments --> Landing
```

Landing shows orders; choosing one opens Order Ship. Order Ship fetches quotes, then checkout/validate, then payment (Braintree or prepay), then order completion. Settings and Previous Shipments are separate pages.

## Live courier rates at checkout

An optional WooCommerce shipping method (`parcel2go_live`, "Live courier rates") shows customers live Parcel2Go prices
with the merchant's markup (or reduction) under simple service names at checkout, and the chosen service is
pre-selected at booking. It runs on the storefront
(not only in WC Admin): `parcel2go_shipping_init` registers it through `woocommerce_shipping_methods`, along with its
background jobs (Action Scheduler) and the hook that saves the checkout quote on the order. Checkout never calls
Parcel2Go for settings or a login renewal; it reads cached settings and the saved token, and falls back to the zone's
other methods on any problem. Full design: [docs/designs/checkout-live-rates.md](designs/checkout-live-rates.md).

### Pickup point delivery (lockers and shops)

Services that deliver to a locker or shop (quote tag `shopdelivery`, any carrier) are offered like any other live rate,
named for example "InPost Locker Pick-up" or "Evri Shop Pick-up" ("Locker" when the quote is also tagged `locker`).
The provider comes from the service's `dropOffProviderGroup`, so a new carrier's pickup service needs no code change. A pickup rate is only shown when Parcel2Go has places near a full UK postcode; otherwise it is dropped and the
other rates still show.

- **Places:** `PickupPoints` looks them up at Parcel2Go
  (`order-process/dropshops/geo/{provider}/GBR/{postcode}?type=Destination`), keeps those that accept deliveries, and
  caches the nearest 10 for 24 hours. Lookups follow the live rates cut-off, so a Parcel2Go outage does not slow checkout.
- **Choosing a place:** the nearest is pre-selected; the customer sees 3 and can show up to 10. The choice is kept in
  the WooCommerce session per package and provider, so switching carrier and back keeps each pick.
  - Classic checkout: `PickupPointCheckout` adds the cards under the rate (`woocommerce_after_shipping_rate`), keeps
    the choice in the WooCommerce session, and checks it at `woocommerce_after_checkout_validation`. The classic cart
    can't save a choice, so under a selected pickup rate it says "Choose locker (or shop) pick-up location at checkout."
  - Block checkout: `PickupPointBlocks` adds the places to the Store API cart and takes the choice through
    `extensionCartUpdate`; the `checkout-pickup-point` script (`src/checkout-pickup-point/`) shows the cards. The cart
    block offers no shipping options, so nothing is shown there.
- **On the order:** the place is saved on the shipping item (`_p2g_pickup_point_id`, `_p2g_pickup_point_provider`,
  `_p2g_pickup_point`) and shown to the customer as "Collect from" in emails and the order page.
- **Booking:** the ship page shows "Customer collects from" and sends the place as `destination.useAddress.shopId`.
  The merchant can change it (for example when the customer asks for another), and is reminded to tell the customer.
  The merchant must pick a place when the customer's place has gone, the order has none, or the service is another
  carrier's; booking waits until then. Bulk booking
  sends each order's own place and skips orders without one (`no-pickup-point`). There is no drop-off shop to choose:
  the merchant drops off at any of the courier's shops. Each booking sends `wooCheckoutService` (the service the
  customer chose) when the order came through Live courier rates.
- **Phone:** both carriers need the customer's phone. The settings screen warns when a pickup service is picked and
  the checkout phone field (`woocommerce_checkout_phone_field`) is not Required; checkout itself is unchanged.

## Parcel2Go OAuth login

The plugin includes a first-party OAuth login flow so the WP admin can link their Parcel2Go account.
The WordPress plugin runs on each user's own domain.  There is no single fixed redirect URI that can be registered on the P2G OAuth client.

The accountlinking edge API acts as a stable, known OAuth redirect URI and encrypts the per-install WordPress site URL into the OAuth `state` parameter. When the user completes the P2G login, the edge decrypts the state and redirects the browser back to the WordPress site's REST API callback endpoint.

### Flow

```
WordPress Admin                         Accountlinking Edge               P2G Auth Server
       |                                           |                              |
       | POST /auth/authorise                      |                              |
       |   (generates PKCE verifier+challenge)     |                              |
       | ─────────────────────────────────────────>|                              |
       |   POST /{profile}/v1/nativeplugins/       |                              |
       |        nativeproxy/woocommerce/authorise  |                              |
       |   { codeChallenge, siteUrl, callbackUrl } |                              |
       |<─────────────────────────────────────────|                              |
       |   { authoriseUrl }                        |                              |
       |                                           |                              |
       | [browser redirects to authoriseUrl]       |                              |
       |                                           | ──────── user logs in ────>|
       |                                           |<── redirect with ?code= ─--|
       |                                           |                              |
       |<────── edge redirects to WP /auth/callback?code= ──────────────────────|
       |                                           |                              |
       | GET /auth/callback?code=                  |                              |
       |   (exchanges code + PKCE verifier)        |                              |
       | ────────────────────────────────── POST token ────────────────────────>|
       |<────────────────────────────────── { access_token, refresh_token } ──--|
       |   stores tokens in wp_options             |                              |
       |   redirects browser to plugin admin page  |                              |
```

### PHP
- `includes/Api/AuthController.php` — five REST endpoints: `authorise`, `callback`, `status`, `user`, and `logout`.
- Tokens are stored in `wp_options` (not autoloaded): `parcel2go_access_token`, `parcel2go_refresh_token`.
- PKCE code_verifier is stored as a short-lived transient (`parcel2go_pkce_code_verifier`, 10 min TTL) and deleted immediately after use.

### React
- `src/shared/context/AuthContext.tsx` — global auth context providing `useParcel2goAuth` hook for OAuth login, logout, and connection status. Checks server-rendered `window.parcel2goShipping.authLinked` and fetches `/auth/status` on mount.
- `src/features/orders/welcome-card.tsx` — renders a "Login to Parcel2Go" / "Connected ✓" button. When clicked, the button redirects the user to the P2G auth flow.
- `window.parcel2goShipping.authLinked` (bool) — set by PHP at page load; avoids an extra fetch on initial render.


| Path | Purpose |
|------|--------|
| `includes/` | PHP backend: `Api/` (REST controllers), `Helpers/` (OrderCompletionService, OrderFormatter, P2GApiClient, etc.), `Constants/P2GApi.php`, `Admin/Setup.php` |
| `src/` | React app: `index.tsx` (entry), `pages/` (LandingPage, OrderShipPage, SettingsPage, PreviousShipmentsPage), `features/` (order-ship, settings, shipments, orders), `shared/` (components, hooks, utils, sentry), `types/`, `constants/`, `public/couriers/` (and flags) |
| `build/` | Output of `npm run build`: `index.js`, `index.asset.php`, `index.css` |
| `vendor/` | Composer autoload; plugin requires `vendor/autoload_packages.php` |
