k.payment.wechat / wechatApp / wechatJsApi / weChatH5
WeChat Pay (Native, App, JSAPI, and H5).
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
KScript API (k.api.get / k.api.post), server-side <script env="server"> | Configure WeChatV3Setting (appId, merchantId, API V3 key, and certificates; CNY only) in site CMS | Initiate WeChat Native QR, H5, JSAPI, or App payments and poll payment status |
For the full WeChat H5 integration flow, see WeChat H5 Payment Process. H5 uses k.payment.weChatH5 and consumes nextAction.redirectUrl; do not mix it with Native QR renderHtml.
Quick Start
k.api.get(() => {
const result = k.payment.wechat.charge({
name: "Kooboo order",
description: "Order payment",
totalAmount: 1.5,
currency: "CNY",
order: "order-id",
redirectUrl: "/payment/success?orderId=order-id",
callbackCodeName: "PaymentCallback"
})
k.response.setHeader("Content-Type", "text/html; charset=utf-8")
k.response.write(result.nextAction.renderHtml)
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
wechat.charge(params) | Create WeChat Native QR payment request (renderHtml) | ChargeResponse |
wechatApp.charge(params) | Create WeChat App payment request (responseData) | ChargeResponse |
weChatH5.charge(params) | Create WeChat H5 payment request (redirectUrl) | ChargeResponse |
wechatJsApi.charge(params) | Create WeChat JSAPI payment request (requires openId) | ChargeResponse |
<provider>.checkStatus(requestId) | Check WeChat payment status by requestId | PaymentStatusResponse |
Differences in Payment Methods
| API | WeChat payment product | Usage scenarios | Where the user completes payment |
|---|---|---|---|
k.payment.wechat | Native payment | Generate QR code on PC website or checkout page | User scans QR code with WeChat |
k.payment.weChatH5 | H5 payment | Mobile browser web payment outside merchant app | Mobile browser redirects to WeChat Pay |
k.payment.wechatApp | App payment | Merchant iOS/Android app invokes WeChat SDK | Native app invokes WeChat Pay |
k.payment.wechatJsApi | JSAPI payment | WeChat built-in browser, official account, or mini program | Inside WeChat runtime |
During H5 payment verification, client redirect or return results must not be treated as final payment confirmation. After payment completes, call checkStatus() or k.payment.getRequest() using the requestId returned by charge().
wechat.charge()
Create a WeChat Native scan code payment request.
| Parameter | Type | Required | Description |
|---|---|---|---|
redirectUrl | string | Yes | Redirect URL after payment when using nextAction.renderHtml |
totalAmount | number | Yes | Order amount |
name | string | Yes | Order name |
description | string | Yes | Order description |
currency | string | Yes | Currency, such as CNY |
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.wechat.charge({
name: "Kooboo order",
description: "Order payment",
totalAmount: 1.5,
currency: "CNY",
order: "order-id",
redirectUrl: "/payment/success?orderId=order-id",
callbackCodeName: "PaymentCallback"
})
k.response.setHeader("Content-Type", "text/html; charset=utf-8")
k.response.write(result.nextAction.renderHtml)
})wechatApp.charge()
Create a WeChat App payment request.
| Parameter | Type | Required | Description |
|---|---|---|---|
totalAmount | number | Yes | Order amount |
name | string | Yes | Order name |
description | string | Yes | Order description |
currency | string | Yes | Currency, such as CNY |
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.post(() => {
const result = k.payment.wechatApp.charge({
name: "Tea",
description: "Tea description",
totalAmount: 0.01,
currency: "CNY",
order: "order-id",
callbackCodeName: "PaymentCallback"
})
return result.nextAction.responseData
})weChatH5.charge()
Create a WeChat H5 payment request. H5 payment requires order, currency, and totalAmount together; missing any of them causes WeChat order creation to fail.
| Parameter | Type | Required | Description |
|---|---|---|---|
totalAmount | number | Yes | Order amount |
currency | string | Yes | Currency, such as CNY |
order | string | Yes | Commerce order ID. Required at runtime for H5; updates order status when paid |
name | string | Yes | Order name |
description | string | Yes | Order description |
callbackCodeName | string | No | Callback code name executed after payment completes |
Returns: ChargeResponse
k.api.get(() => {
const result = k.payment.weChatH5.charge({
name: "Tea",
description: "Tea description",
totalAmount: 0.01,
currency: "CNY",
order: "order-id"
})
k.response.redirect(result.nextAction.redirectUrl)
})wechatJsApi.charge()
Create a WeChat JSAPI payment request. result.nextAction.responseData is a JSON-encoded string that must be parsed with JSON.parse(...) before passing to WeixinJSBridge.invoke('getBrandWCPayRequest', ...). When returning it from k.api.post, parse it with JSON.parse(result.nextAction.responseData) first so the API responds with a JSON object rather than a double-encoded JSON string.
| Parameter | Type | Required | Description |
|---|---|---|---|
openId | string | Yes | WeChat user OpenID |
totalAmount | number | Yes | Order amount |
name | string | Yes | Order name |
description | string | Yes | Order description |
currency | string | Yes | Currency (CNY only) |
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.post(() => {
const result = k.payment.wechatJsApi.charge({
name: "Tea",
description: "Tea description",
totalAmount: 0.01,
currency: "CNY",
order: "order-id",
openId: "<wechat-openid>"
})
return {
requestId: result.requestId,
payParams: JSON.parse(result.nextAction.responseData)
}
})checkStatus()
Check payment status.
| Parameter | Type | Required | Description |
|---|---|---|---|
requestId | string | Yes | requestId returned by charge() |
Returns: PaymentStatusResponse
k.api.get(() => {
const status = k.payment.wechat.checkStatus("request-id")
return { paid: status.paid, failed: status.failed, status: status.status }
})Configuration Structure
WeChatV3Setting
| Property | Type | Description |
|---|---|---|
appId | string | WeChat App ID |
merchantId | string | Merchant ID |
aPIV3Key | string | API V3 key |
certificatePrivateKey | SettingFile | Merchant certificate private key |
certificate | SettingFile | Merchant certificate |
TypeScript Definitions
Show full TypeScript definitions
interface WeChatNative {
setting: WeChatV3Setting;
charge(params: WeChatNativeParams): ChargeResponse;
checkStatus(requestId: string): PaymentStatusResponse;
}
interface WeChatApp {
setting: WeChatV3Setting;
charge(chargeParams: ChargeParams): ChargeResponse;
checkStatus(requestId: string): PaymentStatusResponse;
}
interface WeChatH5 {
setting: WeChatV3Setting;
charge(chargeParams: ChargeParams): ChargeResponse;
checkStatus(requestId: string): PaymentStatusResponse;
}
interface WeChatJsApi {
setting: WeChatV3Setting;
charge(chargeParams: WeChatJsApiChargeParams): ChargeResponse;
checkStatus(requestId: string): PaymentStatusResponse;
}
interface WeChatNativeParams extends ChargeParams {
redirectUrl: string;
}
interface WeChatJsApiChargeParams extends ChargeParams {
openId: string;
}
interface WeChatV3Setting {
name: string;
group: string;
appId: string;
merchantId: string;
aPIV3Key: string;
certificatePrivateKey: any;
certificate: any;
getNotifyUrl(): string;
}