Skip to content

k.payment.paypalCheckout / paypalForm ​

PayPal payments (Checkout and Form modes).

Execution ContextPrerequisitesTypical Use
KScript API (k.api.get / k.api.post), server-side <script env="server">Configure PaypalSetting or PaypalFormSetting in site CMSCreate PayPal Checkout / Form charges and handle completion or IPN callbacks

Quick Start ​

js
k.api.get(() => {
    const result = k.payment.paypalCheckout.charge({
        name: "T-shirt",
        description: "Order payment",
        totalAmount: 1.5,
        currency: "USD",
        order: "order-id",
        returnUrl: "/payment/success?orderId=order-id",
        cancelUrl: "/payment/cancel?orderId=order-id"
    })

    k.response.setHeader("Content-Type", "text/html; charset=utf-8")
    k.response.write(result.nextAction.renderHtml)
})

Quick Reference ​

Method / PropertyDescriptionReturns
paypalCheckout.charge(params)Create a PayPal Checkout payment requestChargeResponse
paypalForm.charge(params)Create a PayPal form payment requestChargeResponse
paypalCheckout.checkStatus(requestId)Check payment status (paypalForm throws on call)PaymentStatusResponse

Payment Modes ​

APIDescription
k.payment.paypalCheckoutPayPal Checkout
k.payment.paypalFormPayPal form payment

paypalCheckout.charge() ​

Create a PayPal Checkout payment request.

ParameterTypeRequiredDescription
cancelUrlstringYesReturn URL when canceling payment
returnUrlstringYesReturn URL after successful payment
brandNamestringNoBrand name displayed on the PayPal checkout page
customerstringNoCustomer identifier (currently unused by the PaypalCheckout implementation; kept for parameter compatibility)
totalAmountnumberYesOrder amount
namestringYesOrder name
descriptionstringYesOrder description
currencystringYesCurrency, such as USD
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.paypalCheckout.charge({
        name: "T-shirt",
        description: "Order payment",
        totalAmount: 1.5,
        currency: "USD",
        order: "order-id",
        returnUrl: "/payment/success?orderId=order-id",
        cancelUrl: "/payment/cancel?orderId=order-id",
        brandName: "Kooboo Store",
        callbackCodeName: "PaymentCallback"
    })

    k.response.setHeader("Content-Type", "text/html; charset=utf-8")
    k.response.write(result.nextAction.renderHtml)
})

paypalForm.charge() ​

Create a PayPal form payment request.

ParameterTypeRequiredDescription
cancelUrlstringYesReturn URL when canceling payment
returnUrlstringYesReturn URL after successful payment
totalAmountnumberYesOrder amount
namestringYesOrder name
descriptionstringYesOrder description
currencystringYesCurrency, such as USD
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.paypalForm.charge({
        name: "T-shirt",
        description: "Order payment",
        totalAmount: 1.5,
        currency: "USD",
        order: "order-id",
        returnUrl: "/payment/success?orderId=order-id",
        cancelUrl: "/payment/cancel?orderId=order-id"
    })

    k.response.setHeader("Content-Type", "text/html; charset=utf-8")
    k.response.write(result.nextAction.renderHtml)
})

checkStatus() ​

Check payment status. Only paypalCheckout supports this method; calling paypalForm.checkStatus() throws "Check status not support in paypal form basic payment method".

ParameterTypeRequiredDescription
requestIdstringYesrequestId returned by charge()

Returns: PaymentStatusResponse

js
k.api.get(() => {
    const status = k.payment.paypalCheckout.checkStatus("request-id")
    return { paid: status.paid, failed: status.failed, status: status.status }
})

Callback Handling ​

Kooboo automatically registers and handles the following built-in callback routes for PayPal:

  • /_api/paymentcallback/PaypalCheckout_UpdateOrder: Automatically sent by paypalCheckout.charge() as return_url. When the buyer returns, Kooboo verifies/captures the PayPal order, invokes PaymentManager.CallBack(...) to update PaymentRequest, mark any linked Commerce order paid, and execute callbackCodeName, then redirects 302 to your returnUrl.
  • /_api/paymentcallback/PaypalForm_NofityUrl: Automatically set by paypalForm.charge() as the form's notify_url (note the NofityUrl spelling). When PayPal posts an IPN message, Kooboo verifies it back against PayPal, checks receiver email, currency, and amount, and invokes PaymentManager.CallBack(...) to update PaymentRequest and linked Commerce orders.

paypalCheckout.updateOrder(context) and paypalForm.nofityUrl(context) are internal callback handlers—do not invoke them directly inside KScript to update orders. Direct calls do not trigger PaymentManager.CallBack(...); furthermore, paypalCheckout.updateOrder throws NullReferenceException when requestid is missing, and paypalForm.nofityUrl returns PaymentCallback | null (null on IPN verification failure or field mismatch).

For custom post-payment handling in KScript, configure callbackCodeName, or call k.payment.paypalCheckout.checkStatus(requestId) (Checkout) / k.payment.getRequest(requestId) (Form or Checkout) in your return handler.

Configuration Structure ​

PaypalSetting ​

PropertyTypeDescription
paypalUrlstringPayPal API endpoint
useSandBoxbooleanWhether sandbox mode is enabled
clientIdstringPayPal Client ID
secretstringPayPal Secret

PaypalFormSetting ​

PropertyTypeDescription
paypalUrlstringPayPal API endpoint
iPNUrlstringRead-only computed URL of PayPal's own IPN verification endpoint (https://ipnpb.paypal.com/cgi-bin/webscr in live mode or https://www.sandbox.paypal.com/cgi-bin/webscr in sandbox), not a configurable site callback URL
emailAddressstringMerchant account email
logoImagestringCheckout page logo URL
useSandBoxbooleanWhether sandbox mode is enabled

TypeScript Definitions ​

Show full TypeScript definitions
ts
interface PaypalCheckout {
    setting: PaypalSetting;
    charge(params: PaypalCheckoutParams): ChargeResponse;
    /** Internal callback handler invoked by /_api/paymentcallback/PaypalCheckout_UpdateOrder; do not call directly in KScript */
    updateOrder(context: any): PaymentCallback;
    checkStatus(requestId: string): PaymentStatusResponse;
}

interface PaypalForm {
    setting: PaypalFormSetting;
    charge(params: PaypalFormParams): ChargeResponse;
    /** Internal IPN handler invoked by /_api/paymentcallback/PaypalForm_NofityUrl; do not call directly in KScript */
    nofityUrl(context: any): PaymentCallback | null;
    /** Not supported by PaypalForm; throws at runtime */
    checkStatus(requestId: string): PaymentStatusResponse;
}

interface PaypalCheckoutParams extends ChargeParams {
    cancelUrl: string;
    returnUrl: string;
    brandName: string;
    customer: string;
}

interface PaypalFormParams extends ChargeParams {
    cancelUrl: string;
    returnUrl: string;
}

interface PaypalSetting {
    name: string;
    group: string;
    paypalUrl: string;
    useSandBox: boolean;
    clientId: string;
    secret: string;
}

interface PaypalFormSetting {
    name: string;
    group: string;
    readonly paypalUrl: string;
    readonly iPNUrl: string;
    emailAddress: string;
    logoImage: string;
    useSandBox: boolean;
}