Files
tripz-llc/payment_server/v2/main/sms_webhook/finalize_wallet_payment.php
T
Hamza-AyedandClaude Opus 5 4d8414c96b 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>
2026-07-27 05:14:13 +03:00

113 lines
4.9 KiB
PHP
Executable File

<?php
// wallet/finalize_wallet_payment.php
// لم نعد بحاجة لـ jwtconnect.php هنا لأن الاتصال يتم تمريره من السكربت الرئيسي
if (!function_exists('logError')) {
function logError($step, $message, $data = null) {
$logFile = __DIR__ . "/../logs/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);
}
}
/**
* دالة لإنشاء توكن دفع فريد وحفظه في قاعدة البيانات.
*/
function generateToken($con, $driverId, $amount): ?string {
// افترض أن secretKey تم تعريفه في ملف jwtconnect.php
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;
}
/**
* دالة لإنشاء سجل دفعة جديد وإرجاع الـ ID الخاص به.
*/
function generatePaymentID($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;
}
/**
* دالة الإتمام الرئيسية، تم تعديلها لتستقبل ID الفاتورة مباشرة
*/
function finalizeWalletPaymentByInvoice($con, $invoiceId) {
logError("FINALIZE", "Starting finalization for invoice ID: {$invoiceId}");
try {
// 1. جلب بيانات الفاتورة المكتملة
$stmt = $con->prepare("SELECT * FROM `invoices_sms` WHERE id = :id AND status = 'completed' LIMIT 1");
$stmt->execute([':id' => $invoiceId]);
$invoice = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$invoice) {
logError("FINALIZE", "Invoice not found or not completed", ['invoiceId' => $invoiceId]);
return false;
}
$driverId = $invoice['driverID'];
$originalAmount = floatval($invoice['amount']);
$paymentMethod = $invoice['sender'] ?? 'sms';
// حساب المكافأة
$bonusAmount = match ((int)$originalAmount) {
10000 => 10000.0,
20000 => 21000.0,
40000 => 45000.0,
100000 => 110000.0,
default => $originalAmount,
};
// إنشاء التوكنات ومعرف الدفع
$tokenDriver = generateToken($con, $driverId, $bonusAmount);
$tokenSiro = generateToken($con, $driverId, $originalAmount);
$paymentID = generatePaymentID($con, $driverId, $bonusAmount, $paymentMethod);
if (!$tokenDriver || !$tokenSiro || !$paymentID) {
throw new Exception('Failed to generate required tokens or payment ID');
}
// --- تحديث المحافظ ---
// driverWallet
$insertDriver = $con->prepare("INSERT INTO driverWallet (driverID, paymentID, amount, paymentMethod) VALUES (:driverID, :paymentID, :amount, :paymentMethod)");
$insertDriver->execute([
':driverID' => $driverId, ':paymentID' => $paymentID,
':amount' => $bonusAmount, ':paymentMethod' => $paymentMethod
]);
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]);
// siroWallet
$insertSiro = $con->prepare("INSERT INTO siroWallet (driverId, passengerId, amount, paymentMethod, token) VALUES (:driverId, 'driver', :amount, :paymentMethod, :token)");
$insertSiro->execute([
':driverId' => $driverId, ':amount' => $originalAmount,
':paymentMethod' => $paymentMethod, ':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]);
logError("FINALIZE", "SUCCESS: Wallets updated successfully for invoice ID: {$invoiceId}");
return true;
} catch (Throwable $e) {
logError("FINALIZE", "EXCEPTION: " . $e->getMessage(), ['invoiceId' => $invoiceId]);
return false;
}
}
?>