# Event Form Critical Fix - Contact ID Verification

**Last Updated:** 2026-01-28

## Critical Issue Found

### Problem: HubSpot Forms API v3 Doesn't Return Contact ID

**Root Cause:**

- HubSpot Forms API v3 only returns `redirectUri` and `inlineMessage` (success message)
- It does NOT return `contactId` in the response
- Code was incorrectly treating `inlineMessage` as a contact ID (line 808)
- `fetchContactIdByEmail()` was using wrong API endpoint format (GET instead of POST search)

**Impact:**

- API returns success even when contact ID can't be verified
- Frontend shows success but contact may not exist in HubSpot
- No way to verify contact was actually created

## Fixes Applied

### 1. Fixed Contact ID Extraction

**Before:**

```php
} elseif (isset($response['inlineMessage'])) {
    $contactId = $response['inlineMessage']; // WRONG - this is a message, not an ID
}
```

**After:**

```php
// Check for actual contact ID fields (NOT inlineMessage - that's just a success message)
if (isset($response['contactId'])) {
    $contactId = $response['contactId'];
} elseif (isset($response['contact'])) {
    $contactId = $response['contact']['id'] ?? null;
} elseif (isset($response['id'])) {
    $contactId = $response['id'];
}
// NOTE: inlineMessage is NOT a contact ID - it's just a success message
// We do NOT use inlineMessage as contactId
```

### 2. Fixed fetchContactIdByEmail() Function

**Before:**

```php
$url = HUBSPOT_CRM_API_V3_CONTACTS . '?properties=email&properties=id&limit=1';
$url .= '&email=' . urlencode($email);
$result = makeHubSpotAPICall($url, 'GET', ...);
```

**Problem:** GET endpoint doesn't support email query parameter. This was silently failing.

**After:**

```php
$searchUrl = HUBSPOT_API_BASE_URL . '/crm/v3/objects/contacts/search';
$searchData = [
    'filterGroups' => [
        [
            'filters' => [
                [
                    'propertyName' => 'email',
                    'operator' => 'EQ',
                    'value' => strtolower(trim($email))
                ]
            ]
        ]
    ],
    'properties' => ['email', 'id'],
    'limit' => 1
];
$result = makeHubSpotAPICall($searchUrl, 'POST', ..., json_encode($searchData), ...);
```

**Fix:** Uses correct POST /search endpoint with filterGroups.

### 3. Added Delay Before Contact Lookup

**Added:** 0.5 second delay before fetching contact ID
**Reason:** HubSpot may create contacts asynchronously, so immediate lookup might fail

### 4. Improved Logging

**Added:** Warning logs when contact ID not found, with note that HubSpot may create contacts asynchronously

## Testing

**Test the fix:**

```bash
php v2/scripts/test-event-api-endpoint.php https://www.ordio.com
```

**Expected Results:**

- API returns success with contactId in response data
- Contact ID is fetched correctly using search endpoint
- If contact ID not found, warning is logged but success still returned (async creation)

## Files Modified

- `v2/api/event-lead-capture.php` - Fixed contact ID extraction and fetchContactIdByEmail function
- `v2/scripts/test-event-api-endpoint.php` - New test script for endpoint testing

## Next Steps

1. Deploy fix to production
2. Test form submission
3. Verify contact appears in HubSpot
4. Check logs for contact ID warnings
5. Monitor if contacts are created asynchronously
