changed imports of AppLocalization
Also added beneficiary validation while quick pay within bank
This commit is contained in:
@@ -4,12 +4,12 @@ import '../../data/repositories/auth_repository.dart';
|
||||
class AuthInterceptor extends Interceptor {
|
||||
final AuthRepository _authRepository;
|
||||
final Dio _dio;
|
||||
|
||||
|
||||
AuthInterceptor(this._authRepository, this._dio);
|
||||
|
||||
|
||||
@override
|
||||
Future<void> onRequest(
|
||||
RequestOptions options,
|
||||
RequestOptions options,
|
||||
RequestInterceptorHandler handler,
|
||||
) async {
|
||||
// Skip auth header for login and refresh endpoints
|
||||
@@ -17,31 +17,38 @@ class AuthInterceptor extends Interceptor {
|
||||
options.path.contains('/auth/refresh')) {
|
||||
return handler.next(options);
|
||||
}
|
||||
|
||||
|
||||
// Get token and add to request
|
||||
final token = await _authRepository.getAccessToken();
|
||||
if (token != null) {
|
||||
options.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
|
||||
|
||||
return handler.next(options);
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
Future<void> onError(
|
||||
DioException err,
|
||||
DioException err,
|
||||
ErrorInterceptorHandler handler,
|
||||
) async {
|
||||
// Handle 401 errors by refreshing token and retrying
|
||||
if (err.response?.statusCode == 401) {
|
||||
final response = err.response;
|
||||
if (response?.statusCode == 401) {
|
||||
final data = response?.data;
|
||||
// Only refresh token if error is NOT INCORRECT_TPIN (or similar business error)
|
||||
if (data is Map && data['error'] == 'INCORRECT_TPIN') {
|
||||
// Pass the error through, do not retry
|
||||
return handler.next(err);
|
||||
}
|
||||
// On 401, try to get a new token
|
||||
final token = await _authRepository.getAccessToken();
|
||||
|
||||
|
||||
if (token != null) {
|
||||
// If we have a new token, retry the request
|
||||
final opts = err.requestOptions;
|
||||
opts.headers['Authorization'] = 'Bearer $token';
|
||||
|
||||
|
||||
try {
|
||||
final response = await _dio.fetch(opts);
|
||||
return handler.resolve(response);
|
||||
@@ -50,7 +57,7 @@ class AuthInterceptor extends Interceptor {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return handler.next(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
25
lib/api/services/beneficiary_service.dart
Normal file
25
lib/api/services/beneficiary_service.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class BeneficiaryService {
|
||||
final Dio _dio;
|
||||
|
||||
BeneficiaryService(this._dio);
|
||||
|
||||
Future<String> validateBeneficiaryWithinBank(String accountNumber) async {
|
||||
try {
|
||||
final response = await _dio.get('/api/beneficiary/validate/within-bank', queryParameters: {
|
||||
'accountNumber': accountNumber,
|
||||
});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return response.data['name'];
|
||||
} else {
|
||||
throw Exception(response.data['error'] ?? 'Failed to validate beneficiary');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw Exception('Network error: ${e.message}');
|
||||
} catch (e) {
|
||||
throw Exception('Unexpected error: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
}
|
@@ -11,8 +11,8 @@ class PaymentService {
|
||||
Future<PaymentResponse> processQuickPayWithinBank(Transfer transfer) async {
|
||||
try {
|
||||
await Future.delayed(const Duration(seconds: 3)); // Simulate delay
|
||||
final response =
|
||||
await _dio.post('/api/payment/transfer', data: transfer.toJson());
|
||||
|
||||
await _dio.post('/api/payment/transfer', data: transfer.toJson());
|
||||
|
||||
return PaymentResponse(
|
||||
isSuccess: true,
|
||||
@@ -20,8 +20,6 @@ class PaymentService {
|
||||
creditedAccount: transfer.toAccount,
|
||||
amount: transfer.amount,
|
||||
currency: "INR",
|
||||
errorMessage: response.data['errorMessage'],
|
||||
errorCode: response.data['errorCode'],
|
||||
);
|
||||
} on DioException catch (e) {
|
||||
log('DioException: ${e.toString()}');
|
||||
|
Reference in New Issue
Block a user