Update: 2026-06-14 22:10:07
This commit is contained in:
@@ -18,8 +18,8 @@ if (!$rideId) {
|
||||
}
|
||||
|
||||
try {
|
||||
// جلب بيانات الرحلة للتحقق
|
||||
$stmt = $con->prepare("SELECT driver_id, status FROM ride WHERE id = ?");
|
||||
// جلب بيانات الرحلة للتحقق (مع passenger_id للإدراج في canecl)
|
||||
$stmt = $con->prepare("SELECT driver_id, passenger_id, status FROM ride WHERE id = ?");
|
||||
$stmt->execute([$rideId]);
|
||||
$ride = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
@@ -55,6 +55,15 @@ try {
|
||||
$updateOrder->execute([$reason, $rideId, $driverId]);
|
||||
}
|
||||
|
||||
// إدراج سبب الإلغاء في جدول canecl المخصص
|
||||
if ($driverId > 0 && !empty($reason)) {
|
||||
$passengerId = $ride['passenger_id'] ?? '0';
|
||||
$insertCanecl = $con->prepare(
|
||||
"INSERT INTO canecl (driverID, passengerID, rideID, note) VALUES (?, ?, ?, ?)"
|
||||
);
|
||||
$insertCanecl->execute([$driverId, $passengerId, $rideId, $reason]);
|
||||
}
|
||||
|
||||
$con->commit();
|
||||
|
||||
// تحديث السيرفر البعيد (Remote DB)
|
||||
|
||||
@@ -15,14 +15,15 @@ try {
|
||||
// and atomically updates all databases within a transaction.
|
||||
//
|
||||
// Flow:
|
||||
// 1. Receive raw params from driver app
|
||||
// 2. Calculate price server-side (from DB + actual distance)
|
||||
// 3. BEGIN TRANSACTION (local DB)
|
||||
// 4. Update ride on local DB + remote DB (con_ride)
|
||||
// 5. Update driver_orders
|
||||
// 6. S2S cURL → Wallet Payment Server (process_ride_payments.php)
|
||||
// 7. If payment OK → COMMIT, notify passenger (Socket + FCM)
|
||||
// 8. If payment FAIL → ROLLBACK, ride stays 'Begin', safe retry
|
||||
// 1. Receive raw params from driver app (including country_code)
|
||||
// 2. Load country pricing from `kazan` table
|
||||
// 3. Calculate price server-side (from DB + actual distance)
|
||||
// 4. BEGIN TRANSACTION (local DB)
|
||||
// 5. Update ride on local DB + remote DB (con_ride)
|
||||
// 6. Update driver_orders
|
||||
// 7. S2S cURL → Wallet Payment Server (process_ride_payments.php)
|
||||
// 8. If payment OK → COMMIT, notify passenger (Socket + FCM)
|
||||
// 9. If payment FAIL → ROLLBACK, ride stays 'Begin', safe retry
|
||||
// ============================================================
|
||||
|
||||
// --- Secure S2S Configuration ---
|
||||
@@ -42,6 +43,7 @@ $passengerToken = filterRequest("passengerToken");
|
||||
$driver_token = filterRequest("driver_token");
|
||||
$walletChecked = filterRequest("walletChecked");
|
||||
$passengerWalletBurc = filterRequest("passengerWalletBurc");
|
||||
$countryCode = filterRequest("country_code"); // 🆕 الدولة: Syria, Egypt, ...
|
||||
|
||||
if (empty($rideId) || empty($newStatus) || empty($driver_id) || empty($passengerId)) {
|
||||
jsonError("Missing required parameters: rideId, driver_id, passengerId, status");
|
||||
@@ -53,8 +55,44 @@ if ($newStatus !== 'Finished') {
|
||||
exit;
|
||||
}
|
||||
|
||||
// 🆕 إذا لم يتم إرسال country_code، نأخذه من قاعدة بيانات الرحلة
|
||||
if (empty($countryCode)) {
|
||||
try {
|
||||
$stmtCountry = $con->prepare("SELECT r.id, d.site AS country_code
|
||||
FROM ride r
|
||||
LEFT JOIN driver d ON r.driver_id = d.id
|
||||
WHERE r.id = ? LIMIT 1");
|
||||
$stmtCountry->execute([$rideId]);
|
||||
$rowCountry = $stmtCountry->fetch(PDO::FETCH_ASSOC);
|
||||
$countryCode = $rowCountry['country_code'] ?? 'Syria';
|
||||
} catch (Exception $e) {
|
||||
$countryCode = 'Syria'; // fallback
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 2. Server-Side Price Calculation (Secure — NOT from client)
|
||||
// 2. Load Country Pricing from `kazan` Table
|
||||
// ============================================================
|
||||
try {
|
||||
$stmtKazan = $con->prepare("SELECT * FROM kazan WHERE country = ? LIMIT 1");
|
||||
$stmtKazan->execute([$countryCode]);
|
||||
$countryPricing = $stmtKazan->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$countryPricing) {
|
||||
// Fallback: إذا لم نجد سعر للدولة، نستخدم Syria كافتراضي
|
||||
error_log("[finish_ride_updates] No pricing found for country: $countryCode. Falling back to Syria.");
|
||||
$stmtKazan->execute(['Syria']);
|
||||
$countryPricing = $stmtKazan->fetch(PDO::FETCH_ASSOC);
|
||||
$countryCode = 'Syria';
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
error_log("[finish_ride_updates] Failed to load country pricing: " . $e->getMessage());
|
||||
jsonError("Failed to load pricing configuration.");
|
||||
exit;
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 3. Server-Side Price Calculation (Secure — NOT from client)
|
||||
// ============================================================
|
||||
try {
|
||||
// Fetch ride data from remote/local DB for server-side calculation
|
||||
@@ -73,10 +111,10 @@ try {
|
||||
}
|
||||
|
||||
$quotedPrice = floatval($rideData['quoted_price'] ?? 0);
|
||||
$kazanPercent = 10;
|
||||
$kazanPercent = floatval($countryPricing['kazanPercent'] ?? $countryPricing['kazan'] ?? 10); // 🆕 من جدول kazan (kazanPercent هو الاسم الجديد)
|
||||
$carType = $rideData['car_type'] ?? 'Fixed Price';
|
||||
|
||||
// Fixed-price types: use quoted price as-is
|
||||
// Fixed-price types, Speed & Awfar: use quoted price as-is
|
||||
$fixedPriceTypes = ['Speed', 'Fixed Price', 'Awfar Car'];
|
||||
if (in_array($carType, $fixedPriceTypes)) {
|
||||
$finalPrice = $quotedPrice;
|
||||
@@ -88,8 +126,9 @@ try {
|
||||
if ($distanceKm <= 0) {
|
||||
$finalPrice = $quotedPrice; // fallback
|
||||
} else {
|
||||
$perKmRate = getPerKmRate($carType);
|
||||
$perMinRate = getPerMinRate();
|
||||
// 🆕 استخدام الأسعار من جدول kazan حسب الدولة (كل نوع سيارة له عمود سعره الخاص)
|
||||
$perKmRate = getPerKmRate($carType, $countryPricing);
|
||||
$perMinRate = getPerMinRate($countryPricing);
|
||||
$durationMin = intval(preg_replace('/[^0-9]/', '', $actualDuration));
|
||||
|
||||
$calculated = ($distanceKm * $perKmRate) + ($durationMin * $perMinRate);
|
||||
@@ -98,18 +137,20 @@ try {
|
||||
$finalPrice = max($quotedPrice, round($calculated, 2));
|
||||
}
|
||||
}
|
||||
|
||||
// 🆕 تحديد رمز العملة حسب الدولة
|
||||
$currency = getCurrencyByCountry($countryCode);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
jsonError("Error calculating price: " . $e->getMessage());
|
||||
exit;
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 3. Atomic Transaction: Update DBs + Process Payment
|
||||
// 4. Atomic Transaction: Update DBs + Process Payment
|
||||
// ============================================================
|
||||
try {
|
||||
// --- Update Remote DB (con_ride) FIRST ---
|
||||
// (Not in transaction — remote DB doesn't support cross-DB rollback,
|
||||
// but we keep it minimal as a "best-effort" update)
|
||||
if (isset($con_ride)) {
|
||||
$stmtRemote = $con_ride->prepare(
|
||||
"UPDATE ride SET status = ?, rideTimeFinish = NOW(), price = ? WHERE id = ? AND status = 'Begin'"
|
||||
@@ -120,7 +161,7 @@ try {
|
||||
// --- BEGIN Local DB Transaction ---
|
||||
$con->beginTransaction();
|
||||
|
||||
// 3a. Update ride (local DB)
|
||||
// 4a. Update ride (local DB)
|
||||
$stmtLocal = $con->prepare(
|
||||
"UPDATE ride SET status = ?, rideTimeFinish = NOW(), price = ? WHERE id = ? AND status = 'Begin'"
|
||||
);
|
||||
@@ -130,7 +171,7 @@ try {
|
||||
throw new Exception("Ride already finished or not found in local DB.");
|
||||
}
|
||||
|
||||
// 3b. Update driver_orders
|
||||
// 4b. Update driver_orders
|
||||
$checkStmt = $con->prepare("SELECT order_id FROM driver_orders WHERE order_id = ?");
|
||||
$checkStmt->execute([$rideId]);
|
||||
|
||||
@@ -143,7 +184,7 @@ try {
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 3c. Server-to-Server Payment Processing (S2S)
|
||||
// 4c. Server-to-Server Payment Processing (S2S)
|
||||
// ============================================================
|
||||
$paymentPayload = [
|
||||
'rideId' => $rideId,
|
||||
@@ -154,6 +195,8 @@ try {
|
||||
'walletChecked' => $walletChecked,
|
||||
'passengerWalletBurc' => $passengerWalletBurc,
|
||||
'authToken' => $driver_token,
|
||||
'currency' => $currency, // 🆕 إرسال العملة لمخدم الدفع
|
||||
'country_code' => $countryCode, // 🆕 إرسال الدولة لمخدم الدفع
|
||||
];
|
||||
|
||||
$ch = curl_init(WALLET_PAYMENT_URL);
|
||||
@@ -202,7 +245,7 @@ try {
|
||||
$con->commit();
|
||||
|
||||
// ============================================================
|
||||
// 4. Notifications (After successful commit)
|
||||
// 5. Notifications (After successful commit)
|
||||
// ============================================================
|
||||
$passenger_id = $passengerId; // alias for legacy code
|
||||
|
||||
@@ -220,6 +263,7 @@ try {
|
||||
'ride_id' => $rideId,
|
||||
'status' => 'finished',
|
||||
'price' => $finalPrice,
|
||||
'currency' => $currency, // 🆕
|
||||
'DriverList' => $legacyList
|
||||
];
|
||||
|
||||
@@ -232,13 +276,14 @@ try {
|
||||
$fcmData = [
|
||||
'ride_id' => (string)$rideId,
|
||||
'price' => (string)$finalPrice,
|
||||
'currency' => $currency, // 🆕
|
||||
'DriverList' => $legacyList
|
||||
];
|
||||
|
||||
sendFCM_Internal(
|
||||
$passengerToken,
|
||||
"تم إنهاء الرحلة 🏁",
|
||||
"المبلغ المطلوب: " . $finalPrice . " ل.س",
|
||||
"المبلغ المطلوب: " . $finalPrice . " " . $currency,
|
||||
$fcmData,
|
||||
'Driver Finish Trip',
|
||||
false
|
||||
@@ -247,11 +292,12 @@ try {
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 5. Return Success with server-calculated price
|
||||
// 6. Return Success with server-calculated price + currency
|
||||
// ============================================================
|
||||
jsonSuccess([
|
||||
'price' => $finalPrice,
|
||||
'rideId' => $rideId
|
||||
'price' => $finalPrice,
|
||||
'currency' => $currency, // 🆕 إرجاع العملة للتطبيق
|
||||
'rideId' => $rideId
|
||||
], "Ride finished and payment processed successfully.");
|
||||
|
||||
} catch (Exception $e) {
|
||||
@@ -263,28 +309,131 @@ try {
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Helper Functions
|
||||
// Helper Functions — الآن تقرأ الأسعار من جدول kazan حسب الدولة
|
||||
// ============================================================
|
||||
|
||||
function getPerKmRate(string $carType): float {
|
||||
$rates = [
|
||||
'Comfort' => 44,
|
||||
'Lady' => 44,
|
||||
'Mishwar Vip' => 50,
|
||||
'Electric' => 45,
|
||||
'Van' => 63,
|
||||
'Delivery' => 25,
|
||||
'Speed' => 36,
|
||||
'Fixed Price' => 36,
|
||||
'Awfar Car' => 36,
|
||||
/**
|
||||
* الحصول على سعر الكيلومتر حسب نوع السيارة من جدول أسعار الدولة
|
||||
*
|
||||
* 🆕 كل نوع سيارة له عمود مستقل في جدول kazan:
|
||||
* Speed → speedPrice | Comfort → comfortPrice | Lady → ladyPrice
|
||||
* Electric → electricPrice | Van → vanPrice | Delivery → deliveryPrice
|
||||
* Mishwar Vip → mishwarVipPrice | Fixed Price → fixedPrice | Awfar → awfarPrice
|
||||
*
|
||||
* @param string $carType نوع السيارة
|
||||
* @param array $countryPricing صف من جدول kazan
|
||||
* @return float
|
||||
*/
|
||||
function getPerKmRate(string $carType, array $countryPricing): float {
|
||||
// 🆕 الخريطة المباشرة: كل نوع سيارة يقابله عمود بنفس الاسم + "Price"
|
||||
$rateColumns = [
|
||||
'Comfort' => 'comfortPrice',
|
||||
'Speed' => 'speedPrice',
|
||||
'Lady' => 'ladyPrice',
|
||||
'Electric' => 'electricPrice',
|
||||
'Van' => 'vanPrice',
|
||||
'Delivery' => 'deliveryPrice',
|
||||
'Mishwar Vip' => 'mishwarVipPrice',
|
||||
'Fixed Price' => 'fixedPrice',
|
||||
'Awfar Car' => 'awfarPrice',
|
||||
];
|
||||
return $rates[$carType] ?? 36;
|
||||
|
||||
$column = $rateColumns[$carType] ?? 'speedPrice';
|
||||
|
||||
// دعم التوافق مع الإصدارات القديمة (backward compatibility)
|
||||
$rate = floatval($countryPricing[$column] ?? 0);
|
||||
|
||||
// إذا كان السعر صفر أو غير موجود، نبحث في الأسماء القديمة
|
||||
if ($rate <= 0) {
|
||||
$oldColumnMap = [
|
||||
'Lady' => 'familyPrice',
|
||||
'Mishwar Vip' => 'freePrice',
|
||||
'Electric' => 'naturePrice',
|
||||
'Van' => 'heavyPrice',
|
||||
];
|
||||
$oldColumn = $oldColumnMap[$carType] ?? null;
|
||||
if ($oldColumn && isset($countryPricing[$oldColumn])) {
|
||||
$rate = floatval($countryPricing[$oldColumn]);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback أخير
|
||||
if ($rate <= 0) {
|
||||
$rate = floatval($countryPricing['speedPrice'] ?? 36);
|
||||
}
|
||||
|
||||
return $rate;
|
||||
}
|
||||
|
||||
function getPerMinRate(): float {
|
||||
/**
|
||||
* الحصول على سعر الدقيقة حسب وقت اليوم من جدول kazan
|
||||
*
|
||||
* 🆕 الآن يقرأ من أعمدة الدقيقة المنفصلة:
|
||||
* normalMinPrice → Normal (9 ص - 2 م / 6 م - 9 م)
|
||||
* peakMinPrice → Peak (2 م - 5 م)
|
||||
* lateMinPrice → Late (9 م - 1 ص)
|
||||
*
|
||||
* @param array $countryPricing صف من جدول kazan
|
||||
* @return float
|
||||
*/
|
||||
function getPerMinRate(array $countryPricing): float {
|
||||
$hour = (int)date('H');
|
||||
if ($hour >= 21 || $hour < 1) return 11; // Late
|
||||
if ($hour >= 14 && $hour <= 17) return 10; // Peak
|
||||
return 9; // Normal
|
||||
|
||||
// 🆕 قراءة الأسعار من الأعمدة الجديدة للدقيقة
|
||||
$normalMinPrice = floatval($countryPricing['normalMinPrice'] ?? 0);
|
||||
$peakMinPrice = floatval($countryPricing['peakMinPrice'] ?? 0);
|
||||
$lateMinPrice = floatval($countryPricing['lateMinPrice'] ?? 0);
|
||||
|
||||
// 🆕 دعم التوافق مع الإصدارات القديمة (latePrice, naturePrice)
|
||||
if ($lateMinPrice <= 0) $lateMinPrice = floatval($countryPricing['latePrice'] ?? 0);
|
||||
if ($normalMinPrice <= 0) $normalMinPrice = floatval($countryPricing['naturePrice'] ?? 0);
|
||||
|
||||
// Fallback: حساب سعر الدقيقة من speedPrice إذا كانت الأعمدة الجديدة فارغة
|
||||
if ($normalMinPrice <= 0) {
|
||||
$speedPrice = floatval($countryPricing['speedPrice'] ?? 36);
|
||||
$normalMinPrice = $speedPrice / 4;
|
||||
}
|
||||
if ($peakMinPrice <= 0) $peakMinPrice = $normalMinPrice * 1.15; // 15% زيادة
|
||||
if ($lateMinPrice <= 0) $lateMinPrice = $normalMinPrice * 1.25; // 25% زيادة
|
||||
|
||||
if ($hour >= 21 || $hour < 1) {
|
||||
return round($lateMinPrice, 2); // Late Night
|
||||
}
|
||||
if ($hour >= 14 && $hour <= 17) {
|
||||
return round($peakMinPrice, 2); // Peak
|
||||
}
|
||||
return round($normalMinPrice, 2); // Normal
|
||||
}
|
||||
?>
|
||||
|
||||
/**
|
||||
* تحديد رمز العملة حسب الدولة
|
||||
*
|
||||
* @param string $countryCode رمز الدولة (Syria, Egypt, Jordan, ...)
|
||||
* @return string رمز العملة (SYP, EGP, JOD, ...)
|
||||
*/
|
||||
function getCurrencyByCountry(string $countryCode): string {
|
||||
$currencies = [
|
||||
'Syria' => 'SYP',
|
||||
'Egypt' => 'EGP',
|
||||
'Jordan' => 'JOD',
|
||||
'Iraq' => 'IQD',
|
||||
'UAE' => 'AED',
|
||||
'Saudi Arabia' => 'SAR',
|
||||
'Qatar' => 'QAR',
|
||||
'Kuwait' => 'KWD',
|
||||
'Bahrain' => 'BHD',
|
||||
'Oman' => 'OMR',
|
||||
'Turkey' => 'TRY',
|
||||
'Lebanon' => 'LBP',
|
||||
'Palestine' => 'ILS',
|
||||
'Yemen' => 'YER',
|
||||
'Libya' => 'LYD',
|
||||
'Tunisia' => 'TND',
|
||||
'Algeria' => 'DZD',
|
||||
'Morocco' => 'MAD',
|
||||
'Sudan' => 'SDG',
|
||||
];
|
||||
|
||||
return $currencies[$countryCode] ?? 'SYP'; // افتراضي: ليرة سورية
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user