Files
flash-call-otp/receiver_app_new/lib/services/api_service.dart
2026-05-23 16:17:20 +03:00

105 lines
3.2 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
class ApiService {
static const String _baseUrl = 'https://otp.intaleqapp.com/api/';
static const String _appKey =
'f3a9e7c1b8d5f2a4c6e9b1d3f5a7c9e1b3d5f7a9c1e3b5d7f9a1c3e5b7d9f1';
/// Request an OTP to be sent to the given phone number
/// Returns a map with {success, expires_in, method}
static Future<Map<String, dynamic>> requestOtp(
String phone,
String deviceType,
) async {
try {
final response = await http
.post(
Uri.parse('${_baseUrl}request-otp.php'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'phone': phone,
'device_type': deviceType,
'app_key': _appKey,
}),
)
.timeout(const Duration(seconds: 30));
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
return {
'success': data['success'] ?? false,
'expires_in': data['expires_in'] ?? 120,
'method': data['method'] ?? 'flash_call',
};
} else {
final data = jsonDecode(response.body) as Map<String, dynamic>;
return {
'success': false,
'expires_in': 0,
'method': 'flash_call',
'message': data['message'] ?? 'Server error: ${response.statusCode}',
};
}
} on http.ClientException catch (e) {
return {
'success': false,
'expires_in': 0,
'method': 'flash_call',
'message': 'Network error: ${e.message}',
};
} on FormatException {
return {
'success': false,
'expires_in': 0,
'method': 'flash_call',
'message': 'Invalid server response',
};
} catch (e) {
return {
'success': false,
'expires_in': 0,
'method': 'flash_call',
'message': 'Connection error: $e',
};
}
}
/// Verify an OTP code for the given phone number
/// Returns a map with {success, message}
static Future<Map<String, dynamic>> verifyOtp(
String phone,
String otp,
) async {
try {
final response = await http
.post(
Uri.parse('${_baseUrl}verify-otp.php'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'phone': phone, 'otp': otp, 'app_key': _appKey}),
)
.timeout(const Duration(seconds: 30));
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
return {
'success': data['success'] ?? false,
'message': data['message'] ?? 'Verification completed',
};
} else {
final data = jsonDecode(response.body) as Map<String, dynamic>;
return {
'success': false,
'message': data['message'] ?? 'Server error: ${response.statusCode}',
};
}
} on http.ClientException catch (e) {
return {'success': false, 'message': 'Network error: ${e.message}'};
} on FormatException {
return {'success': false, 'message': 'Invalid server response'};
} catch (e) {
return {'success': false, 'message': 'Connection error: $e'};
}
}
}