I’m using the customer’s API in ‘square-php-sdk’ to do two types of queries. One for a list of customers and one for a specific customer. Examples of those queries below. You’d think these two would return the same object type. The single customer query returns an object of type Square\Models\Customer Object
and the other returns an object of type stdClass Object
. The issue is that these objects must be managed in completely different ways. For instance, the given_name
for the stdClass can be accessed with $customer->given_name
. But that doesn’t work with the Square\Models\Customer Object
type. For that you need to use $customer->getGivenName();
. The same is true for an update customer result. This is infuriating. I can’t list all my customers, interact with them, update one, or get one, and program in the same manner.
How do I get the API to consistently give the same type of object back?
Here’s an example of a list all customers query:
function getSquareCustomers(){
$client = getApiClient();
$customersApi = $client->getCustomersApi();
$cursor = ‘initial’;
$customers = array();
while ($cursor){
if ($cursor == 'initial'){$cursor = null;}
$apiResponse = $customersApi->listCustomers($cursor);
if ($apiResponse->isSuccess()) {
$listCustomersResponse = $apiResponse->getResult();
$customers = array_merge ($customers, $listCustomersResponse->getCustomers());
$cursor = $listCustomersResponse->getCursor();
} else {
$errors = $apiResponse->getErrors();
}
}
return $customers;
}
Here’s an example of retrieving a single customer.
function getSquareCustomer($customerId, $allow_fail = false){
$client = getApiClient();
$customersApi = $client->getCustomersApi();
$apiResponse = $customersApi->retrieveCustomer($customerId);
if ($apiResponse->isSuccess()) {
$retrieveCustomerResponse = $apiResponse->getResult();
$customer = $retrieveCustomerResponse->getCustomer();
} else {
$errors = $apiResponse->getErrors();
}
return $customer;
}