Enabling payments via a payment link

General information

Note

Starting with version 2.3.0-alpha01, the SDK uses a new workflow. For details, see this article.

How it works

  1. The user clicks the pay button in the application.
  2. The user goes to the Yandex Pay payment form screen, with the following information displayed:

    • A list of the user’s bank cards.
    • Payment options via the Faster Payment System (SBP).
  3. The user selects a payment method and clicks the order confirmation button.

  4. Service Yandex Pay completes a payment and returns the operation result.

Visual representation of a card payment: flow

Visual representation of a Split payment: flow

Visual representation of adding a card: flow

Requirements for connecting

Supported version: Android 7.0 and higher.

Note

If your project's minSdkVersion is 23 or lower, see info about support for lower device versions.

Step 1. Set up the environment

See the documentation to learn how to prepare your project for integration.

Step 2. Enable the Yandex Pay SDK

Add the following dependency to your build.gradle build scripts:

dependencies {
    implementation 'com.yandex.pay:pay:2.3.10'
}

Step 3. Get an object of the payment session

Get an object of the payment session using the getYandexPaymentSession function:

  private val yaPayConfig = YPayConfig(
    merchantData = MerchantData(
        id = MerchantId("merchantId"),
        name = MerchantName("merchantName"),
        url = MerchantUrl("https://merchant.com/"),
    ),
    environment = YPayApiEnvironment.PROD,
  )

  private val paymentSessionKey = PaymentSessionKey(
    generateSessionKey()
  )

  private val paymentSession: PaymentSession = YPay.getYandexPaymentSession(
    context = this,
    config = yaPayConfig,
    sessionKey = paymentSessionKey  // optional if you're using Kotlin for implementation
  )
  private YPayConfig getPayConfig() {
    return new YPayConfig(
        new MerchantData(
            new MerchantId("merchantId"),
            new MerchantName("MERCHANT_NAME"),
            new MerchantUrl("https://merchant.com/")
        ),
        YPayApiEnvironment.PROD
    );
  }

  private PaymentSessionKey getPaymentSessionKey() {
    return new PaymentSessionKey(
        generateSessionKey()
    );
  }

  private PaymentSession getPaymentSession() {
    return YPay.INSTANCE.getYandexPaymentSession(
        this,
        getPaymentSessionKey(),
        getPayConfig()
    );
  }

When getting a PaymentSession, be sure to pass:

  • Context: The context of the application or Activity.
  • PaymentSessionKey: Key of the payment session.
  • YPayConfig: Data about the Yandex Pay SDK configuration.

When creating YPayConfig, be sure to pass:

  • merchantData: Data of the merchant:
    • id: Unique merchant ID. You can get it when registering a merchant in Yandex Pay.
    • 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.

Step 4. (optional) Place the Yandex Pay button on the screen

Add the button to your layout:

<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.YPayButton
        android:id="@+id/y_pay_button"
        android:layout_width="300dp"
        android:layout_height="54dp"
        android:layout_marginTop="4dp"
        app:ypay_color_scheme="by_theme"
        app:ypay_corner_radius="4dp" />

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

  • ypay_color_scheme: With this option, you can 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.
  • ypay_corner_radius: With this option, you can define the corner radius for the button (in dp).

When initializing the view components, bind the current session's object to the button using a PaymentSession object and its bindTo function:

In addition, pass SessionListenerArgs with a list of payment methods allowed for the user.

fun initViews() {
    paymentSession.bindTo(yPayButton, SessionListenerArgs(listOf(PaymentMethodType.CARD,PaymentMethodType.SPLIT)))
}
void initViews() {
    getPaymentSession().bindTo(yPayButton);
}

When bound, the button can show personalized data for the user.

Step 5. Initialize a contract for Yandex Pay launch

When initializing the contract, add a callback that obtains the YPayResult:

private val yandexPayLauncher = YPayLauncher(this) { result: YPayResult ->
    when (result) {
        is YPayResult.Success -> showToast("Finished with success")
        is YPayResult.Cancelled -> showToast("Finished with cancelled event")
        is YPayResult.Failure -> showToast("Finished with domain error")
    }
}
private final YPayLauncher launcher = new YPayLauncher(this, yPayResult -> {
    if (yPayResult instanceof YPayResult.Cancelled) {
        handleResult("Finished with cancelled event");
    } else if (yPayResult instanceof YPayResult.Success) {
        handleResult("Finished with success");
    } else if (yPayResult instanceof YPayResult.Failure) {
        handleResult("Finished with domain error");
    }
});

Step 6. Generate data for launching Yandex Pay

Before launching Yandex Pay, generate payment data using the PaymentData.PaymentUrlFlowData class and payment session data. Create YPayContractParams based on this data.

val paymentData = PaymentData.PaymentUrlFlowData(
    // Order payment URL
    "payment-url"
)

val params = YPayContractParams(
    paymentSession = paymentSession,
    paymentData = paymentData,
)
final PaymentData paymentData = new PaymentData.PaymentUrlFlowData(
    // Order payment URL
    "payment-url"
);

// parameters for launching Yandex Pay
final YPayContractParams params = new YPayContractParams(
    getPaymentSession(),
    paymentData
);

Note

If you want to enable Split payments, pass the required parameters to availablePaymentMethods when generating a link.

For more information about generating a payment link, see the backend documentation.

Step 7. Launch Yandex Pay

Launch via the Yandex Pay button

Pass the previously created launcher to the button using the setOnClickListener function and define its launch logic in a lambda expression. When running the launcher, pass the YPayContractParams parameters to it:

yPayButton.setOnClickListener(yandexPayLauncher) { launcher ->
    val params = YPayContractParams(...)
    launcher.launch(params)
}
yPayButton.setOnClickListener(yandexPayLauncher, launcher -> {
    YPayContractParams params = new YPayContractParams(...);
    launcher.launch(params);
});

Warning

The YPayButton has its own setOnClickListener function created. The entire logic for click handling should be specified in it. Calling the View.setOnClickListener will throw an exception.

Launch without using the button

Run the service using the launcher, passing the YPayContractParams launch parameters:

val params = YPayContractParams(...)
yandexPayLauncher.launch(params)
final YPayContractParams params = new YPayContractParams(...);
yandexPayLauncher.launch(params);

Step 8. Handle the results

After Yandex Pay is launched, the SDK will select the optimal strategy and run the payment form to complete the payment in. The result will be obtained using the launch contract YPayLauncher.