I’m using the PHP SDK to create payment requests
$client->payments->create etc
The docs suggest checking isSuccess() but this fails (I think that refers to previous versions of the API?)
Anyway how on earth do I find the response code (200, 400 etc) to handle errors?
Is there any documentation/examples anywhere of error handling using the PHP SDK?
Thanks
isSuccess() is from the legacy PHP SDK (versions before v40 of square/square, released Jan 2025). In the legacy SDK, calls returned an ApiResponse wrapper you checked manually. Older docs and forum posts still reference that pattern, which is why it looks like it should work.
In the current SDK, error handling is exception-based. A successful call returns the response object directly, and any non-2xx response (400, 401, 500, etc.) throws a SquareApiException:
use Square\Exceptions\SquareApiException;
use Square\Exceptions\SquareException;
try {
$response = $client->payments->create($request);
// If we get here, the request succeeded (2xx)
$payment = $response->getPayment();
} catch (SquareApiException $e) {
// The API returned a 4xx/5xx
echo 'HTTP status: ' . $e->getStatusCode() . "\n"; // e.g. 400
// Square's errors, already parsed into objects:
foreach ($e->getErrors() as $error) {
echo $error->getCategory() . ' / ' . $error->getCode() . ': ' . $error->getDetail() . "\n";
}
} catch (SquareException $e) {
// Non-API failure (e.g. network issue)
echo 'Error: ' . $e->getMessage();
}
$e->getStatusCode() the HTTP status code you were looking for
$e->getErrors() Square’s errors array parsed into Error objects, so you can branch on specific codes like CARD_DECLINED without decoding JSON yourself
$e->getBody() the raw response body
The “Exception Handling” section of the SDK README covers this: GitHub - square/square-php-sdk: PHP client library for the Square API · GitHub
If your code or a tutorial uses isSuccess(), it was written for the legacy SDK. The new package still ships the old SDK under the Square\Legacy\... namespace, and the README has migration steps if you’re upgrading incrementally.
Hope that helps!