Skip to content

k.payment.weChatH5 — WeChat H5 Payment Flow ​

WeChat H5 payment guide covering the full integration flow from creating a payment request to verifying payment status.

Execution ContextPrerequisitesTypical Use
KScript API (k.api.get / k.api.post), server-side <script env="server">Configure WeChat V3 (CNY) in CMS and enable H5 payment domains in WeChat Merchant PlatformLaunch mobile browser H5 checkout via redirectUrl, persist requestId, and verify with checkStatus()

Common Errors

Do not substitute k.payment.wechat (Native) renderHtml for H5. Mobile browser H5 must use k.payment.weChatH5 and redirect the browser to nextAction.redirectUrl.

Quick Start ​

js
k.api.get(() => {
    const orderId = k.request.queryString.get("orderId")
    const order = k.commerce.order.get(orderId)

    const charge = k.payment.weChatH5.charge({
        order: orderId,
        totalAmount: order.totalAmount,
        currency: "CNY",
        name: `Order ${orderId}`,
        description: `Payment for order ${orderId}`
    })

    k.response.redirect(charge.nextAction.redirectUrl)
})

Quick Reference ​

Method / PropertyDescriptionReturns
weChatH5.charge(params)Create WeChat H5 payment request (no redirectUrl input)ChargeResponse
weChatH5.checkStatus(requestId)Query real payment status by requestIdPaymentStatusResponse
k.payment.getRequest(requestId)Read built-in PaymentRequest recordPaymentRequest

Choose the Right API ​

ScenarioAPInextAction
PC QR code scank.payment.wechatrenderHtml (includes QR code and polling)
Mobile browser H5k.payment.weChatH5redirectUrl
WeChat built-in browserk.payment.wechatJsApiresponseData (requires openId)
Native appk.payment.wechatAppresponseData

Configure Before Running ​

Enable WeChat Payment V3 in the site CMS and configure:

SettingDescription
appIdWeChat App ID
merchantIdMerchant ID
aPIV3KeyAPI V3 key
certificatePrivateKeyMerchant API certificate private key
certificateMerchant API certificate

Also enable H5 payment in the WeChat Merchant Platform and configure the payment and callback domains.

WeChat Pay only supports CNY. Multi-currency sites must resolve to CNY when creating the order or payment; see k.payment Overview.

Minimal Flow: Server-Side H5 Payment ​

Key points:

  • order, currency, and totalAmount are required (H5 runtime constraints).
  • weChatH5.charge() accepts ChargeParams, which does not take redirectUrl or returnUrl; Kooboo builds the WeChat notify callback URL automatically and returns the H5 cashier target in charge.nextAction.redirectUrl.
  • When order is supplied, Kooboo updates the Commerce order status automatically upon payment callback; pass callbackCodeName if you also need custom post-payment script logic.
js
k.api.get(() => {
    const orderId = k.request.queryString.get("orderId")
    const order = k.commerce.order.get(orderId)

    const charge = k.payment.weChatH5.charge({
        order: orderId,
        totalAmount: order.totalAmount,
        currency: "CNY",
        name: `Order ${orderId}`,
        description: `Payment for order ${orderId}`
    })

    k.response.redirect(charge.nextAction.redirectUrl)
})

Status Query ​

A client redirect or return does not guarantee payment success. Always query using requestId, and execute fulfillment logic only when status.paid === true (or the built-in Commerce order is marked paid).

js
k.api.get(() => {
    const requestId = k.request.queryString.get("requestId")
    const request = k.payment.getRequest(requestId)
    const status = k.payment.weChatH5.checkStatus(requestId)
    return { request, status }
})

Integration with Commerce Orders ​

js
const order = k.commerce.order.create(cartId, { address: addressInfo })

const charge = k.payment.weChatH5.charge({
    order: order.id,
    totalAmount: order.totalAmount,
    currency: "CNY",
    name: product.title,
    description: "Order payment"
})

k.logger.information("Payment.WeChatH5", `requestId: ${charge.requestId}`)
k.response.redirect(charge.nextAction.redirectUrl)

Callbacks and Status Verification ​

Kooboo automatically handles the asynchronous WeChat callback and updates PaymentRequest and any linked Commerce order. To perform an explicit confirmation when the user returns to your order status route, store charge.requestId before redirecting and verify it with checkStatus():

js
k.api.get(() => {
    const orderId = k.request.queryString.get("orderId")

    // Persist before redirecting: k.DB.keyValue.set(`payment:${orderId}`, charge.requestId)
    const saved = k.DB.keyValue.get(`payment:${orderId}`)
    const requestId = saved ? saved.toString() : null

    if (!requestId) {
        k.response.json({ error: "requestId not found" })
        return k.api.httpCode(400)
    }

    const status = k.payment.weChatH5.checkStatus(requestId)
    if (status.paid) {
        k.response.redirect(`/order/${orderId}`)
        return
    }
    k.response.redirect("/payment-failed")
})

TypeScript Definitions ​

ts
interface WeChatH5 {
    setting: WeChatV3Setting;
    charge(chargeParams: ChargeParams): ChargeResponse;
    checkStatus(requestId: string): PaymentStatusResponse;
}