Update: 2026-07-13 04:13:50

This commit is contained in:
Hamza-Ayed
2026-07-13 04:13:50 +03:00
parent 653d73422f
commit 7bf6dc1be7
265 changed files with 11 additions and 15250 deletions
@@ -0,0 +1,45 @@
<?php
include "../../jwtconnect.php";
//addPassengersWallet.php
$passenger_id = filterRequest("passenger_id");
$balance = filterRequest("balance");
$token = filterRequest("token");
try {
$con->beginTransaction();
// Retrieve token details from the database securely and lock the row
$stmt = $con->prepare("SELECT * FROM payment_tokens_passenger WHERE token = :token AND isUsed = FALSE FOR UPDATE");
$stmt->execute([':token' => $token]);
$tokenData = $stmt->fetch();
if ($tokenData) {
// Insert into passengerWallet securely using prepared statements
$sql = "INSERT INTO `passengerWallet` (`passenger_id`, `balance`) VALUES (:passenger_id, :balance)";
$stmtInsert = $con->prepare($sql);
$stmtInsert->execute([':passenger_id' => $passenger_id, ':balance' => $balance]);
if ($stmtInsert->rowCount() > 0) {
// Mark the token as used
$updateTokenStmt = $con->prepare("UPDATE payment_tokens_passenger SET isUsed = TRUE WHERE id = :tokenID");
$updateTokenStmt->execute([':tokenID' => $tokenData['id']]);
$con->commit();
printSuccess("Wallet record created successfully");
} else {
$con->rollBack();
printFailure("Failed to create wallet record");
}
} else {
$con->rollBack();
printFailure("Invalid or already used token");
}
} catch (Exception $e) {
if ($con->inTransaction()) {
$con->rollBack();
}
error_log("[passengerWallet/add] " . $e->getMessage());
printFailure("Database error");
}
?>
@@ -0,0 +1,54 @@
<?php
include "../../jwtconnect.php";
//addPaymentTokenPassenger.php
$passengerId = filterRequest("passengerId");
$amount = filterRequest("amount");
// Check if required fields are present
if ($passengerId === null || $amount === null) {
printFailure("Missing required fields: passengerId and amount must be provided");
exit;
}
// Generate the token using current time
$token = generateSecureToken($passengerId, $amount, date('Y-m-d H:i:s', time()));
// Store the token in the database, using NOW() for dateCreated
$stmt = $con->prepare("INSERT INTO payment_tokens_passenger (token, passengerId, dateCreated, amount) VALUES (?, ?, NOW(), ?)");
try {
$stmt->execute([$token, $passengerId, $amount]);
if ($stmt->rowCount() > 0) {
printSuccess($token);
} else {
printFailure("Failed to save record");
}
} catch (PDOException $e) {
error_log("[addPaymentTokenPassenger] " . $e->getMessage());
printFailure("Database error");
}
// Rest of your code including the generateSecureToken function...
// Rest of your code including the generateSecureToken function...
function generateSecureToken($passengerId, $amount, $dateCreated) {
global $secretKey;
// Concatenate the parameters
$data = $passengerId . $amount . $dateCreated;
// Add the secret key from the environment variable
$data .= $secretKey;
// Generate a hash
$hash = hash('sha256', $data);
// Add some randomness
$randomBytes = bin2hex(random_bytes(16));
// Combine hash and random bytes
$token = $hash . $randomBytes;
// Truncate to a reasonable length (e.g., 64 characters)
return substr($token, 0, 64);
}
@@ -0,0 +1,60 @@
<?php
/**
* add_s2s_debt.php — Payment Server Endpoint
*
* Inserts passenger wallet credit/debit records (debt/penalty).
* Authenticated via X-S2S-Api-Key header matching the S2S_SHARED_KEY environment variable.
*/
require_once __DIR__ . '/../../jwtconnect.php';
define('S2S_SHARED_KEY', getenv('S2S_SHARED_KEY'));
$providedKey = $_SERVER['HTTP_X_S2S_API_KEY'] ?? '';
if (empty($providedKey) || $providedKey !== S2S_SHARED_KEY) {
http_response_code(401);
printFailure("Unauthorized: Invalid or missing X-S2S-Api-Key.");
exit;
}
$passengerID = filterRequest("passengerID");
$amount = filterRequest("amount");
if (empty($passengerID) || !isset($amount)) {
printFailure("Missing required parameters: passengerID, amount");
exit;
}
try {
$con->beginTransaction();
$sql = "INSERT INTO `passengerWallet` (
`passenger_id`,
`balance`
) VALUES (
:passengerID,
:amount
);";
$stmt = $con->prepare($sql);
$stmt->execute([
':passengerID' => $passengerID,
':amount' => $amount
]);
if ($stmt->rowCount() > 0) {
$con->commit();
printSuccess("Record saved successfully");
} else {
$con->rollBack();
printFailure("Failed to save record");
}
} catch (Exception $e) {
if ($con->inTransaction()) {
$con->rollBack();
}
error_log("add_s2s_debt: " . $e->getMessage()); // logged server-side only
printFailure("An error occurred");
}
?>
@@ -0,0 +1,17 @@
<?php
include "../../connect.php";
$id = filterRequest("id");
$sql = "DELETE FROM `passengerWallet` WHERE `id` = '$id'";
$stmt = $con->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() > 0) {
// Print a success message
printSuccess($message = "Wallet record deleted successfully");
} else {
// Print a failure message
printFailure($message = "Failed to delete wallet record");
}
?>
@@ -0,0 +1,32 @@
<?php
include "../../connect.php";
$passenger_id = filterRequest("passenger_id");
$sql = "SELECT
passengerWallet.`id`,
passengerWallet.`passenger_id`,
SUM(passengerWallet.balance) AS total,
passengers.first_name,
passengers.last_name,
passengers.phone,
passengers.email
FROM
`passengerWallet`
LEFT JOIN passengers ON passengers.id = passengerWallet.passenger_id
GROUP BY
passenger_id";
$stmt = $con->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() > 0) {
// Fetch the record
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
printSuccess( $row);
}
else{
// Print a failure message
printFailure($message = "No wallet record found");
}
?>
@@ -0,0 +1,40 @@
<?php
include "../../connect.php";
$passenger_id = filterRequest("passenger_id");
$sql = "SELECT
`id`,
`passenger_id`,
`balance`,
`created_at`,
`updated_at`,
(
SELECT
SUM(balance)
FROM
passengerWallet
WHERE
passenger_id = '$passenger_id'
) AS total
FROM
`passengerWallet`
WHERE
passenger_id = '$passenger_id'
GROUP BY
`passenger_id`,
`id`;";
$stmt = $con->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() > 0) {
// Fetch the record
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
printSuccess( $row);
}
else{
// Print a failure message
printFailure($message = "No wallet record found");
}
?>
@@ -0,0 +1,30 @@
<?php
include "../../connect.php";
$passenger_id = filterRequest("passenger_id");
$sql = "SELECT
passengerWallet.`id`,
passengerWallet.balance,
passengerWallet.`created_at`
FROM
`passengerWallet`
WHERE
passenger_id = '$passenger_id'AND created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
ORDER BY
`passengerWallet`.`id`
DESC";
$stmt = $con->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() > 0) {
// Fetch the record
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
printSuccess( $row);
}
else{
// Print a failure message
printFailure($message = "No wallet record found");
}
?>
@@ -0,0 +1,33 @@
<?php
include "../../connect.php";
$passenger_id = filterRequest("passenger_id");
$sql = "SELECT
COALESCE(dummy.passenger_id, '$passenger_id') AS passenger_id,
COALESCE(SUM(pw.balance), 0) AS total, -- Adjust column to represent payments
COALESCE(p.first_name, '') AS first_name,
COALESCE(p.last_name, '') AS last_name,
COALESCE(p.phone, '') AS phone
FROM
(SELECT '$passenger_id' AS passenger_id) AS dummy
LEFT JOIN `passengerWallet` pw ON pw.passenger_id = dummy.passenger_id
LEFT JOIN passengers p ON p.id = dummy.passenger_id
GROUP BY
dummy.passenger_id, p.first_name, p.last_name, p.phone
LIMIT 0, 25;
";
$stmt = $con->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() > 0) {
// Fetch the record
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
printSuccess( $row);
}
else{
// Print a failure message
printFailure($message = "No wallet record found");
}
?>
@@ -0,0 +1,35 @@
<?php
require_once __DIR__ . '/../../jwtconnect.php';
$providedKey = $_SERVER['HTTP_X_S2S_API_KEY'] ?? '';
if (empty($providedKey) || $providedKey !== getenv('S2S_SHARED_KEY')) {
http_response_code(401);
printFailure("Unauthorized: Invalid or missing X-S2S-Api-Key.");
exit;
}
$passenger_id = filterRequest("passenger_id");
if (empty($passenger_id)) {
printFailure("Missing required parameter: passenger_id");
exit;
}
try {
$stmtTotalWallet = $con->prepare("
SELECT COALESCE(SUM(balance), 0)
FROM `passengerWallet`
WHERE passenger_id = :passenger_id
");
$stmtTotalWallet->execute([':passenger_id' => $passenger_id]);
$totalWallet = (float)($stmtTotalWallet->fetchColumn() ?: 0.0);
printSuccess([
"totalWallet" => $totalWallet
]);
} catch (Exception $e) {
error_log("[get_s2s_passenger_wallet] " . $e->getMessage());
printFailure("An error occurred");
}
?>
@@ -0,0 +1,98 @@
<?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)
// ---------------------------------------------------------
// أ) فحص توكن السائق (مع FOR UPDATE)
$stmtCheckD = $con->prepare("SELECT id FROM payment_tokens WHERE token = ? AND isUsed = FALSE FOR UPDATE");
$stmtCheckD->execute([$tokenDriver]);
$tokenDriverData = $stmtCheckD->fetch();
if (!$tokenDriverData) {
throw new Exception("Invalid or used Driver Token");
}
// ب) فحص توكن الراكب (مع FOR UPDATE)
$stmtCheckP = $con->prepare("SELECT id FROM payment_tokens_passenger WHERE token = ? AND isUsed = FALSE FOR UPDATE");
$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();
}
error_log("[process_wait_compensation] " . $e->getMessage());
printFailure("Transaction Failed");
}
?>
@@ -0,0 +1,18 @@
<?php
include "../../connect.php";
$id = filterRequest("id");
$balance = filterRequest("balance");
$sql = "UPDATE `passengerWallet` SET `balance` = '$balance' WHERE `id` = '$id'";
$stmt = $con->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() > 0) {
// Print a success message
printSuccess($message = "Wallet record updated successfully");
} else {
// Print a failure message
printFailure($message = "Failed to update wallet record");
}
?>