Skip to content

k.payment.square ​

Square payments (USD only).

Execution ContextPrerequisitesTypical 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 CMSCreate Square Checkout charges (USD only), redirect to hosted checkout, and check status

k.payment.square creates Square Checkout payment requests.

Quick Start ​

js
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 / PropertyDescriptionReturns
square.charge(params)Create a Square Checkout payment request (USD only)ChargeResponse
square.checkStatus(requestId)Check payment status by requestIdPaymentStatusResponse
square.setting / contextSite Square settings and current request contextSquareSetting / context

square.charge() ​

Create a Square Checkout payment request.

ParameterTypeRequiredDescription
returnUrlstringYesReturn URL after successful payment
totalAmountnumberYesOrder amount
namestringYesOrder name
descriptionstringYesOrder description
currencystringYesCurrency (USD only; passing any other currency throws a validation error)
orderstringNoCommerce order ID. Updates order payment status when paid
callbackCodeNamestringNoCallback code name executed after payment completes

Returns: ChargeResponse

js
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.

ParameterTypeRequiredDescription
requestIdstringYesrequestId returned by charge()

Returns: PaymentStatusResponse

js
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 by square.charge() as Square Checkout's redirect_url. When the buyer returns, Kooboo checks the order status, invokes PaymentManager.CallBack(...), and redirects 302 to your returnUrl.
  • /_api/paymentcallback/Square_Notify: Configure this endpoint in your Square webhook settings so Kooboo receives asynchronous payment events and runs PaymentManager.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 ​

PropertyTypeDescription
useSandBoxbooleanWhether sandbox mode is enabled
applicationIdstringSquare Application ID
accessTokenstringSquare Access Token
locationIdstringSquare Location ID
baseURLstringSquare API URL

TypeScript Definitions ​

ts
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;
}