The following snippets helps to get clover merchant info and as well search customers by name and mobile number.
public class CloverClient { public static class Response { int statusCode; String response;
public Response(int statusCode, String response) {
this.statusCode = statusCode;
this.response = response;
}
public String toString() {
return "statusCode=" + statusCode + ", " + response;
}
}
private static final String CLOVER_BASE_URL_V3 = "https://sandbox.dev.clover.com/v3/merchants/";
protected static HttpClient client;
static {
client = HttpClientBuilder.create().setUserAgent("Next Set 2.0 Client").build();
}
protected String merchantId;
protected String authKey;
public CloverClient(String merchantId, String authKey) {
this.merchantId = merchantId;
this.authKey = authKey;
}
protected void setCommonHeaders(HttpRequestBase request) {
request.setHeader("Accept", "application/json");
request.setHeader("Authorization", "Bearer " + authKey);
}
public Response get(String url) {
int statusCode = 500;
String responseStr = null;
try {
HttpGet request = new HttpGet(url);
setCommonHeaders(request);
HttpResponse response = client.execute(request);
statusCode = response.getStatusLine().getStatusCode();
responseStr = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
e.printStackTrace();
}
return new Response(statusCode, responseStr);
}
public Response post(String url, String payload) {
int statusCode = 500;
String responseStr = null;
try {
HttpPost request = new HttpPost(url);
setCommonHeaders(request);
request.setEntity(new StringEntity(payload));
HttpResponse response = client.execute(request);
statusCode = response.getStatusLine().getStatusCode();
responseStr = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
e.printStackTrace();
}
return new Response(statusCode, responseStr);
}
public Response getMerchantInfo() {
String url = CLOVER_BASE_URL_V3 + merchantId;
return get(url);
}
public Customer searchCustomer(String lastName, String phoneNumber) {
String url = null;
if (lastName != null) {
url = CLOVER_BASE_URL_V3 + merchantId + "/customers?filter=lastName=" + URLEncoder.encode(lastName) + "&expand=phoneNumbers";
} else {
url = CLOVER_BASE_URL_V3 + merchantId + "/customers?expand=phoneNumbers";
}
Response response = get(url);
JSONObject responseJSON = (JSONObject) JSONValue.parse(response.response);
JSONArray customersJSONArray = (JSONArray) responseJSON.get("elements");
JSONObject customerJSON = null;
JSONObject phoneNumbersJSON = null;
JSONArray phonesJSONArray = null;
String phone = null;
Customer customer = null;
if (customersJSONArray != null) {
for (int i = 0 ; i < customersJSONArray.size() ; i ++) {
customerJSON = (JSONObject) customersJSONArray.get(i);
phoneNumbersJSON = (JSONObject) customerJSON.get("phoneNumbers");
phonesJSONArray = (JSONArray) phoneNumbersJSON.get("elements");
if (phonesJSONArray == null) {
continue;
}
for (int j = 0 ; j < phonesJSONArray.size() ; j ++) {
phone = String.valueOf(((JSONObject) phonesJSONArray.get(j)).get("phoneNumber"));
if (phone != null && phone.equals(phoneNumber)) {
customer = new Customer();
customer.setId(String.valueOf(customerJSON.get("id")));
customer.setFirstName(String.valueOf(customerJSON.get("firstName")));
customer.setLastName(String.valueOf(customerJSON.get("lastName")));
customer.setPhoneNumber(phoneNumber);
break;
}
}
if (customer != null) {
break;
}
}
}
return customer;
}
}

Log in or sign up for Devpost to join the conversation.