square/connect
Retired Square Connect PHP SDK (EOL 2020-06-10). No longer receives updates or fixes. Migrate to the new Square PHP SDK: require square/square, update namespaces from SquareConnect\ to Square, and adjust client/response handling per docs.
Migrate to square/square (required):
composer require square/square
Replace all use SquareConnect\* imports with use Square\*.
Initialize the client (replace deprecated ApiClient):
use Square\SquareClient;
use Square\Environment;
$client = new SquareClient([
'accessToken' => config('services.square.access_token'),
'environment' => Environment::SANDBOX, // or Environment::PRODUCTION
]);
First API call (e.g., create a payment):
$paymentsApi = $client->getPaymentsApi();
$money = new \Square\Models\Money(['amount' => 100, 'currency' => 'USD']);
$request = new \Square\Models\CreatePaymentRequest(
'nonce_from_client',
uniqid(),
$money
);
$response = $paymentsApi->createPayment($request);
config/services.php):
'square' => [
'access_token' => env('SQUARE_ACCESS_TOKEN'),
'environment' => env('SQUARE_ENVIRONMENT', 'sandbox'),
],
$client = app(SquareClient::class);
$client instance):
$paymentsApi = $client->getPaymentsApi();
$locationsApi = $client->getLocationsApi();
CreatePaymentRequest, Payment):
$request = new CreatePaymentRequest(
$nonce,
$idempotencyKey,
new Money(['amount' => 100, 'currency' => 'USD'])
);
$response = $paymentsApi->createPayment($request);
if ($response->isError()) {
throw new \RuntimeException('Square API Error: ' . $response->getErrors()[0]->getMessage());
}
$payment = $paymentsApi->createPayment($request);
$paymentId = $payment->getPayment()->getId();
$refund = $refundsApi->refundPayment($paymentId, 50);
$customer = $customersApi->createCustomer($customerRequest);
$locations = $locationsApi->listLocations();
ApiException for network/API errors:
try {
$response = $paymentsApi->createPayment($request);
} catch (ApiException $e) {
\Log::error('Square API Error: ' . $e->getMessage());
abort(500, 'Payment failed');
}
if ($response->isError()) {
$errors = $response->getErrors();
foreach ($errors as $error) {
\Log::error($error->getCategory() . ': ' . $error->getCode());
}
}
$webhooksApi = $client->getWebhooksApi();
$webhook = $webhooksApi->createWebhook($webhookRequest);
Deprecated Endpoints:
square/connect (EOL). Migrate to square/square immediately.renewToken is deprecated; use OAuth token refresh.Idempotency Keys:
idempotency_key for payments/refunds to avoid duplicate charges.uniqid() or a UUID.Currency Formatting:
$100 = 100, not 100.00).Money model for consistency:
$money = new Money(['amount' => 100, 'currency' => 'USD']);
Rate Limits:
Webhook Verification:
use Square\Webhook\SignatureVerifier;
$verifier = new SignatureVerifier(config('services.square.webhook_secret'));
if (!$verifier->verify($rawBody, $signatureHeader)) {
abort(403, 'Invalid webhook signature');
}
Enable Debug Logging:
use Square\SquareClient;
$client = new SquareClient([
'accessToken' => $token,
'environment' => Environment::SANDBOX,
'debug' => true, // Enable debug logs
]);
Inspect Raw Responses:
getRawResponse() to debug API calls:
$response = $paymentsApi->createPayment($request);
\Log::debug($response->getRawResponse());
Test in Sandbox First:
$client = new SquareClient([
'accessToken' => 'sandbox-access-token',
'environment' => Environment::SANDBOX,
]);
Custom Request/Response Transformers:
SquareClient to add middleware for request/response modification:
$client = new SquareClient([
'accessToken' => $token,
'middleware' => [
function ($request) {
$request->setHeader('X-Custom-Header', 'value');
},
],
]);
Repository Pattern:
class SquarePaymentRepository {
public function __construct(private SquareClient $client) {}
public function charge(string $nonce, int $amount): Payment {
$request = new CreatePaymentRequest($nonce, uniqid(), new Money(['amount' => $amount, 'currency' => 'USD']));
return $this->client->getPaymentsApi()->createPayment($request)->getPayment();
}
}
Event Dispatching:
$payment = $paymentsApi->createPayment($request);
event(new PaymentCreated($payment));
Environment Variables:
SQUARE_ACCESS_TOKEN and SQUARE_ENVIRONMENT in .env:
SQUARE_ACCESS_TOKEN=sandbox-access-token
SQUARE_ENVIRONMENT=sandbox
Webhook Secrets:
config/services.php:
'square' => [
'webhook_secret' => env('SQUARE_WEBHOOK_SECRET'),
],
Timeouts:
$client = new SquareClient([
'accessToken' => $token,
'httpClient' => new \GuzzleHttp\Client(['timeout' => 60]),
]);
How can I help you explore Laravel packages today?