> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Economy v2 のサブスクリプション

> PlayFab Economy v2 の仮想サブスクリプションを作成して、耐久性のあるゲーム内アイテムへの期間制限付きアクセスを付与し、継続的なプレイヤー特典を管理します。

サブスクリプションを使用すると、耐久財への期間ベースのアクセスを付与できます。サブスクリプション内のアイテムは、[GetInventoryItems](https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/get-inventory-items) を呼び出した後、プレイヤーのインベントリでアンパックされます。

<Note>
  この記事は仮想サブスクリプション、つまりゲーム内に存在するサブスクリプションに焦点を当てています。
</Note>

## サブスクリプションの作成

サブスクリプションを作成するには、まずゲーム内でアクセス/購入可能にしたい公開済みの [カタログ アイテム](/services/playfab/economy-monetization/economy-v2/quickstart#step-3---publish-an-item-to-the-catalog)、[Currency](/services/playfab/economy-monetization/economy-v2/quickstart#part-2-inventory-and-virtual-currencies-overview)、または [ユーザー生成コンテンツ (UGC)](/services/playfab/economy-monetization/economy-v2/ugc/quickstart) があることを確認する必要があります。サブスクリプションを作成する前にカタログをセットアップするには、[Economy V2 のクイックスタート](/services/playfab/economy-monetization/economy-v2/quickstart) を参照してください。

### Game Manager

1. **Economy** > **Catalog (V2)** に移動します。
2. **Subscriptions** タブで **New Subscription** を選択します。

### API

[CreateDraftItem](https://learn.microsoft.com/en-us/rest/api/playfab/economy/catalog/create-draft-item) API を使用してサブスクリプションを作成できます。

サブスクリプションを作成する `CreateDraftItem` API リクエストの例:

```json theme={null}
{
  "Item": {
   "Type": "subscription",
    "Title": {
      "NEUTRAL": "Holiday Skins Pass"
    },
    "StartDate": "2023-03-07T00:01:00.0000000Z",
    "ItemReferences":[
      {
        "Id":"{{GlowingGlovesItemID}}",
        "Amount": 1
      }, 
      {
        "Id":"{{ShinyShoesItemID}}", 
        "Amount": 1
      }
    ],
        "PriceOptions": {
          "Prices": [
        {
            "UnitDurationInSeconds":604800,
              "Amounts": [
                {
                    "ItemId": "{{DiamondItemID}}",
                    "Amount": 5
                },
                {
                    "ItemId": "{{GoldItemID}}",
                    "Amount": 10
                }
                ]
        },      
        {
            "UnitDurationInSeconds":2628000,
              "Amounts": [
                {
                    "ItemId": "{{DiamondItemID}}",
                    "Amount": 7
                },
                {
                    "ItemId": "{{GoldItemID}}",
                    "Amount": 12
                }
                ]
        } ]
          }
        },
  "Publish": true
}
```

***

上記の Holiday Skins Pass サブスクリプションは、`Glowing Gloves` と `Shiny Shoes` の 2 つのアイテムを、2 つの異なる有効期間に対する 2 つの価格ポイントで付与します。最初の 5 個の Diamond と 10 個の Gold の価格セットではサブスクリプションが 1 週間有効となり、2 つ目の 7 個の Diamond と 12 個の Gold の価格セットではサブスクリプションが 1 か月間有効となります。期間は秒単位で設定する必要があることに注意してください。

これにより、サブスクリプションが作成および公開されたことを示すレスポンスが返されるはずです。

```json theme={null}
{
  "code": 200,
    "status": "OK",
    "data": {
        "Item": {
            "Id": "{{NewlyCreatedSubscriptionItemID}}",
            "Type": "subscription",
            "AlternateIds": [],
            "Title": {
                "NEUTRAL": "Holiday Skins Pass"
            },
    ...
}
```

***

## インベントリ内のサブスクリプション

サブスクリプションは、ID、StackId、有効期限、表示プロパティを含むプレイヤーのインベントリ コレクションに存在します。サブスクリプション内のすべての要素は、Add、Subtract、Update などを使用して他のアイテムと同様に変更できます。

Type フィルターと共に [GetInventoryItems](https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/get-inventory-items) を使用して、プレイヤーが現在保有しているすべてのサブスクリプションを取得できます。

```json theme={null}
{ 
    "Entity": {
        "Type": "title_player_account", 
        "Id": "ID" 
    }, 
    "CollectionId": "default", 
    "Filter": "type eq 'subscription'"
}

**Sample Response**

{
    "code": 200,
    "status": "OK",
    "data": {
        "Items": [
            {
                "Id": "{{SubscriptionID}}",
                "StackId": "default",
                "DisplayProperties": {},
                "Type": "subscription",
                "ExpirationDate": "2023-08-27T23:59:59Z"
            }
        ],
        "ETag": "1/Mw=="
    }
}
```

期限切れではないサブスクリプションは、読み取り時にアンパックされます。アンパックされたアイテムには想定される ID、およびサブスクリプション ID とそのサブスクリプションの StackId で構成された一意の複合 StackId が付きます。数量はサブスクリプションのアイテム参照で構成された数量に設定されます。これらのアンパックされたアイテムは変更不可であり、Add、Subtract、Update などを使用して変更することはできません。

```json theme={null}

{ 
    "Entity": {
        "Type": "title_player_account", 
        "Id": "ID" 
    }, 
    "CollectionId": "default", 
    "Filter": "StackId eq 'SubscriptionItemID:default'"
}

**Sample Response**

{
    "code": 200,
    "status": "OK",
    "data": {
        "Items": [
            {
                "Id": "{{GlowingGlovesItemID}}",
                "StackId": "{{SubscriptionID:default}}",
                "DisplayProperties": {},
                "Amount": 1,
                "Type": "catalogItem"
            },
            {
                "Id": "{{ShinyShoesItemID}}",
                "StackId": "{{SubscriptionID:default}}",
                "DisplayProperties": {},
                "Amount": 1,
                "Type": "catalogItem"
            }
        ],
        "ETag": "1/NQ=="
    }
}
```

サブスクリプションのアンパックはフィルタリングとページングの前に行われるため、サブスクリプション アイテムはコレクション内の他のアイテムと同様に確実にフィルターおよびページングできます。

[UpdateInventoryItems](https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/update-inventory-items)、[AddInventoryItems](https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/add-inventory-items)、および [SubtractIventoryItems](https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/subtract-inventory-items) を使用して、プレイヤーのサブスクリプションの有効期限を更新できます。

以下の `UpdateInventoryItems` API 呼び出しは、サブスクリプションの有効期限を更新します。

```json theme={null}
{
   "Entity": { 
      "Type": "title_player_account", 
      "Id": "{{ID}}" 
    }, 
  "CollectionId": "default", 
   "Item": { 
        "Id": "{{SubscriptionID}}", 
        "StackId": "default",
        "DisplayProperties": {},
        "Type": "subscription",
        "ExpirationDate": "2023-03-27T23:59:59Z" 
    } 
} 
```

以下の `AddInventoryItems` API 呼び出しは、DurationInSeconds パラメーターを増やすことで有効期限を延長します。

```json theme={null}
{
    "Entity": { 
        "Type": "title_player_account", 
        "Id": "{{ID}}" 
    }, 
    "CollectionId": "default", 
    "Item": { 
        "Id": "{{SubscriptionID}}" 
    }, 
    "DurationInSeconds":2628000 
} 
```

以下の `SubtractInventoryItems` API 呼び出しは、DurationInSeconds パラメーターを減らすことで有効期限を短縮します。

```json theme={null}
{
    "Entity": { 
        "Type": "title_player_account", 
        "Id": "{{ID}}" 
    }, 
    "CollectionId": "default", 
    "Item": { 
        "Id": "{{SubscriptionID}}" 
    }, 
    "DurationInSeconds":60 
} 
```


## Related topics

- [サービスからサブスクリプション製品を管理する](/ja-jp/publishing/xstore-commerce/xstore-managing-subscriptions.md)
- [サービスから XBOX Game Pass サブスクリプションのアクセスを検出する](/ja-jp/publishing/xstore-commerce/xstore-detecting-game-pass.md)
- [適切な製品タイプを選択する](/ja-jp/publishing/xstore-commerce/xstore-choosing-product-type.md)
- [XBOX services ユーザー特権のクライアント側での使用](/ja-jp/services/xbox-services/fundamentals/identity/privileges/concepts/live-user-privileges-client.md)
- [RTA サービスのベスト プラクティス](/ja-jp/services/xbox-services/fundamentals/rta/concepts/live-rta-best-practices.md)
