k.payment.square
Square payments (
USDonly).
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
KScript API (k.api.get / k.api.post), server-side <script env="server"> | Configure SquareSetting (Application ID, Access Token, and Location ID) in site CMS | Create Square Checkout charges (USD only), redirect to hosted checkout, and check status |
k.payment.square creates Square Checkout payment requests.
Quick Start
k.api.get(() => {
const result = k.payment.square.charge({
name: "Order summary",
description: "Order payment",
totalAmount: 1.5,
currency: "USD",
order: "order-id",
returnUrl: "/payment/success?orderId=order-id",
callbackCodeName: "PaymentCallback"
})
k.response.redirect(result.nextAction.redirectUrl)
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
square.charge(params) | Create a Square Checkout payment request (USD only) | ChargeResponse |
square.checkStatus(requestId) | Check payment status by requestId | PaymentStatusResponse |
square.setting / context | Site Square settings and current request context | SquareSetting / context |
square.charge()
Create a Square Checkout payment request.
| Parameter | Type | Required | Description |
|---|---|---|---|
returnUrl | string | Yes | Return URL after successful payment |
totalAmount | number | Yes | Order amount |
name | string | Yes | Order name |
description | string | Yes | Order description |
currency | string | Yes | Currency (USD only; passing any other currency throws a validation error) |
order | string | No | Commerce order ID. Updates order payment status when paid |
callbackCodeName | string | No | Callback code name executed after payment completes |
Returns: ChargeResponse
k.api.get(() => {
const result = k.payment.square.charge({
name: "Order summary",
description: "Order payment",
totalAmount: 1.5,
currency: "USD",
order: "order-id",
returnUrl: "/payment/success?orderId=order-id",
callbackCodeName: "PaymentCallback"
})
k.response.redirect(result.nextAction.redirectUrl)
})checkStatus()
Check payment status.
| Parameter | Type | Required | Description |
|---|---|---|---|
requestId | string | Yes | requestId returned by charge() |
Returns: PaymentStatusResponse
k.api.get(() => {
const status = k.payment.square.checkStatus("request-id")
return { paid: status.paid, failed: status.failed, status: status.status }
})Callback Handling
Kooboo handles Square return redirects and webhooks automatically through these built-in routes, calling PaymentManager.CallBack(...) to update PaymentRequest, mark any linked Commerce order as paid, and run callbackCodeName:
/_api/paymentcallback/Square_UpdateOrder: Automatically configured bysquare.charge()as Square Checkout'sredirect_url. When the buyer returns, Kooboo checks the order status, invokesPaymentManager.CallBack(...), and redirects 302 to yourreturnUrl./_api/paymentcallback/Square_Notify: Configure this endpoint in your Square webhook settings so Kooboo receives asynchronous payment events and runsPaymentManager.CallBack(...)automatically.
square.notify(context) and square.updateOrder(context) are internal callback handlers—do not invoke them directly in KScript to update orders. Direct calls never trigger PaymentManager.CallBack(...); additionally, notify() returns PaymentCallback | null (null when payload parsing or request lookup fails) and updateOrder() throws NullReferenceException when requestid is missing.
For custom post-payment handling in KScript, pass callbackCodeName to charge() or call k.payment.square.checkStatus(requestId) in your returnUrl handler.
Configuration Structure
SquareSetting
| Property | Type | Description |
|---|---|---|
useSandBox | boolean | Whether sandbox mode is enabled |
applicationId | string | Square Application ID |
accessToken | string | Square Access Token |
locationId | string | Square Location ID |
baseURL | string | Square API URL |
TypeScript Definitions
interface SquareCheckout {
setting: SquareSetting;
charge(params: SquareCheckoutParams): ChargeResponse;
/** Internal webhook handler invoked by /_api/paymentcallback/Square_Notify; do not call directly in KScript */
notify(context: any): PaymentCallback | null;
/** Internal callback handler invoked by /_api/paymentcallback/Square_UpdateOrder; do not call directly in KScript */
updateOrder(context: any): PaymentCallback;
checkStatus(requestId: string): PaymentStatusResponse;
}
interface SquareCheckoutParams extends ChargeParams {
returnUrl: string;
}
interface SquareSetting {
name: string;
useSandBox: boolean;
applicationId: string;
accessToken: string;
locationId: string;
baseURL: string;
group: string;
}