# Exaride.co.uk ↔ ExaDispatch API integration

## Ready-to-use page

Copy **`booking.php`** from this folder to your site (same folder as **`exaride-estimate-all.php`**, **`exaride-book.php`**, and **`config.php`**). Replace **`YOUR_GOOGLE_MAPS_API_KEY`** in the Maps script URL with your browser key.

## Security

- **Never** put the Laravel `X-API-Key` in browser JavaScript.
- Deploy **`config.php`**, **`exaride-estimate-all.php`**, and **`exaride-book.php`** on the Exaride server (same directory as `booking.php`). PHP calls ExaDispatch with the key **server-side**.

## 1. Laravel (ExaDispatch)

1. Create an API key: `php artisan api-key:create`
2. Ensure `POST /api/v1/estimate` and `POST /api/v1/bookings` work with that key.
3. Estimate responses now include `total_distance_km` and `total_duration_minutes` when the distance matrix is available.

## 2. Files on Exaride (cPanel / shared hosting)

1. Copy `config.example.php` → **`config.php`** and set:
   - `base_url` — e.g. `https://your-dispatch-domain.com/api/v1`
   - `api_key` — the plain key from `api-key:create`
2. Upload **`exaride-estimate-all.php`** and **`exaride-book.php`** next to `config.php`.
3. **Permissions:** `config.php` should not be web-readable if your host allows moving it outside `public_html`; at minimum, do not commit it to git.

## 3. Changes in `booking.php`

### A. Hidden ISO datetime (for the API)

After the `journey-date` input (or with other hidden fields), add:

```html
<input type="hidden" id="journey-datetime-iso" value="">
```

### B. When date/time is applied

In `applyDateTime()` (after you set `journey-date` value), add:

```javascript
document.getElementById("journey-datetime-iso").value = selectedDateTime.toISOString();
```

### C. Replace `loadAllCarTypes()` fetch

- **URL:** `exaride-estimate-all.php` (same folder as `booking.php`)
- **Body:** JSON with `pickup_lat`, `pickup_lng`, `drop_lat`, `drop_lng`, `pickup_address`, `drop_address`, `scheduled_at` (ISO string from `#journey-datetime-iso`)

If `journey-datetime-iso` is empty, show “Please select journey date and time first” and return (do not call the API).

### D. Replace `actBook()` fetch

- **URL:** `exaride-book.php`
- **Body:** JSON with:

| Field | Source |
|-------|--------|
| `pickup_address`, `drop_address` | Form |
| `pickup_lat`, `pickup_lng`, `drop_lat`, `drop_lng` | Hidden / Places |
| `scheduled_at` | `#journey-datetime-iso` |
| `vehicle_type_id` | `parseInt(carEl.getAttribute("data-carid"), 10)` |
| `customer_name`, `customer_email`, `customer_phone` | Form |
| `passenger_count` | `1` (or add a field later) |
| `notes` | Drop-off note field `#note` |

Remove checks for `FareEstimateId` / old CabSoluit IDs.

### E. Success handling

`Data` is the booking **reference** string (e.g. `API-XXXXXXXX`). Show it in your modal or redirect URL as `booking_id`.

---

## 4. Duplicate `toggleDatePicker` / `applyDateTime`

Your page defines some functions twice. Merge into a single implementation so `applyDateTime` always updates `journey-datetime-iso`.

---

## 5. Rate limits

ExaDispatch v1 is limited (e.g. **60 requests/minute** per API key). Loading all vehicle types runs **one GET + N POST estimates**. If you have many vehicle types, consider caching or a batch endpoint on Laravel later.

---

## Troubleshooting HTTP 500

### Laravel (`/api/v1/...`)

1. Read **`storage/logs/laravel.log`** (latest errors at the bottom).
2. **Telescope:** If you see `telescope_entries` does not exist, either run `php artisan telescope:install` and `php artisan migrate`, or set **`TELESCOPE_ENABLED=false`** in `.env` and run `php artisan config:clear`.
3. Also check database connection, `APP_KEY`, and that migrations have run.

### PHP proxies (`exaride-estimate-all.php`, `exaride-book.php`)

1. **`config.php`** must sit in the same folder; **`base_url`** and **`api_key`** must be set.
2. PHP **curl** extension must be enabled on the host.
3. **`base_url`** must be reachable from that host (correct HTTPS URL including `/api/v1`).

### Which URL failed?

In the browser **Network** tab, check whether the 500 is from your **Exaride PHP file** or from the **Laravel** domain—then use the matching section above.
