價格物件定義了特定產品的收費金額和頻率。它將價格金額和貨幣與產品關聯起來。例如,一個產品可能有多個價格,每個價格使用不同的貨幣或有不同的計費週期。
本文件詳細說明如何使用 SDK 管理價格物件。
價格物件
一個價格物件包含產品定價模型的所有詳細資訊。
| Attribute | Type | Description |
|---|---|---|
id | string | 價格物件的唯一識別碼。 |
product_id | string | 此價格關聯的產品 ID。 |
product | object | 展開的產品物件。 |
active | boolean | 此價格是否可用於新的購買。 |
currency_id | string | 此價格的預設貨幣 ID。 |
currency | object | 展開的 PaymentCurrency 物件。 |
nickname | string | 價格的簡短描述,會向客戶顯示。 |
type | string | one_time 或 recurring 其中之一。 |
unit_amount | string | 以最小貨幣單位表示的價格(例如,美元的美分)。 |
recurring | object | 對於週期性價格,此雜湊包含計費週期資訊。詳情請見下方。 |
lookup_key | string | 使用者定義的唯一鍵,用於檢索價格。 |
metadata | object | 一組您可以附加到物件上的鍵值對。 |
currency_options | array | 指定多種貨幣的不同定價。 |
quantity_available | number | 此價格的總可用數量。0 表示無限制。 |
quantity_sold | number | 已售出的單位數量。 |
quantity_limit_per_checkout | number | 客戶在單次結帳中可購買的最大數量。0 表示無限制。 |
upsell | object | 用於向上銷售至另一價格的設定。 |
recurring 物件屬性
| Attribute | Type | Description |
|---|---|---|
interval | string | 訂閱的計費頻率。day、week、month 或 year 其中之一。 |
interval_count | number | 訂閱計費之間的間隔數。 |
meter_id | string | 用於基於用量計費的計量器 ID。 |
建立價格
為現有產品建立一個新價格。
建立價格
async function createPrice() {
try {
const price = await payment.prices.create({
product_id: 'prod_xxxxxxxxxxxxxx',
unit_amount: 1500, // 例如,$15.00
currency_id: 'usd_xxxxxxxxxxxxxx',
type: 'recurring',
recurring: {
interval: 'month',
interval_count: 1,
usage_type: 'licensed'
},
nickname: 'Monthly Pro Plan',
lookup_key: 'pro_monthly_usd',
});
console.log('價格已建立:', price);
} catch (error) {
console.error('建立價格時發生錯誤:', error.message);
}
}
createPrice();參數
| Name | Type | Description |
|---|---|---|
product_id | string | 必需。 此價格所屬產品的 ID。 |
unit_amount | number | 必需。 要收取的金額,以最小貨幣單位表示。 |
currency_id | string | 必需。 此價格的貨幣 ID。 |
type | string | 定價類型。可以是 one_time 或 recurring。預設為 one_time。 |
active | boolean | 價格是否啟用。預設為 true。 |
nickname | string | 價格的內部名稱。 |
lookup_key | string | 用於參照價格的唯一字串。 |
recurring | object | 包含週期性計費資訊的物件。如果 type 為 recurring,則為必需。 |
currency_options | array | 一個物件陣列,用於指定不同貨幣的價格。 |
metadata | object | 與價格一起儲存的鍵值資料。 |
quantity_available | number | 總可用數量。預設為 0 (無限制)。 |
quantity_limit_per_checkout | number | 每次結帳的最大數量。預設為 0 (無限制)。 |
返回值
返回新建立的價格物件。
回應
{
"id": "price_xxxxxxxxxxxxxx",
"product_id": "prod_xxxxxxxxxxxxxx",
"active": true,
"currency_id": "usd_xxxxxxxxxxxxxx",
"nickname": "Monthly Pro Plan",
"type": "recurring",
"unit_amount": "1500",
"recurring": {
"interval": "month",
"interval_count": 1,
"meter_id": null
},
"lookup_key": "pro_monthly_usd",
"metadata": {},
"product": {
"id": "prod_xxxxxxxxxxxxxx",
"name": "Pro Plan"
},
"currency": {
"id": "usd_xxxxxxxxxxxxxx",
"name": "United States Dollar"
}
}檢索價格
檢索現有價格的詳細資訊。
檢索價格
async function retrievePrice(priceId) {
try {
const price = await payment.prices.retrieve(priceId);
console.log('已檢索價格:', price);
} catch (error) {
console.error('檢索價格時發生錯誤:', error.message);
}
}
retrievePrice('price_xxxxxxxxxxxxxx');參數
| Name | Type | Description |
|---|---|---|
id | string | 必需。 要檢索的價格的唯一識別碼。也可以是 lookup_key。 |
返回值
返回所請求的價格物件。
更新價格
透過設定傳入參數的值來更新現有價格。
更新價格
async function updatePrice(priceId) {
try {
const price = await payment.prices.update(priceId, {
nickname: 'Updated Pro Plan Nickname',
metadata: { order_id: '6735' },
});
console.log('價格已更新:', price);
} catch (error) {
console.error('更新價格時發生錯誤:', error.message);
}
}
updatePrice('price_xxxxxxxxxxxxxx');參數
| Name | Type | Description |
|---|---|---|
id | string | 必需。 要更新的價格 ID。也可以是 lookup_key。 |
updates | object | 一個包含要更新欄位的物件。可更新欄位列表請參見建立方法。注意:如果價格已被使用 (locked),則無法更改 unit_amount 和 recurring 等核心屬性。 |
返回值
返回更新後的價格物件。
列出所有價格
返回您的價格列表。價格按建立日期排序,最新建立的價格會最先顯示。
列出價格
async function listPrices() {
try {
const prices = await payment.prices.list({
product_id: 'prod_xxxxxxxxxxxxxx',
active: true,
limit: 5,
});
console.log('已檢索價格:', prices.list);
} catch (error) {
console.error('列出價格時發生錯誤:', error.message);
}
}
listPrices();參數
| Name | Type | Description |
|---|---|---|
active | boolean | 僅返回具有此啟用狀態的價格。 |
type | string | 僅返回此類型的價格,例如 recurring。 |
currency_id | string | 僅返回此貨幣 ID 的價格。 |
product_id | string | 僅返回此產品 ID 的價格。 |
lookup_key | string | 僅返回具有此查找鍵的價格。 |
page | number | 分頁的頁碼。預設為 1。 |
pageSize | number | 每頁的項目數。預設為 20。 |
返回值
返回一個分頁物件,其中包含價格物件的 list、總 count 和 paging 資訊。
搜尋價格
搜尋與查詢字串相符的價格。
搜尋價格
async function searchPrices() {
try {
const prices = await payment.prices.search({ query: 'Pro Plan' });
console.log('搜尋結果:', prices.list);
} catch (error) {
console.error('搜尋價格時發生錯誤:', error.message);
}
}
searchPrices();參數
| Name | Type | Description |
|---|---|---|
query | string | 搜尋查詢字串。 |
page | number | 分頁的頁碼。預設為 1。 |
pageSize | number | 每頁的項目數。預設為 20。 |
返回值
返回一個分頁物件,其中包含相符的價格物件 list。
封存價格
封存一個價格,使其無法在新的結帳中使用。如果一個價格已被使用,通常封存它比刪除它更好。
封存價格
async function archivePrice(priceId) {
try {
const price = await payment.prices.archive(priceId);
console.log('價格已封存:', price.id, '啟用:', price.active);
} catch (error) {
console.error('封存價格時發生錯誤:', error.message);
}
}
archivePrice('price_xxxxxxxxxxxxxx');參數
| Name | Type | Description |
|---|---|---|
id | string | 必需。 要封存的價格 ID。也可以是 lookup_key。 |
返回值
返回已封存的價格物件,其 active 設為 false。
刪除價格
永久刪除一個價格。此操作無法復原。如果價格已在交易中使用或因其他原因被鎖定,則無法刪除。
刪除價格
async function deletePrice(priceId) {
try {
const deletedPrice = await payment.prices.del(priceId);
console.log('價格已刪除:', deletedPrice.id);
} catch (error) {
console.error('刪除價格時發生錯誤:', error.message);
}
}
deletePrice('price_xxxxxxxxxxxxxx');參數
| Name | Type | Description |
|---|---|---|
id | string | 必需。 要刪除的價格 ID。也可以是 lookup_key。 |
返回值
返回已刪除的價格物件。
更新庫存
手動調整價格的 quantity_sold。這對於在標準結帳流程之外追蹤庫存很有用。
更新庫存
async function updateInventory(priceId) {
try {
// 將已售數量增加 2
const price = await payment.prices.inventory(priceId, {
quantity: 2,
action: 'increment',
});
console.log('庫存已更新。新的已售數量:', price.quantity_sold);
} catch (error) {
console.error('更新庫存時發生錯誤:', error.message);
}
}
updateInventory('price_xxxxxxxxxxxxxx');參數
| Name | Type | Description |
|---|---|---|
id | string | 必需。 價格的 ID。 |
params | object | 一個包含庫存調整詳細資訊的物件。 |
params 物件屬性
| Name | Type | Description |
|---|---|---|
quantity | number | 必需。 用於調整已售數量的金額。 |
action | string | 必需。 調整操作。必須是 increment 或 decrement。 |
返回值
返回更新後的價格物件,其中包含新的 quantity_sold 值。