Square Setup & Integration Guide
The square provider integrates Square Orders, Payment Links, Subscriptions, Saved Cards, and Disputes into the Kirak payments runtime.
1. Prerequisites & Installation
Section titled “1. Prerequisites & Installation”Square integration requires the official squareup Python SDK. Install the Kirak extra:
pip install "kirak[payments-square]"2. Credentials & Configuration
Section titled “2. Credentials & Configuration”Step 1: Obtain API Keys
Section titled “Step 1: Obtain API Keys”In the Square Developer Dashboard:
- Select or create an application.
- In the Credentials tab, copy the Access Token (Sandbox or Production).
- In the Locations tab, copy the target Location ID.
Step 2: Configure kirak.json
Section titled “Step 2: Configure kirak.json”Declare the Square provider instance:
{ "payments": { "default_provider": "square", "providers": { "square": { "type": "square", "location_id": "LXXXXXXXXXXXX", "environment": "sandbox", "webhook_notification_url": "https://your-domain.com/payments/webhook/square", "success_url": "https://your-domain.com/payment/success", "cancel_url": "https://your-domain.com/payment/cancel" } } }}[!IMPORTANT] Exact URL Match Requirement for Signatures: Square computes its webhook HMAC signature over: $$\text{Signature} = \text{HMAC-SHA256}(\text{notification_url} + \text{raw_body})$$ The
webhook_notification_urlsetting inkirak.jsonmust exactly match the URL registered in the Square Developer Dashboard (including protocol, domain, port, and trailing slashes). Any difference will cause signature verification to fail with400 INVALID_WEBHOOK_SIGNATURE.
Step 3: Set Environment Variables
Section titled “Step 3: Set Environment Variables”KIRAK_PAYMENT_SQUARE_ACCESS_TOKEN=EAAA...KIRAK_PAYMENT_SQUARE_WEBHOOK_SIGNATURE_KEY=... # Obtained in Step 33. Configuring Webhooks
Section titled “3. Configuring Webhooks”In the Square Developer Dashboard $\rightarrow$ your App $\rightarrow$ Webhooks $\rightarrow$ Add Endpoint:
- Endpoint Name: Kirak Webhook
- URL: Use the exact same URL configured in
webhook_notification_url(https://your-domain.com/payments/webhook/square). - Select Events:
- Payments:
payment.created,payment.updated - Orders:
order.updated - Refunds:
refund.created,refund.updated - Subscriptions:
subscription.created,subscription.updated - Saved Cards:
card.disabled,card.forgotten,card.updated,card.automatically_updated
- Payments:
- Copy the Signature Key and export it as
KIRAK_PAYMENT_SQUARE_WEBHOOK_SIGNATURE_KEY.
4. Usage Examples
Section titled “4. Usage Examples”4.1 One-Time Payment Link
Section titled “4.1 One-Time Payment Link”Creates a Square Order and returns a Square-hosted Checkout Link:
result = await kirak.payments.initiate_payment({ "provider": "square", "user_id": "usr_300", "amount": 2999, # 29.99 GBP in pence "currency": "GBP", "name": "Standard License", "type": "one_time",})
checkout_url = result["data"]["url"]4.2 Subscriptions
Section titled “4.2 Subscriptions”Note that Square uses subscription_plan_variation_id (create the subscription plan and variation in Square Dashboard first):
result = await kirak.payments.initiate_payment({ "provider": "square", "user_id": "usr_300", "amount": 0, # Billed at first-phase price from Square catalog "currency": "GBP", "name": "Pro Membership", "type": "subscription", "subscription_plan_variation_id": "SQVARIATION123",})[!NOTE] Cancellation Behavior: Square’s API only supports canceling subscriptions at the end of the current billing cycle. Calling
cancel_subscriptionwithat_period_end: Falseis not available in Square’s API and defaults to period-end cancellation.
4.3 Saved Cards & Off-Session Charges
Section titled “4.3 Saved Cards & Off-Session Charges”- Save while paying: Supported for one-time payments (
save_payment_method: True). - Attach token: Supported from Square Web Payments SDK (
attach_payment_methodwith client card token). - Off-session charge: Executes a payment against the customer’s vaulted card on file.
5. Webhook Event Mapping
Section titled “5. Webhook Event Mapping”Normalized event |
Gateway Events | Description |
|---|---|---|
payment_completed |
payment.created, payment.updated (status="COMPLETED") |
Charge completed successfully |
payment_failed |
payment.created, payment.updated (status="FAILED") |
Payment failed |
subscription_updated |
subscription.created, subscription.updated |
Subscription created or updated |
subscription_cancelled |
subscription.updated with status CANCELED or DEACTIVATED |
Subscription ended |
refund_completed |
refund.created, refund.updated (status="COMPLETED") |
Refund completed |
payment_method_removed |
card.disabled, card.forgotten |
Card vaulted at Square deleted |