# In App Purchases

Handle in app purchases when running as a Mac App Store app.

## Methods

### <mark style="color:purple;">Purchase Product</mark>

<mark style="color:purple;background-color:purple;">**macOS**</mark> Creates an in app purchase.

{% hint style="info" %}
You should be sure to "finish" a transaction once you are done processing the events for it (ex. once a transaction is updated to a terminal state like "failed", or "purchased")
{% endhint %}

{% tabs %}
{% tab title="JavaScript" %}

```javascript
await window.deskifier.purchases.purchase({ arguments })
```

**Arguments**

* `productId` <mark style="color:green;">String</mark> (Required)
* `quantity` <mark style="color:green;">Number</mark> (Optional)\
  Defaults to `1`.
* `username` <mark style="color:green;">String</mark> (Optional)\
  The string that associates the transaction with a user account on your service.

**Returns**

* `success` <mark style="color:green;">Boolean</mark>\
  Returns `true` if the product is valid and added to the payment queue.
* `message` <mark style="color:green;">String</mark>\
  Additional confirmation, or error details if action was unsuccessful.

**Example**

{% code overflow="wrap" %}

```javascript
const result = await window.deskifier.purchases.purchase({
    productId: "example1"
});

console.log(result.success);  // true
```

{% endcode %}
{% endtab %}
{% endtabs %}

***

### <mark style="color:purple;">Restore Completed Transactions</mark>

<mark style="color:purple;background-color:purple;">**macOS**</mark> Generates previously finished transactions. This method can be called either to install purchases on additional devices, or to restore purchases for an application that the user deleted and reinstalled.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
await window.deskifier.purchases.restore()
```

**Returns**

* `success` <mark style="color:green;">Boolean</mark>\
  If the action was successful.
* `message` <mark style="color:green;">String</mark>\
  Additional confirmation, or error details if action was unsuccessful.
  {% endtab %}
  {% endtabs %}

***

### <mark style="color:purple;">Finish Transaction</mark>

<mark style="color:purple;background-color:purple;">**macOS**</mark> Marks a transaction as finished.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
await window.deskifier.purchases.finish({ arguments })
```

**Arguments**

* `transactionDateIso` <mark style="color:green;">String</mark> (Required)\
  The transaction date ISO

**Returns**

* `success` <mark style="color:green;">Boolean</mark>\
  If the update was checked.
* `message` <mark style="color:green;">String</mark>\
  Additional confirmation, or error details if action was unsuccessful.
  {% endtab %}
  {% endtabs %}

***

### <mark style="color:purple;">Finish All Transactions</mark>

<mark style="color:purple;background-color:purple;">**macOS**</mark> Marks all transactions as finished.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
await window.deskifier.purchases.finishAll()
```

**Returns**

* `success` <mark style="color:green;">Boolean</mark>\
  If the update was checked.
* `message` <mark style="color:green;">String</mark>\
  Additional confirmation, or error details if action was unsuccessful.
  {% endtab %}
  {% endtabs %}

***

## Properties

### <mark style="color:purple;">Get Products</mark>

<mark style="color:purple;background-color:purple;">**macOS**</mark> Retrieves the product descriptions.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
await window.deskifier.purchases.getProducts({ arguments })
```

**Arguments**

* `productIds` <mark style="color:blue;">Array of</mark> <mark style="color:green;">String</mark> (Required)\
  The product identifiers to look up.

**Returns**

* `success` <mark style="color:green;">Boolean</mark>\
  Returns `true` if the products were retrieved successfully.
* `message` <mark style="color:green;">String</mark>\
  Additional confirmation, or error details if action was unsuccessful.
* `products` <mark style="color:blue;">Array of</mark> [<mark style="color:blue;">Electron.Product</mark>](https://www.electronjs.org/docs/latest/api/structures/product)
  {% endtab %}
  {% endtabs %}

***

### <mark style="color:purple;">Get Active Transactions</mark>

<mark style="color:purple;background-color:purple;">**macOS**</mark> A list of all transactions that currently aren't finished.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
await window.deskifier.purchases.getActiveTransactions()
```

**Returns**

* `success` <mark style="color:green;">Boolean</mark>\
  Returns `true` if the product is valid and added to the payment queue.
* `message` <mark style="color:green;">String</mark>\
  Additional confirmation, or error details if action was unsuccessful.
* `activeTransactions` <mark style="color:blue;">Array of</mark> [<mark style="color:blue;">Electron.Transaction</mark>](https://www.electronjs.org/docs/latest/api/structures/transaction)
  {% endtab %}
  {% endtabs %}

***

### <mark style="color:purple;">Get Receipt URL</mark>

<mark style="color:purple;background-color:purple;">**macOS**</mark> Retrieves a URL with a link to all of the user's transactions for the given app.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
await window.deskifier.purchases.getReceiptUrl()
```

**Returns**

* `success` <mark style="color:green;">Boolean</mark>\
  Returns `true` if the product is valid and added to the payment queue.
* `message` <mark style="color:green;">String</mark>\
  Additional confirmation, or error details if action was unsuccessful.
* `receiptUrl` <mark style="color:green;">String</mark>
  {% endtab %}
  {% endtabs %}

### <mark style="color:purple;">Can Make Payments</mark>

<mark style="color:purple;background-color:purple;">**macOS**</mark> Whether a user can make a payment.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
await window.deskifier.purchases.canMakePayments()
```

**Returns**

* `canMakePayments` <mark style="color:green;">Boolean</mark>
  {% endtab %}
  {% endtabs %}

***

## Events

### <mark style="color:purple;">Transaction Updated</mark>

<mark style="color:purple;background-color:purple;">**macOS**</mark> Fires when one or more transactions have been updated.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
window.deskifier.purchases.onTransactionUpdated((data) => { })
```

**Arguments**

* `transactions` <mark style="color:blue;">Array of</mark> [<mark style="color:blue;">Electron.Transaction</mark>](https://www.electronjs.org/docs/latest/api/structures/transaction)
  {% endtab %}
  {% endtabs %}


---

# 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://deskifier.gitbook.io/deskifier/in-app-purchases.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.
