Changed from custom header to Make.com's official API key header format. Reference: https://apps.make.com/gateway 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
149 lines
4.7 KiB
PHP
149 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* Form Submission Handler
|
|
* Processes the form and sends data to webhook
|
|
*/
|
|
|
|
// Enable error reporting for debugging
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
ini_set('log_errors', 1);
|
|
|
|
// Set JSON header
|
|
header('Content-Type: application/json');
|
|
|
|
try {
|
|
// Load dependencies
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
require_once __DIR__ . '/AuthMiddleware.php';
|
|
require_once __DIR__ . '/BoxService.php';
|
|
|
|
// Check authentication
|
|
$auth = new AuthMiddleware();
|
|
|
|
// Debug: Log SSO status
|
|
error_log('SSO Enabled: ' . ($auth->isSSOEnabled() ? 'true' : 'false'));
|
|
|
|
$user = $auth->requireAuth();
|
|
|
|
// Debug: Log user
|
|
error_log('Authenticated user: ' . json_encode($user));
|
|
|
|
// 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) {
|
|
// Webhook accepted the data
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Submission successful',
|
|
'webhookResponse' => $webhookData,
|
|
'webhookStatus' => $httpCode
|
|
]);
|
|
} else {
|
|
// 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()
|
|
]);
|
|
}
|