feat: استيراد كود سيرو إلى تريبز (سيرو @ecfe7568) — بلا تعديل
قرار المالك 2026-07-27: باك إند سيرو PHP هو المعتمد، وتطبيقاته المجرّبة ميدانياً تحل محل إعادة البناء المؤرشفة. سيرو نفسه لم يُمسّ. الخريطة: backend · payment_server · loction_server · ride_server · passenger_server · docker · dashboard · stress_test → الجذر siro_rider → apps/rider siro_driver → apps/driver siro_admin → dashboards/admin siro_service → dashboards/service android_bot → apps/android_bot socialBot → apps/socialBot نُسخ المتعقَّب في git سيرو فقط عبر `git archive` (3,198 ملفاً / ~169 م.ب) لا `cp -r` — فاستُثنيت مخلفات البناء تلقائياً. بلا أي تعديل محتوى عمداً: كل ما يلي يصير فرقاً مقروءاً مقابل المصدر. لم يُستورد وسببه: siromove.com (الموقع التسويقي يبقى marketing/ في تريبز، سيرو فيه 8 ملفات) · docs و planning (تريبز له docs/ الخاص) · deploy.sh (ليس نشراً على سيرفر بل `git add . && git push origin --all` — فخّ في مستودع آخر) · transit_dashboard (بانتظار قرار مصير backend-transit و dashboards/transit-web). ⚠️ لا يبني بعد — ثلاثة نواقص متوقعة ومقصودة: 1. `.env` و `lib/env/env.g.dart` غير متعقَّبين في سيرو (أسرار لكل مستأجر): كل تطبيق فلاتر يحتاج .env خاصاً ثم توليد env.g.dart بـ build_runner. 2. إعدادات Firebase (9 ملفات google-services.json و GoogleService-Info.plist) يستبعدها .gitignore تريبز — ولكل مستأجر مشروع Firebase خاص أصلاً. 3. apps/driver في سيرو يشير إلى `../../Intaleq/packages/get` خارج المستودع → يجب ضمّ الحزم داخله أسوة بـ apps/rider. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
9909d9b4c1
commit
4d8414c96b
+157
@@ -0,0 +1,157 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user