fetchRecommendedInstruments method

Future<Map<String, dynamic>?> fetchRecommendedInstruments(
  1. String uniqueRef
)

New API call to fetch saved/recommended instruments

Implementation

Future<Map<String, dynamic>?> fetchRecommendedInstruments(String uniqueRef) async {
  final apiUrl =
      '$_baseUrl/shoppers/$uniqueRef/recommended-instruments';
      // Note: verify the actual endpoint for recommended instruments with BoxPay docs

  try {
    final response = await http.get(
      Uri.parse(apiUrl),
      headers: {
        'Authorization': "Session $shopperToken",
        'Content-Type': 'application/json',
      },
    );

    if (response.statusCode == 200) {
final data = jsonDecode(response.body);

// Check if data is a list and not empty
if (data != null && data is List && data.isNotEmpty) {

  // Look for the first instrument where the type is 'Card'
  final cardInstrument = data.firstWhere(
    (instrument) => instrument['type'] == 'Card',
    orElse: () => null,
  );

  if (cardInstrument != null) {
    return cardInstrument;
  }
}
// If no card is found (e.g., only UPI exists), return null
return null;
    }
  } catch (e) {
    _showErrorDialog();
  }
  return null;
}