37243-vm/api/call-tracking.php
2026-01-11 01:28:40 +00:00

69 lines
2.9 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
check_api_key();
$request_method = $_SERVER['REQUEST_METHOD'];
if ($request_method === 'POST') {
$data = json_decode(file_get_contents('php://input'), true);
// --- Validation ---
$required_fields = ['external_call_id', 'call_start_time'];
foreach ($required_fields as $field) {
if (empty($data[$field])) {
log_and_exit(400, "Missing required field: {$field}");
}
}
try {
$pdo = db();
$stmt = $pdo->prepare("
INSERT INTO call_tracking (
external_call_id, tracking_platform, caller_number, tracking_number,
call_start_time, call_end_time, call_duration_seconds, call_status,
answered_by, traffic_source, campaign_name, recording_url,
was_ai_rescue, attributed_revenue, caller_name, caller_city, caller_state
) VALUES (
:external_call_id, :tracking_platform, :caller_number, :tracking_number,
:call_start_time, :call_end_time, :call_duration_seconds, :call_status,
:answered_by, :traffic_source, :campaign_name, :recording_url,
:was_ai_rescue, :attributed_revenue, :caller_name, :caller_city, :caller_state
)
");
$stmt->execute([
':external_call_id' => $data['external_call_id'],
':tracking_platform' => $data['tracking_platform'] ?? null,
':caller_number' => $data['caller_number'] ?? null,
':tracking_number' => $data['tracking_number'] ?? null,
':call_start_time' => $data['call_start_time'],
':call_end_time' => $data['call_end_time'] ?? null,
':call_duration_seconds' => $data['call_duration_seconds'] ?? null,
':call_status' => $data['call_status'] ?? null,
':answered_by' => $data['answered_by'] ?? null,
':traffic_source' => $data['traffic_source'] ?? null,
':campaign_name' => $data['campaign_name'] ?? null,
':recording_url' => $data['recording_url'] ?? null,
':was_ai_rescue' => $data['was_ai_rescue'] ?? 0,
':attributed_revenue' => $data['attributed_revenue'] ?? null,
':caller_name' => $data['caller_name'] ?? null,
':caller_city' => $data['caller_city'] ?? null,
':caller_state' => $data['caller_state'] ?? null,
]);
header('Content-Type: application/json');
echo json_encode(['success' => true, 'message' => 'Call tracked successfully.', 'id' => $pdo->lastInsertId()]);
} catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // Duplicate entry
log_and_exit(409, "Conflict: A call with the same external_call_id already exists.");
} else {
log_and_exit(500, "Database error: " . $e->getMessage());
}
}
} else {
log_and_exit(405, "Method Not Allowed");
}