128 lines
6.9 KiB
PHP
Executable File
128 lines
6.9 KiB
PHP
Executable File
<?php
|
|
// finalize_payout.php
|
|
|
|
if (!function_exists('logPayoutError')) {
|
|
/**
|
|
* دالة مخصصة لتسجيل الأخطاء والخطوات في ملف سجل خاص بعمليات السحب.
|
|
*/
|
|
function logPayoutError($step, $message, $data = null) {
|
|
$logFile = __DIR__ . "/../logs/payout_finalization.log";
|
|
$logDir = dirname($logFile);
|
|
if (!is_dir($logDir)) { mkdir($logDir, 0755, true); }
|
|
$logEntry = "[" . date('Y-m-d H:i:s') . "] STEP {$step}: {$message}";
|
|
if ($data !== null) { $logEntry .= " | Data: " . json_encode($data, JSON_UNESCAPED_UNICODE); }
|
|
file_put_contents($logFile, $logEntry . PHP_EOL, FILE_APPEND);
|
|
}
|
|
}
|
|
|
|
if (!function_exists('generateToken')) {
|
|
/**
|
|
* دالة لإنشاء توكن دفع فريد وحفظه في قاعدة البيانات.
|
|
*/
|
|
function generateToken(PDO $con, $driverId, $amount): ?string {
|
|
global $secretKey;
|
|
$data = $driverId . $amount . time() . ($secretKey ?? 'default_secret_for_token');
|
|
$hash = hash('sha256', $data);
|
|
$randomBytes = bin2hex(random_bytes(16));
|
|
$token = substr($hash . $randomBytes, 0, 64);
|
|
|
|
$stmt = $con->prepare("INSERT INTO payment_tokens (token, driverID, dateCreated, amount) VALUES (:token, :driverID, NOW(), :amount)");
|
|
$stmt->execute([':token' => $token, ':driverID' => $driverId, ':amount' => $amount]);
|
|
|
|
return $stmt->rowCount() > 0 ? $token : null;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('generatePaymentID')) {
|
|
/**
|
|
* دالة لإنشاء سجل دفعة جديد وإرجاع الـ ID الخاص به.
|
|
* هذا السجل هو المرجع الأساسي للمعاملة.
|
|
*/
|
|
function generatePaymentID(PDO $con, $driverId, $amount, $method): ?string {
|
|
$stmt = $con->prepare("INSERT INTO paymentsDriverPoints (`amount`, `payment_method`, `driverID`) VALUES (:amount, :method, :driverID)");
|
|
$stmt->execute([':driverID' => $driverId, ':amount' => $amount, ':method' => $method]);
|
|
|
|
return $stmt->rowCount() > 0 ? $con->lastInsertId() : null;
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('finalizePayout')) {
|
|
/**
|
|
* دالة الإتمام الرئيسية لعمليات السحب.
|
|
* تقوم بتسجيل كل المعاملات في محافظ السائق والشركة وتحديث حالة الدفعات.
|
|
*/
|
|
function finalizePayout(PDO $con, int $payoutId) {
|
|
logPayoutError("START", "Starting finalization for payout ID: {$payoutId}");
|
|
|
|
try {
|
|
// 1. جلب بيانات طلب السحب المكتمل
|
|
$stmt = $con->prepare("SELECT * FROM `payout_requests` WHERE id = :id AND status = 'completed' LIMIT 1");
|
|
$stmt->execute([':id' => $payoutId]);
|
|
$payout = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$payout) {
|
|
logPayoutError("FETCH_PAYOUT", "Payout request not found or not completed.", ['payoutId' => $payoutId]);
|
|
return false;
|
|
}
|
|
|
|
$driverId = $payout['driver_id'];
|
|
$payoutFee = 3500;
|
|
$netAmount = (float)$payout['amount']-$payoutFee; // المبلغ الصافي الذي طلبه السائق
|
|
$totalDeducted = $netAmount; // المبلغ الإجمالي الذي سيتم خصمه
|
|
|
|
// 2. إنشاء معرف دفع رئيسي لهذه المعاملة
|
|
// نسجل المبلغ الإجمالي المخصوم بالسالب
|
|
$paymentID = generatePaymentID($con, $driverId, -$totalDeducted, 'payout');
|
|
if (!$paymentID) throw new Exception('Failed to generate master payment ID');
|
|
logPayoutError("GEN_PAYMENT_ID", "Master Payment ID created: {$paymentID}");
|
|
|
|
// 3. إنشاء التوكنات اللازمة
|
|
$tokenDriver = generateToken($con, $driverId, -$totalDeducted);
|
|
$tokenSiro = generateToken($con, $driverId, $payoutFee);
|
|
if (!$tokenDriver || !$tokenSiro) throw new Exception('Failed to generate required tokens');
|
|
logPayoutError("GEN_TOKENS", "Driver and Siro tokens generated successfully.");
|
|
|
|
// 4. تسجيل معاملة الخصم في محفظة السائق (driverWallet)
|
|
$insertDriver = $con->prepare("INSERT INTO driverWallet (driverID, paymentID, amount, paymentMethod) VALUES (:driverID, :paymentID, :amount, :paymentMethod)");
|
|
$insertDriver->execute([
|
|
':driverID' => $driverId,
|
|
':paymentID' => $paymentID,
|
|
':amount' => -$totalDeducted, // تسجيل المبلغ بالسالب
|
|
':paymentMethod' => 'payout'
|
|
]);
|
|
if ($insertDriver->rowCount() === 0) throw new Exception('Insert to driverWallet failed');
|
|
$con->prepare("UPDATE payment_tokens SET isUsed = TRUE WHERE token = :token")->execute([':token' => $tokenDriver]);
|
|
logPayoutError("DRIVER_WALLET", "Negative transaction of {$totalDeducted} recorded in driverWallet.");
|
|
|
|
// 5. تسجيل معاملة الربح (العمولة) في محفظة الشركة (siroWallet)
|
|
$insertSiro = $con->prepare("INSERT INTO siroWallet (driverId, passengerId, amount, paymentMethod, token) VALUES (:driverId, :passengerId, :amount, :paymentMethod, :token)");
|
|
$insertSiro->execute([
|
|
':driverId' => $driverId,
|
|
':passengerId' => 'payout_fee', // تمييز المعاملة كعمولة سحب
|
|
':amount' => $payoutFee, // تسجيل العمولة بالموجب
|
|
':paymentMethod' => 'payout_fee',
|
|
':token' => $tokenSiro
|
|
]);
|
|
if ($insertSiro->rowCount() === 0) throw new Exception('Insert to siroWallet failed');
|
|
$con->prepare("UPDATE payment_tokens SET isUsed = TRUE WHERE token = :token")->execute([':token' => $tokenSiro]);
|
|
logPayoutError("SIRO_WALLET", "Fee transaction of {$payoutFee} recorded in siroWallet.");
|
|
|
|
// 6. تحديث حالة الدفعات التي تم سحبها في جدول 'payments'
|
|
// هذا السطر يقوم بتحديث كل الدفعات المعلقة للسائق، معتبراً أن عملية السحب تغطيها
|
|
$updatePayments = $con->prepare("UPDATE payments SET isGiven = TRUE WHERE driverID = :driverId AND isGiven = FALSE");
|
|
$updatePayments->execute([':driverId' => $driverId]);
|
|
logPayoutError("UPDATE_PAYMENTS", "Marked pending payments as 'isGiven' for driver {$driverId}. Rows affected: " . $updatePayments->rowCount());
|
|
|
|
logPayoutError("SUCCESS", "Payout finalization completed successfully for payout ID: {$payoutId}");
|
|
return true;
|
|
|
|
} catch (Throwable $e) {
|
|
logPayoutError("EXCEPTION", "An exception occurred: " . $e->getMessage(), ['payoutId' => $payoutId]);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|