Installation:
composer require nikolag/laravel-square
Publish the config file:
php artisan vendor:publish --provider="Nikolag\Square\SquareServiceProvider"
Configure .env with your Square credentials:
SQUARE_ENVIRONMENT=sandbox
SQUARE_ACCESS_TOKEN=your_access_token
SQUARE_LOCATION_ID=your_location_id
First Use Case: Create a customer in Square via a Laravel controller:
use Nikolag\Square\Traits\Customer;
class CustomerController extends Controller
{
use Customer;
public function createCustomer(Request $request)
{
$customer = $this->createCustomer([
'given_name' => $request->given_name,
'family_name' => $request->family_name,
'email_address' => $request->email,
]);
return response()->json($customer);
}
}
config/square.php for environment-specific settings.Customer Management:
createCustomer() or updateCustomer() with an array of attributes.getCustomer($id).listCustomers() (supports pagination via limit and offset).Example:
$customer = $this->getCustomer($customerId);
$this->updateCustomer($customerId, ['email_address' => 'new@example.com']);
Order Processing:
createOrder() with items, customer ID, and payment details.getOrder($id).cancelOrder($id) for order cancellations.Example:
$order = $this->createOrder([
'idempotency_key' => 'unique_key',
'customer_id' => $customerId,
'items' => [
['name' => 'Product 1', 'quantity' => 1, 'base_price_money' => ['amount' => 1000, 'currency' => 'USD']],
],
]);
Payment Handling:
createPayment() with a source_id (e.g., from a card token).createRefund($paymentId, $amount).Event facade (see Square Webhooks).Example:
$payment = $this->createPayment([
'idempotency_key' => 'unique_key',
'source_id' => $cardToken,
'amount_money' => ['amount' => 1000, 'currency' => 'USD'],
'order_id' => $orderId,
]);
class SquareService {
use Customer, Order, Payment;
public function processOrder(Request $request) {
// Business logic here
$order = $this->createOrder($request->all());
return $order;
}
}
try {
$customer = $this->createCustomer($data);
} catch (\Nikolag\Square\Exceptions\SquareException $e) {
return response()->json(['error' => $e->getMessage()], 400);
}
sandbox environment for testing. Mock the Square client in PHPUnit:
$this->partialMock(SquareClient::class, ['createCustomer']);
Environment Mismatch:
SQUARE_ENVIRONMENT in .env matches your Square account (e.g., sandbox for testing, production for live).environment field in the response to confirm the active environment.Idempotency Keys:
idempotency_key for createOrder() and createPayment() to avoid duplicate transactions.Rate Limits:
429 Too Many Requests errors with retries:
$retryAfter = $e->getRetryAfter();
sleep($retryAfter);
retry();
Webhook Verification:
HasApiTokens or a middleware to validate signatures:
use Nikolag\Square\Traits\Webhook;
class SquareWebhookController extends Controller {
use Webhook;
public function handleWebhook(Request $request) {
if (!$this->verifyWebhook($request)) {
abort(403);
}
// Process webhook
}
}
config/square.php to log API responses:
'debug' => env('SQUARE_DEBUG', false),
trait CustomSquareTrait {
public function applyLoyaltyDiscount($customerId, $discount) {
// Custom logic
}
}
payment.created) via Laravel's event system:
Event::listen('Nikolag\Square\Events\PaymentCreated', function ($event) {
// Send notification
});
Route::middleware(['square.auth'])->group(function () {
// Protected routes
});
SQUARE_LOCATION_ID is set to the correct Square location (required for most endpoints).currency (e.g., USD) in monetary fields. Square defaults may vary by region.config/square.php if Square's API is slow:
'timeout' => 30, // seconds
Customer trait).locale in requests.How can I help you explore Laravel packages today?