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 "

خطأ: الرقم المرجعي للطلب مفقود.

"; 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 "

خطأ في تأكيد الدفع

لم نتمكن من تأكيد دفعتك. قد تستغرق العملية بضع لحظات. يرجى التحقق من رصيدك في التطبيق لاحقاً أو التواصل مع الدعم الفني.

"; 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 "

تمت معالجة هذا الطلب مسبقاً

الدفعة قيد المعالجة، يرجى التحقق من رصيدك في التطبيق.

"; 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 "

تمت العملية بنجاح

تمت إضافة الرصيد إلى محفظتك. يمكنك الآن العودة إلى التطبيق.

"; } catch (Throwable $e) { if ($con->inTransaction()) { $con->rollBack(); } error_log("VERIFY_ERROR: " . $e->getMessage() . " | OrderRef: " . $orderRef); echo "

حدث خطأ

لقد تم استلام دفعتك بنجاح، ولكن حدث خطأ أثناء تحديث رصيدك. يرجى التواصل مع الدعم الفني وتزويدهم بالرقم المرجعي: " . htmlspecialchars($orderRef) . "

"; } ?>