![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/private_html/ |
<?php
/**
* Purchase Failure Monitor
*
* Monitors logs for purchase failures and sends alerts
* Can be run via cron or manually
*/
require_once 'config/database.php';
// Check if CLI or start session for web access
$is_cli = php_sapi_name() === 'cli';
if (!$is_cli) {
session_start();
}
// Check if admin (for manual runs)
$is_admin = isset($_SESSION['is_admin']) && $_SESSION['is_admin'];
if (!$is_cli && !$is_admin) {
die("Admin access required");
}
try {
$pdo = getDBConnection();
} catch (Exception $e) {
if (!$is_cli) {
die("Database connection error: " . htmlspecialchars($e->getMessage()));
} else {
die("Database connection error: " . $e->getMessage());
}
}
$log_dir = __DIR__ . '/logs';
if (!$is_cli) {
echo "<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Purchase Failure Monitor</title></head><body>";
echo "<style>body { font-family: Arial; padding: 20px; background: #1a1a1a; color: white; } .error { color: #e53e3e; } .warning { color: #ffc107; } .success { color: #48bb78; }</style>";
echo "<h2>🔔 Purchase Failure Monitor</h2>";
}
// Check various error logs
$alerts = [];
// 1. Check purchase failure alerts
$alert_file = $log_dir . '/purchase_failure_alerts.log';
if (file_exists($alert_file) && is_readable($alert_file)) {
$lines = @file($alert_file);
if ($lines !== false) {
$recent_alerts = [];
foreach (array_slice($lines, -50) as $line) { // Last 50 lines
$line = trim($line);
if (empty($line)) continue;
$alert = json_decode($line, true);
if ($alert && isset($alert['timestamp'])) {
$alert_time = strtotime($alert['timestamp']);
// Check last 24 hours
if ($alert_time > (time() - 86400)) {
$recent_alerts[] = $alert;
}
}
}
if (!empty($recent_alerts)) {
$alerts[] = [
'type' => 'purchase_failures',
'count' => count($recent_alerts),
'severity' => 'HIGH',
'alerts' => $recent_alerts
];
}
}
}
// 2. Check track purchase errors
$error_file = $log_dir . '/track_purchase_errors.log';
if (file_exists($error_file) && is_readable($error_file)) {
$lines = @file($error_file);
if ($lines !== false) {
$recent_errors = [];
foreach (array_slice($lines, -100) as $line) {
$line = trim($line);
if (empty($line)) continue;
$error = json_decode($line, true);
if ($error && isset($error['timestamp'])) {
$error_time = strtotime($error['timestamp']);
if ($error_time > (time() - 86400)) {
$recent_errors[] = $error;
}
}
}
if (!empty($recent_errors)) {
$alerts[] = [
'type' => 'track_purchase_errors',
'count' => count($recent_errors),
'severity' => 'MEDIUM',
'errors' => $recent_errors
];
}
}
}
// 3. Check mixed cart item errors
$cart_error_file = $log_dir . '/mixed_cart_item_errors.log';
if (file_exists($cart_error_file) && is_readable($cart_error_file)) {
$lines = @file($cart_error_file);
if ($lines !== false) {
$recent_cart_errors = [];
foreach (array_slice($lines, -100) as $line) {
$line = trim($line);
if (empty($line)) continue;
$error = json_decode($line, true);
if ($error && isset($error['timestamp'])) {
$error_time = strtotime($error['timestamp']);
if ($error_time > (time() - 86400)) {
$recent_cart_errors[] = $error;
}
}
}
if (!empty($recent_cart_errors)) {
$alerts[] = [
'type' => 'mixed_cart_errors',
'count' => count($recent_cart_errors),
'severity' => 'HIGH',
'errors' => $recent_cart_errors
];
}
}
}
// 4. Check for payments with no purchases
try {
$stmt = $pdo->prepare("
SELECT
COUNT(*) as count,
DATE(purchase_date) as date
FROM track_purchases
WHERE stripe_payment_intent_id IS NOT NULL
AND purchase_date > DATE_SUB(NOW(), INTERVAL 7 DAY)
GROUP BY DATE(purchase_date)
ORDER BY date DESC
");
$stmt->execute();
$purchase_stats = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
$purchase_stats = [];
if (!$is_cli) {
echo "<p class='error'>Error fetching purchase stats: " . htmlspecialchars($e->getMessage()) . "</p>";
}
}
// Display results
if (!$is_cli) {
echo "<div style='padding: 20px; background: #2a2a2a; border-radius: 8px; margin: 20px 0;'>";
echo "<h3>📊 Recent Purchase Statistics (Last 7 Days)</h3>";
if (!empty($purchase_stats)) {
echo "<table style='width: 100%; border-collapse: collapse;'>";
echo "<tr><th style='padding: 10px; border: 1px solid #444;'>Date</th><th style='padding: 10px; border: 1px solid #444;'>Purchases</th></tr>";
foreach ($purchase_stats as $stat) {
echo "<tr><td style='padding: 10px; border: 1px solid #444;'>{$stat['date']}</td><td style='padding: 10px; border: 1px solid #444;'>{$stat['count']}</td></tr>";
}
echo "</table>";
} else {
echo "<p class='warning'>No purchases found in last 7 days</p>";
}
echo "</div>";
if (!empty($alerts)) {
echo "<div style='padding: 20px; background: #3a1a1a; border-radius: 8px; margin: 20px 0; border-left: 4px solid #e53e3e;'>";
echo "<h3 class='error'>⚠️ ACTIVE ALERTS</h3>";
foreach ($alerts as $alert) {
echo "<div style='margin: 15px 0; padding: 15px; background: #2a1a1a; border-radius: 5px;'>";
echo "<h4>{$alert['type']} - {$alert['count']} issue(s) - Severity: {$alert['severity']}</h4>";
if (isset($alert['alerts'])) {
echo "<p>Recent alerts:</p><ul>";
foreach (array_slice($alert['alerts'], 0, 5) as $item) {
echo "<li>" . htmlspecialchars($item['timestamp'] ?? 'unknown') . " - " . htmlspecialchars($item['payment_intent_id'] ?? 'N/A') . "</li>";
}
echo "</ul>";
}
if (isset($alert['errors'])) {
echo "<p>Recent errors:</p><ul>";
foreach (array_slice($alert['errors'], 0, 5) as $item) {
echo "<li>" . htmlspecialchars($item['timestamp'] ?? 'unknown') . " - " . htmlspecialchars($item['error'] ?? 'N/A') . "</li>";
}
echo "</ul>";
}
echo "</div>";
}
echo "</div>";
} else {
echo "<div style='padding: 20px; background: #2d5016; border-radius: 8px; margin: 20px 0;'>";
echo "<p class='success'><strong>✅ No active alerts</strong></p>";
echo "</div>";
}
echo "<hr>";
echo "<p><a href='/admin.php' style='color: #667eea;'>← Back to Admin</a> | ";
echo "<a href='/reconcile_stripe_purchases.php' style='color: #667eea;'>Run Reconciliation</a></p>";
echo "</body></html>";
} else {
// CLI mode - output JSON for cron
echo json_encode([
'timestamp' => date('Y-m-d H:i:s'),
'alerts_count' => count($alerts),
'alerts' => $alerts,
'purchase_stats' => $purchase_stats
]);
}
// Log the monitoring run
$monitor_log = [
'timestamp' => date('Y-m-d H:i:s'),
'action' => 'monitor_run',
'alerts_found' => count($alerts),
'alerts' => $alerts
];
$monitor_log_file = $log_dir . '/monitor_runs.log';
if (!is_dir($log_dir)) {
mkdir($log_dir, 0755, true);
}
file_put_contents($monitor_log_file, json_encode($monitor_log) . "\n", FILE_APPEND | LOCK_EX);
?>