|
1 | | -import 'dart:convert' show base64Encode; |
| 1 | +import 'dart:convert' show jsonEncode; |
2 | 2 | import 'dart:math' show pow; |
3 | | -import 'dart:typed_data' show Uint8List; |
4 | 3 |
|
5 | 4 | import 'package:bb_mobile/core/errors/bull_exception.dart'; |
6 | 5 | import 'package:bb_mobile/core/exchange/data/models/dca_model.dart'; |
@@ -762,56 +761,53 @@ class BullbitcoinApiDatasource implements BitcoinPriceDatasource { |
762 | 761 |
|
763 | 762 | // ==================== KYC Upload API ==================== |
764 | 763 |
|
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) |
766 | 765 | Future<void> uploadKycDocument({ |
767 | 766 | required String apiKey, |
768 | 767 | required List<int> fileBytes, |
769 | 768 | required String fileName, |
770 | 769 | required String docType, |
771 | 770 | required String sourceDetail, |
772 | 771 | }) 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 | + }, |
796 | 783 | }, |
797 | | - }, |
798 | | - }; |
| 784 | + }), |
| 785 | + }); |
799 | 786 |
|
800 | 787 | 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 | + ), |
804 | 794 | ); |
805 | 795 |
|
806 | 796 | if (resp.statusCode != 200) { |
807 | 797 | throw Exception('File upload failed with status: ${resp.statusCode}'); |
808 | 798 | } |
809 | 799 |
|
| 800 | + // Check if response data indicates an error condition |
810 | 801 | final responseData = resp.data as Map<String, dynamic>?; |
811 | 802 | 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'); |
815 | 811 | } |
816 | 812 | } |
817 | 813 | } |
|
0 commit comments