File: /home/storage/5/78/dd/wicomm2/public_html/clientes/mundoverde/netdeal.php
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
header('Access-Control-Allow-Credentials: true');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(204);
exit;
}
// Read raw JSON input if present
$rawInput = file_get_contents("php://input");
if (!empty($rawInput)) {
$inputData = json_decode($rawInput, true);
if (json_last_error() === JSON_ERROR_NONE && is_array($inputData)) {
$_POST = array_merge($_POST, $inputData);
}
}
$id = $_POST['identifier'] ?? ($_POST['id'] ?? '');
$status = $_POST['status'] ?? 'ACTIVE';
$email = $_POST['customer']['email'] ?? ($_POST['email'] ?? '');
$nome = $_POST['customer']['name'] ?? ($_POST['nome'] ?? '');
$telefone = $_POST['customer']['cellphone'] ?? ($_POST['telefone'] ?? '');
$cpf = $_POST['customer']['cpf'] ?? ($_POST['cpf'] ?? '');
$createdIn = $_POST['start_date'] ?? ($_POST['createdIn'] ?? date('Y-m-d'));
$dueDate = $_POST['due_date'] ?? ($_POST['dueDate'] ?? date('Y-m-d', strtotime("+30 days", strtotime($createdIn))));
$cancellationDate = $_POST['cancellation_date'] ?? ($_POST['cancellationDate'] ?? "");
$productId = $_POST['product']['id'] ?? ($_POST['productId'] ?? "1");
$productName = $_POST['product']['name'] ?? ($_POST['productName'] ?? "Mundo Verde");
$productPlanId = $_POST['product_plan']['id'] ?? ($_POST['productPlanId'] ?? "1");
$productPlanName = $_POST['product_plan']['name'] ?? ($_POST['productPlanName'] ?? "MUNDO VERDE 30D FREE");
$productPlanPrice = $_POST['product_plan']['price'] ?? ($_POST['productPlanPrice'] ?? 0.00);
$productPlanFrequency = $_POST['product_plan']['frequency'] ?? ($_POST['productPlanFrequency'] ?? "MONTHLY");
$createdTs = strtotime($createdIn);
if ($createdTs === false) { $createdTs = time(); }
$startIso = gmdate('Y-m-d\TH:i:s\Z', $createdTs);
$dueIso = gmdate('Y-m-d\TH:i:s\Z', strtotime('+30 days', $createdTs));
// Create logs directory if not exists
$logDir = __DIR__ . '/logs';
if (!is_dir($logDir)) {
mkdir($logDir, 0777, true);
}
// Generate log filename
$logFile = $logDir . '/netdeal_' . date('Y-m-d_H-i-s') . '.txt';
// Prepare log content
$logContent = "==== Netdeal API Request Log ====\n";
$logContent .= "Timestamp: " . date('Y-m-d H:i:s') . "\n\n";
$logContent .= "--- Raw Input ---\n" . $rawInput . "\n\n";
$logContent .= "--- Parsed POST ---\n" . print_r($_POST, true) . "\n\n";
$logContent .= "--- Payload Sent to Netdeal ---\n" . print_r($data, true) . "\n\n";
// Write initial log before sending
file_put_contents($logFile, $logContent, FILE_APPEND);
$data = [
"identifier" => $id,
"status" => "ACTIVE",
"start_date" => $startIso,
"due_date" => $dueIso,
"cancellation_date" => $cancellationDate,
"product" => [
"id" => $productId,
"name" => $productName
],
"product_plan" => [
"id" => $productPlanId,
"name" => $productPlanName,
"price" => (float)$productPlanPrice,
"frequency" => $productPlanFrequency
],
"customer" => [
"email" => $email,
"name" => $nome,
"cellphone" => $telefone,
"cpf" => $cpf // mantém com formatação
]
];
$url = "https://www.netdeal.com.br/open/integration/netdeal/D81429450760DFDFB/webhook/subscription/sync";
$headers = [
"Content-Type: application/json",
"Accept: application/json",
"Api-Token: $2a$10$0DpxoMuEWrV9iY.4R6sjO.qYh9Ti4UIgvtmUTOJRzIx8r6WqYK8Ci"
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Append API response to the log
$logContent = "--- API Response ---\n";
$logContent .= "HTTP Code: " . $httpcode . "\n";
$logContent .= "Response: " . $response . "\n";
file_put_contents($logFile, $logContent, FILE_APPEND);
curl_close($ch);
header('Content-Type: application/json');
echo json_encode([
"status" => $httpcode,
"response" => json_decode($response, true)
]);
?>