Skip to content

Commit 8c412f2

Browse files
committed
feat: refactor KYC document upload to use multipart form data
- Changed the KYC document upload method to utilize multipart form data instead of base64 encoding. - Updated the API request structure to remove file type and size parameters, focusing on file upload via FormData. - Enhanced error handling to check for various error indicators in the API response.
1 parent c8b5189 commit 8c412f2

1 file changed

Lines changed: 30 additions & 34 deletions

File tree

lib/core/exchange/data/datasources/bullbitcoin_api_datasource.dart

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import 'dart:convert' show base64Encode;
1+
import 'dart:convert' show jsonEncode;
22
import 'dart:math' show pow;
3-
import 'dart:typed_data' show Uint8List;
43

54
import 'package:bb_mobile/core/errors/bull_exception.dart';
65
import 'package:bb_mobile/core/exchange/data/models/dca_model.dart';
@@ -762,56 +761,53 @@ class BullbitcoinApiDatasource implements BitcoinPriceDatasource {
762761

763762
// ==================== KYC Upload API ====================
764763

765-
/// Upload a KYC document file using base64 encoding (same approach as chat)
764+
/// Upload a KYC document file using multipart form (same as BB-Exchange)
766765
Future<void> uploadKycDocument({
767766
required String apiKey,
768767
required List<int> fileBytes,
769768
required String fileName,
770769
required String docType,
771770
required String sourceDetail,
772771
}) async {
773-
// Determine file type from extension
774-
final extension = fileName.split('.').last.toLowerCase();
775-
final fileType = switch (extension) {
776-
'jpg' || 'jpeg' => 'image/jpeg',
777-
'png' => 'image/png',
778-
'pdf' => 'application/pdf',
779-
'gif' => 'image/gif',
780-
'webp' => 'image/webp',
781-
_ => 'application/octet-stream',
782-
};
783-
784-
final requestData = {
785-
'jsonrpc': '2.0',
786-
'id': '1',
787-
'method': 'createMyKYCIDDocument',
788-
'params': {
789-
'element': {
790-
'idTypeCode': docType,
791-
'sourceDetail': sourceDetail,
792-
'fileName': fileName,
793-
'fileType': fileType,
794-
'fileSize': fileBytes.length,
795-
'fileData': base64Encode(Uint8List.fromList(fileBytes)),
772+
final formData = FormData.fromMap({
773+
'file': MultipartFile.fromBytes(fileBytes, filename: fileName),
774+
'kycIDDocument': jsonEncode({
775+
'jsonrpc': '2.0',
776+
'id': 1,
777+
'method': 'createMyKYCIDDocument',
778+
'params': {
779+
'element': {
780+
'idTypeCode': docType,
781+
'sourceDetail': sourceDetail,
782+
},
796783
},
797-
},
798-
};
784+
}),
785+
});
799786

800787
final resp = await _http.post(
801-
_kycPath,
802-
data: requestData,
803-
options: Options(headers: {'X-API-Key': apiKey}),
788+
'$_kycPath/upload',
789+
data: formData,
790+
options: Options(
791+
headers: {'X-API-Key': apiKey},
792+
contentType: 'multipart/form-data',
793+
),
804794
);
805795

806796
if (resp.statusCode != 200) {
807797
throw Exception('File upload failed with status: ${resp.statusCode}');
808798
}
809799

800+
// Check if response data indicates an error condition
810801
final responseData = resp.data as Map<String, dynamic>?;
811802
if (responseData != null) {
812-
final error = responseData['error'];
813-
if (error != null) {
814-
throw Exception('File upload failed: $error');
803+
// Check for various error indicators in the response
804+
if (responseData.containsKey('error') ||
805+
responseData.containsKey('errors') ||
806+
(responseData.containsKey('result') &&
807+
responseData['result'] is Map<String, dynamic> &&
808+
(responseData['result']['error'] != null ||
809+
responseData['result']['errors'] != null))) {
810+
throw Exception('File upload failed: API returned error in response');
815811
}
816812
}
817813
}

0 commit comments

Comments
 (0)