IFSC Code Validation and Add Beneficiary Validation API integration
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:kmobile/data/models/ifsc.dart';
|
||||
import 'package:kmobile/data/models/beneficiary.dart';
|
||||
|
||||
class BeneficiaryService {
|
||||
final Dio _dio;
|
||||
@@ -22,4 +24,84 @@ class BeneficiaryService {
|
||||
throw Exception('Unexpected error: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Future<ifsc?> validateIFSC(String ifscCode) async {
|
||||
try {
|
||||
final response = await _dio.get('/api/beneficiary/ifsc-details', queryParameters: {
|
||||
"ifscCode": ifscCode
|
||||
}
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return ifsc.fromJson(response.data);
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.response?.statusCode == 404) {
|
||||
print('Invalid IFSC code.');
|
||||
} else {
|
||||
print('API error: ${e.message}');
|
||||
}
|
||||
} catch (e) {
|
||||
print('Unexpected error: $e');
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
// Send Data for Validation
|
||||
Future<void> sendForValidation(Beneficiary beneficiary) async {
|
||||
try {
|
||||
print(beneficiary.toJson());
|
||||
final response = await _dio.post(
|
||||
'/api/beneficiary/add',
|
||||
data: beneficiary.toJson(),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print("SENT FOR VALIDATION");
|
||||
} else {
|
||||
print("VALIDATION REQUEST FAILED: ${response.statusCode}");
|
||||
}
|
||||
} catch (e) {
|
||||
print("ERROR IN SENDING REQUEST: $e");
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Poll to check if beneficiary is found
|
||||
Future<bool> checkIfFound(String accountNo) async {
|
||||
const int timeoutInSeconds = 30;
|
||||
const int intervalInSeconds = 2;
|
||||
const int maxTries = timeoutInSeconds ~/ intervalInSeconds;
|
||||
|
||||
int attempts = 0;
|
||||
|
||||
while (attempts < maxTries) {
|
||||
try {
|
||||
final response = await _dio.get(
|
||||
'/api/beneficiary/check?',
|
||||
queryParameters: {
|
||||
'accountNo': accountNo
|
||||
}
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print("FOUND");
|
||||
return true;
|
||||
} else if (response.statusCode == 404) {
|
||||
print("NOT FOUND");
|
||||
}
|
||||
} catch (e) {
|
||||
print("ERROR DURING STATUS: $e");
|
||||
}
|
||||
attempts++;
|
||||
}
|
||||
|
||||
print("Beneficiary not found within timeout.");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user