![]() 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
/**
* Automatic Purchase Reconciliation
* Runs automatically via cron to detect and alert on purchase discrepancies
*
* Cron setup: Run every hour
* 0 * * * * /usr/bin/php /home/gositeme/domains/soundstudiopro.com/public_html/auto_reconcile_purchases.php
*/
require_once 'config/database.php';
$pdo = getDBConnection();
$stripe_secret = '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';
require_once __DIR__ . '/webhooks/purchase_validation.php';
// Get recent purchases (last 24 hours)
$stmt = $pdo->prepare("
SELECT DISTINCT stripe_payment_intent_id, user_id
FROM track_purchases
WHERE stripe_payment_intent_id IS NOT NULL
AND purchase_date > DATE_SUB(NOW(), INTERVAL 24 HOUR)
ORDER BY purchase_date DESC
LIMIT 100
");
$stmt->execute();
$recent_purchases = $stmt->fetchAll(PDO::FETCH_ASSOC);
$discrepancies = [];
foreach ($recent_purchases as $purchase) {
$payment_intent_id = $purchase['stripe_payment_intent_id'];
$user_id = $purchase['user_id'];
$validation = validatePurchase($payment_intent_id, $user_id);
if (!$validation['valid']) {
$discrepancies[] = [
'payment_intent_id' => $payment_intent_id,
'user_id' => $user_id,
'issues' => $validation['issues'],
'expected' => $validation['expected'] ?? [],
'actual' => $validation['actual'] ?? []
];
}
}
// Log results
$log_entry = [
'timestamp' => date('Y-m-d H:i:s'),
'action' => 'auto_reconciliation_run',
'purchases_checked' => count($recent_purchases),
'discrepancies_found' => count($discrepancies),
'discrepancies' => $discrepancies
];
$log_file = __DIR__ . '/logs/auto_reconciliation.log';
if (!is_dir(__DIR__ . '/logs')) {
mkdir(__DIR__ . '/logs', 0755, true);
}
file_put_contents($log_file, json_encode($log_entry) . "\n", FILE_APPEND | LOCK_EX);
// If discrepancies found, create alert
if (!empty($discrepancies)) {
$alert_log = [
'timestamp' => date('Y-m-d H:i:s'),
'action' => 'ALERT_auto_reconciliation_discrepancies',
'discrepancies_count' => count($discrepancies),
'discrepancies' => $discrepancies,
'severity' => 'HIGH'
];
$alert_file = __DIR__ . '/logs/purchase_failure_alerts.log';
file_put_contents($alert_file, json_encode($alert_log) . "\n", FILE_APPEND | LOCK_EX);
// Output for cron logging
echo "ALERT: Found " . count($discrepancies) . " purchase discrepancy(ies) in last 24 hours\n";
foreach ($discrepancies as $disc) {
echo " - Payment Intent: {$disc['payment_intent_id']}, User: {$disc['user_id']}, Issues: " . implode(', ', $disc['issues']) . "\n";
}
} else {
echo "OK: All purchases validated successfully (" . count($recent_purchases) . " checked)\n";
}
?>