cancelInvoice method

Future<bool> cancelInvoice(
  1. String transactionNo
)

Cancels an existing invoice.

Implementation

Future<bool> cancelInvoice(String transactionNo) async {
  try {
    if (idToken == null) await _authenticate();

    /// Request body parameters
    Map<String, dynamic> requestBody = {
      'transactionNo': transactionNo,
    };

    final response = await http.post(
      Uri.parse('$apiBaseUrl/api/cancelInvoice'),
      headers: {
        'accept': '*/*',
        'content-type': 'application/json',
        'Authorization': 'Bearer $idToken',
      },
      body: jsonEncode(requestBody),
    );

    if (response.statusCode != 200) {
      _handleResponseError(response, 'Failed to cancel the invoice');
    }

    if (response.body.isEmpty) {
      throw Exception('Failed to cancel the invoice');
    }

    /// Decode the JSON response and extract the token
    final responseData = json.decode(response.body);

    /// check if the response contains an id_token
    if (responseData is! Map<String, dynamic> ||
        !responseData.containsKey('success')) {
      throw Exception('Failed to cancel the invoice');
    }

    return responseData['success'] == 'true';
  } catch (e) {
    rethrow;
  }
}