Update: 2026-06-11 18:22:57
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
// process_wait_compensation.php
|
||||
// يوضع هذا الملف على سيرفر المدفوعات (Payment Server)
|
||||
|
||||
include "../../connect.php"; // تأكد من مسار الاتصال
|
||||
|
||||
// 1. استقبال البيانات
|
||||
$rideId = filterRequest("ride_id");
|
||||
$driverId = filterRequest("driver_id");
|
||||
$passengerId = filterRequest("passenger_id");
|
||||
$amount = filterRequest("amount"); // المبلغ الموجب (للسائق)
|
||||
$amountPassenger= filterRequest("amount_passenger"); // المبلغ السالب (للراكب)
|
||||
$tokenDriver = filterRequest("token_driver");
|
||||
$tokenPassenger = filterRequest("token_passenger");
|
||||
$paymentMethod = "wait-cancel"; // أو يمكن استقباله من التطبيق
|
||||
|
||||
if (!$rideId || !$driverId || !$passengerId || !$amount || !$tokenDriver || !$tokenPassenger) {
|
||||
printFailure("Missing parameters");
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// 🔥 بدء المعاملة المالية (Transaction)
|
||||
$con->beginTransaction();
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// الخطوة 1: التحقق من التوكنات (Security Check)
|
||||
// ---------------------------------------------------------
|
||||
|
||||
// أ) فحص توكن السائق
|
||||
$stmtCheckD = $con->prepare("SELECT id FROM payment_tokens WHERE token = ? AND isUsed = FALSE");
|
||||
$stmtCheckD->execute([$tokenDriver]);
|
||||
$tokenDriverData = $stmtCheckD->fetch();
|
||||
|
||||
if (!$tokenDriverData) {
|
||||
throw new Exception("Invalid or used Driver Token");
|
||||
}
|
||||
|
||||
// ب) فحص توكن الراكب
|
||||
$stmtCheckP = $con->prepare("SELECT id FROM payment_tokens_passenger WHERE token = ? AND isUsed = FALSE");
|
||||
$stmtCheckP->execute([$tokenPassenger]);
|
||||
$tokenPassengerData = $stmtCheckP->fetch();
|
||||
|
||||
if (!$tokenPassengerData) {
|
||||
throw new Exception("Invalid or used Passenger Token");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// الخطوة 2: إضافة سجل النقاط (paymentsDriverPoints)
|
||||
// ---------------------------------------------------------
|
||||
// هذا الجدول يبدو أنه "سجل العمليات" الرئيسي
|
||||
$sqlPoints = "INSERT INTO `paymentsDriverPoints` (`amount`, `payment_method`, `driverID`) VALUES (?, ?, ?)";
|
||||
$stmtPoints = $con->prepare($sqlPoints);
|
||||
$stmtPoints->execute([$amount, $paymentMethod, $driverId]);
|
||||
|
||||
// نحصل على ID العملية لنربطه بالمحفظة
|
||||
$paymentRecordID = $con->lastInsertId();
|
||||
|
||||
if ($stmtPoints->rowCount() == 0) {
|
||||
throw new Exception("Failed to insert into paymentsDriverPoints");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// الخطوة 3: إضافة الرصيد لمحفظة السائق (driverWallet)
|
||||
// ---------------------------------------------------------
|
||||
// نستخدم $paymentRecordID كمرجع للعملية
|
||||
$sqlWalletD = "INSERT INTO `driverWallet` (`driverID`, `paymentID`, `amount`, `paymentMethod`) VALUES (?, ?, ?, ?)";
|
||||
$stmtWalletD = $con->prepare($sqlWalletD);
|
||||
$stmtWalletD->execute([$driverId, $paymentRecordID, $amount, $paymentMethod]);
|
||||
|
||||
// حرق توكن السائق
|
||||
$con->prepare("UPDATE payment_tokens SET isUsed = TRUE WHERE id = ?")->execute([$tokenDriverData['id']]);
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// الخطوة 4: خصم الرصيد من محفظة الراكب (passengerWallet)
|
||||
// ---------------------------------------------------------
|
||||
$sqlWalletP = "INSERT INTO `passengerWallet` (`passenger_id`, `balance`) VALUES (?, ?)";
|
||||
$stmtWalletP = $con->prepare($sqlWalletP);
|
||||
$stmtWalletP->execute([$passengerId, $amountPassenger]);
|
||||
|
||||
// حرق توكن الراكب
|
||||
$con->prepare("UPDATE payment_tokens_passenger SET isUsed = TRUE WHERE id = ?")->execute([$tokenPassengerData['id']]);
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// إتمام العملية (Commit)
|
||||
// ---------------------------------------------------------
|
||||
$con->commit();
|
||||
printSuccess("Compensation processed successfully");
|
||||
|
||||
} catch (Exception $e) {
|
||||
// في حال حدوث أي خطأ، يتم التراجع عن كل العمليات السابقة
|
||||
if ($con->inTransaction()) {
|
||||
$con->rollBack();
|
||||
}
|
||||
printFailure("Transaction Failed: " . $e->getMessage());
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user