searchCustomer method

Future<CustomerResponse> searchCustomer({
  1. required SearchCustomerRequest request,
  2. String? authToken,
})

Searches the customer profiles associated with a Square account using a supported query filter.

Calling SearchCustomers without any explicit query filter returns all customer profiles ordered alphabetically based on given_name and family_name.

Implementation

Future<CustomerResponse> searchCustomer({
  required SearchCustomerRequest 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/customers/search");

  //print (endpoint.toString());

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

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