Customers API returning different object types

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;
}

I was able to cast it to a stdClass Object using the following function. However, I still don’t see why the API is providing different types of objects.

function cast($destination, $sourceObject)
{
    if (is_string($destination)) {
        $destination = new $destination();
    }
    $sourceReflection = new ReflectionObject($sourceObject);
    $destinationReflection = new ReflectionObject($destination);
    $sourceProperties = $sourceReflection->getProperties();
    foreach ($sourceProperties as $sourceProperty) {
        $sourceProperty->setAccessible(true);
        $name = $sourceProperty->getName();
        $value = $sourceProperty->getValue($sourceObject);
        if ($destinationReflection->hasProperty($name)) {
            $propDest = $destinationReflection->getProperty($name);
            $propDest->setAccessible(true);
            $propDest->setValue($destination,$value);
        } else {
            $destination->$name = $value;
        }
    }
    return $destination;
}

$customer = cast('stdClass', $customer);

Thanks for reporting, I agree that’s very strange and frustrating! I’ll escalate that to our SDK team and follow up once I have more information from them.

In addition to the issue I outlined above, using the $catalogApi->listCatalog will return objects with different field names. For instance listCatalog contains field names with underscores like item_data and item_data->variations->item_variation_data.

Meanwhile, $catalogApi->retrieveCatalogObject will return objects without underscores like itemData.

Additionally, you’ll also notice that $retrieveCatalogObject returns most of it’s data as Square catalog objects. However, itemData->variations is returned as a stdClass object and again, it has the wrong field formatting, where item_variation_data has underscores instead of being itemVariationData.

Apologies for the delay here. Just a head’s up, the next PHP SDK release should resolve this issue, be on the lookout :slight_smile:.

2 Likes

Thank you @sjosey, much appreciated!