---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://pay.yandex.ru/docs/en/psp/android-sdk.md
  - href: en/psp/android-sdk.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

# Yandex Pay Android SDK integration

 Yandex Pay Android SDK is a library that enables you to add the Yandex Pay payment button to your app and start accepting payments from users.

## Requirements for connecting {#requirements}

Supported version: Android 6.0 Marshmallow and higher.

## Step 1. Get a YANDEX_CLIENT_ID {#create-oauth-app}

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

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

   Once you enter the command, the hash value will be shown under `Certificate fingerprints: SHA256`.

1. Go to [https://oauth.yandex.com/](https://oauth.yandex.com/).
1. Click **Create app**.
1. Enter the service name.
1. Select Android as a platform and specify the `Android package name` (a unique app name – ⁣`applicationId`) and the hash value in the **SHA256 Fingerprints** field.
1. Under **Which data do you need?**, select `Yandex Pay` → `Pay by Yandex Pay`.
1. Click **Create app**.

## Step 2. Add the SDK {#include-sdk}

1. Add the dependency to your build scripts:

   ```Gradle
   dependencies {
     implementation 'com.yandex.pay:core:0.2.1'
   }
   ```

1. Specify the resulting`YANDEX_CLIENT_ID` in the `build.gradle`build script as a value in `manifestPlaceholders`:

   ```Gradle
   android {
     defaultConfig {
       manifestPlaceholders = [
         // Use your yandex_client_id
         YANDEX_CLIENT_ID: "12345678901234567890",
       ]
     }
   }
   ```

## Step 3. Initialize the library {#init-sdk}

Before using YandexPayLib for the first time (and also before you render the Yandex Pay button for the first time) , execute the following code:

```Kotlin
if (YandexPayLib.isSupported) {
    YandexPayLib.initialize(
        context = this,
        config = YandexPayLibConfig(
            merchantDetails = Merchant(
                id = MerchantID.from("bbb9c171-2fab-45e6-b1f8-6212980aa9bb"),
                name = "MERCHANT_NAME",
                url = "https://merchant.com/",
            ),
            environment = YandexPayEnvironment.PROD,
            locale = YandexPayLocale.SYSTEM,
            logging = true
        )
    )
}
```

When initializing the SDK, pass to `YandexPayLibConfig`:

* `merchantData`: Data of the `Merchant`:
  * `id`: Merchant ID (in the test environment, you can use the test store ID): `bbb9c171-2fab-45e6-b1f8-6212980aa9bb`).
  * `name`: The name of the merchant that the user will see.
  * `url`: The URL of the merchant that the user will see.

* `environment`: The Yandex Pay SDK execution environment:
  * `PROD`: A production environment.
  * `SANDBOX`: A test environment.

* `locale`: Selecting a locale for Yandex Pay SDK:
  * `SYSTEM`: Based on the system settings: `RU` or `EN`.
  * `RU`: Always RU regardless of system settings.
  * `EN`: Always EN regardless of system settings.

* `logging`: This attribute specifies that the event should be logged.

## Step 4. Place the Yandex Pay button on the screen {#create-button}

```XML
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <com.yandex.pay.core.ui.YandexPayButton
        android:id="@+id/button"
        android:layout_width="300dp"
        android:layout_height="54dp"
        android:layout_marginTop="4dp"
        app:yandexpay_color_scheme="by_theme"
        app:yandexpay_corner_radius="4dp"
     />
```

When creating a button, you can pass values for the attributes:

* `yandexpay_color_scheme` enables you to define the color palette of the button. Acceptable values:
  * `by_theme`: The color palette is adapted to match the system theme.
  * `light`: The color palette is always light.
  * `dark`: The color palette is always dark.

* `yandexpay_corner_radius` enables you to define the corner radius for the button (in `dp`).

When adding the button on the screen (`onAttachedToWindow`), the button sends a request to update the profile data.

## Step 5. Register a contract for Yandex Pay launch {#register-launcher}

To learn more about this approach, see the [documentation](https://developer.android.com/training/basics/intents/result/).

When registering the contract, add a callback with obtaining the `YandexPayResult`:

```Kotlin
private val yandexPayLauncher = registerForActivityResult(OpenYandexPayContract()) { result ->
        when (result) {
            is YandexPayResult.Success -> showToast("token: ${result.paymentToken}")
            is YandexPayResult.Failure -> when (result) {
                is YandexPayResult.Failure.Validation -> showToast("failure: ${result.details}")
                is YandexPayResult.Failure.Internal -> showToast("failure: ${result.message}")
            }
            is YandexPayResult.Cancelled -> showToast("cancelled")
        }
    }
```

## Step 6. Set up the button handler {#register-handler}

```Kotlin
val button = findViewById(R.id.button)
button.setOnClickListener { ->

  // Order details
  val orderDetails = OrderDetails(
    Order(
      // Order ID
      OrderID.from("ORDER1"),
      // Total price
      Amount.from("150000.00"),
      // Order label (for the user)
      "ORDER 1",
      // Order items. You can pass an empty list.
      // If you pass at least one item, the total will be validated
      listOf(
        OrderItem(
          // Item name
          label = "item1",
          // Item price
          amount = Amount.from("50000.00"),
          // Item type
          type = OrderItemType.Pickup,
          // Data on quantity of the item
          quantity = OrderItemQuantity(1),
        ),
          OrderItem(
          label = "item2",
          amount = Amount.from("100000.00"),
          type = OrderItemType.Shipping,
          quantity = OrderItemQuantity(1)
        )
      ),
    ),
    // List of available payment methods in your PSP
    listOf(
      PaymentMethod(
        // What the payment token will include: encrypted bank card data
        // or a tokenized card
        listOf(AuthMethod.PanOnly),
        // Payment method
        PaymentMethodType.Card,
        // ID of the payment service provider
        Gateway.from("gatewayID"),
        // List of supported payment systems
        listOf(CardNetwork.Visa, CardNetwork.MasterCard, CardNetwork.MIR),
        // ID of the merchant in the payment service provider system
        GatewayMerchantID.from("MerchantGW1"),
      ),
    ),
  )

  // Launch the service using the launcher, passing the orderDetails generated
  yandexPayLauncher.launch(orderDetails)

}
```

## Step 7. Pass the obtained token {#send-token}

Pass `YandexPayResult.Success.paymentToken` to the payment service provider.
