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 Context | Prerequisites | Typical 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 Platform | Launch 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
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 / Property | Description | Returns |
|---|---|---|
weChatH5.charge(params) | Create WeChat H5 payment request (no redirectUrl input) | ChargeResponse |
weChatH5.checkStatus(requestId) | Query real payment status by requestId | PaymentStatusResponse |
k.payment.getRequest(requestId) | Read built-in PaymentRequest record | PaymentRequest |
Choose the Right API
| Scenario | API | nextAction |
|---|---|---|
| PC QR code scan | k.payment.wechat | renderHtml (includes QR code and polling) |
| Mobile browser H5 | k.payment.weChatH5 | redirectUrl |
| WeChat built-in browser | k.payment.wechatJsApi | responseData (requires openId) |
| Native app | k.payment.wechatApp | responseData |
Configure Before Running
Enable WeChat Payment V3 in the site CMS and configure:
| Setting | Description |
|---|---|
appId | WeChat App ID |
merchantId | Merchant ID |
aPIV3Key | API V3 key |
certificatePrivateKey | Merchant API certificate private key |
certificate | Merchant 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, andtotalAmountare required (H5 runtime constraints).weChatH5.charge()acceptsChargeParams, which does not takeredirectUrlorreturnUrl; Kooboo builds the WeChatnotifycallback URL automatically and returns the H5 cashier target incharge.nextAction.redirectUrl.- When
orderis supplied, Kooboo updates the Commerce order status automatically upon payment callback; passcallbackCodeNameif you also need custom post-payment script logic.
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).
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
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():
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
interface WeChatH5 {
setting: WeChatV3Setting;
charge(chargeParams: ChargeParams): ChargeResponse;
checkStatus(requestId: string): PaymentStatusResponse;
}