Yandex Pay Web SDK integration

With this Yandex Pay connection type, the merchant first receives the payment token and then makes the payment.

This payment method is basically similar to paying via Apple Pay and Google Pay.

Note

You can also use Yandex Pay to create an order and make a payment.
Learn more in Web SDK guide.

Requirements for connecting

To install the Yandex Pay button, make sure your site meets the requirements:

  • The site works over the HTTPS protocol.

  • The site doesn't cut out the __YP__ parameter from the URL (learn more here).

  • Your site's CSP policies allow using:

    script-src: https://pay.yandex.ru
    frame-src: https://pay.yandex.ru
    img-src: https://pay.yandex.ru
    connect-src: https://pay.yandex.ru
    style-src: 'unsafe-inline'
    
  • Your site doesn't use the headers:

    Cross-Origin-Opener-Policy: *
    

Connect Yandex Pay

Step 1. Adding the SDK to the page

Add the HTML code to all pages where you put the Yandex Pay button before the closing </body> tag. Pass the name of the method for initializing and processing payments to the onload attribute.

<script src="https://pay.yandex.ru/sdk/v1/pay.js" onload="onYaPayLoad()" async></script>

Step 2. Creating payments

function onYaPayLoad() {
    const YaPay = window.YaPay;

    // Payment details
    const paymentData = {
        // For debugging, you need to explicitly specify the `SANDBOX` environment.
        // For the production environment, you can omit this parameter or specify `PRODUCTION`.
        env: YaPay.PaymentEnv.Sandbox,

        // Version 2 specifies the payment type using a token
        // Yandex Pay provides the payment token and the merchant uses it to accept the payment
        version: 2,

        // Codes of your payment currency and country of business
        countryCode: YaPay.CountryCode.Ru,
        currencyCode: YaPay.CurrencyCode.Rub,

        // Merchant data:
        //   -id: The ID that the merchant receives when they register in Yandex Pay.
        //   - name: Merchant name
        //   - url: Merchant website URL
        merchant: {
            id: '<YOUR_MERCHANT_ID>',
            name: 'test-merchant-name',
            url: 'https://test-merchant-url.ru',
        },

        // Data about items and payment amount
        order: {
            // Order ID
            id: 'order-id',
            // Total cost of items
            total: { amount: '43980.00' },
            // Payment items itemization (optional)
            items: [
                { label: 'Yandex Station, black', amount: '26990.00' },
                { label: 'Yandex Station 2, antracite', amount: '16990.00' },
            ],
        },

        // Details of available payment methods
        // (the example uses values for the test environment)
        paymentMethods: [
            {
                type: YaPay.PaymentMethodType.Card,
                gateway: 'test-gateway',
                gatewayMerchantId: 'test-gateway-merchant-id',
                allowedAuthMethods: [YaPay.AllowedAuthMethod.PanOnly],
                allowedCardNetworks: [
                    YaPay.AllowedCardNetwork.Visa,
                    YaPay.AllowedCardNetwork.Mastercard,
                    YaPay.AllowedCardNetwork.Mir,
                    YaPay.AllowedCardNetwork.Maestro,
                    YaPay.AllowedCardNetwork.VisaElectron,
                ],
            },
        ],
    };

    // Handler to obtain a payment token
    function onPaymentProcess(event) {
        // Payment token.
        console.log(`Payment token — ${event.token}`);

        // ...accepting the payment using the payment token...
    }

    // Payment error handler
    function onPaymentError(event) {
        // Show a message that the payment method is unavailable at the moment
        // and suggest the user to try another method.
        console.log('Payment error — ' + event.reason);
    }

    // Payment cancellation handler
    function onPaymentAbort(event) {
        // The user closed the Yandex Pay form.
        // Suggest the user to try another payment method.
    }

    // Create a payment session
    YaPay.createSession(paymentData, {
        onProcess: onPaymentProcess,
        onAbort: onPaymentAbort,
        onError: onPaymentError,
    })
        .then(function (paymentSession) {
            // Show the Yandex Pay button on the page.
            paymentSession.mountButton(document.querySelector('#button_container'), {
                type: YaPay.ButtonType.Pay,
                theme: YaPay.ButtonTheme.Black,
                width: YaPay.ButtonWidth.Auto,
            });
        })
        .catch(function (err) {
            // Couldn't create a payment session. 
        });
}

Examples of working with the Web SDK

Embedded components

  1. Yandex Pay payment buttons

Operations with the payment

  1. Updating the cart.
  2. Getting the payer data.

Specifics

  1. How Yandex Pay transfers payments via redirect queries.
  2. Common mistakes.