cancelInvoice method

Future<Invoice> cancelInvoice({
  1. required int invoiceId,
  2. required int version,
  3. String? authToken,
})

Cancels an invoice.

The seller cannot collect payments for the canceled invoice.

You cannot cancel an invoice in the DRAFT state or in a terminal state: PAID, REFUNDED, CANCELED, or FAILED.

Implementation

Future<Invoice> cancelInvoice({
  required int invoiceId,
  required int version,
  String? authToken,
}) async {

  authToken ??= authenticationService.getCachedToken()?.accessToken;

  Map<String, String> headers = {
    "Authorization": "Bearer ${authToken ?? ""}",
    'Content-Type': 'application/json; charset=UTF-8',
    'Accept': 'application/json',

  };

  Uri endpoint = Uri.https(
      baseUrl, "/v2/invoices/$invoiceId}/cancel");

  //print (endpoint.toString());

  var request = {
    "version": version
  };

  var response = await
  http.post(endpoint, body: jsonEncode(request), headers: headers);

  if (response.statusCode == 200) {
    print (jsonDecode(response.body));
    return InvoiceResponse.fromJson(jsonDecode(response.body)).invoice!;
  }
  else {
    print (response.body);
    throw InvoiceException(statusCode: response.statusCode, message: InvoiceResponse.fromJson(jsonDecode(response.body)).errors?[0].detail?.toString());
  }
}