Files
Siro/walletintaleq.intaleq.xyz/v2/main/ride/ecash/driver/ecash_verify.php

150 lines
7.5 KiB
PHP
Executable File

<?php
// هذا الملف هو نقطة النهاية بعد الدفع، ويقوم بكل عمليات التحقق وإضافة الرصيد
// This file is the final endpoint after payment, handling all verification and balance updates.
include "../../../jwtconnect.php";
// -------------------------------------------------
// دوال مساعدة لإنشاء التوكنات ومعرفات الدفع
// Helper functions for creating tokens and payment IDs
// -------------------------------------------------
/**
* إنشاء توكن فريد لعملية المحفظة وتخزينه في قاعدة البيانات
* Creates a unique token for a wallet transaction and stores it in the database.
*/
define("BASE_URL", "https://wl.tripz-egypt.com/v1/main/ride"); // تأكد من صحة هذا الرابط
define("LOG_FILE", "../logs/payment_verification.log");
function logError($step, $message, $data = null) {
$logDir = dirname(LOG_FILE);
if (!is_dir($logDir)) { mkdir($logDir, 0755, true); }
$logEntry = "[" . date('Y-m-d H:i:s') . "] STEP {$step}: {$message}";
if ($data !== null) { $logEntry .= " | Data: " . json_encode($data, JSON_UNESCAPED_UNICODE); }
file_put_contents(LOG_FILE, $logEntry . PHP_EOL, FILE_APPEND);
}
function generateToken($con, $driverId, $amount): ?string
{
global $secretKey; // يفترض أن هذا المتغير متاح من ملف الاتصال
$data = $driverId . $amount . time() . ($secretKey ?? 'default_secret');
$hash = hash('sha256', $data);
$randomBytes = bin2hex(random_bytes(16));
$token = substr($hash . $randomBytes, 0, 64);
$stmt = $con->prepare("INSERT INTO payment_tokens (token, driverID, dateCreated, amount) VALUES (:token, :driverID, NOW(), :amount)");
$stmt->execute([':token' => $token, ':driverID' => $driverId, ':amount' => $amount]);
return $stmt->rowCount() > 0 ? $token : null;
}
/**
* تسجيل دفعة في جدول النقاط وإعادة المعرف الخاص بها
* Logs a payment in the points table and returns its ID.
*/
function generatePaymentID($con, $driverId, $amount, $method): ?string
{
$stmt = $con->prepare("INSERT INTO paymentsDriverPoints (`amount`, `payment_method`, `driverID`) VALUES (:amount, :method, :driverID)");
$stmt->execute([':driverID' => $driverId, ':amount' => $amount, ':method' => $method]);
return $stmt->rowCount() > 0 ? $con->lastInsertId() : null;
}
// -------------------------------------------------
// المنطق الرئيسي للمعالجة
// Main processing logic
// -------------------------------------------------
// 1. استقبال الرقم المرجعي من الرابط
// 1. Receive the order reference from the URL.
$orderRef = $_GET['orderRef'] ?? null;
if (empty($orderRef)) {
echo "<h1>خطأ: الرقم المرجعي للطلب مفقود.</h1>";
exit;
}
// 2. الانتظار والتأكد من وصول الـ Webhook
// 2. Wait and verify that the webhook has updated the status.
$payment = null;
$max_attempts = 5; // محاولة لمدة 10 ثوانٍ - Poll for 10 seconds
for ($attempts = 0; $attempts < $max_attempts; $attempts++) {
// تأكد من أن اسم الجدول صحيح
// Make sure the table name is correct.
$stmt = $con->prepare("SELECT * FROM `paymentsLogSyriaDriver` WHERE order_ref = :order_ref AND status = 1 LIMIT 1");
$stmt->execute([':order_ref' => $orderRef]);
$payment = $stmt->fetch(PDO::FETCH_ASSOC);
if ($payment) {
break; // تم العثور على الدفعة الناجحة - Successful payment found
}
sleep(2); // الانتظار لمدة ثانيتين قبل المحاولة التالية - Wait 2 seconds before retrying
}
// 3. التحقق من نتيجة البحث
// 3. Check the polling result.
if (!$payment) {
echo "<h1>خطأ في تأكيد الدفع</h1><p>لم نتمكن من تأكيد دفعتك. قد تستغرق العملية بضع لحظات. يرجى التحقق من رصيدك في التطبيق لاحقاً أو التواصل مع الدعم الفني.</p>";
exit;
}
// 4. Atomic status claim + wallet update (prevents double-processing)
// 4. معالجة ذرية لمنح الرصيد — تمنع التكرار في حال التزامن
try {
$driverId = $payment['user_id'];
$originalAmount = floatval($payment['amount']);
$paymentMethod = $payment['payment_method'] ?? 'ecash';
$bonusAmount = match ((int)$originalAmount) {
80 => 80.0,
200 => 215.0,
400 => 450.0,
1000 => 1140.0,
default => $originalAmount,
};
// بدء معاملة: تحديث الحالة claim + إضافة المحافظ
$con->beginTransaction();
// محاولة ذرية لـ claim المعاملة (فقط إذا كانت لا تزال status = 1)
$claimStmt = $con->prepare("UPDATE paymentsLogSyriaDriver SET status = 2 WHERE order_ref = :ref AND status = 1");
$claimStmt->execute([':ref' => $orderRef]);
if ($claimStmt->rowCount() === 0) {
$con->rollBack();
error_log("VERIFY_RACE: Concurrent claim for OrderRef " . $orderRef);
echo "<h1>تمت معالجة هذا الطلب مسبقاً</h1><p>الدفعة قيد المعالجة، يرجى التحقق من رصيدك في التطبيق.</p>";
exit;
}
$tokenDriver = generateToken($con, $driverId, $bonusAmount);
if (!$tokenDriver) throw new Exception('Failed to generate token for driver wallet.');
$tokenSiro = generateToken($con, $driverId, $originalAmount);
if (!$tokenSiro) throw new Exception('Failed to generate token for siro wallet.');
$paymentID = generatePaymentID($con, $driverId, $bonusAmount, $paymentMethod);
if (!$paymentID) throw new Exception('Failed to generate payment ID.');
$insertDriver = $con->prepare("INSERT INTO driverWallet (driverID, paymentID, amount, paymentMethod) VALUES (:driverID, :paymentID, :amount, :paymentMethod)");
$insertDriver->execute([':driverID' => $driverId, ':paymentID' => $paymentID, ':amount' => $bonusAmount, ':paymentMethod' => $paymentMethod]);
if ($insertDriver->rowCount() === 0) throw new Exception('Failed to insert into driverWallet.');
$markTokenDriver = $con->prepare("UPDATE payment_tokens SET isUsed = TRUE WHERE token = :token");
$markTokenDriver->execute([':token' => $tokenDriver]);
$insertSiro = $con->prepare("INSERT INTO siroWallet (driverId, passengerId, amount, paymentMethod, token, createdAt) VALUES (:driverId, :passengerId, :amount, :paymentMethod, :token, CURRENT_TIMESTAMP)");
$insertSiro->execute([':driverId' => $driverId, ':passengerId' => 'driver', ':amount' => $originalAmount, ':paymentMethod' => $paymentMethod, ':token' => $tokenSiro]);
$markTokenSiro = $con->prepare("UPDATE payment_tokens SET isUsed = TRUE WHERE token = :token");
$markTokenSiro->execute([':token' => $tokenSiro]);
$con->commit();
echo "<h1>تمت العملية بنجاح</h1><p>تمت إضافة الرصيد إلى محفظتك. يمكنك الآن العودة إلى التطبيق.</p>";
} catch (Throwable $e) {
if ($con->inTransaction()) { $con->rollBack(); }
error_log("VERIFY_ERROR: " . $e->getMessage() . " | OrderRef: " . $orderRef);
echo "<h1>حدث خطأ</h1><p>لقد تم استلام دفعتك بنجاح، ولكن حدث خطأ أثناء تحديث رصيدك. يرجى التواصل مع الدعم الفني وتزويدهم بالرقم المرجعي: " . htmlspecialchars($orderRef) . "</p>";
}
?>