prepare("SELECT id FROM companies WHERE id = ? AND tenant_id = ?"); $stmt->execute([$companyId, $tenantId]); if (!$stmt->fetch()) json_error('Access denied', 403); // 3. Test Connection (Optional but recommended) $jofotara = new JoFotara(); // Here you would typically call a health check endpoint if JoFotara provides one, // or just assume the credentials are correct for now. // 4. Update Company with Encrypted Credentials $stmtUpdate = $db->prepare(" UPDATE companies SET jofotara_client_id_encrypted = ?, jofotara_secret_key_encrypted = ?, jofotara_income_source_sequence = ?, updated_at = NOW() WHERE id = ? "); $stmtUpdate->execute([ Encryption::encrypt($clientId), Encryption::encrypt($secretKey), $sequence, $companyId ]); json_success(null, 'تم ربط الشركة بنظام جوفوترة بنجاح'); } catch (\Exception $e) { error_log("JoFotara Connection Error: " . $e->getMessage()); safe_error($e, 'companies/connect_jofotara', 'فشل في ربط جوفوترا. يرجى المحاولة مرة أخرى.'); }