Port all fixes from IntaleqApp (Wallet, CLIQ, OTP, Docker)
This commit is contained in:
@@ -1,55 +1,63 @@
|
||||
<?php
|
||||
// Load environment variables from .env file
|
||||
require_once realpath(__DIR__ . '/../vendor/autoload.php');
|
||||
require_once 'load_env.php';
|
||||
$envFile = '/home/intaleq-walletintaleq/env/.env';
|
||||
if (!file_exists($envFile)) {
|
||||
$envFile = __DIR__ . '/../.env';
|
||||
// connect.php — نسخة الدوكر
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
// • vendor/ مخبوز داخل الصورة في /var/www/v2/vendor (Dockerfile يشغّل composer install)
|
||||
// • متغيّرات البيئة تصل عبر env_file في docker-compose → getenv() يعمل مباشرةً
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
|
||||
// 1. Autoload — vendor مخبوز داخل الصورة
|
||||
$autoload = __DIR__ . '/../vendor/autoload.php';
|
||||
if (file_exists($autoload)) {
|
||||
require_once $autoload;
|
||||
}
|
||||
loadEnvironment($envFile);
|
||||
|
||||
// Get environment variables (You don't need user/pass for JWT auth itself)
|
||||
$secretKey = getenv('SECRET_KEY'); // Only need the secret key now
|
||||
// 2. لا حاجة لتحميل .env يدوياً: Docker يحقن المتغيّرات عبر env_file
|
||||
// نحتفظ بـ load_env.php فقط لبيئة التطوير خارج الدوكر
|
||||
if (!getenv('DB_PAYMENT_HOST')) {
|
||||
// نحن خارج بيئة الدوكر — جرّب تحميل .env يدوياً
|
||||
if (file_exists(__DIR__ . '/load_env.php')) {
|
||||
require_once 'load_env.php';
|
||||
$envFile = __DIR__ . '/../.env';
|
||||
if (file_exists($envFile)) loadEnvironment($envFile);
|
||||
}
|
||||
}
|
||||
|
||||
// --- CORS Headers ---
|
||||
header("Access-Control-Allow-Origin: https://wallet.siromove.com");
|
||||
header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); // Adjust as needed
|
||||
// 3. CORS Headers
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Authorization");
|
||||
header('Content-Type: application/json'); // Set content type to JSON
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Handle preflight requests (OPTIONS)
|
||||
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit;
|
||||
}
|
||||
|
||||
// --- Database Connection (Still needed for your application logic) ---
|
||||
// 4. Database Connection
|
||||
try {
|
||||
$dbname = getenv('DB_PAYMENT_NAME') ?: (getenv('dbname') ?: 'payment');
|
||||
$dbHost = getenv('DB_PAYMENT_HOST') ?: (getenv('DB_HOST') ?: 'mysql');
|
||||
$dbname = getenv('DB_PAYMENT_NAME') ?: 'paymentDB';
|
||||
$dbHost = getenv('DB_PAYMENT_HOST') ?: 'mysql';
|
||||
$user = getenv('DB_PAYMENT_USER') ?: 'root';
|
||||
$pass = getenv('DB_PAYMENT_PASS') ?: getenv('MYSQL_ROOT_PASSWORD') ?: '';
|
||||
|
||||
$dsn = "mysql:host=$dbHost;dbname=$dbname;charset=utf8mb4";
|
||||
|
||||
$options = [
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"
|
||||
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8",
|
||||
];
|
||||
$user = getenv('DB_PAYMENT_USER') ?: (getenv('USER') ?: 'root');
|
||||
$pass = getenv('DB_PAYMENT_PASS') ?: (getenv('PASS') ?: getenv('MYSQL_ROOT_PASSWORD'));
|
||||
$con = new PDO($dsn, $user, $pass, $options);
|
||||
|
||||
// echo $con;
|
||||
// --- JWT Authentication ---
|
||||
include "functions.php"; // Include the functions file
|
||||
|
||||
$decodedToken = authenticateJWT(); // Call the authentication function
|
||||
|
||||
// 5. JWT Authentication
|
||||
include __DIR__ . "/functions.php";
|
||||
$decodedToken = authenticateJWT();
|
||||
|
||||
} catch (PDOException $e) {
|
||||
error_log($e->getMessage());
|
||||
http_response_code(500); // Internal Server Error
|
||||
error_log('[WALLET] DB Error: ' . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'A database error occurred.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -7,7 +7,7 @@ try {
|
||||
$userId = filterRequest("user_id");
|
||||
$userType = filterRequest("user_type");
|
||||
$amount = filterRequest("amount");
|
||||
$cliqPhone = filterRequest("cliq_phone");
|
||||
$cliqPhone = filterRequest("cliq_phone") ?: filterRequest("click_phone");
|
||||
$phone = filterRequest("phone");
|
||||
|
||||
if (empty($userId) || empty($userType) || !is_numeric($amount) || $amount <= 0 || empty($cliqPhone)) {
|
||||
@@ -37,14 +37,12 @@ try {
|
||||
$upd = $con->prepare("
|
||||
UPDATE cliq_invoices
|
||||
SET amount = :amount,
|
||||
phone = :phone,
|
||||
cliq_phone = :cliq_phone,
|
||||
updated_at = NOW()
|
||||
WHERE id = :id
|
||||
");
|
||||
$upd->execute([
|
||||
':amount' => $amount,
|
||||
':phone' => $phone ?: null,
|
||||
':cliq_phone' => $cliqPhone,
|
||||
':id' => $existing['id'],
|
||||
]);
|
||||
@@ -62,15 +60,14 @@ try {
|
||||
|
||||
$ins = $con->prepare("
|
||||
INSERT INTO cliq_invoices
|
||||
(invoice_number, user_id, user_type, phone, amount, cliq_phone, status, created_at, updated_at)
|
||||
(invoice_number, user_id, user_type, amount, cliq_phone, status, created_at, updated_at)
|
||||
VALUES
|
||||
(:invoice_number, :user_id, :user_type, :phone, :amount, :cliq_phone, 'pending', NOW(), NOW())
|
||||
(:invoice_number, :user_id, :user_type, :amount, :cliq_phone, 'pending', NOW(), NOW())
|
||||
");
|
||||
$ins->execute([
|
||||
':invoice_number' => $invoiceNumber,
|
||||
':user_id' => $userId,
|
||||
':user_type' => $userType,
|
||||
':phone' => $phone ?: null,
|
||||
':amount' => $amount,
|
||||
':cliq_phone' => $cliqPhone
|
||||
]);
|
||||
|
||||
@@ -3,13 +3,13 @@ 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(dummy.passenger_id, :passenger_id) AS passenger_id,
|
||||
COALESCE(SUM(pw.balance), 0) AS total,
|
||||
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
|
||||
(SELECT :passenger_id2 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
|
||||
@@ -17,17 +17,13 @@ GROUP BY
|
||||
LIMIT 0, 25;
|
||||
";
|
||||
$stmt = $con->prepare($sql);
|
||||
$stmt->execute();
|
||||
$stmt->execute(['passenger_id' => $passenger_id, 'passenger_id2' => $passenger_id]);
|
||||
|
||||
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");
|
||||
// LEFT JOIN يُرجع دائماً صفاً واحداً على الأقل (balance=0 إذا لم توجد محفظة)
|
||||
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
if (!empty($row)) {
|
||||
printSuccess($row);
|
||||
} else {
|
||||
printFailure("No wallet record found");
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user