# Cordova In-app Purchase Plugin

This Cordova/PhoneGap plugin is used for In-App purchases on iOS, Android and Windows apps.

* Repository: <https://github.com/j3k0/cordova-plugin-purchase>
* Plugin ID/package name: `cordova-plugin-purchase`
* Tested version: "10.5.0

{% hint style="info" %}
In-app billing setup in the Google Play Console and App Store Connect is required in order to make the demo app to work.

To check the third party Cordova plugins, you need to create a custom build debugger ([Android version](/products_guide/debugger/installation/debugger_android.md#build-and-install-a-custom-monaca-debugger) or [iOS version](/products_guide/debugger/installation/debugger_ios.md#building-a-custom-monaca-debugger)).
{% endhint %}

## Demo

[<img src="https://docs.monaca.io/images/common/import_img.png" alt="" data-size="line">Import the in-app purchase plugin demo to your Monaca account](https://monaca.mobi/directimport?pid=5ac6e55ee788855e368b4567)

![](https://docs.monaca.io/images/samples/in-app_purchase.png)

## Enable the plugin in the Monaca IDE

1. From the IDE menu, go to **Config → Manage Cordova Plugins** .
2. Click the **Import Cordova Plugin** button. Then, you can choose to import the plugin using a ZIP file or a URL/package name.

## Usage

The `store` API is mostly events based. As a user of this plugin, you will have to register listeners to changes happening to the products you register.

The core of the listening mechanism is the `when()` method. It allows you to be notified of changes to one or a set of products using a query mechanism:

```javascript
store.when("product").updated(refreshScreen);
store.when("full version").owned(unlockApp);
store.when("subscription").approved(serverCheck);
store.when("downloadable content").downloaded(showContent);
etc.
```

The `updated` event is fired whenever one of the fields of a product is changed (its `owned` status for instance).

This event provides a generic way to track the status of the purchases, to unlock features and to refresh the views accordingly.

## API references

This section describes some of the main functions used in the [demo](https://monaca.mobi/directimport?pid=5ac6e55ee788855e368b4567). For complete API references, please refer to the [repository](https://github.com/j3k0/cordova-plugin-purchase).

### store.verbosity

The `verbosity` property defines how much the code in the `store.js` file writes to the console. Available values:

* `store.QUIET` or `0` to disable all logging (default)
* `store.ERROR` or `1` to show only the error messages
* `store.WARNING` or `2` to show also the warnings
* `store.INFO` or `3` to show also the information messages
* `store.DEBUG` or `4` to show also the internal debugging messages.

```javascript
store.verbosity
```

**Example**

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

### store.register()

Register a product to the store.

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

**Parameter**

| Name      | Type        | Description         |
| --------- | ----------- | ------------------- |
| `product` | JSON Object | Product information |

**Example**

```javascript
store.register({
    id:    'consumable1', // id without package name!
    alias: 'Extra Life',
    type:   store.CONSUMABLE
});
```

### store.refresh()

Load the product data from the servers and restore whatever has already been purchased by the user.

```javascript
store.refresh();
```

**Example**

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

### store.get(id/alias)

Retrieve a product by its `id` or `alias`.

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

| Name    | Type   | Description  |
| ------- | ------ | ------------ |
| `id`    | String | Product ID   |
| `alias` | String | Product name |

**Example**

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

### store.when(query, event, callback)

Register a callback for a product related event. For more details about this api, please refer to [here](https://github.com/j3k0/cordova-plugin-purchase/blob/master/doc/api.md#storewhenquery).

```javascript
store.when(query, event, callback)
```

**Return Value**

* `Promise`

**Example**

```javascript
store.when("subscription1", "approved", function(product) { 
    console.log("Subscription approved!");
});
```

### store.error(callback)

Register an error handler.

```javascript
store.error(callback);
```

**Example**

```javascript
store.error(function(e){
    console.log("ERROR " + e.code + ": " + e.message);
});
```

### store.ready(callback)

Register the callback to be called when the store is ready to be used.

If the store is already ready, callback is executed immediately. `store.ready()` without arguments will return the `ready` status.

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

**Example**

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


---

# 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://en.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.
