# B2C Partner Module & Package Booking API

B2C partners are businesses that sell our tour packages **directly to their own customers**. They can sell in two ways:

1. **Partner portal** (`/b2c`): sign in, pick a package and departure, enter the traveller details, and confirm. The booking is paid from the partner's prepaid wallet.
2. **Package Booking API**: the partner's own website searches our catalogue, holds seats, takes payment and confirms the booking through the API.

The API comes in two environments:

| | Sandbox (demo) | Live |
|---|---|---|
| Base URL | `https://<admin-host>/partner/b2c/sandbox/v1` | `https://<admin-host>/partner/b2c/v1` |
| Credentials | `gfc_test_…` / `gfs_test_…` / `gfk_test_…` | `gfc_live_…` / `gfs_live_…` / `gfk_live_…` |
| Available | as soon as the partner account exists (pending or active) | once the account is **active** and **Live API** is approved |
| Effect | real catalogue and prices; simulated holds and bookings (no seats held, no customer, no core booking, no wallet movement, `TEST-` vouchers) | real holds and bookings; wallet charged |
| Error simulation | `X-Sandbox-Simulate` header | ignored |

A credential works only on its own environment's base URL. B2C credentials also do not work on the BDTickets aggregator API (`/partner/v1`), and aggregator credentials do not work on the B2C API.

The complete developer reference (every endpoint, field, error code, webhooks, code samples and the go-live checklist) is rendered inside the app, so its base URLs are always correct:

- Partner portal: **API & Developers → API Documentation** (`/b2c/developer/docs`)
- Admin panel: `/admin/b2c-partners/api-docs`

Its source is `resources/views/b2c/docs/_content.blade.php`.

---

## Where it lives

The module is **not in the admin sidebar** on purpose. `AdminMenuSeeder` has no entries for it, and a test (`B2cAdminTest::test_module_is_not_in_the_admin_sidebar`) enforces that. Reach it by URL:

| Screen | URL |
|---|---|
| Partners (list, stats, add) | `/admin/b2c-partners` |
| Partner profile (access, wallet, credentials, bookings, API calls) | `/admin/b2c-partners/{id}` |
| All B2C bookings (live / sandbox, filters, cancel) | `/admin/b2c-partners/bookings` |
| API call logs | `/admin/b2c-partners/api-logs` |
| API documentation | `/admin/b2c-partners/api-docs` |
| Partner portal sign-in | `/b2c/login` |

Admin routes sit in the normal `auth` + `permission` group. Super-admins have access, and other roles need the matching `admin.b2c-partners.*` permissions (created by `RolePermissionMenuSeeder` from the route names).

**To publish the menu later**, add a node to `AdminMenuSeeder::$tree` (for example under *CRM & Sales*: `['B2C Partners', 'fas fa-store', 'admin.b2c-partners.index']`), ship a migration that inserts the `menus` row, and update the sidebar test.

## Onboarding a partner

1. **Add B2C Partner** (`/admin/b2c-partners/create`): business details, portal email and password, commission %, credit limit, optional opening deposit, and package access (all packages or a selected list).
2. Status **Pending**: the partner can sign in, browse, and use the **sandbox** API. It cannot sell.
3. Status **Active**: the partner can sell from the portal.
4. **Live API approved** (on the profile page, or in the edit form): live API credentials start working. The partner generates them under *API & Developers → API Credentials*. Admins can also regenerate them and hand them over; the secret is shown once.
5. **Suspended**: portal and both APIs are cut off immediately. Outstanding access tokens are revoked, and signed-in portal sessions are ended on the next request.

Self-registration at `/b2c/register` is **off** by default. Set `B2C_SELF_REGISTRATION=true` to allow it. New sign-ups start as *Pending*.

## Money

- The partner sells at our price (`grandTotal`) and keeps `commission_percent`. Every **live** sale (API or portal) debits `amountPayableToProvider` (price minus commission) from the partner's wallet.
- A sale is refused with `402 INSUFFICIENT_BALANCE` unless *wallet balance + credit limit* covers that amount. The partner row is locked during confirm, so two concurrent sales cannot spend the same balance.
- Cancelling a booking (by the partner through the API or portal, or by an admin) applies the cancellation policy captured at booking time. The partner wallet is credited the refundable share of what was debited.
- Deposits and adjustments are recorded by admins on the partner profile. Partners see their statement under *Wallet*.

## What a live sale creates

Each live sale creates the following, so operations handle B2C sales exactly like the existing partner sales:

- a `partner_reservations` row and a `partner_bookings` row (with `channel` = `api` or `portal`)
- a core `bookings` row (`booking_type` = `online`, `service_type` = `tour_package`, paid, confirmed, with `agent_commission_amount` = the partner's commission, and `special_requirements` naming the partner and their order reference)
- a customer (code prefix `B2C-`) and the traveller list
- a wallet debit

Every B2C booking also gets a **signed voucher URL** (`/b2c/voucher/{bookingRef}?signature=…`). It needs no login, so partners can send it to their travellers.

## Implementation map

| Concern | Where |
|---|---|
| Schema | `database/migrations/2026_09_30_000001_create_b2c_partner_module.php` |
| Partner account (portal guard `b2c`) | `App\Models\B2cPartner`, `config/auth.php` |
| Two API clients per partner, credential rotation, webhook/IP settings | `App\Services\B2c\B2cCredentialService` |
| Wallet | `App\Services\B2c\B2cWalletService`, `App\Models\B2cWalletTransaction` |
| Portal direct sale (reserve + confirm in one step) | `App\Services\B2c\B2cSaleService` |
| Reserve / confirm / cancel engine (shared with BDTickets) | `App\Services\Partner\ReservationService` |
| Environment and audience pinning | `App\Http\Middleware\Partner\PartnerEnvironment`, `PartnerAuthenticate::assertEnvironment()` |
| API call log (metadata only, pruned after `B2C_API_LOG_RETENTION_DAYS`, default 30) | `App\Http\Middleware\Partner\B2cApiLogger`, `App\Models\B2cApiLog`, `routes/console.php` |
| Routes | `routes/partner.php` (API), `routes/web.php` (`admin/b2c-partners*`, `b2c/*`) |
| Admin UI | `App\Http\Controllers\Admin\B2cPartnerController`, `resources/views/admin/b2c-partners` |
| Portal UI | `App\Http\Controllers\B2c\*`, `resources/views/b2c` |
| Tests | `tests/Feature/B2c` |

Config: `config/b2c.php` (self-registration, log retention, sandbox simulations). Hold TTL, token TTL, currency and the default cancellation policy come from `config/partner.php`.

## Deploying

```bash
php artisan migrate
php artisan config:cache && php artisan route:cache && php artisan view:cache
```

The scheduler (already required for this app) prunes old API logs daily.
