requireAuth(); // Initialize logger $logger = new ApplicationLogger(); // Check if this is a POST request if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode([ 'success' => false, 'error' => 'Method not allowed' ]); exit; } // Get request body $input = json_decode(file_get_contents('php://input'), true); // Validate required fields $requiredFields = ['boxId', 'supplyDate', 'liveDate', 'endDate', 'boxData']; foreach ($requiredFields as $field) { if (!isset($input[$field]) || empty($input[$field])) { http_response_code(400); echo json_encode([ 'success' => false, 'error' => "Missing required field: $field" ]); exit; } } // Load configuration $config = require __DIR__ . '/config.php'; $webhookConfig = $config['webhook']; // Prepare webhook payload $payload = [ 'userEmail' => $user['email'], 'userName' => $user['name'], 'boxId' => $input['boxId'], 'masterCampaignNumber' => $input['boxData']['masterCampaignNumber'] ?? 'N/A', 'masterCampaignId' => $input['boxData']['masterCampaignId'] ?? null, 'supplyDate' => $input['supplyDate'], 'liveDate' => $input['liveDate'], 'endDate' => $input['endDate'], 'boxContents' => [ 'folderName' => $input['boxData']['folderName'] ?? '', 'totalItems' => $input['boxData']['contents']['total'] ?? 0, 'folders' => $input['boxData']['contents']['folders'] ?? [], 'files' => $input['boxData']['contents']['files'] ?? [] ], 'submittedAt' => date('Y-m-d H:i:s') ]; // Send to webhook // Make.com webhooks typically don't need API key headers // The URL itself is the authentication $ch = curl_init($webhookConfig['url']); $jsonPayload = json_encode($payload); error_log('Sending to webhook: ' . $webhookConfig['url']); error_log('Payload: ' . $jsonPayload); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $jsonPayload, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $webhookConfig['timeout'], CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'x-make-apikey: ' . $webhookConfig['api_key'] ] ]); $webhookResponse = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); curl_close($ch); if ($curlError) { throw new Exception('Webhook request failed: ' . $curlError); } // Parse webhook response $webhookData = json_decode($webhookResponse, true); // Check if webhook returned success if ($httpCode >= 200 && $httpCode < 300) { // Log successful submission $logger->logAssetSubmission( $user, $input['boxId'], $input['boxData']['masterCampaignNumber'] ?? 'N/A', [ 'supply' => $input['supplyDate'], 'live' => $input['liveDate'], 'end' => $input['endDate'] ], 'success' ); // Webhook accepted the data echo json_encode([ 'success' => true, 'message' => 'Submission successful', 'webhookResponse' => $webhookData, 'webhookStatus' => $httpCode ]); } else { // Log failed submission $logger->logAssetSubmission( $user, $input['boxId'], $input['boxData']['masterCampaignNumber'] ?? 'N/A', [ 'supply' => $input['supplyDate'], 'live' => $input['liveDate'], 'end' => $input['endDate'] ], 'error', 'Webhook returned status ' . $httpCode ); // Webhook rejected or failed - return 200 to client but indicate webhook failure error_log('Webhook failed with status: ' . $httpCode); error_log('Webhook response: ' . $webhookResponse); echo json_encode([ 'success' => false, 'error' => 'Webhook processing failed', 'webhookResponse' => $webhookData ?? $webhookResponse, 'webhookStatus' => $httpCode, 'message' => 'The webhook returned status ' . $httpCode . '. Please check the webhook configuration.' ]); } } catch (Exception $e) { // Log error error_log('Submission error: ' . $e->getMessage()); error_log('Stack trace: ' . $e->getTraceAsString()); http_response_code(500); echo json_encode([ 'success' => false, 'error' => 'Server error occurred during submission', 'debug' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine() ]); }