<?php
// ----- DB CONNECTION -----
$host = 'localhost';
$dbname = 'demoptma_publications';
$username = 'demoptma_publications';
$password = 'dj$JN)hJerjG';

try {
    $pdo = new PDO(
        "mysql:host=$host;dbname=$dbname;charset=utf8mb4",
        $username,
        $password,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ]
    );
} catch (PDOException $e) {
    die("DB Error: " . $e->getMessage());
}

// ----- GET INVOICE ID -----
$invoiceId = $_GET['invoice'] ?? null;

if (!$invoiceId) {
    die("Invoice missing");
}

// ----- UPDATE STATUS TO PAID (only if not already) -----
$stmt = $pdo->prepare("
    UPDATE invoices 
    SET payment_status = 'paid' 
    WHERE id = ?");
$stmt->execute([$invoiceId]);

// ----- REDIRECT BACK TO INVOICE PAGE -----
header("Location: https://invoices.solvingpublishing.com/checkout.php?reg=" . urlencode($invoiceId));
exit;
