---
metadata:
  - name: generator
    content: Diplodoc Platform v5.55.3
alternate:
  - https://pay.yandex.ru/docs/en/custom/inapps/unity/integration.md
  - href: en/custom/inapps/unity/integration.md
    type: text/markdown
    title: Markdown version
  - href: ../../../llms.txt
    type: text/markdown
    title: llms.txt
---
> **Documentation Index:** Fetch the complete configuration index at https://pay.yandex.ru/docs/en/llms.txt

# Unity-plugin integration

### Integration requirements {#requirements}

Android version: **Android 7.0 (API Level 24)** or higher.
<br>Unity version: **Unity 2022.3** or higher.

Before starting the integration, obtain several IDs and add them to the project:

- [Merchant ID](https://pay.yandex.ru/docs/en/console/index.md)
- SHA256 Fingerprints
- Client ID (`YANDEX_CLIENT_ID`)
- Android package name (`applicationId`)

## Step 1. Get the necessary IDs {#create-oauth-app}

1. Get the hash value of SHA256 Fingerprints using the `keytool` utility:

   ```Bash
   keytool -list -v -alias <your-key-name> -keystore <path-to-production-keystore>
   ```

   After entering the command, the hash value will be displayed in the block `Certificate fingerprints: SHA256`.

1. <!-- source: en/custom/_includes/yandex-oauth.md -->
   To [register](../../_includes/(https:/yandex.ru/dev/id/doc/en/register-client)) the app, go to [Yandex OAuth](https://oauth.yandex.com/) and click **Create app**.
   <!-- endsource: en/custom/_includes/yandex-oauth.md -->
1. <!-- source: en/custom/_includes/yandex-oauth.md -->
   In the **Service name** field, enter the name to be displayed to users on the authorization screen, and upload the application icon.
   <!-- endsource: en/custom/_includes/yandex-oauth.md -->
1. <!-- source: en/custom/_includes/yandex-oauth.md -->
   Under **Platforms**, select **Android app** and set its parameters:

   - **Android package name**: Unique app name from the project config file's `applicationId`.
   - **SHA256 Fingerprints**: SHA256 hash value from step 1. **The hash must contain capital letters only**.
   <!-- endsource: en/custom/_includes/yandex-oauth.md -->

1. <!-- source: en/custom/_includes/yandex-oauth.md -->
   Make sure that your app has access to Yandex Pay added in Yandex OAuth. To do this, select **Payment via Yandex Pay** in the **Permission name** field of the **Data access** section.
   <!-- endsource: en/custom/_includes/yandex-oauth.md -->
1. <!-- source: en/custom/_includes/yandex-oauth.md -->
   Click **Create app** and copy the **Client ID** field value.
   <!-- endsource: en/custom/_includes/yandex-oauth.md -->
1. <!-- source: en/custom/_includes/yandex-oauth.md -->
   On the [Settings](https://console.pay.yandex.ru/settings) page of the Yandex Pay dashboard, specify the Client ID, SHA256, and Android package name values in the **Client ID**, **SHA256 Fingerprints**, and **Android package name** fields, respectively.
   <!-- endsource: en/custom/_includes/yandex-oauth.md -->

## Step 2. Connect the Yandex Pay InApps plugin {#integration}

{% list tabs %}

- UPM (GitHub)

  1. Connect the External Dependency Manager according to the [documentation](https://github.com/googlesamples/unity-jar-resolver/tree/master?tab=readme-ov-file#getting-started).

  1. Add the Yandex Pay InApps plugin depending on the [Packages/manifest.json](https://docs.unity3d.com/Manual/upm-manifestPrj.html):

    ```json translate=no
    {
      "dependencies": {
        "com.yandex.pay.inapps": "https://github.com/yandex-pay-mobile/inapps-unity-plugin.git#v1.3.0"
      }
    }
     ```

{% endlist %}

## Step 3. Specify the Client ID {#include-yandex-client-id}

Specify the received Client ID in the `Assets/Plugins/Android/launcherTemplate.gradle` assembly script in `manifestPlaceholders` as the `YANDEX_CLIENT_ID` value:

```Gradle
android {
    defaultConfig {
        // Add your Client ID
        manifestPlaceholders["YANDEX_CLIENT_ID"] = "12345678901234567890"
    }
}
```

{% cut "If there is no such file" %}

1. In the Unity editor window, select the menu item: **Edit** → **Project Settings** → **Player** and open the Android tab.
1. Open the **Publishing Settings** section.
1. In the **Build** section, enable the **Custom Launcher Gradle Template** option.

{% note info %}

The next step will also require a custom ```AndroidManifest.xml```. To create it, enable the **Custom Main Manifest** option in the **Build** section.

{% endnote %}

{% endcut %}

## Step 4. Add Android Activity to the manifest {#add-activity-to-manifest}

Add YPayActivity with the specified theme to the manifest `Assets/Plugins/Android/AndroidManifest.xml`:

```XML
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android">
    <application>
        ...
        <activity android:name="com.yandex.pay.inapps.YPayActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
    </application>
</manifest>

```

{% cut "If there is no such file" %}

1. In the Unity editor window, select the menu item: **Edit** → **Project Settings** → **Player** and open the Android tab.
1. Open the **Publishing Settings** section.
1. In the **Build** section, enable the **Custom Main Manifest** option.

{% endcut %}

## Step 5. Resolve Android dependencies using the External Dependency Manager  {#resolve-dependencies}

In the Unity Editor window, select the menu item: **Assets** → **External Dependency Manager** → **Android** → **Force Resolve**. 

## Step 6. Place the payment button on the stage {#add-button-to-scene}

1. In the **Project** Unity Editor tab, open the **Packages** folder.
1. Find **Yandex Pay InApps** in the list of connected packages.
1. Find the button template in the ```Yandex Pay InApps/Runtime/Prefabs/YPay Button``` directory and drag it onto the stage.

{% note warning %}

The payment button, like all UI elements, must be located inside the **Canvas** object. **Canvas** also requires an additional **EventSystem** object to work. For more information, see [Unity documentation](https://docs.unity3d.com/2023.2/Documentation/Manual/UICanvas.html).

{% endnote %}

![prefab](./_assets/button-prefab.png)

## Step 7. Add merchant data {#configure-button}

![button](./_assets/button-editor.png){width=70%}

In the Unity inspector, select the **Y Pay Button** object. Fill in the following information in the button editor:
- `Merchant Id` — the unique identifier of the merchant. It can be obtained when the merchant registers in the Yandex Pay service;
- `Merchant Name` — the name of the merchant that will be displayed to the user;
- `Merchant Url` — the URL of the merchant that will be displayed to the user.

Enable the `Sandbox` option to use the test environment.

## Step 8. Generate the payment details {#implement-data-provider}

Develop a class that implements the **IYPayDataProvider** interface. This class should generate a payment session key and receive a payment link from the Yandex Pay service.

{% cut "Implementation example" %}

```C#
using System.Threading.Tasks;
using UnityEngine;
using YPay;

public class YPayDataProvider : MonoBehaviour, IYPayDataProvider
{
    public string GetPaymentSessionKey()
    {
        // Generating a payment session key
        var sessionKeyGenerator = new SomeSessionKeyGenerator();
        var sessionKey = sessionKeyGenerator.GenerateSessionKey();

        return sessionKey;
    }

    public async Task<string> GetPaymentUrlAsync()
    {
        // Getting a payment link from the Pay API
        var networkManager = new SomeNetworkManager();
        var paymentUrl = await networkManager.GetPaymentUrlAsync();

        return paymentUrl;
    }
}
```

{% endcut %}

Add the created script as a component to the payment button.

{% note info %}

For more information about generating a payment link, see the [backend documentation](https://pay.yandex.ru/docs/en/custom/backend/yandex-pay-api/order/merchant_v1_orders-post).

{% endnote %}

## Step 9. Process the payment result {#implement-result-listener}

Develop a class that implements the **IYPayResultListener** interface.

{% cut "Implementation example" %}

```C#
using UnityEngine;
using YPay;

public class YPayResultListener : MonoBehaviour, IYPayResultListener
{
    public void OnPaymentResult(IYPayResult result)
    {
        // Processing the payment result
        switch (result)
        {
            case IYPayResult.Success success:
                HandleSuccess(success.OrderId);
                break;
            case IYPayResult.Failure failure:
                HandleError(failure.ErrorMessage);
                break;
            case IYPayResult.Cancelled cancelled:
                HandleCancellation();
                break;
        }
    }
}
```

{% endcut %}

{% cut "Error codes" %}

In the event of payment failure, the `errorMsg` error code is also returned:

| **Error codes**               | **Description**                                                                   |
|-------------------------------|-----------------------------------------------------------------------------------|
| `incorrect payment url`       | Wrong payment link                                                                |
| `transaction error`           | Transaction processing error                                                      |
| `failed to parse order ID`    | Couldn't get the `orderId` when returning the result                                |
| `invalid intent parsing`      | Couldn't receive payment result                                                   |
| `invalid result code`         | Invalid payment result code                                                       |
| `unresolved payment strategy` | Couldn't handle the payment results because your app shut down in the background |
| `session key not provided`    | The session key was not provided                                                  |
| `config data not provided`    | The `СonfigData` object was not provided                                           |
| `payment data not provided`   | The `PaymentData` object was not provided                                          |

{% endcut %}

Add the created script as a component to the payment button.

## Step 10. Customize the payment button {#customize-button}

Customize the appearance of the payment button using the Unity editor to meet your design requirements.
