本文へ移動
商品とサブスクリプションチェックアウトセッションの作成

プロダクトアイテムで Checkout Session を作成

Tokenzプロダクトカタログのバーチャルアイテムとバンドルを使用して Checkout Session を作成する方法を説明します。

概要

Tokenzプロダクトシステムを使用すると、Tokenz Dashboard からバーチャルアイテムとバンドルを管理できます。これらのプロダクトアイテムは、チェックアウトセッション作成時に直接参照でき、プロダクト管理からチェックアウト完了まで完全にプログラムによるフローを可能にします。

前提条件

プロダクトアイテムでチェックアウトセッションを作成する前に、以下を確認してください:

  1. API認証情報: Tokenz Dashboard からシークレットキーを取得
  2. プロダクト設定: Tokenz Dashboard
  3. プロダクトID: 使用するアイテムのIDをメモ(形式:virtualitem_xxxまたはbundle_xxx) - これらは Product API から取得でき、完全にプログラムによるチェックアウトフロー作成が可能

プロダクトアイテムで Checkout Session を作成

バーチャルアイテムの使用

Checkout Session 作成時に、プロダクトIDでバーチャルアイテムを直接参照できます。価格と製品情報はプロダクト設定から自動的に取得されます。

bash
curl --request POST \
  --url https://api.tokenz.one/v1/checkoutsession \
  --header 'Authorization: Bearer secret_test_YOUR_KEY_HERE' \
  --header 'Content-Type: application/json' \
  --data '{
    "itemDetails": [
      {
        "virtualItem":{
          "id": "virtualitem_5xA9BnKfvH7",
          "quantity": 2
        }
      },
      {
        "virtualItem":{
          "id": "virtualitem_3yB8CmLgwI8",
          "quantity": 1
        }
      }
    ],
    "customerInfo": {},
    "successUrl": "https://yourdomain.com/success",
    "pendingUrl": "https://yourdomain.com/pending",
    "cancelUrl": "https://yourdomain.com/cancel",
    "locale": "ja_JP"
  }'

バンドルの使用

bash
curl --request POST \
  --url https://api.tokenz.one/v1/checkoutsession \
  --header 'Authorization: Bearer secret_test_YOUR_KEY_HERE' \
  --header 'Content-Type: application/json' \
  --data '{
    "itemDetails": [
      {
        "bundle": {
          "id": "bundle_8mR5TnKfvH7",
          "quantity": 1
        }
      }
    ],
    "customerInfo": {},
    "successUrl": "https://yourdomain.com/success",
    "pendingUrl": "https://yourdomain.com/pending",
    "cancelUrl": "https://yourdomain.com/cancel",
    "locale": "ja_JP"
  }'

アイテムとバンドルの組み合わせ

単一の Checkout Session でバーチャルアイテムとバンドルの両方を組み合わせることができます:

bash
curl --request POST \
  --url https://api.tokenz.one/v1/checkoutsession \
  --header 'Authorization: Bearer secret_test_YOUR_KEY_HERE' \
  --header 'Content-Type: application/json' \
  --data '{
    "itemDetails": [
      {
        "bundle": {
          "id": "bundle_8mR5TnKfvH7",
          "quantity": 1
        }
      },
      {
        "virtualItem": {
          "id": "virtualitem_5xA9BnKfvH7",
          "quantity": 3
        }
      }
    ],
    "customerInfo": {},
    "successUrl": "https://yourdomain.com/success",
    "pendingUrl": "https://yourdomain.com/pending",
    "cancelUrl": "https://yourdomain.com/cancel"
    "locale": "ja_JP"
  }'

実装例

javascript
import fetch from 'node-fetch';

async function createCheckoutSession() {
  const response = await fetch('https://api.tokenz.one/v1/checkoutsession', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.TOKENZ_SECRET_KEY}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      itemDetails: [
        {
          virtualItem: {
            id: "virtualitem_5xA9BnKfvH7",
            quantity: 2
          }
        }
      ],
      successUrl: "https://yourdomain.com/success",
      pendingUrl: "https://yourdomain.com/pending",
      cancelUrl: "https://yourdomain.com/cancel",
      locale: "ja_JP"
    })
  });

  const session = await response.json();

  // ユーザーをチェックアウトにリダイレクト
  return session.url;
}

レスポンス

APIは Checkout Session オブジェクトを返します:

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": 900,
      "currency": "JPY"
    },
    "items": [
      {
        "id": "item_1xdzo1ZpRcT",
        "detail": {
          "product": {
            "price": {
              "amount": 450,
              "currency": "JPY"
            },
            "quantity": 2,
            "label": "マナポーション",
            "description": "",
            "images": [
              "https://images.ctfassets.net/z82qbo7cv7ia/2JyshO1smpYQwi0my9Rtif/f7df805472f4a1a0bf4e295afe9378a7/mana-potion.png"
            ],
            "sku": "mana-potion-450",
            "taxCategory": "DIGITAL_GOODS_AND_SERVICES"
          }
        }
      }
    ],
    "test": true,
    "createdAt": "2025-09-22T04:32:04.696Z",
    "updatedAt": "2025-09-22T04:32:04.696Z"
  },
  "createdAt": "2025-09-22T04:32:04.374Z",
  "updatedAt": "2025-09-22T04:32:04.374Z",
  "test": true,
  "customerInfo": {},
  "locale": "en_US",
  "preferApplePay": false,
  "preferGooglePay": false
}

ベストプラクティス

  1. アイテムIDの検証: セッション作成前にプロダクトアイテムIDが存在することを常に確認
  2. エラー処理: 無効または販売終了アイテムの適切なエラー処理を実装
  3. Webhookの使用: 決済ステータスのリアルタイム更新を受信するため Webhook (ウェブフック) を設定
  4. 徹底的なテスト: 本番稼働前にテストモードで統合を検証

次のステップ