Learn how the Square PHP SDK supports the common Square API features.
SDKs and Samples

Common Square API Patterns

Some of the Square API patterns are used across various APIs. These include:

  • Pagination. Many Square API operations limit the size of the response. When the result of the API operation exceeds the limit, the API truncates the result. You must make a series of requests to retrieve all the data. This is referred to as pagination.

  • Idempotency key. Most Square APIs that perform create, update, or delete operations require idempotency keys to protect against making duplicate calls that can have negative consequences (for example, charging a card on file twice).

  • Object versioning. Some of the Square resources (for example, the Customer object) have versions assigned. The version numbers enable optimistic concurrency, which is the ability for multiple transactions to complete without interfering with each other.

These Square API patterns are exposed in the Square PHP SDK.

Square API pagination support lets you split a full query result set into pages that are retrieved over a sequence of requests. For example, when you call listCustomers, you can limit the number of customers returned in the response. If there are more customers to retrieve, the response includes a pagination cursor. You include this cursor in your subsequent listCustomers request to retrieve the next set of customers. When the response no longer returns a cursor (the cursor is null), there are no more customers to retrieve.

The following code example calls the listCustomers method. The request limits the number of customers returned to 10. The do...while loop repeats while the pagination cursor is not null. After the first listCustomers call, the subsequent call includes the pagination cursor returned by the previous call.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
$limit = 10;

try {
    $apiResponse = $client->getCustomersApi()->listCustomers(
        null,
        $limit,
        'DEFAULT',
        'DESC'
    );

    if ($apiResponse->isSuccess()) {

        $cursor = $apiResponse->getCursor();

        do {
            foreach ($apiResponse->getResult()->getCustomers() as $cust) {
                printf("customer: ID:%s Version:%s Given name:%s Family name:%s<p/>",
                    $cust->getId(),
                    $cust->getVersion(),
                    $cust->getGivenName(),
                    $cust->getFamilyName());
            }

            $cursor = $apiResponse->getCursor(); 

            $apiResponse = $client->getCustomersApi()->listCustomers(
                $cursor,
                $limit,
                'DEFAULT',
                'DESC'
            ); 

        } while ($cursor);

    } else {
        $errors = $apiResponse->getErrors();
        // Your error-handling code here
    }

} catch (ApiException $e) {
    echo "ApiException occurred: <b/>";
    echo $e->getMessage() . "<p/>";
}

When an application calls a Square API, it must be able to repeat an API operation when needed and get the same result each time. For example, if a network error occurs while updating a catalog item, the application might retry the same request and must ensure that the item updates only once.

This behavior is called idempotency. Most Square APIs that modify data (create, update, or delete) require you to provide an idempotency key that uniquely identifies the request. This allows you to retry the request if necessary, without duplicating work.

You can provide a custom unique key or simply generate one. There are language specific functions that you can use to generate unique keys. For more information, see Idempotency.

Some Square API resources support versioning. For example, each Customer object has a version field. Initially, the version number is 0. Each update increases the version number. If you do not specify a version number in the request, the latest version is assumed.

This resource version number enables optimistic concurrency; multiple transactions can complete without interfering with each other. As a best practice, you should include the version field in the request to enable optimistic concurrency. The value must be set to the current version. For more information, see Optimistic Concurrency.

The following code example updates a customer name. The updateCustomer method also includes a version number. The method succeeds only if the specified version number is the latest version of the customer object on the server.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
use Square\Models\UpdateCustomerRequest;

$body = new UpdateCustomerRequest();
$body->setGivenName('Fred');
$body->setFamilyName('Jones');
$body->setVersion(7);

try {
    $apiResponse = 
        $client->getCustomersApi()->updateCustomer(
            'GZ48C4P2CWVXV7F7K2ZH795RSG', $body
        );

    if ($apiResponse->isSuccess()) {
        $result = $apiResponse->getResult();
        $customer = $result->getCustomer();
        printf("customer updated:<br/>Id: %s, %s, %s, %s<p/>", 
            $customer->getId(),
            $customer->getVersion(),
            $customer->getGivenName(),
            $customer->getFamilyName()
        );

    } else { 
        $errors = $apiResponse->getErrors();
        foreach ($errors as $error) {
            printf("%s<br/> %s<br/> %s<p/>", 
                $error->getCategory(),
                $error->getCode(),
                $error->getDetail());
        }
    }

} catch (ApiException $e) {
    echo "ApiException occurred: <b/>";
    echo $e->getMessage() . "<p/>";
}

If the version numbers on the client and server don't match, the call fails with the following:

  • Category: INVALID_REQUEST_ERROR

  • Code: CONFLICT

  • Detail: Version is not up to date.

If you need more assistance, contact Developer Support or ask for help in the Developer Forums.