サブスクリプションアイテムは、顧客がサブスクライブしている特定の製品と価格を表します。これらはサブスクリプションのコンポーネントであり、特に従量制の使用量を持つプランの継続請求を管理するために不可欠です。
このAPIを使用すると、サブスクリプションアイテムの作成、取得、更新、削除ができます。重要なことに、従量制課金のための使用量を報告するメソッドも提供します。
サブスクリプションアイテムオブジェクト
サブスクリプションアイテムオブジェクトには、サブスクリプション内の特定のアイテムに関する詳細が含まれています。
The TSubscriptionItemExpanded Object
interface TSubscriptionItemExpanded {
id: string; // サブスクリプションアイテムの一意の識別子
subscription_id: string; // このアイテムが属するサブスクリプションのID
price_id: string; // このアイテムに関連付けられた価格のID
quantity: number; // 価格の数量
billing_thresholds: object | null; // 使用量ベースの請求しきい値
metadata: object | null; // 追加情報のためのキーと値のペアのセット
created_at: number; // 作成時のタイムスタンプ
updated_at: number; // 最終更新時のタイムスタンプ
price: TPrice; // 展開されたPriceオブジェクト
}サブスクリプションアイテムの作成
既存のサブスクリプションに新しいアイテム(製品と価格)を追加します。
Create Subscription Item
const subscriptionItem = await payment.subscriptionItems.create({
subscription_id: 'sub_xxxxxxxxxxxxxx',
price_id: 'price_xxxxxxxxxxxxxx',
quantity: 1,
});パラメータ
| Name | Type | Description |
|---|---|---|
subscription_id | string | 必須。このアイテムを追加するサブスクリプションのID。 |
price_id | string | 必須。顧客をサブスクライブさせる価格のID。 |
quantity | number | 価格の数量。デフォルトは 1 です。 |
billing_thresholds | object | このアイテムの使用量ベースの請求しきい値。 |
metadata | object | オブジェクトに添付できるキーと値のペアのセット。これは、オブジェクトに関する追加情報を構造化された形式で保存するのに役立ちます。 |
戻り値
新しく作成された TSubscriptionItem オブジェクトを返します。
サブスクリプションアイテムの取得
既存のサブスクリプションアイテムの詳細を取得します。
Retrieve Subscription Item
const subscriptionItem = await payment.subscriptionItems.retrieve(
'si_xxxxxxxxxxxxxx' // Subscription Item ID
);パラメータ
| Name | Type | Description |
|---|---|---|
id | string | 必須。取得するサブスクリプションアイテムの一意の識別子。 |
戻り値
有効なIDが提供された場合、TSubscriptionItemExpanded オブジェクトを返します。
サブスクリプションアイテムの更新
サブスクリプションアイテムのプロパティを更新します。price_id、quantity、および metadata を更新できます。
Update Subscription Item
const subscriptionItem = await payment.subscriptionItems.update(
'si_xxxxxxxxxxxxxx', // Subscription Item ID
{
quantity: 2,
}
);パラメータ
| Name | Type | Description |
|---|---|---|
id | string | 必須。更新するサブスクリプションアイテムのID。 |
updates | object | 更新するフィールドを含むオブジェクト。 |
更新可能なフィールド
| Name | Type | Description |
|---|---|---|
price_id | string | このアイテムの新しい価格のID。 |
quantity | number | このアイテムの新しい数量。 |
billing_thresholds | object | このアイテムの使用量ベースの請求しきい値。 |
metadata | object | 更新するキーと値のペアのセット。 |
戻り値
更新された TSubscriptionItem オブジェクトを返します。
サブスクリプションアイテムの一覧表示
指定されたサブスクリプションのサブスクリプションアイテムのリストを返します。
List Subscription Items
const subscriptionItems = await payment.subscriptionItems.list({
subscription_id: 'sub_xxxxxxxxxxxxxx',
});パラメータ
| Name | Type | Description |
|---|---|---|
subscription_id | string | 必須。アイテムを一覧表示したいサブスクリプションのID。 |
page | number | ページネーション用のページ番号。デフォルトは 1 です。 |
pageSize | number | ページごとに返すアイテムの数。デフォルトは 20 です。 |
戻り値
ページ分割された TSubscriptionItemExpanded オブジェクトのリスト。
サブスクリプションアイテムの削除
サブスクリプションからアイテムを削除します。これは永続的な操作です。
Delete Subscription Item
const deletedItem = await payment.subscriptionItems.del(
'si_xxxxxxxxxxxxxx', // Subscription Item ID
{
clear_usage: true, // Optional
}
);パラメータ
| Name | Type | Description |
|---|---|---|
id | string | 必須。削除するサブスクリプションアイテムのID。 |
body.clear_usage | boolean | true の場合、従量制価格のすべての使用量レコードがクリアされます。デフォルトは false です。 |
戻り値
削除された TSubscriptionItem オブジェクトを返します。
使用量レコードの作成
従量制 の価格の場合、顧客に正しく請求するために使用量を報告する必要があります。このメソッドは、指定されたサブスクリプションアイテムの使用量レコードを作成します。
Create Usage Record
const usageRecord = await payment.subscriptionItems.createUsageRecord({
subscription_item_id: 'si_xxxxxxxxxxxxxx',
quantity: 100,
action: 'increment',
timestamp: Math.floor(Date.now() / 1000),
});パラメータ
| Name | Type | Description |
|---|---|---|
subscription_item_id | string | 必須。使用量を報告するサブスクリプションアイテムのID。 |
quantity | number | 必須。報告する使用量。正の整数である必要があります。 |
action | string | 報告された quantity の適用方法を決定します。increment (既存の使用量に追加) または set (指定されたタイムスタンプで使用量を上書き) のいずれかです。デフォルトは increment です。 |
timestamp | number | 使用量イベントのUNIXタイムスタンプ。現在の請求期間内である必要があります。デフォルトは現在時刻です。 |
戻り値
作成された TUsageRecord オブジェクトを返します。
使用量レコードサマリーの一覧表示
指定されたサブスクリプションアイテムに対して、請求期間(インボイス)ごとに集計された使用量レコードサマリーのリストを返します。
List Usage Record Summaries
const summaries = await payment.subscriptionItems.listUsageRecordSummaries({
subscription_item_id: 'si_xxxxxxxxxxxxxx',
});パラメータ
| Name | Type | Description |
|---|---|---|
subscription_item_id | string | 必須。サブスクリプションアイテムのID。 |
page | number | ページネーション用のページ番号。デフォルトは 1 です。 |
pageSize | number | ページごとに返すアイテムの数。デフォルトは 20 です。 |
戻り値
ページ分割された TUsageRecordSummary オブジェクトのリスト。
The TUsageRecordSummary Object
interface TUsageRecordSummary {
invoice_id: string; // この期間のインボイスのID
subscription_item_id: string;
total_usage: number; // この期間の総使用量
period: {
start: number; // 請求期間の開始(UNIXタイムスタンプ)
end: number; // 請求期間の終了(UNIXタイムスタンプ)
};
}使用量レコードの一覧表示
サブスクリプションアイテムの個々の使用量レコードのリストを取得します。これにより、報告されたすべての使用量の詳細で集計されていないビューが提供されます。
List Usage Records
const records = await payment.subscriptionItems.listUsageRecords({
subscription_item_id: 'si_xxxxxxxxxxxxxx',
});パラメータ
| Name | Type | Description |
|---|---|---|
subscription_item_id | string | 必須。サブスクリプションアイテムのID。 |
start | number | この時刻以降の使用量レコードをフィルタリングするためのUNIXタイムスタンプ。 |
end | number | この時刻以前またはこの時刻の使用量レコードをフィルタリングするためのUNIXタイムスタンプ。 |
page | number | ページネーション用のページ番号。デフォルトは 1 です。 |
pageSize | number | ページごとに返すアイテムの数。デフォルトは 20 です。 |
戻り値
ページ分割された TUsageRecord オブジェクトのリスト。
The TUsageRecord Object
interface TUsageRecord {
id: string;
subscription_item_id: string;
quantity: number;
timestamp: number; // レコードのUNIXタイムスタンプ
billed: boolean; // この使用量が請求済みかどうか
}