forked from hesabix/arc
114 lines
3.6 KiB
Dart
Executable file
114 lines
3.6 KiB
Dart
Executable file
import 'package:dio/dio.dart';
|
|
import 'package:hesabix_ui/core/api_client.dart';
|
|
|
|
class PublicInvoiceShareService {
|
|
final ApiClient _apiClient;
|
|
|
|
PublicInvoiceShareService({ApiClient? apiClient})
|
|
: _apiClient = apiClient ?? ApiClient();
|
|
|
|
/// بدون احراز هویت — مسیر public API
|
|
Future<Map<String, dynamic>> fetchByCode(String code) async {
|
|
try {
|
|
final response = await _apiClient.get<Map<String, dynamic>>(
|
|
'/api/v1/public/invoice-links/${Uri.encodeComponent(code)}',
|
|
);
|
|
final body = response.data;
|
|
if (body is Map<String, dynamic>) {
|
|
if (body['success'] == true) {
|
|
final data = body['data'];
|
|
if (data is Map<String, dynamic>) {
|
|
return data;
|
|
}
|
|
}
|
|
final error = body['error'];
|
|
throw DioException(
|
|
requestOptions: response.requestOptions,
|
|
error: error ?? {'message': 'خطا در دریافت اطلاعات'},
|
|
response: response,
|
|
);
|
|
}
|
|
throw DioException(
|
|
requestOptions: response.requestOptions,
|
|
error: {'message': 'پاسخ نامعتبر'},
|
|
response: response,
|
|
);
|
|
} on DioException {
|
|
rethrow;
|
|
} catch (e) {
|
|
throw DioException(
|
|
requestOptions: RequestOptions(path: '/api/v1/public/invoice-links/$code'),
|
|
error: e,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<List<int>> fetchPdfByCode(String code, {String? calendarType}) async {
|
|
try {
|
|
final response = await _apiClient.get<List<int>>(
|
|
'/api/v1/public/invoice-links/${Uri.encodeComponent(code)}/pdf',
|
|
responseType: ResponseType.bytes,
|
|
options: Options(
|
|
headers: {
|
|
'Accept': 'application/pdf',
|
|
if (calendarType != null && calendarType.trim().isNotEmpty)
|
|
'X-Calendar-Type': calendarType.trim(),
|
|
},
|
|
),
|
|
);
|
|
final bytes = response.data;
|
|
if (bytes != null && bytes.isNotEmpty) {
|
|
return bytes;
|
|
}
|
|
throw DioException(
|
|
requestOptions: response.requestOptions,
|
|
error: {'message': 'PDF خالی دریافت شد'},
|
|
response: response,
|
|
);
|
|
} on DioException {
|
|
rethrow;
|
|
} catch (e) {
|
|
throw DioException(
|
|
requestOptions: RequestOptions(path: '/api/v1/public/invoice-links/$code/pdf'),
|
|
error: e,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// بدون احراز هویت — شروع پرداخت آنلاین (بازگشت لینک درگاه)
|
|
Future<Map<String, dynamic>> startOnlinePayment(String code, double amount) async {
|
|
try {
|
|
final response = await _apiClient.post<Map<String, dynamic>>(
|
|
'/api/v1/public/invoice-links/${Uri.encodeComponent(code)}/pay/start',
|
|
data: {'amount': amount},
|
|
);
|
|
final body = response.data;
|
|
if (body is Map<String, dynamic>) {
|
|
if (body['success'] == true) {
|
|
final data = body['data'];
|
|
if (data is Map<String, dynamic>) {
|
|
return data;
|
|
}
|
|
}
|
|
final error = body['error'];
|
|
throw DioException(
|
|
requestOptions: response.requestOptions,
|
|
error: error ?? {'message': 'خطا در شروع پرداخت'},
|
|
response: response,
|
|
);
|
|
}
|
|
throw DioException(
|
|
requestOptions: response.requestOptions,
|
|
error: {'message': 'پاسخ نامعتبر'},
|
|
response: response,
|
|
);
|
|
} on DioException {
|
|
rethrow;
|
|
} catch (e) {
|
|
throw DioException(
|
|
requestOptions: RequestOptions(path: '/api/v1/public/invoice-links/$code/pay/start'),
|
|
error: e,
|
|
);
|
|
}
|
|
}
|
|
}
|