Update: 2026-07-06 18:23:46

This commit is contained in:
Hamza-Ayed
2026-07-06 18:23:46 +03:00
parent 9d257c5d7d
commit d6ab09c19b
7 changed files with 505 additions and 202 deletions
+19 -1
View File
@@ -120,7 +120,25 @@ try {
// Fixed-price types, Speed & Awfar: use quoted price as-is
$fixedPriceTypes = ['Speed', 'Fixed Price', 'Awfar Car'];
if (in_array($carType, $fixedPriceTypes)) {
$finalPrice = $quotedPrice;
$finalPrice = $quotedPrice; // Fallback if Redis fails
// 🆕 Immutable Fare Lock: Force use of Redis locked price
try {
global $redis;
if ($redis) {
$lockedPrice = $redis->get("ride_locked_price_{$rideId}");
if ($lockedPrice !== false) {
$finalPrice = floatval($lockedPrice);
error_log("[finish_ride_updates] Using Redis Locked Price for ride {$rideId}: {$finalPrice}");
} else {
error_log("[finish_ride_updates] Redis Locked Price not found for ride {$rideId}. Using DB quoted price.");
}
} else {
error_log("[finish_ride_updates] Global Redis instance not available, using DB quoted price.");
}
} catch (Exception $e) {
error_log("[finish_ride_updates] Redis Error (reading locked price): " . $e->getMessage());
}
} else {
// Variable pricing: calculate from actual distance
$cleanDist = preg_replace('/[^0-9.]/', '', $actualDistance);
+39 -15
View File
@@ -36,6 +36,29 @@ try {
$stmtMainRide = $con->prepare("UPDATE ride SET status = ?, rideTimeStart = NOW() WHERE id = ?");
$stmtMainRide->execute([$status, $ride_id]);
// 🆕 Immutable Fare Lock: Save agreed fixed price in Redis
try {
$stmtRideInfo = $con->prepare("SELECT price, car_type FROM ride WHERE id = ? LIMIT 1");
$stmtRideInfo->execute([$ride_id]);
$rideInfo = $stmtRideInfo->fetch(PDO::FETCH_ASSOC);
if ($rideInfo) {
$agreedPrice = floatval($rideInfo['price']);
$carTypeStr = $rideInfo['car_type'] ?? 'Fixed Price';
$fixedPriceTypes = ['Speed', 'Fixed Price', 'Awfar Car'];
if (in_array($carTypeStr, $fixedPriceTypes)) {
global $redis;
if ($redis) {
$redis->setex("ride_locked_price_{$ride_id}", 86400, $agreedPrice);
error_log("[start_ride] Locked Fixed Price for ride {$ride_id}: {$agreedPrice}");
} else {
error_log("[start_ride] Failed to lock price: Global Redis instance not available.");
}
}
}
} catch (Exception $e) {
error_log("[start_ride] Redis Error (locking price): " . $e->getMessage());
}
// تحديث أو إدخال في جدول Driver Orders
$checkSql = "SELECT `order_id` FROM `driver_orders` WHERE `order_id` = ?";
$checkStmt = $con->prepare($checkSql);
@@ -61,21 +84,22 @@ try {
// 2.5 تصفير الدين من Redis عند بدء الرحلة (كما طلبت)
if ($passenger_id) {
try {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redisKey = "passenger_debt_" . $passenger_id;
// قراءة الدين الحالي من Redis قبل الحذف (إن لزم الأمر للتسجيل مستقبلاً)
$currentDebt = (float) $redis->get($redisKey);
// تصفير / حذف الدين
$redis->del($redisKey);
// يمكنك هنا أيضاً إدراج حركة معاكسة في جدول passengerWallet إذا أردت تسوية قاعدة البيانات
if ($currentDebt < 0) {
$positiveOffset = abs($currentDebt);
$stmtWallet = $con->prepare("INSERT INTO `passengerWallet` (passenger_id, balance) VALUES (?, ?)");
$stmtWallet->execute([$passenger_id, $positiveOffset]);
global $redis;
if ($redis) {
$redisKey = "passenger_debt_" . $passenger_id;
// قراءة الدين الحالي من Redis قبل الحذف (إن لزم الأمر للتسجيل مستقبلاً)
$currentDebt = (float) $redis->get($redisKey);
// تصفير / حذف الدين
$redis->del($redisKey);
// يمكنك هنا أيضاً إدراج حركة معاكسة في جدول passengerWallet إذا أردت تسوية قاعدة البيانات
if ($currentDebt < 0) {
$positiveOffset = abs($currentDebt);
$stmtWallet = $con->prepare("INSERT INTO `passengerWallet` (passenger_id, balance) VALUES (?, ?)");
$stmtWallet->execute([$passenger_id, $positiveOffset]);
}
}
} catch (Exception $e) {
error_log("Redis Error (zeroing debt): " . $e->getMessage());