# Cordova In-app Purchase プラグイン

この Cordova プラグインは、iOS、Android、Windows アプリでのアプリ内課金に使用されます。

* Repo: <https://github.com/j3k0/cordova-plugin-purchase>
* Plugin ID/Package Name: `cordova-plugin-purchase`
* Tested Version: 13.6.0

{% hint style="info" %}
デモアプリを動作させるためには、Google Play Console と App Store Connect でアプリ内課金の設定が必要になります。

サードパーティー製 Cordova プラグインを確認する場合は、カスタムビルドデバッガー ( [Android 版](/products_guide/debugger/installation/debugger_android.md#kasutamubirudo-monaca-debaggnobirudotoinsutru) または [iOS 版](/products_guide/debugger/installation/debugger_ios.md#kasutamubirudo-monaca-debaggnobirudo) ) を作成する必要があります。
{% endhint %}

## デモ

[<img src="https://docs.monaca.io/images/common/import_img.png" alt="" data-size="line">プロジェクトをインポート](https://monaca.mobi/ja/directimport?pid=64ff13c7e78885e951195c31)

<figure><img src="/files/8usoWpxeSBoXTxZZLLS9" alt="" width="375"><figcaption></figcaption></figure>

## Monaca クラウド IDE でプラグインを有効にする

1. IDE メニューか&#x3089;**`設定 → Cordova プラグインの管理`**&#x3078;移動します。
2. `Cordova プラグインのインポート`ボタンをクリックします。 次に、\[ZIP ファイル] または \[パッケージ名 / URL] を使用してプラグインをインポートします。

## 使い方

ストアAPI は、主にイベントを使用します。 このプラグインを使用する場合は、イベントを登録する必要があります。以下、`store`は`CdvPurchase.store`オブジェクトを表します。

イベントの中心は `when()` メソッドです。 クエリーを使用して、1つまたは一連の製品への変更を通知することができます。

```javascript
  store.when()
    .productUpdated(updatedPurchase)
    .approved(approvedPurchase)
    .finished(finishedPurchase);
```

`productUpdated` イベントは、製品のフィールドの1つが変更されると起動されます (例: `owned` ステータス) 。

このイベントは、購入状況を追跡し、必要に応じてロック解除を行い、それに応じてビューを更新するための一般的な方法を提供します。

## API リファレンス

ここでは、[デモ](https://monaca.mobi/ja/directimport?pid=64ff13c7e78885e951195c31) で使用されている主な API について説明します。 完全なAPIリファレンスについては、[こちら](https://github.com/j3k0/cordova-plugin-purchase) を参照してください。

### store.verbosity

`verbosity` プロパティは、 `store.js` のコンソール出力の種類を定義します。 使用可能な設定は、次のとおりです。

* `store.QUIET` または `0` すべてのログを無効にします。 (デフォルト)
* `store.ERROR` または `1` エラーメッセージのみを表示します。
* `store.WARNING` または `2` 警告やエラーを表示します。
* `store.INFO` または `3` 情報メッセージも表示します。
* `store.DEBUG` または `4` 内部デバッグメッセージを有効にします。

```javascript
store.verbosity
```

**例**

```javascript
// Enable maximum logging level
store.verbosity = store.DEBUG;
```

### store.register()

このメソッドを使用する前に、ストアに課金アイテムを登録する必要があります。

```javascript
store.register(product);
```

**パラメーター**

| パラメーター名   | 型           | 説明   |
| --------- | ----------- | ---- |
| `product` | JSON Object | 製品情報 |

**例**

```javascript
  store.register({
    id:    ITEM_CONSUMABLE_1, // id without package name!
    alias: 'Consumable Item',
    type:  CdvPurchase.ProductType.CONSUMABLE,
    platform: CdvPurchase.Platform.APPLE_APPSTORE
  });
```

`alias`はプラグインの機能としてあるわけではなく、このアプリ内で参照するために定義しています。\
productの`id` が分かっていれば、

```
store.registeredProducts.find(product.platform, product.id).alias;
```

で取得することが出来ます。\
また、`platform`は、`Android`なら`CdvPurchase.Platform.GOOGLE_PLAY`とします。

### store.restorePurchase()

サーバーから製品データをロードし、ユーザーが既に購入したものを復元します。

```javascript
store.restorePurchase();
```

**例**

```javascript
// ...
// register products and events handlers here
// ...
//
// then and only then, call refresh.
store.restorePurchases();
```

### store.get(id)

`id` で、製品を取得します。

```javascript
store.get(id)
```

| パラメーター名 | 型      | 説明    |
| ------- | ------ | ----- |
| `id`    | String | 製品 ID |

**例**

```javascript
var product = store.get("consumable1");
```

### store.when()

製品関連イベントのコールバックを登録するためのオブジェクトを作成します。 この api の詳細については、[こちら](https://github.com/j3k0/cordova-plugin-purchase/blob/master/doc/api.md#storewhenquery) を参照してください。

```javascript
store.when()
```

**戻り値**

* `Promise`

**例**

```javascript
store.when()
  .productUpdated(updatedPurchase)
  .approved(approvedPurchase)
  .finished(finishedPurchase);
```

### store.ready(callback)

ストアが使用できる状態なったときに呼び出されるコールバックを登録します。

ストアがすでに使用できる状態の場合は、コールバックは直ちに実行されます。 引数なしで `store.ready()` は、 `ready` ステータスを返します。

```javascript
store.ready(callback)
```

**例**

```javascript
store.ready(function() {
    console.log("Store is ready");
});
```

### store.initialize(\[platform])

ストア機能を初期化し、ストアから情報を取得します。`store.ready`のコールバック内で呼び出して下さい。引数は`platform`の配列になりますが、iOSであれば \[ `CdvPurchase.Platform.APPLE_APPSTORE` ]、Androidであれば \[ `CdvPurchase.Platform.GOOGLE_PLAY` ]となります。

```
const platform = store.defaultPlatform();
store.initialize([platform]);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ja.docs.monaca.io/tutorials/in-app_purchase.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
