update gen qr

This commit is contained in:
Phuoc Nguyen
2025-11-21 17:31:49 +07:00
parent 4913a4e04b
commit 06b0834822
8 changed files with 192 additions and 44 deletions

View File

@@ -139,19 +139,59 @@ class OrderRemoteDataSource {
}
// Extract order info from Frappe response
// Response format: { message: { success: true, message: "...", data: { name: "SAL-ORD-2025-00078", ... } } }
final message = data['message'] as Map<String, dynamic>?;
if (message == null) {
throw Exception('No message field in createOrder response');
}
final orderData = message['data'] as Map<String, dynamic>?;
if (orderData == null) {
throw Exception('No data field in createOrder response');
}
final orderId = orderData['name'] as String?;
if (orderId == null || orderId.isEmpty) {
throw Exception('No order ID (name) in createOrder response');
}
// Return standardized response
return {
'orderId': message['name'] ?? '',
'orderNumber': message['name'] ?? '',
'fullResponse': message,
'orderId': orderId,
'orderNumber': orderId,
'fullResponse': orderData,
};
} catch (e) {
throw Exception('Failed to create order: $e');
}
}
/// Generate QR code for payment
///
/// Calls: POST /api/method/building_material.building_material.api.v1.qrcode.generate
/// Body: { "order_id": "SAL-ORD-2025-00048" }
/// Returns: { "qr_code": "...", "amount": null, "transaction_id": "...", "bank_info": {...} }
Future<Map<String, dynamic>> generateQrCode(String orderId) async {
try {
final response = await _dioClient.post<Map<String, dynamic>>(
'${ApiConstants.frappeApiMethod}${ApiConstants.generateQrCode}',
data: {'order_id': orderId},
);
final data = response.data;
if (data == null) {
throw Exception('No data received from generateQrCode API');
}
// Extract QR code info from Frappe response
final message = data['message'] as Map<String, dynamic>?;
if (message == null) {
throw Exception('No message field in generateQrCode response');
}
return message;
} catch (e) {
throw Exception('Failed to generate QR code: $e');
}
}
}

View File

@@ -56,4 +56,13 @@ class OrderRepositoryImpl implements OrderRepository {
throw Exception('Failed to create order: $e');
}
}
@override
Future<Map<String, dynamic>> generateQrCode(String orderId) async {
try {
return await _remoteDataSource.generateQrCode(orderId);
} catch (e) {
throw Exception('Failed to generate QR code: $e');
}
}
}