PHP SDK Migration Guide

Version 41.0.0.20250220 of the PHP SDK represents a full rewrite of the SDK with a number of breaking changes, outlined in the following sections. When upgrading to this version or later versions, take note of the changes in the SDK, including client construction and parameter names. If necessary, you can use the legacy version of the SDK along with the latest version.

SDK versions 40.0.0.20250123 and earlier continue to function as expected, but you should plan to update your integration as soon as possible to ensure continued support.

Link to section

Client construction

The new Client class has some breaking changes and is constructed in a slightly different way than the legacy version of the SDK.

LegacyNewAdditional information
EnvironmentbaseUrlAn enum for specifying the environment (Sandbox or Production).
accessTokentokenThe access token used for authentication.
customUrlbaseUrlThe base URL for API requests.
numberOfRetriesmaxRetriesThe configuration used to control the number of retries.
additionalHeadersheadersAdditional headers can be set at individual methods.
httpCallbackRemovedThis option has been removed.
userAgentDetailRemovedThis option has been removed.
Link to section

Use the legacy and new SDK side by side

While the new SDK has many improvements, we understand that it takes time to upgrade when there are breaking changes. To make the migration easier, the new SDK exports the legacy SDK as Square/Legacy. The following example shows how to use the new and legacy SDKs inside a single file:

You should migrate to the new SDK using the following steps:

  1. Upgrade the square/square package to ^41.0.0.20250220.
  2. Search and replace all requires and imports from Square to Square\Legacy.
  3. Gradually introduce methods from the new SDK by importing them from the Square import.
Link to section

Array constructors

The previous version of the SDK relied on using a mix of builders and setter methods, which created verbose integrations. For example, consider the following createOrder method call:

// LEGACY $order = new Order($location['id']); $request = new CreateOrderRequest; $request->setIdempotencyKey(uniqid()); $request->setOrder($order); $legacyClient->getOrdersApi()->createOrder($request);

In the new SDK, you can consolidate all the parameters in a single statement. All the parameters are still specified by their name, which makes their meaning instantly clear.

// NEW $client->orders->create( new CreateOrderRequest([ 'idempotencyKey' => uniquid(), 'order' => new Order([ 'locationId' => $location['id'], ]), ]), )
Link to section

Enums

This SDK leverages the PHP 8.1 first-class enums to improve type safety and usability. To maintain forward compatibility with the API (where new enum values might be introduced in the future), enum properties are defined as string and use value-of annotations to specify the corresponding enum type.

The best example of this is when the client's base URL or environment is configured.

Link to section

API classes and method name changes

Previously, API classes were named after the resource you're interacting with and suffixed with Api. To interact with customer resources, you used the client->getCustomersApi() method.

The method was named based on the action you want to perform and the same resource name specified earlier. To list customers, you call client->getCustomersApi()->listCustomers.

// LEGACY $client->getCustomersApi()->listCustomers();

The new API classes are named after the resource without the Api suffix and their methods are named after the action without repeating the resource name.

// NEW $client->customers->list();

When interacting with a nested resource, you can chain the resources using the arrow-notation.

// NEW $client->customers->groups->list();

Other class and method names might have changed. The differences are easiest to discover by exploring the new method signatures.

Link to section

Auto-pagination

The Square paginated endpoints now support auto-pagination with an iterator. Callers don’t need to manually fetch the next page. You can now iterate over the entire list and the client automatically fetches the next page behind the scenes. The following example shows the same code, with the legacy and new SDK:

If you need to paginate page by page, you can still do so with a lot less code:

  • Use $customers->getPages() to iterate over each page individually.
  • Use $customers->getItems() to get the customers of the current page.

If you need to paginate page by page, you can still do so with a lot less code:

  • Use $customers.getPages() to iterate over each page individually.
  • Use $customers.getItems() to get the customers of the current page.