diff --git a/apps/edr-passenger-web/portal/src/app/booking/passengers/page.tsx b/apps/edr-passenger-web/portal/src/app/booking/passengers/page.tsx index b38234732..4b46859d6 100644 --- a/apps/edr-passenger-web/portal/src/app/booking/passengers/page.tsx +++ b/apps/edr-passenger-web/portal/src/app/booking/passengers/page.tsx @@ -512,6 +512,8 @@ export default function PassengersPage() { const [verificationStatus, setVerificationStatus] = useState>({}); const [saving, setSaving] = useState(false); const [formInitialized, setFormInitialized] = useState(false); + const [faydaParams, setFaydaParams] = useState<{ code: string; state: string } | null>(null); + const [faydaCompleting, setFaydaCompleting] = useState(false); const totalPassengers = (searchCriteria?.adultCount || 1) + (searchCriteria?.childCount || 0); @@ -576,6 +578,54 @@ export default function PassengersPage() { checkFaydaStatus(); }, []); + // Read Fayda callback params from the URL on mount + useEffect(() => { + if (typeof window === 'undefined') return; + const params = new URLSearchParams(window.location.search); + const code = params.get('code'); + const state = params.get('state'); + if (code && state) setFaydaParams({ code, state }); + }, []); + + // Complete Fayda verification once the form is ready and callback params are present + useEffect(() => { + if (!faydaParams || !formInitialized) return; + + const complete = async () => { + setFaydaCompleting(true); + try { + const response: any = await apiClient.get( + `/fayda/verification/complete?code=${encodeURIComponent(faydaParams.code)}&state=${encodeURIComponent(faydaParams.state)}` + ); + + if (response?.success && response?.data?.verified) { + const d = response.data; + // Convert "1980/12/01" → "1980-12-01" + const dob = d.birthdate ? (d.birthdate as string).replace(/\//g, '-') : ''; + + setValue('passengers.0.name', d.fullName || '', { shouldValidate: true }); + if (dob) setValue('passengers.0.dateOfBirth', dob, { shouldValidate: true }); + if (d.email) setValue('passengers.0.email', d.email, { shouldValidate: true }); + if (d.phoneNumber) setValue('passengers.0.phone', d.phoneNumber, { shouldValidate: true }); + setValue('passengers.0.faydaVerified', true); + setValue('passengers.0.formExpanded', true); + setVerificationStatus((prev) => ({ ...prev, 0: 'success' })); + + // Remove code/state from the URL so a refresh doesn't re-trigger + router.replace('/booking/passengers'); + } + } catch (error) { + console.error('Failed to complete Fayda verification:', error); + } finally { + setFaydaCompleting(false); + setFaydaParams(null); + } + }; + + complete(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [faydaParams, formInitialized]); + useEffect(() => { if (isAuthenticated && user?.faydaVerified) { setVerificationStatus({ 0: 'success' }); @@ -752,13 +802,15 @@ export default function PassengersPage() { if (!searchCriteria) return null; - if (!formInitialized) { + if (!formInitialized || faydaCompleting) { return (
-

Loading passenger details...

+

+ {faydaCompleting ? 'Completing Fayda verification...' : 'Loading passenger details...'} +