64 lines
1.9 KiB
PHP
64 lines
1.9 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
sendJsonResponse(['error' => 'Invalid request method'], 405);
|
|
exit;
|
|
}
|
|
|
|
if (!validateApiKey()) {
|
|
logWebhook('call-tracking', file_get_contents('php://input'), 401);
|
|
sendJsonResponse(['error' => 'Unauthorized'], 401);
|
|
exit;
|
|
}
|
|
|
|
$request_body = file_get_contents('php://input');
|
|
$data = json_decode($request_body, true);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
logWebhook('call-tracking', $request_body, 400);
|
|
sendJsonResponse(['error' => 'Invalid JSON'], 400);
|
|
exit;
|
|
}
|
|
|
|
$errors = [];
|
|
if (empty($data['external_call_id'])) {
|
|
$errors[] = 'external_call_id is required';
|
|
}
|
|
if (empty($data['tracking_platform'])) {
|
|
$errors[] = 'tracking_platform is required';
|
|
}
|
|
if (empty($data['call_start_time'])) {
|
|
$errors[] = 'call_start_time is required';
|
|
}
|
|
if (empty($data['call_status'])) {
|
|
$errors[] = 'call_status is required';
|
|
}
|
|
if (empty($data['traffic_source'])) {
|
|
$errors[] = 'traffic_source is required';
|
|
}
|
|
|
|
|
|
if (!empty($errors)) {
|
|
logWebhook('call-tracking', $request_body, 422);
|
|
sendJsonResponse(['errors' => $errors], 422);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$stmt = db()->prepare("INSERT INTO call_tracking (external_call_id, tracking_platform, call_start_time, call_status, traffic_source) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([
|
|
$data['external_call_id'],
|
|
$data['tracking_platform'],
|
|
$data['call_start_time'],
|
|
$data['call_status'],
|
|
$data['traffic_source']
|
|
]);
|
|
$new_id = db()->lastInsertId();
|
|
logWebhook('call-tracking', $request_body, 201);
|
|
sendJsonResponse(['success' => true, 'id' => $new_id, 'message' => 'Call tracking created'], 201);
|
|
} catch (PDOException $e) {
|
|
error_log($e->getMessage());
|
|
logWebhook('call-tracking', $request_body, 500);
|
|
sendJsonResponse(['error' => 'Database error'], 500);
|
|
} |