Initial commit for Tripz Admin
This commit is contained in:
@@ -1,22 +1,144 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:jwt_decoder/jwt_decoder.dart';
|
||||
import 'package:secure_string_operations/secure_string_operations.dart';
|
||||
|
||||
import '../../constant/api_key.dart';
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/char_map.dart';
|
||||
import '../../constant/info.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../../env/env.dart';
|
||||
import '../../main.dart';
|
||||
import '../../print.dart';
|
||||
import '../../views/widgets/elevated_btn.dart';
|
||||
import 'device_info.dart';
|
||||
import 'encrypt_decrypt.dart';
|
||||
|
||||
class CRUD {
|
||||
var dev = '';
|
||||
getJWT() async {
|
||||
dev = Platform.isAndroid ? 'android' : 'ios';
|
||||
|
||||
var payload = {
|
||||
'id': 'admin',
|
||||
'password': AK.passnpassenger,
|
||||
'aud': '${AK.allowed}$dev',
|
||||
};
|
||||
var response1 = await http.post(
|
||||
Uri.parse(AppLink.loginJwtDriver),
|
||||
body: payload,
|
||||
);
|
||||
|
||||
if (response1.statusCode == 200) {
|
||||
final decodedResponse1 = jsonDecode(response1.body);
|
||||
|
||||
final jwt = decodedResponse1['jwt'];
|
||||
await box.write(BoxName.jwt, X.c(X.c(X.c(jwt, cn), cC), cs));
|
||||
// await AppInitializer().getKey();
|
||||
}
|
||||
}
|
||||
|
||||
getJwtWallet() async {
|
||||
String fingerPrint = await DeviceHelper.getDeviceFingerprint();
|
||||
print('fingerPrint: ${fingerPrint}');
|
||||
dev = Platform.isAndroid ? 'android' : 'ios';
|
||||
var payload = {
|
||||
'id': '1',
|
||||
'password': AK.passnpassenger,
|
||||
'aud': '${Env.allowedWallet}$dev',
|
||||
'fingerPrint': fingerPrint
|
||||
};
|
||||
// Log.print('payload: ${payload}');
|
||||
var response1 = await http.post(
|
||||
Uri.parse(AppLink.loginWalletAdmin),
|
||||
body: payload,
|
||||
);
|
||||
// Log.print('response.request: ${response1.request}');
|
||||
// Log.print('response.body: ${response1.body}');
|
||||
// print(payload);
|
||||
// Log.print(
|
||||
// 'jsonDecode(response1.body)["jwt"]: ${jsonDecode(response1.body)['jwt']}');
|
||||
await box.write(BoxName.hmac, jsonDecode(response1.body)['hmac']);
|
||||
return jsonDecode(response1.body)['jwt'].toString();
|
||||
}
|
||||
|
||||
Future<dynamic> postWallet(
|
||||
{required String link, Map<String, dynamic>? payload}) async {
|
||||
var s = await getJwtWallet();
|
||||
Log.print('jwt: ${s}');
|
||||
final hmac = box.read(BoxName.hmac);
|
||||
Log.print('hmac: ${hmac}');
|
||||
var url = Uri.parse(link);
|
||||
Log.print('url: ${url}');
|
||||
try {
|
||||
// await LoginDriverController().getJWT();
|
||||
|
||||
var response = await http.post(
|
||||
url,
|
||||
body: payload,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
'Authorization': 'Bearer $s',
|
||||
'X-HMAC-Auth': hmac.toString(),
|
||||
},
|
||||
);
|
||||
Log.print('response.request:${response.request}');
|
||||
Log.print('response.body: ${response.body}');
|
||||
Log.print('payload:$payload');
|
||||
if (response.statusCode == 200) {
|
||||
try {
|
||||
var jsonData = jsonDecode(response.body);
|
||||
if (jsonData['status'] == 'success') {
|
||||
return jsonData;
|
||||
} else {
|
||||
return jsonData['status'];
|
||||
}
|
||||
} catch (e) {
|
||||
// addError(e.toString(), 'crud().post - JSON decoding');
|
||||
return 'failure';
|
||||
}
|
||||
} else if (response.statusCode == 401) {
|
||||
// Specifically handle 401 Unauthorized
|
||||
var jsonData = jsonDecode(response.body);
|
||||
|
||||
if (jsonData['error'] == 'Token expired') {
|
||||
return 'token_expired'; // Return a specific value for token expiration
|
||||
} else {
|
||||
// Other 401 errors
|
||||
// addError('Unauthorized: ${jsonData['error']}', 'crud().post - 401');
|
||||
return 'failure';
|
||||
}
|
||||
} else {
|
||||
// addError('Non-200 response code: ${response.statusCode}',
|
||||
// 'crud().post - Other');
|
||||
return 'failure';
|
||||
}
|
||||
} catch (e) {
|
||||
// addError('HTTP request error: $e', 'crud().post - HTTP');
|
||||
return 'failure';
|
||||
}
|
||||
}
|
||||
|
||||
Future<dynamic> get({
|
||||
required String link,
|
||||
Map<String, dynamic>? payload,
|
||||
}) async {
|
||||
if (box.read(BoxName.jwt) == null) {
|
||||
await getJWT();
|
||||
}
|
||||
bool isTokenExpired = JwtDecoder.isExpired(X
|
||||
.r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs)
|
||||
.toString()
|
||||
.split(AppInformation.addd)[0]);
|
||||
if (isTokenExpired) {
|
||||
await getJWT();
|
||||
}
|
||||
// await Get.put(LoginDriverController()).getJWT();
|
||||
var url = Uri.parse(
|
||||
link,
|
||||
);
|
||||
@@ -26,18 +148,148 @@ class CRUD {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
'Authorization':
|
||||
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials.toString()))}',
|
||||
'Bearer ${X.r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs).toString().split(AppInformation.addd)[0]}'
|
||||
},
|
||||
);
|
||||
Log.print('response: ${response.request}');
|
||||
// if (response.statusCode == 200) {
|
||||
var jsonData = jsonDecode(response.body);
|
||||
if (response.statusCode == 200) {
|
||||
Log.print('response: ${response.body}');
|
||||
var jsonData = jsonDecode(response.body);
|
||||
if (jsonData['status'] == 'success') {
|
||||
return response.body;
|
||||
}
|
||||
|
||||
if (jsonData['status'] == 'success') {
|
||||
return response.body;
|
||||
return jsonData['status'];
|
||||
} else if (response.statusCode == 401) {
|
||||
// Specifically handle 401 Unauthorized
|
||||
var jsonData = jsonDecode(response.body);
|
||||
|
||||
if (jsonData['error'] == 'Token expired') {
|
||||
// Show snackbar prompting to re-login
|
||||
// await Get.put(LoginDriverController()).getJWT();
|
||||
// mySnackbarSuccess('please order now'.tr);
|
||||
|
||||
return 'token_expired'; // Return a specific value for token expiration
|
||||
} else {
|
||||
// Other 401 errors
|
||||
// addError('Unauthorized: ${jsonData['error']}', 'crud().post - 401');
|
||||
return 'failure';
|
||||
}
|
||||
} else {
|
||||
// addError('Non-200 response code: ${response.statusCode}',
|
||||
// 'crud().post - Other');
|
||||
return 'failure';
|
||||
}
|
||||
}
|
||||
|
||||
return jsonData['status'];
|
||||
// Future<dynamic> getWallet({
|
||||
// required String link,
|
||||
// Map<String, dynamic>? payload,
|
||||
// }) async {
|
||||
// var s = await getJwtWallet();
|
||||
// var url = Uri.parse(
|
||||
// link,
|
||||
// );
|
||||
// var response = await http.post(
|
||||
// url,
|
||||
// body: payload,
|
||||
// headers: {
|
||||
// "Content-Type": "application/x-www-form-urlencoded",
|
||||
// 'Authorization': 'Bearer $s'
|
||||
// },
|
||||
// );
|
||||
// if (response.statusCode == 200) {
|
||||
// var jsonData = jsonDecode(response.body);
|
||||
// if (jsonData['status'] == 'success') {
|
||||
// return response.body;
|
||||
// }
|
||||
|
||||
// return jsonData['status'];
|
||||
// } else if (response.statusCode == 401) {
|
||||
// // Specifically handle 401 Unauthorized
|
||||
// var jsonData = jsonDecode(response.body);
|
||||
|
||||
// if (jsonData['error'] == 'Token expired') {
|
||||
// // Show snackbar prompting to re-login
|
||||
// // await Get.put(LoginDriverController()).getJwtWallet();
|
||||
|
||||
// return 'token_expired'; // Return a specific value for token expiration
|
||||
// } else {
|
||||
// // Other 401 errors
|
||||
// addError('Unauthorized: ${jsonData['error']}', 'crud().post - 401');
|
||||
// return 'failure';
|
||||
// }
|
||||
// } else {
|
||||
// addError('Non-200 response code: ${response.statusCode}',
|
||||
// 'crud().post - Other');
|
||||
// return 'failure';
|
||||
// }
|
||||
// }
|
||||
|
||||
Future<dynamic> post(
|
||||
{required String link, Map<String, dynamic>? payload}) async {
|
||||
var url = Uri.parse(link);
|
||||
try {
|
||||
bool isTokenExpired = JwtDecoder.isExpired(X
|
||||
.r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs)
|
||||
.toString()
|
||||
.split(AppInformation.addd)[0]);
|
||||
if (isTokenExpired) {
|
||||
await getJWT();
|
||||
}
|
||||
var response = await http.post(
|
||||
url,
|
||||
body: payload,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
'Authorization':
|
||||
'Bearer ${X.r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs).toString().split(AppInformation.addd)[0]}'
|
||||
// 'Authorization': 'Bearer ${box.read(BoxName.jwt)}'
|
||||
},
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
try {
|
||||
var jsonData = jsonDecode(response.body);
|
||||
if (jsonData['status'] == 'success') {
|
||||
return jsonData;
|
||||
} else {
|
||||
return jsonData['status'];
|
||||
}
|
||||
} catch (e) {
|
||||
// addError(e.toString(), 'crud().post - JSON decoding');
|
||||
return 'failure';
|
||||
}
|
||||
} else if (response.statusCode == 401) {
|
||||
// Specifically handle 401 Unauthorized
|
||||
var jsonData = jsonDecode(response.body);
|
||||
|
||||
if (jsonData['error'] == 'Token expired') {
|
||||
// Show snackbar prompting to re-login
|
||||
await getJWT();
|
||||
// MyDialog().getDialog(
|
||||
// 'Session expired. Please log in again.'.tr,
|
||||
// '',
|
||||
// () {
|
||||
// Get.put(LoginController()).loginUsingCredentials(
|
||||
// box.read(BoxName.passengerID), box.read(BoxName.email));
|
||||
// Get.back();
|
||||
// },
|
||||
// );
|
||||
|
||||
return 'token_expired'; // Return a specific value for token expiration
|
||||
} else {
|
||||
// Other 401 errors
|
||||
// addError('Unauthorized: ${jsonData['error']}', 'crud().post - 401');
|
||||
return 'failure';
|
||||
}
|
||||
} else {
|
||||
// addError('Non-200 response code: ${response.statusCode}',
|
||||
// 'crud().post - Other');
|
||||
return 'failure';
|
||||
}
|
||||
} catch (e) {
|
||||
// addError('HTTP request error: $e', 'crud().post - HTTP');
|
||||
return 'failure';
|
||||
}
|
||||
}
|
||||
|
||||
// }
|
||||
@@ -67,10 +319,7 @@ class CRUD {
|
||||
http.StreamedResponse response = await request.send();
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print(await response.stream.bytesToString());
|
||||
} else {
|
||||
print(response.reasonPhrase);
|
||||
}
|
||||
} else {}
|
||||
}
|
||||
|
||||
Future<dynamic> getAgoraToken({
|
||||
@@ -209,15 +458,12 @@ class CRUD {
|
||||
|
||||
String imagePathFull =
|
||||
'${AppLink.server}/card_image/$imagePath-$driverID.jpg';
|
||||
Log.print('imagePathFull: $imagePathFull');
|
||||
var request = http.Request(
|
||||
'POST',
|
||||
Uri.parse(
|
||||
'https://eastus.api.cognitive.microsoft.com/computervision/imageanalysis:analyze?features=caption,read&model-version=latest&language=en&api-version=2024-02-01'));
|
||||
request.body = json.encode({"url": imagePathFull});
|
||||
Log.print('request.body: ${request.body}');
|
||||
request.headers.addAll(headers);
|
||||
Log.print('request.headers: ${request.headers}');
|
||||
|
||||
http.StreamedResponse response = await request.send();
|
||||
|
||||
@@ -281,49 +527,6 @@ class CRUD {
|
||||
} else {}
|
||||
}
|
||||
|
||||
Future<dynamic> post({
|
||||
required String link,
|
||||
Map<String, dynamic>? payload,
|
||||
}) async {
|
||||
try {
|
||||
var url = Uri.parse(link);
|
||||
var response = await http.post(
|
||||
url,
|
||||
body: payload,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
'Authorization':
|
||||
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}',
|
||||
},
|
||||
);
|
||||
Log.print('payload: ${payload}');
|
||||
var jsonData = jsonDecode(response.body);
|
||||
Log.print('jsonData: ${jsonData}');
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
if (jsonData['status'] == 'success') {
|
||||
return response.body;
|
||||
} else {
|
||||
return jsonData['status'];
|
||||
}
|
||||
} else {
|
||||
// Log non-fatal errors to Crashlytics
|
||||
await FirebaseCrashlytics.instance.log("Failed API request :$link");
|
||||
FirebaseCrashlytics.instance.recordError(
|
||||
Exception('Error: ${response.statusCode}'),
|
||||
null,
|
||||
reason: 'Failed HTTP POST request',
|
||||
);
|
||||
return response.statusCode;
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
// Log error to Crashlytics
|
||||
await FirebaseCrashlytics.instance
|
||||
.recordError(e, stackTrace, reason: 'Exception in post request');
|
||||
return 'Error occurred';
|
||||
}
|
||||
}
|
||||
|
||||
Future<dynamic> kazumiSMS({
|
||||
required String link,
|
||||
Map<String, dynamic>? payload,
|
||||
@@ -344,7 +547,6 @@ class CRUD {
|
||||
if (response.statusCode == 200) {
|
||||
var responseBody = await response.stream.bytesToString();
|
||||
var data = json.decode(responseBody);
|
||||
Log.print('data: ${data}');
|
||||
return data;
|
||||
} else {}
|
||||
}
|
||||
@@ -366,7 +568,6 @@ class CRUD {
|
||||
body: body,
|
||||
headers: headers,
|
||||
);
|
||||
Log.print('res: ${res.body}');
|
||||
}
|
||||
|
||||
Future<dynamic> postPayMob({
|
||||
|
||||
@@ -1,6 +1,48 @@
|
||||
// import 'dart:io';
|
||||
|
||||
// import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'dart:io';
|
||||
|
||||
class DeviceHelper {
|
||||
static Future<String> getDeviceFingerprint() async {
|
||||
final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
|
||||
var deviceData;
|
||||
|
||||
try {
|
||||
if (Platform.isAndroid) {
|
||||
// Fetch Android-specific device information
|
||||
AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
|
||||
deviceData = androidInfo.toMap(); // Convert to a map for easier access
|
||||
// Log.print('deviceData: ${jsonEncode(deviceData)}');
|
||||
} else if (Platform.isIOS) {
|
||||
// Fetch iOS-specific device information
|
||||
IosDeviceInfo iosInfo = await deviceInfoPlugin.iosInfo;
|
||||
deviceData = iosInfo.toMap(); // Convert to a map for easier access
|
||||
} else {
|
||||
throw UnsupportedError('Unsupported platform');
|
||||
}
|
||||
|
||||
// Extract relevant device information
|
||||
final String deviceId = Platform.isAndroid
|
||||
? deviceData['androidId'] ?? deviceData['serialNumber'] ?? 'unknown'
|
||||
: deviceData['identifierForVendor'] ?? 'unknown';
|
||||
|
||||
final String deviceModel = deviceData['model'] ?? 'unknown';
|
||||
final String osVersion = Platform.isAndroid
|
||||
? deviceData['version']['release'] ?? 'unknown'
|
||||
: deviceData['systemVersion'] ?? 'unknown';
|
||||
|
||||
// Log the extracted information
|
||||
|
||||
// Generate and return the encrypted fingerprint
|
||||
final String fingerprint = '${deviceId}_${deviceModel}_$osVersion';
|
||||
// print(EncryptionHelper.instance.encryptData(fingerprint));
|
||||
return (fingerprint);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to generate device fingerprint');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// class DeviceInfoPlus {
|
||||
// static List<Map<String, dynamic>> deviceDataList = [];
|
||||
|
||||
70
lib/controller/functions/encrypt_decrypt.dart
Normal file
70
lib/controller/functions/encrypt_decrypt.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
import 'package:encrypt/encrypt.dart' as encrypt;
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:sefer_admin1/env/env.dart';
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../main.dart';
|
||||
|
||||
class EncryptionHelper {
|
||||
static EncryptionHelper? _instance;
|
||||
|
||||
late final encrypt.Key key;
|
||||
late final encrypt.IV iv;
|
||||
|
||||
EncryptionHelper._(this.key, this.iv);
|
||||
static EncryptionHelper get instance {
|
||||
if (_instance == null) {
|
||||
throw Exception(
|
||||
"EncryptionHelper is not initialized. Call `await EncryptionHelper.initialize()` in main.");
|
||||
}
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
/// Initializes and stores the instance globally
|
||||
static Future<void> initialize() async {
|
||||
if (_instance != null) {
|
||||
debugPrint("EncryptionHelper is already initialized.");
|
||||
return; // Prevent re-initialization
|
||||
}
|
||||
debugPrint("Initializing EncryptionHelper...");
|
||||
// Read stored keys
|
||||
String? keyOfApp = Env.keyOfApp.toString().split('XrXlBl')[0];
|
||||
//await storage.read(key: BoxName.keyOfApp);
|
||||
// Log.print('keyOfApp: ${keyOfApp}');
|
||||
String? initializationVector = Env.initializationVector;
|
||||
// await storage.read(key: BoxName.initializationVector);
|
||||
// Log.print('initializationVector: ${initializationVector}');
|
||||
// Set the global instance
|
||||
_instance = EncryptionHelper._(
|
||||
encrypt.Key.fromUtf8(keyOfApp!),
|
||||
encrypt.IV.fromUtf8(initializationVector!),
|
||||
);
|
||||
debugPrint("EncryptionHelper initialized successfully.");
|
||||
}
|
||||
|
||||
/// Encrypts a string
|
||||
String encryptData(String plainText) {
|
||||
try {
|
||||
final encrypter =
|
||||
encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc));
|
||||
final encrypted = encrypter.encrypt(plainText, iv: iv);
|
||||
return encrypted.base64;
|
||||
} catch (e) {
|
||||
debugPrint('Encryption Error: $e');
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/// Decrypts a string
|
||||
String decryptData(String encryptedText) {
|
||||
try {
|
||||
final encrypter =
|
||||
encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc));
|
||||
final encrypted = encrypt.Encrypted.fromBase64(encryptedText);
|
||||
return encrypter.decrypt(encrypted, iv: iv);
|
||||
} catch (e) {
|
||||
debugPrint('Decryption Error: $e');
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,17 +21,17 @@ class WalletController extends GetxController {
|
||||
addPaymentToDriver(String amount, driverID, token) async {
|
||||
paymentToken = await generateTokenDriver(amount.toString(), driverID);
|
||||
var res = await CRUD().post(link: AppLink.addDrivePayment, payload: {
|
||||
'rideId': 'gift$driverID',
|
||||
'rideId': 'gift_$driverID _${DateTime.now().toIso8601String()}',
|
||||
'amount': amount,
|
||||
'payment_method': 'visaRide',
|
||||
'passengerID': 'passengerId',
|
||||
'passengerID': 'gift',
|
||||
'token': paymentToken,
|
||||
'driverID': driverID.toString(),
|
||||
});
|
||||
if (res != 'failure') {
|
||||
FirebaseMessagesController().sendNotificationToAnyWithoutData(
|
||||
'you have gift from SEFER'.tr,
|
||||
'',
|
||||
"لديك هدية من سفَر".tr,
|
||||
'لقد حصلت على هدية من سفر بقيمة $amount ',
|
||||
token, // Access token correctly
|
||||
'ding.wav',
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user