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

Supported version: Android 6.0 Marshmallow and higher.

Step 1. Get a YANDEX_CLIENT_ID

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

    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.

  2. Go to https://oauth.yandex.com/.

  3. Click Create app.

  4. Enter the service name.

  5. 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.

  6. Under Which data do you need?, select Yandex Pay → Pay by Yandex Pay.

  7. Click Create app.

Step 2. Add the SDK

  1. Add the dependency to your build scripts:

    dependencies {
      implementation 'com.yandex.pay:core:0.2.1'
    }
    
  2. Specify the resultingYANDEX_CLIENT_ID in the build.gradlebuild script as a value in manifestPlaceholders:

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

Step 3. Initialize the library

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

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

<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

To learn more about this approach, see the documentation.

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

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

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

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