使用目錄商品建立結帳工作階段
了解如何使用您在 Tokenz Dashboard 管理的目錄商品建立結帳工作階段。
概述
虛擬物品和組合包在 Tokenz Dashboard 中都以目錄商品管理。建立結帳工作階段時,您以 id 引用目錄商品及其中一個價格方案。名稱、圖片和價格都取自您的目錄,因此請求中不需要帶價格。
前提條件
在使用目錄商品建立結帳工作階段之前,請確保您已:
- API 憑證:從 Tokenz Dashboard 取得的金鑰。
- 目錄設定:在 Tokenz Dashboard 中發布要販售的目錄商品,且至少有一個已上架的價格方案。
- 目錄商品與價格方案 id:目錄商品的 id(
catalogitem_…)以及其價格方案的 id(priceplan_…)。兩者都可從目錄商品端點取得。測試金鑰回傳測試商品,其 id 以_t結尾。
使用目錄商品建立結帳工作階段
每項目錄商品在 itemDetails 中新增一個項目。每個項目含有一個 catalogItem 物件,三個欄位皆為必填:
catalogItemId:目錄商品的id。pricePlanId:商品pricePlans中其中一個方案的id。商品的defaultPricePlanId是優先顯示的方案。quantity:販售數量,至少為 1。
虛擬物品和組合包的引用方式相同。
單一目錄商品
bash
curl --request POST \
--url https://api.tokenz.one/v2/checkoutsession \
--header 'Authorization: Bearer secret_test_YOUR_KEY_HERE' \
--header 'Content-Type: application/json' \
--data '{
"itemDetails": [
{
"catalogItem": {
"catalogItemId": "catalogitem_7Gp1KlY3wSz_t",
"pricePlanId": "priceplan_4Tm8XuQ5pFg_t",
"quantity": 2
}
}
],
"customerInfo": {},
"successUrl": "https://yourdomain.com/success",
"pendingUrl": "https://yourdomain.com/pending",
"cancelUrl": "https://yourdomain.com/cancel",
"locale": "zh_TW"
}'
多項目錄商品
一個結帳工作階段可以包含多項目錄商品,例如一個組合包加一個虛擬物品:
bash
curl --request POST \
--url https://api.tokenz.one/v2/checkoutsession \
--header 'Authorization: Bearer secret_test_YOUR_KEY_HERE' \
--header 'Content-Type: application/json' \
--data '{
"itemDetails": [
{
"catalogItem": {
"catalogItemId": "catalogitem_8Hq2LmZ4xTa_t",
"pricePlanId": "priceplan_3Rk9VwP2nDe_t",
"quantity": 1
}
},
{
"catalogItem": {
"catalogItemId": "catalogitem_7Gp1KlY3wSz_t",
"pricePlanId": "priceplan_4Tm8XuQ5pFg_t",
"quantity": 3
}
}
],
"customerInfo": {},
"successUrl": "https://yourdomain.com/success",
"pendingUrl": "https://yourdomain.com/pending",
"cancelUrl": "https://yourdomain.com/cancel",
"locale": "zh_TW"
}'
實作範例
javascript
import fetch from 'node-fetch';
async function createCheckoutSession() {
const response = await fetch('https://api.tokenz.one/v2/checkoutsession', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.TOKENZ_SECRET_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
itemDetails: [
{
catalogItem: {
catalogItemId: "catalogitem_7Gp1KlY3wSz_t",
pricePlanId: "priceplan_4Tm8XuQ5pFg_t",
quantity: 2
}
}
],
customerInfo: {},
successUrl: "https://yourdomain.com/success",
pendingUrl: "https://yourdomain.com/pending",
cancelUrl: "https://yourdomain.com/cancel",
locale: "zh_TW"
})
});
const session = await response.json();
// 將使用者重新導向到結帳頁面
return session.url;
}
回應
API 回傳一個結帳工作階段物件:
json
{
"id": "checkoutsession_12345678QsZ",
"object": "checkoutsession",
"url": "https://checkout.tokenz.one/12345678QsZ",
"cancelUrl": "https://yourdomain.com/cancel",
"pendingUrl": "https://yourdomain.com/pending",
"successUrl": "https://yourdomain.com/success",
"order": {
"id": "order_1xdzo1aZRGB",
"object": "order",
"status": "requiresPayment",
"amount": {
"amount": 398,
"currency": "USD"
},
"items": [
{
"id": "item_1xdzo1ZpRcT",
"detail": {
"product": {
"price": {
"amount": 199,
"currency": "USD"
},
"quantity": 2,
"label": "100 Gems",
"description": "",
"images": [
"https://example.com/images/gems-100.png"
],
"sku": "gems-100",
"taxCategory": "DIGITAL_GOODS_AND_SERVICES",
"catalogItemId": "catalogitem_7Gp1KlY3wSz_t",
"pricePlanId": "priceplan_4Tm8XuQ5pFg_t"
}
}
}
],
"test": true,
"createdAt": "2026-11-20T04:32:04.696Z",
"updatedAt": "2026-11-20T04:32:04.696Z"
},
"createdAt": "2026-11-20T04:32:04.374Z",
"updatedAt": "2026-11-20T04:32:04.374Z",
"test": true,
"customerInfo": {},
"locale": "zh_TW",
"preferApplePay": false,
"preferGooglePay": false
}
order.items 中每個 product 都帶有定價來源的 catalogItemId 和 pricePlanId。請用它們判斷要發放的內容。
錯誤
含有目錄商品項目的請求可能因下列錯誤而失敗:
400itemDetails.catalogItem.not-supported:您的帳戶使用 Payment Gateway 模式,此模式不支援目錄商品。400pricePlan.recurring-not-supported:在不建立訂閱的結帳工作階段中使用RECURRING方案。400pricePlan.one-time-not-supported:在建立訂閱的結帳工作階段中使用ONE_TIME方案。404entity.not-found:找不到引用的目錄商品或價格方案。
最佳實踐
- 從 API 讀取 id:從目錄商品端點取得目錄商品和價格方案的 id,不要寫死在程式中
- 錯誤處理:為上述錯誤實作適當的錯誤處理
- 使用 Webhook:設定 webhook 以接收支付狀態的即時更新
- 充分測試:上線前使用測試模式驗證您的整合
後續步驟
- 設定 webhook 處理支付確認
- 使用測試模式測試您的整合
- 在 Tokenz Dashboard 監控交易