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; } // 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) . "
"; } ?>