# Unity In App Purchases Wrapper

{% hint style="danger" %}
*<mark style="color:red;">**Install**</mark>* <mark style="color:blue;">**In App Purchase**</mark> *package* in Package Manager before importing package below.
{% endhint %}

{% file src="<https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FgT9BhH1ollsV2LuLslw4%2FInAppPurchases%20v2.1.unitypackage?alt=media&token=905c0356-df22-4da1-bd4f-3d7b9e2dd1f6>" %}
Package
{% endfile %}

***

Unity IAP requires UnityServices in order to work, so enable it.

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FH8iZTwOplTneh07TqUts%2Fimage.png?alt=media&#x26;token=260f6297-d3f5-4ef1-a412-2bac3b8ae4f9" alt=""><figcaption><p>Services tab</p></figcaption></figure>

Select organization and Create project ID if you see the same window as above. \
Then toggle ads to "ON".

### For Android:

* Add billing permission in AndroidManifest:

```
<uses-permission android:name="android.permission.BILLING" />
```

* Then upload aab with added permission to store. When it's done you can setup products in google console (if you have already setupped finance info in google console)
* Add products in google console

{% hint style="danger" %}
!!! if unity IAP package < v4.9 -> Go in top bar to Services/In App Purchasing/Configure.. and "Automatically initialize Unity Gaming Services" must be turned off! because IAP.cs do it.
{% endhint %}

Add all product ids from google console to IAP\_Products.cs with product types, you will refer them then. *<mark style="color:green;">**By default added NO\_ADS with id "no\_ads" from google console as example.**</mark>*

{% hint style="danger" %}
Add to IAP.cs function InitializePurchasing() all your products.\
for example I have PRO\_VERSION product:

builder.AddProduct(IAP\_Products.PRO\_VERSION, IAP\_Products.PRO\_VERSION\_TYPE);&#x20;
{% endhint %}

{% hint style="success" %}
To test IAP purchases you need to add testers \
in Google play console in tab "Test Licences" (Настройка > Тестирование лицензий)
{% endhint %}

Now you can call methods from IAP.cs by instance (IAP.instance.IsInitialized and etc).\
Below will be some snippets.

{% code title="check if product bought:" %}

```csharp
if (IAP.instance.IsNonConsumableOwned(IAP_Products.PRODUCT_ID)) {
    // player has it - handle
}
else {
    // player doesn't own this product
}
```

{% endcode %}

{% code title="use product bought event:" %}

```csharp
private void OnIAP_ProductBought(string id) {
    if (String.Equals(id, IAP_Products.NO_ADS, StringComparison.Ordinal)) {
        // player has bought no ads!
    }
}
```

{% endcode %}

{% code title="get currency code and cost:" %}

```csharp
var product = IAP.instance.GetProduct(IAP_Products.PRODUCT_ID);
Debug.Log(product.metadata.localizedPriceString); // $4,99 or $4.99 (culture depends)
Debug.Log(product.metadata.localizedPrice);       // 4,99 or 4.99 (culture depends)
Debug.Log(product.metadata.isoCurrencyCode);      // USD
```

{% endcode %}

{% hint style="info" %}
How to test guide links:

* <https://developer.android.com/google/play/billing/test#one-time>
* <https://developer.android.com/google/play/billing/test#subs>
  {% endhint %}
