Added callback url for fayda

This commit is contained in:
Roba Boru
2026-07-01 08:00:47 +03:00
parent be9c6c4adb
commit 8a7799be0e

View File

@@ -512,6 +512,8 @@ export default function PassengersPage() {
const [verificationStatus, setVerificationStatus] = useState<Record<number, 'success' | 'error'>>({});
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 (
<div className="min-h-screen bg-gray-50 dark:bg-gray-900 py-12">
<div className="container mx-auto px-4">
<div className="max-w-lg mx-auto text-center">
<Loader2 className="w-8 h-8 animate-spin mx-auto text-blue-600" />
<p className="text-gray-600 dark:text-gray-400 mt-4">Loading passenger details...</p>
<p className="text-gray-600 dark:text-gray-400 mt-4">
{faydaCompleting ? 'Completing Fayda verification...' : 'Loading passenger details...'}
</p>
</div>
</div>
</div>