# TripFindy flights

The integration follows the supplied **API guide: Flights** (29 September 2026 export). The React website calls Laravel; only Laravel sends TripFindy credentials to the supplier. No supplier credentials belong in `VITE_*` variables.

## Setup

1. Configure this Laravel installation normally, including its existing `APP_KEY`, database and public `APP_URL`. Do not rotate an existing application key: flight passenger records and API credentials are encrypted with it.
2. Run `php artisan migrate` to create `flight_orders` (and, on existing installations, add its `mode` column for held bookings).
3. Open **Business Settings → TripFindy Flight API setup** (`/admin/tripfindy`). Save the sandbox key and secret, select Sandbox and enable the integration. Blank password fields preserve the saved value. Live credentials are stored separately.
4. Add the deployment server's outbound public IP under **API access** in the TripFindy portal. Use **Test saved connection / server IP** to check connectivity. IP whitelisting and live certification take place in TripFindy, not in this application.
5. In **Business Settings → Payment Gateway**, enable ShurjoPay or SSLCommerz in the **same environment** as TripFindy. Set the website URL and gateway callback/IPN configuration as for existing website bookings.
6. Run a persistent queue worker, for example `php artisan queue:work --timeout=210 --tries=3`, and Laravel's scheduler (`php artisan schedule:work` locally; the usual `schedule:run` cron in production). Use a persistent shared cache with atomic locks. Set the queue visibility / `retry_after` above 240 seconds; database, Redis and Beanstalk defaults are now 300 seconds. Remove or update any older environment override setting these to 90. Restart workers after deployment.
7. Build the website with `VITE_API_URL` pointing to this Laravel `/api` base. Non-local production URLs must use HTTPS.

The website exposes `/flights`, `/flights/offers/{offer_id}` and `/account/flights`. The homepage Flights tab opens live search. Customers must sign in before checkout; their account menu links to tickets.

## Booking and payment behavior

- Search supports one-way, round-trip and 3–6-leg multi-city itineraries, branded fares grouped by itinerary, cabins, passenger counts and nonstop filtering.
- Prices use `sell.total` when present, otherwise `total`. Supplier and selling amounts are stored separately in integer minor units. Existing payment gateways support BDT flight checkout; other currencies are rejected rather than converted.
- Price confirmation and extras determine passenger order, types and age validation. Passport information is encrypted at rest. Local airport times display without timezone conversion. Unknown baggage and refundability stay unknown.
- A local order and hosted gateway checkout are created first. **No airline booking, instant ticket or deposit debit happens before verified payment.** Checkout retries reuse the reference and pending/processing gateway session. Order access requires the owning customer's token.
- **Hold, pay later.** Checkout offers two buttons: **Pay** (the flow above) and **Hold seats, pay later**. Hold sends `mode: hold`, books the offer with TripFindy at once under the same `book-{order UUID}` key and charges nothing: the order is `held` and unpaid, and the response has no `checkout_url`. The customer pays from **My trips** until `tripfindy.hold_payment_margin` minutes (default 15) before the airline's `hold_expires_at`; payment re-reads the booking first, so no money is taken for seats already released. Verified payment then issues the existing booking instead of booking again. The customer can cancel an unpaid hold unless a payment has been started. Fares marked `instant_ticket` are never held, because booking them debits the deposit immediately; `hold_unavailable` is shown as "pay now". A customer may have `tripfindy.max_open_holds` (default 2) unpaid holds, since holds that end without a ticket count against the agency (`churn_limit_reached`). A refused hold reserves nothing and leaves no order.
- The verified-payment handler schedules fulfilment after database commit. The worker confirms the supplier price, books, and issues held fares. Instant fares are already ticketed by the booking call. Stable `book-{order UUID}` and `issue-{order UUID}` keys survive timeouts, worker restarts and repeated payment callbacks. Customer input never supplies a supplier price or a fare-change acceptance flag.
- Airline availability and fares can change while the customer pays. A fare change, expired offer, insufficient deposit or rejected booking after payment requires staff review. A changed fare is never silently accepted. Customer payment is **not automatically refunded**: staff resolve the booking or refund through the original payment gateway. The checkout explicitly explains this before payment.
- The scheduler reconciles paid/booking/pending/held/ticketing orders every 30 seconds. Unpaid holds are followed too (a confirmed hold every 5 minutes) and become `expired` or `cancelled` when the airline releases them; a hold request whose answer was lost is replayed with its original key. Browsers refresh their own orders every 30 seconds; workers remain responsible for supplier polling even if the browser closes. Ticket numbers, not a payment redirect, establish that tickets were issued.
- An order is bound to the original API environment and key fingerprint. Changing either pauses outstanding fulfilment for review rather than creating a booking in a different account.

## Operations and after-sales

Open **Flight Bookings → Online Flight Orders** (`/admin/online-flight-orders`). Existing flight-booking view/update permissions control this screen and its actions; payment-settings update permission controls API credentials. Super administrators have both.

The order screen refreshes the supplier booking and shows payment transactions, ticket numbers, errors and after-sales requests. It supports held-seat cancellation, reading refund quotes, requesting a manual refund quote, voiding, reissue search for all travellers, requesting a reissue quote and accepting/declining the current quote. The provider enforces booking state and quote/void deadlines. Reissue search results include the offer IDs to use for the next step. Airline refunds credit the **agency deposit**; staff must separately refund the customer through the original gateway. Only recoverable access/deposit failures can be retried from the screen; fare changes need explicit staff resolution.

Optional paid seats, meals, bags and wheelchair requests are not offered by this checkout. They are not silently added to the booking. Frequent-flyer numbers appear only when supported. Full provider certification, including any additional ancillary scenarios requested by TripFindy, still requires sandbox credentials, a whitelisted server, and actual sandbox bookings.

## Verification

Backend: `php vendor/phpunit/phpunit/phpunit --filter=TripFindyFlightTest`.

Website: `npm run lint`, `npm run build` with `VITE_API_URL` set. `scripts/test-flight-browser.cjs` runs against Vite on port 3011 (override with `FLIGHT_TEST_URL`), requires Playwright (or `PLAYWRIGHT_MODULE`), and mocks all API/payment responses. It checks search, grouping, changed fares, passenger fields, identical checkout retries, issued tickets and mobile overflow without contacting a supplier or taking payment.
