publishInvoice method

Future<Invoice> publishInvoice({
  1. required String invoiceId,
  2. required PublishInvoiceRequest request,
  3. String? authToken,
})

Publishes the specified draft invoice.

After an invoice is published, Square follows up based on the invoice configuration. For example, Square sends the invoice to the customer's email address, charges the customer's card on file, or does nothing. Square also makes the invoice available on a Square-hosted invoice page.

Implementation

Future<Invoice> publishInvoice({
  required String invoiceId,
  required PublishInvoiceRequest request,
  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/publish");

  //print (endpoint.toString());

  var response = await
  http.post(endpoint, body: jsonEncode(request.toJson()), 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());
  }
}