# HubSpot Integration Setup Status

**Last Updated:** 2026-02-12  
**Status:** ✅ Setup Complete - Integration Working

## Important: Lead vs Deal Attribution

**Partners refer Ordio users (leads/customers), not partner dashboard users.**

- **Leads (Contacts):** Automatically tracked when users submit forms
- **Deals:** Created by sales team; sales must manually set affiliate properties for MRR tracking

See [HUBSPOT_SALES_WORKFLOW.md](HUBSPOT_SALES_WORKFLOW.md) for sales team guide.

## Current Status

### ✅ Setup Complete

- ✅ API token is configured and valid
- ✅ All required scopes are present and verified
- ✅ Custom object `affiliate_partner` created (Object Type ID: `2-197867933`)
- ✅ All contact properties created (`affiliate_partner_id`, `affiliate_referral_date`)
- ✅ All deal properties created (`affiliate_partner_id`, `affiliate_mrr`, `affiliate_subscription_status`)
- ✅ Property group `affiliate_info` exists for contacts
- ✅ Integration tested and verified working

### ⚠️ Notes

- Deal properties created in default group (can be moved to `affiliate_info` group in HubSpot UI if desired)
- HubSpot search indexing is asynchronous - newly created records may not appear immediately (expected behavior)

## Verification Results

### Comprehensive Verification: ✅ PASSED

**Command:** `php v2/scripts/hubspot/verify-affiliate-setup.php`

**Results:**

- ✅ API Token: Valid and connected
- ✅ Custom Object: Exists with all properties
- ✅ Contact Properties: All exist
- ✅ Deal Properties: All exist
- ✅ Scopes: All required scopes present
- ✅ End-to-End Test: Partner creation/read/delete works

**Overall Status:** PASS (15/15 checks passed)

## Integration Tests

### Partner Registration Flow: ✅ PASSED

**Command:** `php v2/scripts/hubspot/test-partner-registration.php`

- ✅ Partner creation in HubSpot works
- ✅ All properties set correctly
- ✅ Object appears in HubSpot

### Sync Process: ✅ PASSED

**Command:** `php v2/cron/sync-affiliate-hubspot.php`

- ✅ Sync completes successfully
- ✅ Fetches partner data from HubSpot
- ✅ Searches for contacts and deals
- ✅ Calculates MRR
- ✅ Updates cache file

### Lead Attribution: ✅ PASSED

**Command:** `php v2/scripts/hubspot/test-lead-attribution.php`

- ✅ Contact creation with affiliate properties works
- ✅ Deal creation with affiliate properties works
- ✅ Properties are set correctly
- ⚠️ Search indexing is asynchronous (expected behavior)

## Configuration

### Object Type ID

**Updated:** `v2/config/affiliate-config.php`

```php
define('HUBSPOT_AFFILIATE_OBJECT_TYPE', '2-197867933');
```

## Code Fixes Applied

### 1. Date Format Fix

HubSpot date properties require `YYYY-MM-DD` format (midnight UTC).

**Fixed in:**

- `v2/helpers/hubspot-affiliate-api.php` - Added `formatHubSpotDate()` helper
- `v2/api/lead-capture.php` - Updated date format
- `v2/scripts/hubspot/test-affiliate-integration.php` - Updated date format

### 2. Property Creation Endpoint Fix

Changed endpoint from `/crm/v3/properties/{objectType}/{propertyName}` to `/crm/v3/properties/{objectType}`.

**Fixed in:** `v2/scripts/hubspot/setup-affiliate-properties.php`

### 3. Partner Search Function Fix

Updated to search by `partner_id` property directly.

**Fixed in:** `v2/helpers/hubspot-affiliate-api.php`

### 4. Deal Stage ID Fix

Updated to use actual pipeline stage ID instead of string.

**Fixed in:** `v2/scripts/hubspot/test-affiliate-integration.php`

## Production Readiness

### ✅ Ready for Production

All core functionality is working:

- Partner registration creates HubSpot objects ✅
- Contact/deal creation with affiliate properties ✅
- Sync process fetches and processes data ✅
- MRR calculation works ✅
- Level calculation works ✅

### Recommended Next Steps

1. **Set up hourly sync cron job:**

   ```bash
   0 * * * * php /path/to/v2/cron/sync-affiliate-hubspot.php
   ```

2. **Monitor sync health:**

   ```bash
   php v2/scripts/affiliate/monitor-sync-health.php
   ```

3. **Optional: Move deal properties to group:**
   - Go to HubSpot Settings → Properties → Deal Properties
   - Edit each affiliate property
   - Move to `affiliate_info` group (if group exists for deals)

4. **Test real registration:**
   - Register a test partner via registration form
   - Verify HubSpot object is created
   - Check properties are set correctly

## Verification Commands

```bash
# Full verification
php v2/scripts/hubspot/verify-affiliate-setup.php

# Test scopes
php v2/scripts/hubspot/test-api-token-scopes.php

# Test registration flow
php v2/scripts/hubspot/test-partner-registration.php

# Test sync process
php v2/cron/sync-affiliate-hubspot.php

# Test lead attribution
php v2/scripts/hubspot/test-lead-attribution.php

# End-to-end integration test
php v2/scripts/hubspot/test-affiliate-integration.php
```

## Related Documentation

- [HUBSPOT_SETUP.md](HUBSPOT_SETUP.md) - Complete setup guide
- [HUBSPOT_INTEGRATION_SUMMARY.md](HUBSPOT_INTEGRATION_SUMMARY.md) - Integration summary
- [ARCHITECTURE.md](ARCHITECTURE.md) - System architecture
- [DEPLOYMENT_CHECKLIST.md](DEPLOYMENT_CHECKLIST.md) - Deployment checklist
