نسخة كاملة من مستودع سيرو عند ecfe7568 لتكون أساس تطبيق «انطلق». نُسخ المتعقَّب في git فقط (12,509 ملفاً / 302 م.ب) بـ git archive، لا `cp -r` — فاستُثنيت تلقائياً مخلفات البناء (build · node_modules · .dart_tool · .gradle · Pods ≈ 10.7 غ.ب) وكل ما يستثنيه .gitignore. هذا الكوميت **بلا أي تعديل عمداً** حتى يكون كل ما يليه فرقاً مقروءاً مقابل سيرو الأصلي. سيرو نفسه لم يُمسّ. ⚠️ لا يبني بعد: `.env` و`lib/env/env.g.dart` غير متعقَّبين في سيرو (وهذا صحيح — أسرار لكل مستأجر). كل تطبيق فلاتر هنا يحتاج .env خاصاً بانطلق ثم توليد env.g.dart عبر build_runner. لا تُنسخ أسرار سيرو. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
158 lines
8.0 KiB
PHP
Executable File
158 lines
8.0 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) {
|
|
global $encryptionHelper;
|
|
logPayoutError("START", "Starting finalization for payout ID: {$payoutId}");
|
|
|
|
$inTransaction = false;
|
|
|
|
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'];
|
|
// Fetch driver site to calculate dynamic fee
|
|
$stmtDriver = $con->prepare("SELECT site FROM driver WHERE id = :id");
|
|
$stmtDriver->execute([':id' => $driverId]);
|
|
$driverInfo = $stmtDriver->fetch(PDO::FETCH_ASSOC);
|
|
$site = $encryptionHelper->decryptData($driverInfo['site'] ?? '');
|
|
|
|
$payoutFee = 0.0;
|
|
if (strtolower($site) === 'syria') {
|
|
$payoutFee = 35.00;
|
|
} elseif (strtolower($site) === 'egypt') {
|
|
$payoutFee = 10.00;
|
|
} elseif (strtolower($site) === 'jordan') {
|
|
$payoutFee = 0.00;
|
|
} else {
|
|
$payoutFee = 35.00;
|
|
}
|
|
|
|
$netAmount = (float)$payout['amount']; // The amount requested
|
|
$totalDeducted = $netAmount + $payoutFee; // Total deducted
|
|
|
|
// --- S4 Fix: كل الكتابات (2–6) في معاملة واحدة، مع تراجع كامل عند أي فشل ---
|
|
$con->beginTransaction();
|
|
$inTransaction = true;
|
|
|
|
// 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. Update the reserved deduction in driverWallet with the real paymentID
|
|
$updateDriver = $con->prepare("UPDATE driverWallet SET paymentID = :paymentID, paymentMethod = 'payout' WHERE driverID = :driverID AND paymentMethod = 'payout_reserved' AND amount = :amount LIMIT 1");
|
|
$updateDriver->execute([
|
|
':driverID' => $driverId,
|
|
':paymentID' => $paymentID,
|
|
':amount' => -$totalDeducted
|
|
]);
|
|
if ($updateDriver->rowCount() === 0) throw new Exception('No matching reserved driverWallet row to finalize');
|
|
|
|
$con->prepare("UPDATE payment_tokens SET isUsed = TRUE WHERE token = :token")->execute([':token' => $tokenDriver]);
|
|
logPayoutError("DRIVER_WALLET", "Updated reserved transaction of {$totalDeducted} 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());
|
|
|
|
$con->commit();
|
|
$inTransaction = false;
|
|
|
|
logPayoutError("SUCCESS", "Payout finalization completed successfully for payout ID: {$payoutId}");
|
|
return true;
|
|
|
|
} catch (Throwable $e) {
|
|
if ($inTransaction && $con->inTransaction()) {
|
|
$con->rollBack();
|
|
}
|
|
logPayoutError("EXCEPTION", "An exception occurred: " . $e->getMessage(), ['payoutId' => $payoutId]);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|