Announcing Square’s New PHP SDK

Announcing Square’s New PHP SDK

Use the New PHP SDK to Integrate with Square APIs

We’re excited to announce our new PHP SDK. You can use the SDK to integrate Square payments into your app. Run your business with Square APIs including Catalog, Customers, Employees, Inventory, Labor, Locations, Orders, and more.

There are a few ways to install the Square package.

Install with Composer

composer require square/square

After installing and importing a few SDK packages, you’ll be ready to instantiate a client with your Square access token.

$client = new SquareClient([
    'accessToken' => 'YOUR SANDBOX ACCESS TOKEN HERE',
    'environment' => Environment::SANDBOX,
]);

With a client, we’re ready to use the Square API. For example, we can list locations.

$locationsApi = $client->getLocationsApi();
$apiResponse = $locationsApi->listLocations();

One of the great features in the new SDK is that the response object contains new rich information about both the request and response. These details can be used for control flow and debugging.

If you were previously using the square/connect PHP package, you might be wondering how the new PHP SDK compares. Let’s take a look! For example, let’s create the following customer along with their address (these examples are for demonstrating the differences, for full examples please head over to the GitHub page for the SDK):

$address = new Models\Address;
$address->setAddressLine1('500 Electric Ave');
$address->setAddressLine2('Suite 600');
$address->setLocality('New York');
$address->setAdministrativeDistrictLevel1('NY');
$address->setPostalCode('10003');
$address->setCountry(Models\Country::US);

$customer = new Models\CreateCustomerRequest;
$customer->setGivenName('Amelia');
$customer->setFamilyName('Earhart');
$customer->setEmailAddress('[email protected]');
$customer->setAddress($address);
$customer->setPhoneNumber('1-212-555-4240');

First, let’s look at how we used to create a customer with the deprecated PHP Connect SDK:

$customerAddress = new \SquareConnect\Model\Address();
$customerAddress->setAddressLine1('500 Electric Ave');
$customerAddress->setAddressLine2('Suite 600');
$customerAddress->setLocality('New York');
$customerAddress->setAdministrativeDistrictLevel1('NY');
$customerAddress->setPostalCode('10003');
$customerAddress->setCountry(Models\Country::US);

$customer = new \SquareConnect\Model\CreateCustomerRequest();
$customer->setGivenName('Amelia');
$customer->setFamilyName('Earhart');
$customer->setEmailAddress('[email protected]');
$customer->setAddress($address);
$customer->setPhoneNumber('1-212-555-4240');

$defaultApiConfig = new \SquareConnect\Configuration();
$defaultApiConfig->setHost("https://connect.squareupsandbox.com");
$defaultApiConfig->setAccessToken('{REPLACE_ME}');

$defaultApiClient = new \SquareConnect\ApiClient($defaultApiConfig);
$customersApi = new SquareConnect\Api\CustomersApi($defaultApiClient);

$result = $customersApi->createCustomer($customer);
print_r($result);

For comparison, let’s create the same customer with the new Square PHP SDK:

$address = new Models\Address;
$address->setAddressLine1('500 Electric Ave');
$address->setAddressLine2('Suite 600');
$address->setLocality('New York');
$address->setAdministrativeDistrictLevel1('NY');
$address->setPostalCode('10003');
$address->setCountry(Models\Country::US);

$customerRequest = new Models\CreateCustomerRequest;
$customerRequest->setGivenName('Amelia');
$customerRequest->setFamilyName('Earhart');
$customerRequest->setEmailAddress('[email protected]');
$customerRequest->setAddress($address);
$customerRequest->setPhoneNumber('1-212-555-4240');

$client = new SquareClient([
    'accessToken' => 'REPLACE_ME',
    'environment' => Environment::SANDBOX,
]);
$customersApi = $client->getCustomersApi();

$apiResponse = $customersApi->createCustomer($customerRequest);

if ($apiResponse->isSuccess()) {
    $createCustomerResponse = $apiResponse->getBody();
} else {
    $errors = $apiResponse->getErrors();
}

That’s all there is to it!

As you can see, the new PHP SDK interface is much simpler, but provides richer data. The new package also ships with a ton of usability and debugging features.

Give the new PHP SDK a try today by installing it with composer require square/square.

With the new PHP SDK it’s easier than ever to use Square as a platform to run your business with PHP. We can’t wait to see what you’ll build! If you have any questions or feedback, feel free to ask us in our forums or drop by our Slack. We’d love to hear from you.

Table Of Contents
View More Articles ›