prepare(" SELECT SUM(amount) AS balance FROM driverWallet WHERE driverID = :id LIMIT 1 "); $stmt_driver->execute([':id' => $driverId]); $driver = $stmt_driver->fetch(PDO::FETCH_ASSOC); if (!$driver) { printFailure("Driver not found."); exit; } $payout_fee = 3500.00; // عمولة السحب $total_deduction = $amount + $payout_fee; // المبلغ المطلوب مع العمولة if ($driver['balance'] < $total_deduction) { printFailure("Insufficient balance. Required: $total_deduction"); exit; } // --- 3. إنشاء طلب السحب في قاعدة البيانات --- $sql = " INSERT INTO payout_requests (driver_id, driver_phone, amount, wallet_type) VALUES (:did, :phone, :amount, :wallet) "; $stmt = $con->prepare($sql); $stmt->execute([ ':did' => $driverId, ':phone' => $phone, ':amount'=> $amount, ':wallet'=> $wallet_type ]); if ($stmt->rowCount() > 0) { // --- 4. إرسال إشعار لخدمة العملاء --- $customerServicePhone = getenv('CUSTOMER_SERVICE_PHONE'); $message = "⚠️ طلب دفع جديد:\n" . "ID السائق: {$driverId}\n" . "هاتف السائق: {$phone}\n" . "نوع المحفظة: {$wallet_type}\n" . "المبلغ: {$amount} SYP\n\n" . "الرجاء من فريق خدمة العملاء تنفيذ عملية الدفع الآن."; // الإرسال (الفنكشن مُضمّن لديك مسبقًا) sendWhatsAppFromServer($customerServicePhone, $message); error_log("[RequestPayout] Successfully created payout request for driver ID: $driverId"); printSuccess("Payout request created successfully. It will be processed shortly."); } else { printFailure("Failed to create payout request."); } } catch (PDOException $e) { error_log("[RequestPayout] PDOException: " . $e->getMessage()); printFailure("A database error occurred."); } ?>