60 lines
2.2 KiB
PHP
60 lines
2.2 KiB
PHP
<?php
|
|
// This script is designed to be run from the command line by a cron job.
|
|
// e.g., 0 1 * * * /usr/bin/php /path/to/your/project/cron/process_passive_income.php
|
|
|
|
require_once dirname(__DIR__) . '/db/config.php';
|
|
|
|
echo "Starting passive income processing...\n";
|
|
|
|
$db = db();
|
|
$db->beginTransaction();
|
|
|
|
try {
|
|
// 1. Find pending payments
|
|
$stmt = $db->prepare("SELECT * FROM passive_income_schedule WHERE status = 'pending' AND payment_date <= CURDATE()");
|
|
$stmt->execute();
|
|
$pending_payments = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (empty($pending_payments)) {
|
|
echo "No pending passive income payments to process.\n";
|
|
$db->commit();
|
|
exit;
|
|
}
|
|
|
|
echo "Found " . count($pending_payments) . " pending payment(s).\n";
|
|
|
|
foreach ($pending_payments as $payment) {
|
|
$userId = $payment['user_id'];
|
|
$amount = $payment['amount'];
|
|
$scheduleId = $payment['id'];
|
|
|
|
// 2. Insert into transactions
|
|
$trans_stmt = $db->prepare(
|
|
"INSERT INTO transactions (user_id, amount, type, description, related_user_id) VALUES (:user_id, :amount, 'passive_income', :description, NULL)"
|
|
);
|
|
$trans_stmt->execute([
|
|
':user_id' => $userId,
|
|
':amount' => $amount,
|
|
':description' => 'Monthly passive income payment from schedule #' . $scheduleId
|
|
]);
|
|
|
|
// 3. Update user's wallet
|
|
$user_stmt = $db->prepare("UPDATE users SET wallet_balance = wallet_balance + :amount, total_passive_income = total_passive_income + :amount WHERE id = :user_id");
|
|
$user_stmt->execute([':amount' => $amount, ':user_id' => $userId]);
|
|
|
|
// 4. Update schedule status to 'paid'
|
|
$schedule_stmt = $db->prepare("UPDATE passive_income_schedule SET status = 'paid' WHERE id = :id");
|
|
$schedule_stmt->execute([':id' => $scheduleId]);
|
|
|
|
echo "Processed payment for user #$userId (Amount: $amount).\n";
|
|
}
|
|
|
|
$db->commit();
|
|
echo "Passive income processing finished successfully.\n";
|
|
|
|
} catch (Exception $e) {
|
|
$db->rollBack();
|
|
echo "Error processing passive income: " . $e->getMessage() . "\n";
|
|
// It's crucial to log this error to a file in a real-world scenario
|
|
error_log("Passive Income Cron Failed: " . $e->getMessage());
|
|
} |